-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
206 lines (167 loc) · 5.39 KB
/
main.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
'use strict';
const NGSIClient = require('/opt/ngsildAdapter/ngsi/ngsiclient.js');
const NGSIAgent = require('/opt/ngsildAdapter/ngsi/ngsiagent.js');
const fogfunction = require('/opt/ngsildAdapter/function.js');
var ngsi10client = null;
var brokerURL;
var outputs = [];
var threshold = 30;
var myReferenceURL;
var mySubscriptionId = null;
var isConfigured = false;
var buffer = [];
function startApp()
{
console.log('start to receive input data streams via a listening port');
}
function stopApp()
{
console.log('clean up the app');
}
// handle the commands received from the engine
function handleAdmin(req, commands, res)
{
console.log('=============configuration commands=============');
console.log(commands);
handleCmds(commands);
isConfigured = true;
res.status(200).json({});
}
function handleCmds(commands)
{
console.log("handleCmds....commands+++++++++++++++++++++", commands);
for(var i = 0; i < commands.length; i++) {
var cmd = commands[i];
console.log(cmd);
handleCmd(cmd);
console.log("handle next command");
}
// send the updates in the buffer
sendUpdateWithinBuffer();
}
function handleCmd(commandObj)
{
console.log("handleCmd....commandObj+++++++++++++++++++++", commandObj);
if (commandObj.command == 'CONNECT_BROKER') {
connectBroker(commandObj);
} else if (commandObj.command == 'SET_OUTPUTS') {
setOutputs(commandObj);
} else if (commandObj.command == 'SET_REFERENCE'){
setReferenceURL(commandObj);
}
}
// connect to the IoT Broker
function connectBroker(cmd)
{
brokerURL = cmd.brokerURL;
ngsi10client = new NGSIClient.NGSI10Client(brokerURL);
console.log('connected to broker', cmd.brokerURL);
}
function setReferenceURL(cmd)
{
myReferenceURL = cmd.url
console.log('your application can subscribe addtional inputs under the reference URL: ', myReferenceURL);
}
function setOutputs(cmd)
{
console.log('Inside setOutputs.......... ', cmd);
var outputStream = {};
outputStream.id = cmd.id;
outputStream.type = cmd.type;
outputs.push(outputStream);
console.log('output has been set: ', outputs);
}
function sendUpdateWithinBuffer()
{
console.log('Inside sendUpdateWithinBuffer ==========================');
for(var i=0; i<buffer.length; i++){
var tmp = buffer[i];
if (tmp.outputIdx > 0) {
tmp.ctxObj.entityId.id = outputs[i].id;
tmp.ctxObj.entityId.type = outputs[i].type;
}
ngsi10client.updateContext(tmp.ctxObj).then( function(data) {
console.log('======send update======');
console.log(data);
}).catch(function(error) {
console.log(error);
console.log('failed to update context');
});
}
buffer= [];
console.log('Updates Alarm, Finished!!!');
}
//
// query results from the assigned nearby IoT broker
//
function query(queryCtxReq, f)
{
if (ngsi10client == null) {
console.log("=== broker is not configured for your query");
return
}
ngsi10client.queryContext(queryCtxReq).then(f).catch( function(error) {
console.log('failed to subscribe context');
});
}
//
// send subscriptions to IoT broker
//
function subscribe(subscribeCtxReq)
{
if (ngsi10client == null) {
console.log("=== broker is not configured for your subscription");
return
}
subscribeCtxReq.reference = myReferenceURL;
console.log("================trigger my own subscription===================");
console.log(subscribeCtxReq);
ngsi10client.subscribeContext(subscribeCtxReq).then( function(subscriptionId) {
console.log("subscription id = " + subscriptionId);
mySubscriptionId = subscriptionId;
}).catch(function(error) {
console.log('failed to subscribe context');
});
}
//
// publish context entities:
//
function publish(ctxUpdate)
{
buffer.push(ctxUpdate)
if (ngsi10client == null) {
console.log("=== broker is not configured for your update");
return
}
for(var i=0; i<buffer.length; i++){
var update = buffer[i];
ngsi10client.updateContext(update).then( function(data) {
console.log('======send update======');
console.log(data);
}).catch(function(error) {
console.log(error);
console.log('failed to update context');
});
}
buffer= [];
}
// handle the received results
function handleNotify(req, ctxObjects, res)
{
console.log('============handle notify==========================');
for(var i = 0; i < ctxObjects.length; i++) {
console.log(ctxObjects[i]);
fogfunction.handler(ctxObjects[i], publish, query, subscribe);
}
}
// get the listening port number from the environment variables given by the FogFlow edge worker
var myport = process.env.myport;
// set up the NGSI agent to listen on
NGSIAgent.setNotifyHandler(handleNotify);
NGSIAgent.setAdminHandler(handleAdmin);
NGSIAgent.start(myport, startApp);
process.on('SIGINT', function() {
NGSIAgent.stop();
stopApp();
process.exit(0);
});