forked from ssjoholm/rfxcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfxcmd.py
executable file
·4842 lines (4110 loc) · 206 KB
/
rfxcmd.py
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# coding=UTF-8
"""
RFXCMD.PY
Copyright (C) 2012-2014 Sebastian Sjoholm, [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Version history can be found at
http://www.rfxcmd.eu
NOTES
RFXCOM is a Trademark of RFSmartLink.
----------------------------------------------------------------------------
Protocol License Agreement
The RFXtrx protocols are owned by RFXCOM, and are protected under applicable
copyright laws.
============================================================================
It is only allowed to use this protocol or any part of it for RFXCOM products
============================================================================
The above Protocol License Agreement and the permission notice shall be
included in all software using the RFXtrx protocols.
Any use in violation of the foregoing restrictions may subject the user to
criminal sanctions under applicable laws, as well as to civil liability for
the breach of the terms and conditions of this license.
"""
__author__ = "Sebastian Sjoholm"
__copyright__ = "Copyright 2012-2014, Sebastian Sjoholm"
__license__ = "GPL"
__version__ = "0.3 (" + filter(str.isdigit, "$Rev: 739 $") + ")"
__maintainer__ = "Sebastian Sjoholm"
__email__ = "[email protected]"
__status__ = "Development-Beta-1"
__date__ = "$Date: 2014-11-27 08:05:33 +0100 (Thu, 27 Nov 2014) $"
# Default modules
import pdb
import string
import sys
import os
import time
import datetime
import binascii
import traceback
import subprocess
import re
import logging
import signal
import xml.dom.minidom as minidom
from optparse import OptionParser
import socket
import select
import inspect
# RFXCMD modules
try:
from lib.rfx_socket import *
except ImportError:
print "Error: importing module from lib folder"
sys.exit(1)
try:
from lib.rfx_command import *
except ImportError:
print "Error: module lib/rfx_command not found"
sys.exit(1)
try:
from lib.rfx_utils import *
except ImportError:
print "Error: module lib/rfx_utils not found"
sys.exit(1)
try:
import lib.rfx_sensors
import lib.rfx_decode as rfxdecode
import lib.rfx_rrd as rfxrrd
import lib.rfx_xplcom as xpl
import lib.rfx_protocols as protocol
except ImportError as err:
print("Error: %s " % str(err))
sys.exit(1)
# 3rd party modules
# These might not be needed, depended on usage
# SQLite
try:
import sqlite3
except ImportError:
pass
# MySQL
try:
import MySQLdb
except ImportError:
pass
# PgSQL
try:
import psycopg2
except ImportError:
pass
# Serial
try:
import serial
except ImportError:
pass
# ------------------------------------------------------------------------------
# VARIABLE CLASSS
# ------------------------------------------------------------------------------
class config_data:
def __init__(
self,
serial_active = True,
serial_device = None,
serial_rate = 38400,
serial_timeout = 9,
mysql_active = False,
mysql_server = '',
mysql_database = '',
mysql_username = "",
mysql_password = "",
trigger_active = False,
trigger_onematch = False,
trigger_file = "",
trigger_timeout = 10,
sqlite_active = False,
sqlite_database = "",
sqlite_table = "",
pgsql_active = False,
pgsql_server = '',
pgsql_database = '',
pgsql_port = '',
pgsql_username = '',
pgsql_password = '',
pgsql_table = '',
loglevel = "info",
logfile = "rfxcmd.log",
graphite_active = False,
graphite_server = "",
graphite_port = "",
program_path = "",
xpl_active = False,
xpl_host = "",
xpl_sourcename = "rfxcmd-",
xpl_includehostname = True,
socketserver = False,
sockethost = "",
socketport = "",
whitelist_active = False,
whitelist_file = "",
daemon_active = False,
daemon_pidfile = "rfxcmd.pid",
process_rfxmsg = True,
weewx_active = False,
weewx_config = "weewx.xml",
rrd_active = False,
rrd_path = "",
barometric = 0,
log_msg = False,
log_msgfile = "",
protocol_startup = False,
protocol_file = "protocol.xml"
):
self.serial_active = serial_active
self.serial_device = serial_device
self.serial_rate = serial_rate
self.serial_timeout = serial_timeout
self.mysql_active = mysql_active
self.mysql_server = mysql_server
self.mysql_database = mysql_database
self.mysql_username = mysql_username
self.mysql_password = mysql_password
self.pgsql_active = pgsql_active
self.pgsql_server = pgsql_server
self.pgsql_database = pgsql_database
self.pgsql_port = pgsql_port
self.pgsql_username = pgsql_username
self.pgsql_password = pgsql_password
self.pgsql_table = pgsql_table
self.trigger_active = trigger_active
self.trigger_onematch = trigger_onematch
self.trigger_file = trigger_file
self.trigger_timeout = trigger_timeout
self.sqlite_active = sqlite_active
self.sqlite_database = sqlite_database
self.sqlite_table = sqlite_table
self.loglevel = loglevel
self.logfile = logfile
self.graphite_active = graphite_active
self.graphite_server = graphite_server
self.graphite_port = graphite_port
self.program_path = program_path
self.xpl_active = xpl_active
self.xpl_host = xpl_host
self.xpl_sourcename = xpl_sourcename
self.xpl_includehostname = xpl_includehostname
self.socketserver = socketserver
self.sockethost = sockethost
self.socketport = socketport
self.whitelist_active = whitelist_active
self.whitelist_file = whitelist_file
self.daemon_active = daemon_active
self.daemon_pidfile = daemon_pidfile
self.process_rfxmsg = process_rfxmsg
self.weewx_active = weewx_active
self.weewx_config = weewx_config
self.rrd_active = rrd_active
self.rrd_path = rrd_path
self.barometric = barometric
self.log_msg = log_msg
self.log_msgfile = log_msgfile
self.protocol_startup = protocol_startup
self.protocol_file = protocol_file
class cmdarg_data:
def __init__(
self,
configfile = "",
action = "",
rawcmd = "",
device = "",
createpid = False,
pidfile = "",
printout_complete = True,
printout_csv = False
):
self.configfile = configfile
self.action = action
self.rawcmd = rawcmd
self.device = device
self.createpid = createpid
self.pidfile = pidfile
self.printout_complete = printout_complete
self.printout_csv = printout_csv
class rfxcmd_data:
def __init__(
self,
reset = "0d00000000000000000000000000",
status = "0d00000002000000000000000000",
save = "0d00000006000000000000000000"
):
self.reset = reset
self.status = status
self.save = save
class serial_data:
def __init__(
self,
port = None,
rate = 38400,
timeout = 9
):
self.port = port
self.rate = rate
self.timeout = timeout
# Store the trigger data from xml file
class trigger_data:
def __init__(
self,
data = ""
):
self.data = data
# Store the whitelist data from xml file
class whitelist_data:
def __init__(
self,
data = ""
):
self.data = data
# Store the sensor id that should be received by WeeWx
class weewx_data:
def __init__(
self,
data = ""
):
self.data = data
# ----------------------------------------------------------------------------
# DEAMONIZE
# Credit: George Henze
# ----------------------------------------------------------------------------
def shutdown():
# clean up PID file after us
logger.debug("Shutdown")
if cmdarg.createpid:
logger.debug("Removing PID file " + str(cmdarg.pidfile))
os.remove(cmdarg.pidfile)
if serial_param.port is not None:
logger.debug("Close serial port")
serial_param.port.close()
serial_param.port = None
logger.debug("Exit 0")
sys.stdout.flush()
os._exit(0)
def handler(signum=None, frame=None):
if type(signum) != type(None):
logger.debug("Signal %i caught, exiting..." % int(signum))
shutdown()
def daemonize():
try:
pid = os.fork()
if pid != 0:
sys.exit(0)
except OSError, e:
raise RuntimeError("1st fork failed: %s [%d]" % (e.strerror, e.errno))
os.setsid()
prev = os.umask(0)
os.umask(prev and int('077', 8))
try:
pid = os.fork()
if pid != 0:
sys.exit(0)
except OSError, e:
raise RuntimeError("2nd fork failed: %s [%d]" % (e.strerror, e.errno))
dev_null = file('/dev/null', 'r')
os.dup2(dev_null.fileno(), sys.stdin.fileno())
if cmdarg.createpid == True:
pid = str(os.getpid())
logger.debug("Writing PID " + pid + " to " + str(cmdarg.pidfile))
file(cmdarg.pidfile, 'w').write("%s\n" % pid)
# ----------------------------------------------------------------------------
# C __LINE__ equivalent in Python by Elf Sternberg
# http://www.elfsternberg.com/2008/09/23/c-__line__-equivalent-in-python/
# ----------------------------------------------------------------------------
def _line():
info = inspect.getframeinfo(inspect.currentframe().f_back)[0:3]
return '[%s:%d]' % (info[2], info[1])
# ----------------------------------------------------------------------------
def send_graphite(CARBON_SERVER, CARBON_PORT, lines):
"""
Send data to graphite
Credit: Frédéric Pégé
"""
sock = None
for res in socket.getaddrinfo(CARBON_SERVER,int(CARBON_PORT), socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
except socket.error as msg:
sock = None
continue
try:
sock.connect(sa)
except socket.error as msg:
sock.close()
sock = None
continue
break
if sock is None:
print 'could not open socket'
sys.exit(1)
message = '\n'.join(lines) + '\n' #all lines must end in a newline
sock.sendall(message)
sock.close()
# ----------------------------------------------------------------------------
def readbytes(number):
"""
Read x amount of bytes from serial port.
Credit: Boris Smus http://smus.com
"""
buf = ''
for i in range(number):
try:
byte = serial_param.port.read()
except IOError, e:
print "Error: %s" % e
except OSError, e:
print "Error: %s" % e
buf += byte
return buf
# ----------------------------------------------------------------------------
def insert_database(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13):
"""
Choose in which database insert datas
"""
logger.debug('insert_database')
# MYSQL
if config.mysql_active:
logger.debug('-> MySQL')
insert_mysql(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13)
# SQLITE
if config.sqlite_active:
logger.debug('-> SqLite')
insert_sqlite(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13)
# PGSQL
if config.pgsql_active:
logger.debug('-> PGSql')
insert_pgsql(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13)
# ----------------------------------------------------------------------------
def insert_mysql(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13):
"""
Insert data to MySQL.
"""
db = None
try:
if data13 == 0:
data13 = "0000-00-00 00:00:00"
db = MySQLdb.connect(config.mysql_server, config.mysql_username, config.mysql_password, config.mysql_database)
cursor = db.cursor()
sql = """
INSERT INTO rfxcmd (datetime, unixtime, packettype, subtype, seqnbr, battery, rssi, processed, data1, data2, data3, data4,
data5, data6, data7, data8, data9, data10, data11, data12, data13)
VALUES ('%s','%s','%s','%s','%s','%s','%s',0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
""" % (timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3, data4, data5, data6, data7,
data8, data9, data10, data11, data12, data13)
cursor.execute(sql)
db.commit()
except MySQLdb.Error, e:
logger.error("Line: " + _line())
logger.error("SqLite error: %d: %s" % (e.args[0], e.args[1]))
print "MySQL error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
finally:
if db:
db.close()
# ----------------------------------------------------------------------------
def insert_sqlite(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13):
"""
Insert data to SqLite.
"""
cx = None
try:
cx = sqlite3.connect(config.sqlite_database)
cu = cx.cursor()
sql = """
INSERT INTO '%s' (datetime, unixtime, packettype, subtype, seqnbr, battery, rssi, processed, data1, data2, data3, data4,
data5, data6, data7, data8, data9, data10, data11, data12, data13)
VALUES('%s','%s','%s','%s','%s','%s','%s',0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
""" % (config.sqlite_table, timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13)
cu.executescript(sql)
cx.commit()
except sqlite3.Error, e:
if cx:
cx.rollback()
logger.error("Line: " + _line())
logger.error("SqLite error: %s" % e.args[0])
print "SqLite error: %s" % e.args[0]
sys.exit(1)
finally:
if cx:
cx.close()
# ----------------------------------------------------------------------------
def insert_pgsql(timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3,
data4, data5, data6, data7, data8, data9, data10, data11, data12, data13):
"""
Insert data to PgSQL
Credits: Pierre-Yves
"""
db = None
dsn = "dbname='%s' user='%s' host='%s' port=%s password=%s" \
% (config.pgsql_database, config.pgsql_username, config.pgsql_server, config.pgsql_port, config.pgsql_password)
try:
if data13 == 0:
data13 = "NULL"
db = psycopg2.connect(dsn)
cursor = db.cursor()
sql = """
INSERT INTO %s (datetime, unixtime, packettype, subtype, seqnbr, battery, rssi, processed, data1, data2, data3, data4,
data5, data6, data7, data8, data9, data10, data11, data12, data13)
VALUES ('%s','%s','%s','%s','%s','%s','%s',0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', '%s UTC')
""" % (config.pgsql_table, timestamp, unixtime, packettype, subtype, seqnbr, battery, signal, data1, data2, data3, data4, data5, data6, data7,
data8, data9, data10, data11, data12, data13)
logger.debug("SQL: %s" % str(sql))
cursor.execute(sql)
db.commit()
except psycopg2.DatabaseError, e:
logger.error("Line: " + _line())
logger.error("PgSQL error: %s" % e)
print "Error : (PgSQL Query) : %s " % e
sys.exit(1)
finally:
if db:
db.close()
# ----------------------------------------------------------------------------
def decodePacket(message):
"""
Decode incoming RFXtrx message.
"""
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
unixtime_utc = int(time.time())
decoded = False
db = ""
# Verify incoming message
logger.debug("Verify incoming packet")
if not test_rfx( ByteToHex(message) ):
logger.error("The incoming message is invalid (" + ByteToHex(message) + ") Line: " + _line())
if cmdarg.printout_complete == True:
print "Error: The incoming message is invalid " + _line()
return
else:
logger.debug("Verified OK")
raw_message = ByteToHex(message)
raw_message = raw_message.replace(' ', '')
packettype = ByteToHex(message[1])
logger.debug("PacketType: %s" % str(packettype))
if len(message) > 2:
subtype = ByteToHex(message[2])
logger.debug("SubType: %s" % str(subtype))
if len(message) > 3:
seqnbr = ByteToHex(message[3])
logger.debug("SeqNbr: %s" % str(seqnbr))
if len(message) > 4:
id1 = ByteToHex(message[4])
logger.debug("Id1: %s" % str(id1))
if len(message) > 5:
id2 = ByteToHex(message[5])
logger.debug("Id2: %s" % str(id2))
if cmdarg.printout_complete:
print "Packettype\t\t= " + rfx.rfx_packettype[packettype]
# ---------------------------------------
# Check if the packet is a special WeeWx packet
# 0A1100FF001100FF001100
# ---------------------------------------
if raw_message == "0A1100FF001100FF001100":
logger.debug("Incoming WeeWx packet, do not decode")
if cmdarg.printout_complete:
print("Info\t\t\t= Incoming WeeWx packet")
decoded = True
return
# ---------------------------------------
# Verify correct length on packets
# ---------------------------------------
logger.debug("Verify correct packet length")
if packettype == '00' and len(message) <> 14:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '01' and len(message) <> 14:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '02' and len(message) <> 5:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '10' and len(message) <> 8:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '11' and len(message) <> 12:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '12' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '13' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '15' and len(message) <> 12:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '16' and len(message) <> 8:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '17' and len(message) <> 8:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '18' and len(message) <> 8:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '19' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '1A' and len(message) <> 13:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '20' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '28' and len(message) <> 7:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '30' and len(message) <> 7:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '40' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '41' and len(message) <> 7:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '42' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '4E' and len(message) <> 11:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '4F' and len(message) <> 11:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '50' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '51' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '52' and len(message) <> 11:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '53' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '54' and len(message) <> 14:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '55' and len(message) <> 12:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '56' and len(message) <> 17:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '57' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '58' and len(message) <> 14:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '59' and len(message) <> 14:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '5A' and len(message) <> 18:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '5B' and len(message) <> 20:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '5C' and len(message) <> 16:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '5D' and len(message) <> 9:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '70' and len(message) <> 8:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '71' and len(message) <> 11:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
if packettype == '72' and len(message) <> 10:
logger.error("Packet has wrong length, discarding")
decoded = True
packettype = None
# ---------------------------------------
# If packet is OK and the log_msg is active
# then save the packet to log_msgfile designated
# file on disk
# ---------------------------------------
if decoded == False and config.log_msg == True:
logger.debug("Save packet to log_msgfile")
try:
data = str(ByteToHex(message))
data = data.replace(' ', '')
file = open(config.log_msgfile,"a+")
file.write(data + "\n")
file.close()
except Exception, e:
logger.error("Error when trying to write message log")
logger.error("Exception: %s" % str(e))
pass
# ---------------------------------------
# 0x0 - Interface Control
# ---------------------------------------
if packettype == '00':
logger.debug("Decode packetType 0x" + str(packettype) + " - Start")
decoded = True
# ---------------------------------------
# 0x01 - Interface Message
# ---------------------------------------
if packettype == '01':
logger.debug("Decode packetType 0x" + str(packettype) + " - Start")
decoded = True
if cmdarg.printout_complete:
data = {
'packetlen' : ByteToHex(message[0]),
'packettype' : ByteToHex(message[1]),
'subtype' : ByteToHex(message[2]),
'seqnbr' : ByteToHex(message[3]),
'cmnd' : ByteToHex(message[4]),
'msg1' : ByteToHex(message[5]),
'msg2' : ByteToHex(message[6]),
'msg3' : ByteToHex(message[7]),
'msg4' : ByteToHex(message[8]),
'msg5' : ByteToHex(message[9]),
'msg6' : ByteToHex(message[10]),
'msg7' : ByteToHex(message[11]),
'msg8' : ByteToHex(message[12]),
'msg9' : ByteToHex(message[13])
}
# Subtype
if data['subtype'] == '00':
print "Subtype\t\t\t= Interface response"
else:
print "Subtype\t\t\t= Unknown type (" + data['packettype'] + ")"
# Seq
print "Sequence nbr\t\t= " + data['seqnbr']
# Command
print "Response on cmnd\t= " + rfx.rfx_cmnd[data['cmnd']]
# MSG 1
print "Transceiver type\t= " + rfx.rfx_subtype_01_msg1[data['msg1']]
# MSG 2
print "Firmware version\t= " + str(int(data['msg2'],16))
print "Protocols:"
# ------------------------------------------------------
# MSG 3
protocol = str(rfx.rfx_subtype_01_msg3['128'])
if testBit(int(data['msg3'],16),7) == 128:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['64'])
if testBit(int(data['msg3'],16),6) == 64:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['32'])
if testBit(int(data['msg3'],16),5) == 32:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['16'])
if testBit(int(data['msg3'],16),4) == 16:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['8'])
if testBit(int(data['msg3'],16),3) == 8:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['4'])
if testBit(int(data['msg3'],16),2) == 4:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['2'])
if testBit(int(data['msg3'],16),1) == 2:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg3['1'])
if testBit(int(data['msg3'],16),0) == 1:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
# ------------------------------------------------------
# MSG 4
protocol = str(rfx.rfx_subtype_01_msg4['128'])
if testBit(int(data['msg4'],16),7) == 128:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['64'])
if testBit(int(data['msg4'],16),6) == 64:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['32'])
if testBit(int(data['msg4'],16),5) == 32:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['16'])
if testBit(int(data['msg4'],16),4) == 16:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['8'])
if testBit(int(data['msg4'],16),3) == 8:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['4'])
if testBit(int(data['msg4'],16),2) == 4:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['2'])
if testBit(int(data['msg4'],16),1) == 2:
print("%-25s Enabled" % protocol)
else:
print("%-25s Disabled" % protocol)
protocol = str(rfx.rfx_subtype_01_msg4['1'])
if testBit(int(data['msg4'],16),0) == 1: