-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamsys.js
More file actions
130 lines (103 loc) · 3.04 KB
/
jamsys.js
File metadata and controls
130 lines (103 loc) · 3.04 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//===================================================================
// This module holds JAM system state. So the "runtime" state of
// JAM virtual machine is captured by this module.
// We can inspect or even change the state using the functions
// provided here.
//
// These functions are quite handy.
//===================================================================
var globals = require('./constants').globals;
var reggie = undefined;
var advertised = new Map();
var fogid,
cloudid;
var redserver,
redport,
mqttserver,
mqttport;
module.exports = new function() {
this.init = function(reg, mtype, tgs, ifl, ofl, nodeid, link, long, lat) {
reggie = reg;
this.type = mtype;
this.id = nodeid;
this.tags = tgs;
this.iflow = ifl;
this.oflow = ofl;
this.link = link;
this.long = long;
this.lat = lat;
}
this.fullid = function() {
return this.id + "_" + this.type;
}
this.setFog = function(fid) {
if (fogid === undefined)
fogid = fid;
}
this.setCloud = function(cid) {
if (cloudid === undefined)
cloudid = cid;
}
this.setRedis = function(host, port) {
redserver = host;
redport = port;
}
this.setLong = function(val) {
this.long = val;
}
this.setLat = function(val) {
this.lat = val;
}
this.setMQTT = function(host, port) {
mqttserver = host;
mqttport = port;
}
this.getMQTT = function() {
return {host: mqttserver, port: mqttport};
}
this.getRedis = function() {
if (redserver === undefined)
return undefined;
else
return {host: redserver, port: redport};
}
this.unsetFog = function(fid) {
if (fogid !== fid)
fogid = undefined;
}
this.unsetCloud = function(cid) {
if (cloudid !== cid)
cloudid = undefined;
}
// Advertise the given attribute as on
this.adUp = function(aname, info) {
var attr = {};
attr[aname] = info;
if (advertised.get(aname) === undefined) {
advertised.set(aname, 1);
this.setupAdvertisement(aname);
}
reggie.setAttributes(attr);
}
// Advertise aname off
this.adDown = function(aname) {
reggie.removeAttributes(aname);
}
this.setupAdvertisement = function(aname) {
console.log("Setup advertisement... ");
var fup = 'fog-' + aname + '-up';
var fdown = 'fog-' + aname + '-down';
var cup = 'cloud-' + aname + '-up';
var cdown = 'cloud-' + aname + '-down';
var fobj = {};
fobj[aname] = {onAdd: fup, onRemove: fdown};
var cobj = {};
cobj[aname] = {onAdd: cup, onRemove: cdown};
if (this.type === globals.NodeType.DEVICE) {
reggie.discoverAttributes({fog: fobj, cloud: cobj});
}
else if (this.type === globals.NodeType.FOG) {
reggie.discoverAttributes({cloud: cobj});
}
}
}