datafeed.js
1.29 KB
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
const EVENT_LOGIN = 1;
const EVENT_LOGOUT = 2;
const EVENT_SYNC = 3;
const EVENT_CLEAR_COMMAND = 4;
var FEED = {
xhr: new XMLHttpRequest(),
id: null,
payload: "",
login: function(devid, ts)
{
var url = serverURL + "notify/" + devid + "?EV=" + EVENT_LOGIN + "&TS=" + ts;
this.xhr.open("GET", url, false);
this.xhr.send(null);
if (this.xhr.status == 200) {
this.id = devid;
return true;
}
return false;
},
logout: function()
{
var url = serverURL + "notify/" + this.id + "?EV=" + EVENT_LOGOUT;
this.xhr.open("GET", url, false);
this.xhr.send(null);
if (this.xhr.status == 200) {
this.id = null;
return true;
}
return false;
},
post: function()
{
var url = serverURL + "post/" + this.id;
this.xhr.open("POST", url, false);
this.xhr.send(this.payload);
return this.xhr.status == 200;
},
purge: function()
{
this.payload = "";
},
add: function(pid, value)
{
if (value != null && value != "") {
if (this.payload != "") this.payload += ",";
this.payload += pid.toString(16) + ":" + value;
}
},
connected: function()
{
return this.id != null;
}
}