-
Notifications
You must be signed in to change notification settings - Fork 4
/
agent.js
209 lines (180 loc) · 5.51 KB
/
agent.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*jslint node: true */
/**
* Main function of Agent
*
* Usage: nodejs index.js
*
* Author: Wei-Ting Chou <[email protected]>
*
*/
'use strict';
var log = null,
EventEmitter = require('events').EventEmitter,
emitter = new EventEmitter,
WebSocket = require('ws');
var socket = null,
currentState = 'STOPPED',
config = {};
function commandHandler(data, flags) {
try {
var message = JSON.parse(data).data,
msgHeader = message.header || undefined,
msgPayload = message.payload || undefined;
if (msgHeader && msgPayload) {
switch (msgHeader.type) {
case 'modules':
// publish command to the specified module
var commandType = msgPayload.commandType || undefined,
commandName = msgPayload.commandName || undefined,
commandData = msgPayload.commandData || undefined;
if (commandType && commandName && commandData) {
emitter.emit('module.command', msgPayload);
} else {
log.error('Malformed module command: '+JSON.stringify(message));
}
break;
default:
log.error('Unknown message type received: '+msgHeader);
}
}
} catch (err) {
log.error('Unexpected error: '+err);
}
}
function registerModulesToThink(moduleList) {
}
function publish_syncResult(syncCmdId, message) {
if (socket && socket.readyState === WebSocket.OPEN) {
var event = {
senseId: config.id,
syncCmdId: syncCmdId,
data: message
};
socket.send(JSON.stringify(event), function(error) {
if (error) {
log.error('Error occurred while publising event: %s, ERRMSG: %s', JSON.stringify(event), error);
}
});
} else {
log.error('Connection to Think is broken, message: '+message);
}
}
function publish(module, event, message) {
if (socket && socket.readyState === WebSocket.OPEN) {
var event = {
senseId: config.id,
data: {
eventType: module,
eventName: event,
message: message
}
};
socket.send(JSON.stringify(event), function(error) {
if (error) {
log.error('Error occurred while publising event: %s, ERRMSG: %s', JSON.stringify(event), error);
}
});
} else {
log.error('Connection to Think is broken, message: '+message);
}
}
/**
* WebSocket client
*
* Remote WebSocket server is the place that provides the dynamic routing data,
* which URL is given as the third argument
*
*/
function connect(id) {
if (!id) {
log.error('Sense Id is not provided.');
throw new Error('Sense Id is not provided');
}
socket = new WebSocket(config.url);
function reconnect(id) {
// clean up the old socket if any
destroyConnection();
setTimeout(function() {
connect(id);
}, config.options.hasOwnProperty('reconnectInterval') ? config.options.reconnectInterval : 3000);
}
socket.on('open', function () {
log.info('Connected SenseId %s to Think successfully.', config.id);
currentState = 'CONNECTED';
var initData = {
senseId : config.id,
data: {
eventType: 'humix-think',
eventName: 'sense.status',
message: 'connected'
}
}
socket.send(JSON.stringify(initData), function(error) {
if (error) {
log.error('Error occurred while publising senseid : %s, ERRMSG: %s', config.id, error);
}
});
});
socket.on('message', commandHandler);
socket.on('error', function (err) {
log.error('Connection to Think is broken! ERRMSG: ' + err);
if (config.options.hasOwnProperty('autoreconnect') &&
config.options.autoreconnect === true) {
reconnect(id);
}
});
socket.on('close', function () {
log.info('Disconnected to Think!');
if (config.options.hasOwnProperty('autoreconnect') &&
config.options.autoreconnect === true) {
reconnect(id);
}
});
}
function init(thinkUrl, senseId, options) {
var url = null;
if (thinkUrl) {
if (typeof thinkUrl === 'string') {
if (thinkUrl.slice(-1) !== '/') {
url = thinkUrl+'/node-red/comms_sense';
} else {
url = thinkUrl+'node-red/comms_sense';
}
} else {
throw new Error('Invalid Url format');
}
}
currentState = 'INITIALING';
if (!url) {
throw new Error('Url is not provided');
} else if (!senseId) {
throw new Error('Sense Id is not provided');
}
config.url = url;
config.id = senseId || undefined;
config.options = options || {};
log = options.logger;
}
function start() {
currentState = 'CONNECTING';
connect(config.id);
}
function stop() {
currentState = 'STOPPED';
destroyConnection();
}
function destroyConnection() {
if (socket) {
socket.close();
socket = null;
}
}
module.exports = {
init: init,
start: start,
stop: stop,
publish: publish,
events: emitter,
publish_syncResult: publish_syncResult,
getState: function () { return currentState; }
};