-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (103 loc) · 4.29 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var requestify = require("requestify");
var Talk2M = function(cred) {
if (!cred || !cred.t2mdeveloperid || !cred.t2maccount || !cred.t2musername || !cred.t2mpassword) {
console.log("WARNING: NO CREDENTIALS SPECIFIED!!!");
} else {
credentials = cred;
}
if (!(this instanceof Talk2M)) {
return new Talk2M(cred);
}
};
var serverURL = "https://m2web.talk2m.com/t2mapi";
var dataMailboxURL = "https://data.talk2m.com";
var credentials;
Talk2M.prototype.getAccountInfo = function(callback) {
requestify.post(buildUrl("getaccountinfo"))
.then(function(response) {
callback(null, JSON.parse(response.body));
})
.fail(function(response) {
callback(JSON.parse(response.body));
});
};
Talk2M.prototype.getEwons = function(callback) {
requestify.post(buildUrl("getewons"))
.then(function(response) {
callback(null, JSON.parse(response.body));
})
.fail(function(response) {
callback(JSON.parse(response.body));
});
};
Talk2M.prototype.getEwon = function(name, callback) {
if (!name) return callback(new Error("eWon Name ID not specified!"));
requestify.post(buildUrl("getewon", "name=" + name))
.then(function(response) {
callback(null, JSON.parse(response.body));
})
.fail(function(response) {
callback(JSON.parse(response.body));
});
};
Talk2M.prototype.readTags = function(ewon, callback) {
if (!ewon || !ewon.name || !ewon.m2webServer) return callback(new Error("eWon not specified!"));
if (!ewon.login || !ewon.password) return callback(new Error("eWon device credentials not specified!"));
requestify.post(buildEwonUrl(ewon, "/rcgi.bin/ParamForm", "AST_Param=$dtIV$ftT"))
.then(function(response) {
callback(null, response.body);
})
.fail(function(response) {
callback(response.body);
});
};
Talk2M.prototype.writeTag = function(ewon, tagName, tagValue, callback) {
if (!ewon || !ewon.name || !ewon.m2webServer) return callback(new Error("eWon not specified!"));
if (!ewon.login || !ewon.password) return callback(new Error("eWon device credentials not specified!"));
requestify.post(buildEwonUrl(ewon, "/rcgi.bin/UpdateTagForm", "TagName1=" + tagName + "&TagValue1=" + tagValue))
.then(function(response) {
callback(null, response.body);
})
.fail(function(response) {
callback(response.body);
});
};
// PARAMS CAN BE LIKE THIS: {tagId, from, to, limit}
Talk2M.prototype.getDMData = function(ewon, params, callback) {
if (!ewon || !ewon.id) return callback(new Error("eWon not specified!"));
// ewonId, tagId, from, to, Limit
var paramString = "ewonId=" + ewon.id;
if (params && params.tagId) paramString += "&tagId=" + params.tagId;
if (params && params.from) paramString += "&from=" + params.from;
if (params && params.to) paramString += "&to=" + params.to;
if (params && params.limit) paramString += "&limit=" + params.limit;
requestify.post(buildUrl('getdata', paramString, true))
.then(function(response) {
callback(null, response.body);
})
.fail(function(response) {
callback(response.body);
});
}
function buildUrl(action, params, isDM) {
var result = (isDM ? dataMailboxURL : serverURL) + "/" + action
+ (isDM ? "?t2mdevid=" : "?t2mdeveloperid=")
+ credentials.t2mdeveloperid
+ "&t2maccount=" + credentials.t2maccount
+ "&t2musername=" + credentials.t2musername
+ "&t2mpassword=" + credentials.t2mpassword;
if (params) result += "&" + params;
return result;
}
function buildEwonUrl(ewon, path, query) {
var result = "https://" + ewon.m2webServer + "/t2mapi/get/" + ewon.name + "/" + path + "?";
if (query) result += query + "&";
result += "t2mdeviceusername=" + ewon.login + "&t2mdevicepassword=" + ewon.password
+ "&t2mdeveloperid=" + credentials.t2mdeveloperid
+ "&t2maccount=" + credentials.t2maccount
+ "&t2musername=" + credentials.t2musername
+ "&t2mpassword=" + credentials.t2mpassword;
//console.log(result);
return result;
}
module.exports = Talk2M;