forked from DVR-Ltd/node-red-rcm-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmg2-connector.js
88 lines (66 loc) · 1.73 KB
/
mmg2-connector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//jshint esversion:10
const
{
MMG2CoreAPI
} = require("rcm-core-client"),
getDeviceTypes = (node, conn) => {
conn.request({
resource: "/API/meta/getDeviceTypes",
onSuccess: (res) => {
console.log("Device types retreived");
node.context().global.deviceTypes = res.data.deviceTypes;
getServiceClassifications(node, conn);
},
onFailure: () => {
console.log("Failed to retreive device types.");
}
});
},
getServiceClassifications = (node, conn) => {
conn.request({
resource: "/API/meta/getServiceClassifications",
onSuccess: (res) => {
console.log("Service Classifications retreived");
node.context().global.serviceClassifications = res.data.serviceClassifications;
node.context().global.connection = conn;
},
onFailure: () => {
console.log("Failed to retreive service classifications.");
}
});
};
module.exports = function(RED) {
//Don't judge me... (it's a temporary hack)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
RED.nodes.registerType(
"mmg2-connector",
function(config) {
RED.nodes.createNode(this, config);
if ((!config.host) || (!config.username) || (!config.password)) {
console.log("Connection parameters missing.");
return;
}
let connection = new MMG2CoreAPI({
domain: config.host,
username: config.username,
password: config.password
});
connection.on("error", (err) => {
console.log(err);
});
connection.logOn(
() => {
console.log("LOGGED IN");
getDeviceTypes(this, connection);
// this.send({
// payload: connection
// });
},
(err) => {
console.log("LOG IN FAIL");
this.context().global.connection = err;
}
);
}
);
};