-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmonitor.cpp
403 lines (368 loc) · 9.13 KB
/
netmonitor.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Sensor network monitor using the nRF2401+ radio.
*
* TODO:
* - Change from file to socket-based comms with web client
* - Store configs in database
*/
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "message.h"
#include "msgqueue.h"
#include <fcntl.h>
#include <signal.h>
#include <queue>
#include "sensorconfig.h"
#include <glob.h>
using namespace std;
// CE Pin, CSN Pin, SPI Speed
RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
int sensor_list[100];
queue<MessageQueueItem> message_queue;
void
queueMessage(int sensor_id, int msg_type, message_t &msg)
{
MessageQueueItem item(sensor_id, msg_type, msg);
message_queue.push(item);
}
void
set_value(int node, int item, int value)
{
message_t response;
response.payload.config.item = item;
response.payload.config.value = value;
queueMessage(node, 'c', response);
}
void
set_time(int node)
{
set_value(node, 't', time(NULL));
}
void
set_value_str(int node, string item, int value)
{
message_t status;
if (item == "low_point") {
set_value(node, 'l', value);
} else if (item == "high_point") {
set_value(node, 'h', value);
} else if (item == "low_time") {
set_value(node, 's', value);
} else if (item == "high_time") {
set_value(node, 'e', value);
} else if (item == "reference") {
set_value(node, 'r', value);
} else if (item == "mode") {
set_value(node, 'm', value);
} else if (item == "address") {
set_value(node, 'a', value);
} else if (item == "time") {
set_time(node);
} else if (item == "status") {
queueMessage(node, 's', status);
} else if (item == "clock") {
queueMessage(node, 't', status);
} else if (item == "config") {
queueMessage(node, 'r', status);
}
}
void
write_status(int node, message_t *msg)
{
ostringstream ofs;
ofs << "sensor." << setw(5) << setfill('0') << setbase(8) << node << ".status" << setbase(10) << ends;
ofstream ofile(ofs.str().c_str(), ios::out | ios::trunc);
ofile << "test_value " << msg->payload.sensor.value / 10.0 << endl;
ofile << "reference " << msg->payload.sensor.value_2 / 10.0 << endl;
ofile << "output_1 " << msg->payload.sensor.value_3 << endl;
ofile << "output_2 " << msg->payload.sensor.value_4 << endl;
ofile.close();
}
int
convertPathToInt(char * path)
{
char * begin;
char * end;
long interim;
begin = index(path, '.');
end = rindex(path, '.');
*end = '\0';
interim = strtol(++begin, NULL, 8);
return interim / 1;
}
void
fillSensorList(void)
{
glob_t glbuf;
memset(sensor_list, 0, sizeof(int) * 100);
if (glob("sensor.0*.status", GLOB_ERR, NULL, &glbuf) == 0) {
cout << "Found " << glbuf.gl_pathc << " active sensors" << endl;
for (unsigned int i = 0; i < glbuf.gl_pathc; i++) {
sensor_list[i] = convertPathToInt(glbuf.gl_pathv[i]);
}
}
globfree(&glbuf);
}
void roll_log()
{
int outfd;
outfd = open("netmonitor.log",
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
);
close(1);
close(2);
dup2(outfd, 1);
dup2(outfd, 2);
close(outfd);
}
void
handleMessage(void)
{
RF24NetworkHeader header;
SensorConfig scfg;
message_t msg;
network.read(header,&msg,sizeof(msg));
cout << setfill('0') << setbase(8) << setw(5) << header.from_node << ": " << setbase(10);
switch (header.type) {
case 's': // Sensor
cout << "Temp: " << msg.payload.sensor.value / 10.0
<< "C, " << msg.payload.sensor.value_2 / 10.0 << "C "
<< "LIGHT(" << (msg.payload.sensor.value_3 ? "ON" : "OFF")
<< ") OUTPUT(" << (msg.payload.sensor.value_4 ? "ON" : "OFF")
<< ")" << endl;
// Write out a sensor status file
write_status(header.from_node, &msg);
break;
case 't': // Time
cout << setw(4) << msg.payload.time.year
<< '-' << setfill('0') << setw(2) << msg.payload.time.month
<< '-' << setfill('0') << setw(2) << msg.payload.time.day
<< ' ' << setfill(' ') << setw(2) << msg.payload.time.hour
<< ':' << setfill('0') << setw(2) << msg.payload.time.minute
<< ':' << setfill('0') << setw(2) << msg.payload.time.second
<< endl;
break;
case 'C': // Current config
scfg.load(header.from_node); // Load any existing config
switch (msg.payload.config.item) {
case 'l':
cout << "Low Temp: " << msg.payload.config.value << "C" << endl;
scfg.low_point = msg.payload.config.value;
break;
case 'h':
cout << "High Temp: " << msg.payload.config.value << "C" << endl;
scfg.high_point = msg.payload.config.value;
break;
case 'r':
cout << "Reference: " << msg.payload.config.value << endl;
scfg.reference = msg.payload.config.value;
break;
case 's':
cout << "Start Time: " << msg.payload.config.value << endl;
scfg.low_time = msg.payload.config.value;
break;
case 'e':
cout << "End Time: " << msg.payload.config.value << endl;
scfg.high_time = msg.payload.config.value;
break;
case 'm':
cout << "Mode: " << msg.payload.config.value << endl;
scfg.mode = msg.payload.config.value;
break;
}
scfg.save(header.from_node);
break;
case 'c': // Request config
scfg.load(header.from_node);
cout << "Remote requested config: " << msg.payload.config.item << endl;
switch (msg.payload.config.item) {
case 'a': // All
set_time(header.from_node);
// need to grab the other details.
set_value(scfg.id, 'l', scfg.low_point);
set_value(scfg.id, 'h', scfg.high_point);
set_value(scfg.id, 'm', scfg.mode);
set_value(scfg.id, 'r', scfg.reference);
set_value(scfg.id, 's', scfg.low_time);
set_value(scfg.id, 'e', scfg.high_time);
break;
case 't': // Time
set_time(header.from_node);
break;
case 'h': // High point
set_value(scfg.id, 'h', scfg.high_point);
break;
case 'l':
set_value(scfg.id, 'l', scfg.low_point);
break;
case 'r':
set_value(scfg.id, 'r', scfg.reference);
break;
case 's':
set_value(scfg.id, 's', scfg.low_time);
break;
case 'e':
set_value(scfg.id, 'e', scfg.high_time);
break;
case 'm':
set_value(scfg.id, 'm', scfg.mode);
break;
}
break;
}
}
int
octal_value(int input)
{
int ptr = 0;
int result = 0;
while (input > 0) {
result += (input % 10) << (3 * ptr);
input /= 10;
ptr++;
}
return result;
}
void
checkForConfig(void)
{
struct stat st;
// Check for an update file
if (stat("sensor.ctl", &st) == 0) {
string line;
string item;
SensorConfig scfg;
int value;
int node = -1;
ifstream fh("sensor.ctl");
if (fh.is_open()) {
while (getline(fh, line)) {
stringstream _l(line);
_l >> item >> value;
if (item == "node") {
node = octal_value(value);
scfg.load(value);
} else {
if (node != -1) {
set_value_str(node, item, value);
scfg.set(item, value);
}
}
}
fh.close();
}
if (node != -1) {
scfg.save();
}
unlink("sensor.ctl");
}
}
void
requestAllConfig(void)
{
message_t msg;
for (int i = 0; sensor_list[i] > 0 ; i++) {
queueMessage(sensor_list[i], 'r', msg);
}
}
void
setAllTime(void)
{
for (int i = 0; sensor_list[i] > 0; i++) {
set_time(sensor_list[i]);
}
}
void
requestAllStatus(void)
{
message_t msg;
for (int i = 0; sensor_list[i] > 0; i++) {
queueMessage(sensor_list[i], 's', msg);
queueMessage(sensor_list[i], 't', msg);
}
}
void
checkRequests(void)
{
}
void
sendPendingMessages(void)
{
if (message_queue.empty()) {
return;
}
MessageQueueItem item = message_queue.front();
RF24NetworkHeader hdr(item.get_id(), item.get_type());
network.write(hdr, item.get_message(), item.get_size());
cout << "Sending to node " << setw(5) << setfill('0') << setbase(8) << item.get_id();
cout << setbase(10) << " type=" << item.get_type() << " msgtype=" << item.get_message()->payload.config.item << endl;
message_queue.pop();
}
void handle_signal(int sig)
{
switch (sig) {
case SIGHUP:
roll_log();
break;
case SIGUSR1:
fillSensorList();
requestAllConfig();
setAllTime();
break;
case SIGUSR2:
fillSensorList();
requestAllStatus();
break;
}
}
int main(int argc, char** argv)
{
bool startup = false;
struct sigaction newsig;
close(0);
roll_log();
newsig.sa_handler = handle_signal;
sigaction(SIGHUP, &newsig, NULL);
sigaction(SIGUSR1, &newsig, NULL);
sigaction(SIGUSR2, &newsig, NULL);
daemon(1, 1);
radio.begin();
delay(5);
network.begin(/*channel*/ 90, /*node address*/ this_node);
radio.printDetails();
// Grab the list of sensors and send out requests for config
fillSensorList();
while(1)
{
network.update();
while ( network.available() ) { // Is there anything ready for us?
handleMessage();
}
checkForConfig();
if (!startup) {
startup = true;
setAllTime();
requestAllConfig();
}
checkRequests();
sendPendingMessages();
delay(5);
}
return 0;
}
// vi:set ai sw=2: