-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathukhasnet.pl
More file actions
executable file
·367 lines (334 loc) · 14.2 KB
/
ukhasnet.pl
File metadata and controls
executable file
·367 lines (334 loc) · 14.2 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
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
#!/usr/bin/perl -w
use strict;
use Time::Local;
use Digest::CRC qw(crcccitt);
use DBI();
use POSIX qw(setsid);
use Sys::Syslog;
use JSON::XS qw( decode_json);
# TODO List
# Check SQL inserts/updates work otherwise log an error message and set $error=1 - or $error=1 on ->execute() ??
# Move packet data Processing into sub?
# Test Transactions work (i.e. if a query fails we roll it all back) - How to test?
# How to deal with sequence = a
# Should also not link to previous packets (requires us to remember state)
# Load/cache fieldtypes into hashref
# Load Configuration from config.json
my $config;
if ( -f 'config.json' ){
open(_CONF, "<", "config.json");
my $config_raw=do { local $/=undef; <_CONF>};
close(_CONF);
$config=decode_json($config_raw);
} else {
die "config.json is not present - you need to create it\n";
}
# TODO Test that config.json has the required values for us to run
# TODO We should probably check it's safe to eval the value here...
my $lock_file=eval($config->{'lock_file'});
if ( -f $lock_file){
print "A process is already running\n";
exit -1;
}
open (_LOCK, ">", $lock_file) or die "Unable to create lockfile\n";
&daemonize;
print _LOCK "$$";
close _LOCK;
my $lastpos=0;
my $loop=1;
my $entries=0;
my $dbh=0;
# Syslog
openlog('ukhasnet', 'cons,pid', $config->{'syslog'}{'facility'});
$SIG{HUP}= \&catch_hup;
$SIG{INT}= \&catch_hup;
syslog('info', 'Starting');
while ($loop){
$entries=0; # Reset Counter
$dbh = DBI->connect("DBI:Pg:host=$config->{'database'}{'host'};database=$config->{'database'}{'database'}", $config->{'database'}{'user'},$config->{'database'}{'password'});
# Check we're connected to the DB
if ($dbh){
$dbh->do("SET search_path TO ukhasnet");
# Prep SQL Statements
my $getUploads=$dbh->prepare("select id, nodeid, extract(epoch from time) as time, packet from upload where state='Pending' ORDER BY time DESC LIMIT 50");
my $findPacket=$dbh->prepare("
select packet.id as packetid from packet left join nodes on packet.originid=nodes.id where
name=? and sequence=? and checksum=? and to_timestamp(?)
between
(select min(packet_rx_time) from packet_rx where packetid=packet.id) - interval '1' minute
and
(select max(packet_rx_time) from packet_rx where packetid=packet.id) + interval '1' minute");
my $addPacket=$dbh->prepare("insert into packet (originid, sequence, checksum) values (?, ?, ?)");
my $nodeLastPacket=$dbh->prepare("update nodes set lastpacket=? where id=?");
my $addPacketRX=$dbh->prepare("insert into packet_rx (packetid, gatewayid, packet_rx_time, uploadid) values (?, ?, to_timestamp(?), ?)");
my $addPath=$dbh->prepare("insert into path (packet_rx_id, position, nodeid) values (?, ?, ?)");
my $uploadUpdate=$dbh->prepare("update upload set packetid=?, state='Processed' where id=?");
my $uploadFailed=$dbh->prepare("update upload set state='Error' where id=?");
# Queries for handling data portion of the packet
my $getRawData=$dbh->prepare("select id, packetid, data from rawdata where state='Pending' LIMIT 60");
my $getField=$dbh->prepare("select id, type from fieldtypes where dataid=?"); # TODO Can we cache this ???
my $getNodeFromPacket=$dbh->prepare("select originid from ukhasnet.packet where id=?");
my $data_float=$dbh->prepare("insert into data_float (packetid, fieldid, data, position) values (?, ?, ?, ?)");
my $data_int=$dbh->prepare("insert into data_int (packetid, fieldid, data, position) values (?, ?, ?, ?)");
my $data_location=$dbh->prepare("insert into data_location (packetid, latitude, longitude, altitude) values (?, ?, ?, ?)");
my $updateNodeLocation=$dbh->prepare('update nodes set locationid=$2 where id=$1');
# data int
# data string
my $data_raw=$dbh->prepare("insert into rawdata (packetid, data, state) values (?, ?, ?)");
my $dataProcessed=$dbh->prepare('update rawdata set state=$2 where id=$1');
my $delRaw=$dbh->prepare('delete from rawdata where id=?');
# Notify for websockets
my $notify=$dbh->prepare('NOTIFY upload_parse, \'{"i": ?,"s":? }\'');
# Get Pending records from the upload Table
$getUploads->execute();
while (my $record=$getUploads->fetchrow_hashref){
syslog('info', "Processing Packet \"".$record->{'packet'}."\"(".$record->{'id'}.")");
$entries++;
if ($record->{'packet'} =~ /^\r?([0-9])([a-z])([A-Z:][^\[\]|]+)\[([A-Za-z0-9,]+)\]\r?$/){
$dbh->begin_work(); # Start Transaction
my $error=0;
#my $repeat=$1;
my $seq=$2;
my $data=$3;
my $path=$4;
my ($origin,$therest) = split(',', $path);
my $csum=crcccitt($seq . $data . $origin);
# See if the packet has already been seen - If not store it.
my $PacketID=0;
$findPacket->execute($origin, $seq, $csum, $record->{'time'});
if ($findPacket->rows()==1){ # Single Match - GOOD
my $row=$findPacket->fetchrow_hashref;
$PacketID=$row->{'packetid'};
} elsif ($findPacket->rows()>1){ # Multiple Matches - Bad
$error=1;
syslog('warning', "Error: Input line ".$record->{'packet'}."(".$record->{'id'}.") matches more than one packet in DB");
while (my $row=$findPacket->fetchrow_hashref){
syslog('warning', "Info: Upload ".$record->{'id'}." Potentially matches packet:" . $row->{'packetid'});
}
} else {
# Get NodeID for Origin
my $nodeID=&getNodeID($origin);
if ($nodeID >0){
$addPacket->execute($nodeID,$seq,$csum);
$PacketID=$dbh->last_insert_id(undef, "ukhasnet", "packet", undef);
# Store the Data portion of the packet in rawdata for later processing
$data_raw->execute($PacketID, $data, 'Pending');
# Update the nodes table to set the last packet value
$nodeLastPacket->execute($PacketID, $nodeID);
# Notify the IRC bot if the data portion contains a :
if ($data =~/:/){
$dbh->do("NOTIFY ircbot, 'msg_rx:".$PacketID."'");
}
} else {
$error=1;
syslog('warning', "Error: (addPacket) Unable to get NodeID");
}
}
# Store details of this reception of the packet
if ($PacketID > 0){
$addPacketRX->execute($PacketID, $record->{'nodeid'}, $record->{'time'},$record->{'id'});
my $rxID=$dbh->last_insert_id(undef, "ukhasnet", "packet_rx", undef);
$uploadUpdate->execute($PacketID,$record->{'id'});
my $hop=0;
foreach (split(',', $path)){
my $nodeID=&getNodeID($_);
if ($nodeID >0){
$addPath->execute($rxID, $hop++, $nodeID);
} else {
$error=1;
syslog('warning', "Error: (addPath) Unable to get NodeID($PacketID:$rxID)");
}
}
}
if ($error == 0){
$dbh->commit();
#$notify->execute($record->{'id'},"Processed");
$dbh->do("NOTIFY upload_parse, '{\"i\": ".$record->{'id'}.",\"s\": \"Processed\"}'");
# NOTIFY upload_parse, '{"i":<uploadid>,"s":"Processed"}';
} else {
# Rollback
$dbh->rollback();
$uploadFailed->execute($record->{'id'});
syslog("warning", "Error: DB Transaction failed for upload: ".$record->{'id'});
#die "DB Transaction failed\n";
}
} else {
# Unknown packet format
print "?> ".$record->{'packet'}."(".$record->{'id'}.")\n";
$uploadFailed->execute($record->{'id'}); # Removed for testing
}
} #while (my $record=$getUploads->fetchrow_hashref){
# Process the data in rawdata
$getRawData->execute();
while (my $datarow=$getRawData->fetchrow_hashref){
$dbh->begin_work(); # Start Transaction
my $error=0;
my $data=$datarow->{'data'};
# Process packet data
while ($data) {
syslog('info', "Processing Data \"$data(".$datarow->{'id'}.")");
my ($var, $val);
($var, $val, $data) = &splitData($data);
if ($var =~ /[A-Z]/){
$getField->execute($var);
if ($getField->rows()==1){
my $type=$getField->fetchrow_hashref;
if ($type->{'type'} eq "Float"){
my $p=0;
foreach my $v (split(/,/, $val)){
if ($v =~ /^[-]?\d+(?:[.]\d+)?$/){
$data_float->execute($datarow->{'packetid'}, $type->{'id'}, $v, $p++);
} else {
syslog('warning', "Error processing type ".$type->{'id'}." with value $v (".$datarow->{'packetid'}.")");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
}
} elsif ($type->{'type'} eq "Integer"){
my $p=0;
foreach my $v (split(/,/, $val)){
if ($v =~ /^[-]?\d{1,9}$/){
$data_int->execute($datarow->{'packetid'}, $type->{'id'}, $v, $p++);
} else {
syslog('warning', "Error processing type ".$type->{'id'}." with value $v (".$datarow->{'packetid'}.")");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
}
#syslog('warning', "Error: Cant store ".$type->{'type'}." for ".$var.$val."($datarow->{'packetid'})");
#$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
} elsif ($type->{'type'} eq "String"){
syslog('warning', "Error: Cant store ".$type->{'type'}." for ".$var.$val."($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
} elsif ($type->{'type'} eq "Location"){
if ($var eq 'L'){ # We only know how to deal with Locations of type L anything else is an error
my $locID=0;
if ($val =~ /^([+-]?[0-9]+\.[0-9]+),([+-]?[0-9]+\.[0-9]+)$/){ # Lat,Lon
$data_location->execute($datarow->{'packetid'}, $1, $2, undef);
$locID=$dbh->last_insert_id(undef, "ukhasnet", "data_location", undef);
} elsif ($val =~ /^([+-]?[0-9]+\.[0-9]+),([+-]?[0-9]+\.[0-9]+),([+-]?[0-9]+\.?[0-9]*)$/){ # Lat,Lon, Alt
$data_location->execute($datarow->{'packetid'}, $1, $2, $3);
$locID=$dbh->last_insert_id(undef, "ukhasnet", "data_location", undef);
} elsif ($val =~ /^([+-]?[0-9]+),([+-]?[0-9]+)$/){ # integer Lat,Lon (Hacky for balloons)
$data_location->execute($datarow->{'packetid'}, ($1/10000), ($2/10000), undef);
$locID=$dbh->last_insert_id(undef, "ukhasnet", "data_location", undef);
} elsif ($val =~ /^([+-]?[0-9]+),([+-]?[0-9]+),([+-]?[0-9]+)$/){ # Integer Lat,Lon, Alt (Hacky for balloons)
$data_location->execute($datarow->{'packetid'}, ($1/10000), ($2/10000), $3);
$locID=$dbh->last_insert_id(undef, "ukhasnet", "data_location", undef);
} else {
syslog('warning', "Error: Can't parse Location (".$var.$val.":".$datarow->{'packetid'}.")");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
if ($locID>0){
$getNodeFromPacket->execute($datarow->{'packetid'});
if ($getNodeFromPacket->rows()==1){
my $nodeRow=$getNodeFromPacket->fetchrow_hashref;
$updateNodeLocation->execute($nodeRow->{'originid'},$locID);
#print "Packet:" . $datarow->{'packetid'} .", LocationID: $locID, Node:".$nodeRow->{'originid'}."\n";
} else {
syslog('warning', "Error: Can't get node for packet: ".$datarow->{'packetid'});
}
}
} else { # if eq L
syslog('warning', "Error: Cant store ".$type->{'type'}." for ".$var.$val."($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
} elsif ($type->{'type'} eq "NULL"){
syslog('warning', "Error: Cant store ".$type->{'type'}." for ".$var.$val."($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
} else {
syslog('warning', "Error: Unknown type(".$type->{'type'}.") for ".$var.$val."($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
} else {
syslog('warning', "Error: Wrong number of rows for ".$var.$val."($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $var.$val, 'Error');
}
} else {
syslog('warning', "Error: Cannot process $data($datarow->{'packetid'})");
$data_raw->execute($datarow->{'packetid'}, $data, 'Error');
undef($data);
}
} # while($data)
#$dataProcessed->execute($datarow->{'id'}, 'Processed');
$delRaw->execute($datarow->{'id'});
if ($error == 0){
$dbh->commit();
} else {
# Rollback
$dbh->rollback();
$dataProcessed->execute($datarow->{'id'}, 'Error');
die "DB Transaction failed\n";
}
} # while($datarow
# Close SQL Connections
$getUploads->finish();
$findPacket->finish();
$addPacket->finish();
$nodeLastPacket->finish();
$addPacketRX->finish();
$addPath->finish();
$uploadUpdate->finish();
$uploadFailed->finish();
$getRawData->finish();
$getField->finish();
$getNodeFromPacket->finish();
$data_float->finish();
$data_location->finish();
$updateNodeLocation->finish();
$data_raw->finish();
$dataProcessed->finish();
$delRaw->finish();
$dbh->disconnect();
sleep (5) if ($loop && ($entries==0));
} else { #if ($dbh)
syslog('warning', "Error: Unable to connect to DB");
sleep(60) if $loop;
}
} #while($loop)
unlink $lock_file;
syslog('info', 'Finished');
sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
sub catch_hup {
$loop=0;
syslog('warning', 'Got HUP');
}
sub getNodeID {
die "Wrong number of args to getNodeID\n" if (scalar(@_) !=1 );
my $origin=$_[0];
my $nodeID=0;
my $getNode=$dbh->prepare("select id from nodes where name = ?");
my $addNode=$dbh->prepare("insert into nodes (name) values (?)");
$getNode->execute($origin);
if ($getNode->rows()==1){
my $nodeRow=$getNode->fetchrow_hashref;
$nodeID=$nodeRow->{'id'};
} elsif ($getNode->rows()>1){
syslog('warning', "Error(getNodeID): Matched more than one node for $origin");
#while (my $nodeRow=$getNode->fetchrow_hashref){
# syslog('warning', "Error(getNodeID): Node $origin could be id: " . $nodeRow->{'id'});
#}
} else {
$addNode->execute($origin);
$nodeID=$dbh->last_insert_id(undef, "ukhasnet", "nodes", undef);
syslog('info', "(getNodeID): Added Node $origin($nodeID)");
}
#$getNode->finish();
#$addNode->finish();
return $nodeID;
}
sub splitData {
die "Wrong number of args to splitData\n" if (scalar(@_) !=1 );
my $data=$_[0];
#if ($data =~ /^([A-Z])([0-9\.,\-]+)(.*)$/ ){
if ($data =~ /^([A-Z])([0-9\.,\-]+)([A-Z:].*)?$/ ){
return ($1, $2, $3);
} else {
syslog('warning', "Error(splitData): No Match for $data");
return (-1,-1,$data);
}
}