-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
202 lines (163 loc) · 6.49 KB
/
app.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
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Use sctrict mode
'use strict';
//------------------------------------------------------------------------------
// Change variable values in this section to customize emitted data
//------------------------------------------------------------------------------
// Name of the MQTT topic that the data should be published on
var topicToBePublishedOn = 'myTopic';
// Wait this many seconds before publishing the next set of data
var varIntervalBetweenData = 2;
// Quality of Serive for the publish event. Supported values : 0, 1, 2
var QosLevel = 0;
var flag = false;
// read the id of the IoT foundation org out of a local .env file
// format of .env file:
// iotf_org=<id of IoT Foundation organization>
require('dotenv').load();
// Note that the following configuration must match with the parameters that
// the device-simulator was registered with. This device registration can
// either be done in the dashboard of the IoT Foundation service or via its
// API
var iotfConfig = {
"org" : process.env.iotf_org,
"id" : process.env.iotf_id,
"auth-token" : process.env.iotf_authtoken,
"type" : process.env.iotf_type,
"auth-method" : "token"
};
//------------------------------------------------------------------------------
// Setup all the required node modules we'll need
//------------------------------------------------------------------------------
// Require the express framework
var express = require('express');
// Initialize the app as an express application
var app = express();
//---The ibmiotf package simplifies intractions with the IoT Foundation Service-
var Iotf = require("ibmiotf");
//---The cfenv module helps to access the Cloud Foundry environment-------------
var cfenv = require('cfenv');
// get the application environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
console.log('');
console.log('--- DEBUG appENV: ---');
console.log(appEnv);
console.log('');
//---Start the express server---------------------------------------------------
app.listen(appEnv.port, function() {
console.log("Server started on " + appEnv.url);
}).on('error', function(err) {
if (err.errno === 'EADDRINUSE') {
console.log('Server not started, port ' + appEnv.url + ' is busy');
} else {
console.log(err);
}
});
//---Connect to the IoT Foundation service--------------------------------------
console.log('');
console.log('--- DEBUG iotConfig: ---');
console.log(iotfConfig);
console.log('');
// Create a client (used to send data)
var iotfClient = new Iotf.IotfDevice(iotfConfig);
// Connect to the initialized iotf service
iotfClient.connect();
// Handle errors coming from the iotf service
iotfClient.on("error", function (err) {
// console.log("Error received while connecting to IoTF service: " + err.message);
if (err.message.indexOf('authorized') > -1) {
console.log('');
console.log("Make sure the device-simulator is registered in the IotF org with the following configuration:")
console.log(iotfConfig);
console.log('');
}
process.exit( );
});
iotfClient.on("connect", function () {
console.log("Device simulator is connected to the IoT Foundation service");
console.log("QoS level set to: " + QosLevel);
// inital data packet to be emitted as a JSON object
var dataPacket = {
"d" : {
"nivelLiquido" : 75,
//"distance" : 200,
//"humidity" : 10,
//"luminosity" : 5
}
};
//--loop forever------------------------------------------------------------
setInterval(function(){
// add a time stamp to the data packet
var date = new Date();
dataPacket.ts = date.toISOString();
// convert the data packet into a string and then publish it
iotfClient.publish("status","json", JSON.stringify(dataPacket) );
// log out the emitted dataPacket
console.log(JSON.stringify(dataPacket));
//
// increment nivelLiquido up to 100 then back down to 0
//
//if (dataPacket.d.nivelLiquido === 0) {
// var countingUp = true;
//} else if (dataPacket.d.nivelLiquido === 100) {
// var countingUp = false;
//}
//if (countingUp === true) {
// if (dataPacket.d.nivelLiquido === 100) {
// var countingUp = false;
// } else1{
// var countingUp = true;
// }
// var nivelLiquidoIncrement = 20;
//} else {
// if (dataPacket.d.nivelLiquido === 0) {
// var countingUp = true
// } else {
// var countingUp = false;
// }
var nivelLiquidoIncrement = 0.25;
//var distanceDecrement = 20;
// var luminosityIncrement = 1;
//}
//if (dataPacket.d.nivelLiquido === 100) {
// dataPacket.d.nivelLiquido = 0;
//}
if(dataPacket.d.nivelLiquido > 80){
dataPacket.d.nivelLiquido = dataPacket.d.nivelLiquido - nivelLiquidoIncrement;
flag = true;
}else if(dataPacket.d.nivelLiquido < 70){
dataPacket.d.nivelLiquido = dataPacket.d.nivelLiquido + nivelLiquidoIncrement;
flag = false;
}else if(flag){
dataPacket.d.nivelLiquido = dataPacket.d.nivelLiquido - nivelLiquidoIncrement;
}else if(!flag){
dataPacket.d.nivelLiquido = dataPacket.d.nivelLiquido + nivelLiquidoIncrement;
}
//if (dataPacket.d.humidity === 100) {
// dataPacket.d.humidity = 0;
//}
//dataPacket.d.humidity = dataPacket.d.humidity + distanceDecrement;
//
// increment the distance until 100 and start again at 0
//
//var distanceDecrement = 1;
//if (dataPacket.d.distance === 100) {
// dataPacket.d.distance = 0;
//}
//dataPacket.d.distance = dataPacket.d.distance - distanceDecrement;
}, varIntervalBetweenData*1000);
});