Add basic plugins for paradox

parent 6e93808d
# Ignore everything in this directory
*
# Except this file
!.gitignore
"""
This package is statically programmed and dynamically refernce in from the main script.
Within the config.ini file your "Alarm_Registry_Map" and "Alarm_Event_Map" values are appended
with "Registers()" and "EventMap" respectively to load the correct classes for your alarm.
To enable the updating of label names to those configured in your alarm, update the "SupportedItems"
dictionary in the ..."Registers" Class below. The main script will iterate through this list and dynamically
build the relevant get and set functions from the Registers and EventMap classes to update the values
it publishes.
For this reason (dynamic build of function names) the function names listed in the "supportedItems" dictionary
must be an exact (case-sensitive) replica of the relevant functions to interact with its dictionaries.
"""
"""
MG5050: This class maps all MG5050 V4.xx register to labels or output actions
"""
class ParadoxMG5050Registers():
# Link different registry mappings here to event label entries, note that functions to
# poll & retrieve these externally must be structure as get/set/getall in the respctive classes
# with names that exactly match the "supportedItems" below and dictionary names.
supportedItems = {"zoneLabel",
"outputLabel",
"partitionLabel",
"userLabel",
"busModuleLabel",
"wirelessRepeaterLabel",
"wirelessSirenLabel",
"wirelessKeypadLabel",
"siteNameLabel"
}
zoneLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x00\x10", "Receive": {"Start": 20, "Finish": 36}}, # I.e. get data from register \x50000010, extract text from posisiton 20 to 36
2: {"Send": "\x50\x00\x00\x10", "Receive": {"Start": 36, "Finish": 52}},
3: {"Send": "\x50\x00\x00\x30", "Receive": {"Start": 20, "Finish": 36}},
4: {"Send": "\x50\x00\x00\x30", "Receive": {"Start": 36, "Finish": 52}},
5: {"Send": "\x50\x00\x00\x50", "Receive": {"Start": 20, "Finish": 36}},
6: {"Send": "\x50\x00\x00\x50", "Receive": {"Start": 36, "Finish": 52}},
7: {"Send": "\x50\x00\x00\x70", "Receive": {"Start": 20, "Finish": 36}},
8: {"Send": "\x50\x00\x00\x70", "Receive": {"Start": 36, "Finish": 52}},
9: {"Send": "\x50\x00\x00\x90", "Receive": {"Start": 20, "Finish": 36}},
10: {"Send": "\x50\x00\x00\x90", "Receive": {"Start": 36, "Finish": 52}},
11: {"Send": "\x50\x00\x00\xb0", "Receive": {"Start": 20, "Finish": 36}},
12: {"Send": "\x50\x00\x00\xb0", "Receive": {"Start": 36, "Finish": 52}},
13: {"Send": "\x50\x00\x00\xd0", "Receive": {"Start": 20, "Finish": 36}},
14: {"Send": "\x50\x00\x00\xd0", "Receive": {"Start": 36, "Finish": 52}},
15: {"Send": "\x50\x00\x00\xf0", "Receive": {"Start": 20, "Finish": 36}},
16: {"Send": "\x50\x00\x00\xf0", "Receive": {"Start": 36, "Finish": 52}},
17: {"Send": "\x50\x00\x01\x10", "Receive": {"Start": 20, "Finish": 36}},
18: {"Send": "\x50\x00\x01\x10", "Receive": {"Start": 36, "Finish": 52}},
19: {"Send": "\x50\x00\x01\x30", "Receive": {"Start": 20, "Finish": 36}},
20: {"Send": "\x50\x00\x01\x30", "Receive": {"Start": 36, "Finish": 52}},
21: {"Send": "\x50\x00\x01\x50", "Receive": {"Start": 20, "Finish": 36}},
22: {"Send": "\x50\x00\x01\x50", "Receive": {"Start": 36, "Finish": 52}},
23: {"Send": "\x50\x00\x01\x70", "Receive": {"Start": 20, "Finish": 36}},
24: {"Send": "\x50\x00\x01\x70", "Receive": {"Start": 36, "Finish": 52}},
25: {"Send": "\x50\x00\x01\x90", "Receive": {"Start": 20, "Finish": 36}},
26: {"Send": "\x50\x00\x01\x90", "Receive": {"Start": 36, "Finish": 52}},
27: {"Send": "\x50\x00\x01\xb0", "Receive": {"Start": 20, "Finish": 36}},
28: {"Send": "\x50\x00\x01\xb0", "Receive": {"Start": 36, "Finish": 52}},
29: {"Send": "\x50\x00\x01\xd0", "Receive": {"Start": 20, "Finish": 36}},
30: {"Send": "\x50\x00\x01\xd0", "Receive": {"Start": 36, "Finish": 52}},
31: {"Send": "\x50\x00\x01\xf0", "Receive": {"Start": 20, "Finish": 36}},
32: {"Send": "\x50\x00\x01\xf0", "Receive": {"Start": 36, "Finish": 52}},
}
outputLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x02\x10", "Receive": {"Start": 20, "Finish": 36}},
2: {"Send": "\x50\x00\x02\x10", "Receive": {"Start": 36, "Finish": 52}},
3: {"Send": "\x50\x00\x02\x30", "Receive": {"Start": 20, "Finish": 36}},
4: {"Send": "\x50\x00\x02\x30", "Receive": {"Start": 36, "Finish": 52}},
5: {"Send": "\x50\x00\x02\x50", "Receive": {"Start": 20, "Finish": 36}},
6: {"Send": "\x50\x00\x02\x50", "Receive": {"Start": 36, "Finish": 52}},
7: {"Send": "\x50\x00\x02\x70", "Receive": {"Start": 20, "Finish": 36}},
8: {"Send": "\x50\x00\x02\x70", "Receive": {"Start": 36, "Finish": 52}},
9: {"Send": "\x50\x00\x02\x90", "Receive": {"Start": 20, "Finish": 36}},
10: {"Send": "\x50\x00\x02\x90", "Receive": {"Start": 36, "Finish": 52}},
11: {"Send": "\x50\x00\x02\xb0", "Receive": {"Start": 20, "Finish": 36}},
12: {"Send": "\x50\x00\x02\xb0", "Receive": {"Start": 36, "Finish": 52}},
13: {"Send": "\x50\x00\x02\xd0", "Receive": {"Start": 20, "Finish": 36}},
14: {"Send": "\x50\x00\x02\xd0", "Receive": {"Start": 36, "Finish": 52}},
15: {"Send": "\x50\x00\x02\xf0", "Receive": {"Start": 20, "Finish": 36}},
16: {"Send": "\x50\x00\x02\xf0", "Receive": {"Start": 36, "Finish": 52}}
}
partitionLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x03\x10", "Receive": {"Start": 20, "Finish": 36}},
2: {"Send": "\x50\x00\x03\x10", "Receive": {"Start": 36, "Finish": 52}}
}
userLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x03\x30", "Receive": {"Start": 20, "Finish": 36}},
2: {"Send": "\x50\x00\x03\x30", "Receive": {"Start": 36, "Finish": 52}},
3: {"Send": "\x50\x00\x03\x50", "Receive": {"Start": 20, "Finish": 36}},
4: {"Send": "\x50\x00\x03\x50", "Receive": {"Start": 36, "Finish": 52}},
5: {"Send": "\x50\x00\x03\x70", "Receive": {"Start": 20, "Finish": 36}},
6: {"Send": "\x50\x00\x03\x70", "Receive": {"Start": 36, "Finish": 52}},
7: {"Send": "\x50\x00\x03\x90", "Receive": {"Start": 20, "Finish": 36}},
8: {"Send": "\x50\x00\x03\x90", "Receive": {"Start": 36, "Finish": 52}},
9: {"Send": "\x50\x00\x03\xb0", "Receive": {"Start": 20, "Finish": 36}},
10: {"Send": "\x50\x00\x03\xb0", "Receive": {"Start": 36, "Finish": 52}},
11: {"Send": "\x50\x00\x03\xd0", "Receive": {"Start": 20, "Finish": 36}},
12: {"Send": "\x50\x00\x03\xd0", "Receive": {"Start": 36, "Finish": 52}},
13: {"Send": "\x50\x00\x03\xf0", "Receive": {"Start": 20, "Finish": 36}},
14: {"Send": "\x50\x00\x03\xf0", "Receive": {"Start": 36, "Finish": 52}},
15: {"Send": "\x50\x00\x04\x10", "Receive": {"Start": 20, "Finish": 36}},
16: {"Send": "\x50\x00\x04\x10", "Receive": {"Start": 36, "Finish": 52}},
17: {"Send": "\x50\x00\x04\x30", "Receive": {"Start": 20, "Finish": 36}},
18: {"Send": "\x50\x00\x04\x30", "Receive": {"Start": 36, "Finish": 52}},
19: {"Send": "\x50\x00\x04\x50", "Receive": {"Start": 20, "Finish": 36}},
20: {"Send": "\x50\x00\x04\x50", "Receive": {"Start": 36, "Finish": 52}},
21: {"Send": "\x50\x00\x04\x70", "Receive": {"Start": 20, "Finish": 36}},
22: {"Send": "\x50\x00\x04\x70", "Receive": {"Start": 36, "Finish": 52}},
23: {"Send": "\x50\x00\x04\x90", "Receive": {"Start": 20, "Finish": 36}},
24: {"Send": "\x50\x00\x04\x90", "Receive": {"Start": 36, "Finish": 52}},
25: {"Send": "\x50\x00\x04\xb0", "Receive": {"Start": 20, "Finish": 36}},
26: {"Send": "\x50\x00\x04\xb0", "Receive": {"Start": 36, "Finish": 52}},
27: {"Send": "\x50\x00\x04\xd0", "Receive": {"Start": 20, "Finish": 36}},
28: {"Send": "\x50\x00\x04\xd0", "Receive": {"Start": 36, "Finish": 52}},
29: {"Send": "\x50\x00\x04\xf0", "Receive": {"Start": 20, "Finish": 36}},
30: {"Send": "\x50\x00\x04\xf0", "Receive": {"Start": 36, "Finish": 52}},
31: {"Send": "\x50\x00\x05\x10", "Receive": {"Start": 20, "Finish": 36}},
32: {"Send": "\x50\x00\x05\x10", "Receive": {"Start": 36, "Finish": 52}},
}
busModuleLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x05\x30", "Receive": {"Start": 20, "Finish": 36}},
2: {"Send": "\x50\x00\x05\x30", "Receive": {"Start": 36, "Finish": 52}},
3: {"Send": "\x50\x00\x05\x50", "Receive": {"Start": 20, "Finish": 36}},
4: {"Send": "\x50\x00\x05\x50", "Receive": {"Start": 36, "Finish": 52}},
5: {"Send": "\x50\x00\x05\x70", "Receive": {"Start": 20, "Finish": 36}},
6: {"Send": "\x50\x00\x05\x70", "Receive": {"Start": 36, "Finish": 52}},
7: {"Send": "\x50\x00\x05\x90", "Receive": {"Start": 20, "Finish": 36}},
8: {"Send": "\x50\x00\x05\x90", "Receive": {"Start": 36, "Finish": 52}},
9: {"Send": "\x50\x00\x05\xb0", "Receive": {"Start": 20, "Finish": 36}},
10: {"Send": "\x50\x00\x05\xb0", "Receive": {"Start": 36, "Finish": 52}},
11: {"Send": "\x50\x00\x05\xd0", "Receive": {"Start": 20, "Finish": 36}},
12: {"Send": "\x50\x00\x05\xd0", "Receive": {"Start": 36, "Finish": 52}},
13: {"Send": "\x50\x00\x05\xf0", "Receive": {"Start": 20, "Finish": 36}},
14: {"Send": "\x50\x00\x05\xf0", "Receive": {"Start": 36, "Finish": 52}},
15: {"Send": "\x50\x00\x06\x10", "Receive": {"Start": 20, "Finish": 36}},
}
wirelessRepeaterLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x06\x10", "Receive": {"Start": 36, "Finish": 52}},
2: {"Send": "\x50\x00\x06\x30", "Receive": {"Start": 20, "Finish": 36}}
}
wirelessKeypadLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x06\x30", "Receive": {"Start": 36, "Finish": 52}},
2: {"Send": "\x50\x00\x06\x50", "Receive": {"Start": 20, "Finish": 36}},
3: {"Send": "\x50\x00\x06\x50", "Receive": {"Start": 36, "Finish": 52}},
4: {"Send": "\x50\x00\x06\x70", "Receive": {"Start": 20, "Finish": 36}},
5: {"Send": "\x50\x00\x06\x70", "Receive": {"Start": 36, "Finish": 52}},
6: {"Send": "\x50\x00\x06\x90", "Receive": {"Start": 20, "Finish": 36}},
7: {"Send": "\x50\x00\x06\x90", "Receive": {"Start": 36, "Finish": 52}},
8: {"Send": "\x50\x00\x06\xb0", "Receive": {"Start": 20, "Finish": 36}}
}
siteNameLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x06\xb0", "Receive": {"Start": 36, "Finish": 52}}
}
wirelessSirenLabel = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"Send": "\x50\x00\x06\xd0", "Receive": {"Start": 20, "Finish": 36}},
2: {"Send": "\x50\x00\x06\xd0", "Receive": {"Start": 36, "Finish": 52}},
3: {"Send": "\x50\x00\x06\xf0", "Receive": {"Start": 20, "Finish": 36}},
4: {"Send": "\x50\x00\x06\xf0", "Receive": {"Start": 36, "Finish": 52}}
}
controlOutput = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"ON": "\x40\x00\x30\x00", "OFF": "\x40\x00\x31\x00"},
2: {"ON": "\x40\x00\x30\x01", "OFF": "\x40\x00\x31\x01"},
3: {"ON": "\x40\x00\x30\x02", "OFF": "\x40\x00\x31\x02"},
4: {"ON": "\x40\x00\x30\x03", "OFF": "\x40\x00\x31\x03"},
5: {"ON": "\x40\x00\x30\x04", "OFF": "\x40\x00\x31\x04"},
6: {"ON": "\x40\x00\x30\x05", "OFF": "\x40\x00\x31\x05"},
7: {"ON": "\x40\x00\x30\x06", "OFF": "\x40\x00\x31\x06"},
8: {"ON": "\x40\x00\x30\x07", "OFF": "\x40\x00\x31\x07"},
9: {"ON": "\x40\x00\x30\x08", "OFF": "\x40\x00\x31\x08"},
10: {"ON": "\x40\x00\x30\x09", "OFF": "\x40\x00\x31\x09"},
11: {"ON": "\x40\x00\x30\x0a", "OFF": "\x40\x00\x31\x0a"},
12: {"ON": "\x40\x00\x30\x0b", "OFF": "\x40\x00\x31\x0b"},
13: {"ON": "\x40\x00\x30\x0c", "OFF": "\x40\x00\x31\x0c"},
14: {"ON": "\x40\x00\x30\x0d", "OFF": "\x40\x00\x31\x0d"},
15: {"ON": "\x40\x00\x30\x0e", "OFF": "\x40\x00\x31\x0e"},
16: {"ON": "\x40\x00\x30\x0f", "OFF": "\x40\x00\x31\x0f"},
}
controlAlarm = {"Header": "\xaa\x25\x00\x04\x08\x00\x00\x14\xee\xee\xee\xee\xee\xee\xee\xee",
1: {"ARM": "\x40\x00\x04\x00", "DISARM": "\x40\x00\x05\x00", "SLEEP": "\x40\x00\x03\x00", "STAY": "\x40\x00\x01\x00"},
2: {"ARM": "\x40\x00\x04\x01", "DISARM": "\x40\x00\x05\x01", "SLEEP": "\x40\x00\x03\x01", "STAY": "\x40\x00\x01\x01"}}
@staticmethod
def getzoneLabelRegister():
return ParadoxMG5050Registers.zoneLabel
@staticmethod
def getpartitionLabelRegister():
return ParadoxMG5050Registers.partitionLabel
@staticmethod
def getuserLabelRegister():
return ParadoxMG5050Registers.userLabel
@staticmethod
def getbusModuleLabelRegister():
return ParadoxMG5050Registers.busModuleLabel
@staticmethod
def getwirelessRepeaterLabelRegister():
return ParadoxMG5050Registers.wirelessRepeaterLabel
@staticmethod
def getwirelessKeypadLabelRegister():
return ParadoxMG5050Registers.wirelessKeypadLabel
@staticmethod
def getsiteNameLabelRegister():
return ParadoxMG5050Registers.siteNameLabel
@staticmethod
def getwirelessSirenLabelRegister():
return ParadoxMG5050Registers.wirelessSirenLabel
@staticmethod
def getoutputLabelRegister():
# try:
return ParadoxMG5050Registers.outputLabel
@staticmethod
def getcontrolOutputRegister():
# try:
return ParadoxMG5050Registers.controlOutput
@staticmethod
def getcontrolAlarmRegister():
# try:
return ParadoxMG5050Registers.controlAlarm
@staticmethod
def getsupportedItems():
# try:
return ParadoxMG5050Registers.supportedItems
"""
MG5050: This class is used for reporting events. The generic names for grouped items will be
updated if a corresponding register location is given in the Registers class
See: E.g. the mappings for an MG5050: http://www.imotionsecurite.com/pdf/paradox/MGSP-EP20.pdf
"""
class ParadoxMG5050EventMap():
def __init__(self):
print 'init'
'''mapping of event and subevent groups here'''
eventGroupMap = {0: 'Zone OK',
1: 'Zone open',
2: 'Partition status',
3: 'Bell status (Partition 1)',
# 5: 'Non-Reportable Event',
6: 'Non-reportable event',
# 7: 'PGM Activation',
8: 'Button B pressed on remote',
9: 'Button C pressed on remote',
10: 'Button D pressed on remote',
11: 'Button E pressed on remote',
12: 'Cold start wireless zone',
13: 'Cold start wireless module (Partition 1)',
14: 'Bypass programming',
15: 'User code activated output (Partition 1)',
16: 'Wireless smoke maintenance signal',
17: 'Delay zone alarm transmission',
18: 'Zone signal strength weak 1 (Partition 1)',
19: 'Zone signal strength weak 2 (Partition 1)',
20: 'Zone signal strength weak 3 (Partition 1)',
21: 'Zone signal strength weak 4 (Partition 1)',
22: 'Button 5 pressed on remote',
23: 'Button 6 pressed on remote',
24: 'Fire delay started',
# 25: 'N/A',
26: 'Software access',
27: 'Bus module event',
28: 'StayD pass acknowledged',
29: 'Arming with user',
30: 'Special arming',
31: 'Disarming with user',
32: 'Disarming after alarm with user',
33: 'Alarm cancelled with user',
34: 'Special disarming',
35: 'Zone bypassed',
36: 'Zone in alarm',
37: 'Fire alarm',
38: 'Zone alarm restore',
39: 'Fire alarm restore',
40: 'Special alarm',
41: 'Zone shutdown',
42: 'Zone tampered',
43: 'Zone tamper restore',
44: 'New trouble (Partition 1, both for sub event 7',
45: 'Trouble restored ',
46: 'Bus / EBus / Wireless module new trouble (Partition 1)',
47: 'Bus / EBus / Wireless module trouble restored (Partition 1)',
48: 'Special (Partition 1)',
49: 'Low battery on zone',
50: 'Low battery on zone restore',
51: 'Zone supervision trouble',
52: 'Zone supervision restore',
53: 'Wireless module supervision trouble (Partition 1)',
54: 'Wireless module supervision restore (Partition 1)',
55: 'Wireless module tamper trouble (Partition 1)',
56: 'Wireless module tamper restore (Partition 1)',
57: 'Non-medical alarm (paramedic)',
58: 'Zone forced',
59: 'Zone included',
64: 'System Status'
}
_zoneLabel = {1: 'Zone 1',
2: 'Zone 2',
3: 'Zone 3',
4: 'Zone 4',
5: 'Zone 5',
6: 'Zone 6',
7: 'Zone 7',
8: 'Zone 8',
9: 'Zone 9',
10: 'Zone 10',
11: 'Zone 11',
12: 'Zone 12',
13: 'Zone 13',
14: 'Zone 14',
15: 'Zone 15',
16: 'Zone 16',
17: 'Zone 17',
18: 'Zone 18',
19: 'Zone 19',
20: 'Zone 20',
21: 'Zone 21',
22: 'Zone 22',
23: 'Zone 23',
24: 'Zone 24',
25: 'Zone 25',
26: 'Zone 26',
27: 'Zone 27',
28: 'Zone 28',
29: 'Zone 29',
30: 'Zone 30',
31: 'Zone 31',
32: 'Zone 32',
99: 'Any zone'}
_partitionStatus = {0: 'N/A',
1: 'N/A',
2: 'Silent alarm',
3: 'Buzzer alarm',
4: 'Steady alarm',
5: 'Pulse alarm',
6: 'Strobe',
7: 'Alarm stopped',
8: 'Squawk ON (Partition 1)',
9: 'Squawk OFF (Partition 1)',
10: 'Ground Start (Partition 1)',
11: 'Disarm partition',
12: 'Arm partition',
13: 'Entry delay started',
14: 'Exit delay started',
15: 'Pre-alarm delay',
16: 'Report confirmation',
99: 'Any partition status event'
}
_bellStatus = {0: ' Bell OFF',
1: ' Bell ON',
2: ' Bell squawk arm',
3: ' Bell squawk disarm',
99: 'Any bell status event'}
_nonReportableEvents = {0: 'Telephone line trouble',
1: '[ENTER]/[CLEAR]/[POWER] key was pressed (Partition 1 only)',
2: 'N/A',
3: 'Arm in stay mode',
4: 'Arm in sleep mode',
5: 'Arm in force mode',
6: 'Full arm when armed in stay mode',
7: 'PC fail to communicate (Partition 1)',
8: 'Utility Key 1 pressed (keys [1] and [2]) (Partition 1)',
9: 'Utility Key 2 pressed (keys [4] and [5]) (Partition 1)',
10: 'Utility Key 3 pressed (keys [7] and [8]) (Partition 1)',
11: 'Utility Key 4 pressed (keys [2] and [3]) (Partition 1)',
12: 'Utility Key 5 pressed (keys [5] and [6]) (Partition 1)',
13: 'Utility Key 6 pressed (keys [8] and [9]) (Partition 1)',
14: 'Tamper generated alarm',
15: 'Supervision loss generated alarm',
16: 'N/A',
17: 'N/Ad',
18: 'N/A',
19: 'N/A',
20: 'Full arm when armed in sleep mode',
21: 'Firmware upgrade -Partition 1 only (non-PGM event)',
22: 'N/A',
23: 'StayD mode activated',
24: 'StayD mode deactivated',
25: 'IP Registration status change',
26: 'GPRS Registration status change',
99: 'Any non-reportable event'}
_userLabel = {1: 'User Number 1',
2: 'User Number 2',
3: 'User Number 3',
4: 'User Number 4',
5: 'User Number 5',
6: 'User Number 6',
7: 'User Number 7',
8: 'User Number 8',
9: 'User Number 9',
10: 'User Number 10',
11: 'User Number 11',
12: 'User Number 12',
13: 'User Number 13',
14: 'User Number 14',
15: 'User Number 15',
16: 'User Number 16',
17: 'User Number 17',
18: 'User Number 18',
19: 'User Number 19',
20: 'User Number 20',
21: 'User Number 21',
22: 'User Number 22',
23: 'User Number 23',
24: 'User Number 24',
25: 'User Number 25',
26: 'User Number 26',
27: 'User Number 27',
28: 'User Number 28',
29: 'User Number 29',
30: 'User Number 30',
31: 'User Number 31',
32: 'User Number 32'
}
_remoteLabel = {1: 'Remote control number 1',
2: 'Remote control number 2',
3: 'Remote control number 3',
4: 'Remote control number 4',
5: 'Remote control number 5',
6: 'Remote control number 6',
7: 'Remote control number 7',
8: 'Remote control number 8',
9: 'Remote control number 9',
10: 'Remote control number 10',
11: 'Remote control number 11',
12: 'Remote control number 12',
13: 'Remote control number 13',
14: 'Remote control number 14',
15: 'Remote control number 15',
16: 'Remote control number 16',
17: 'Remote control number 17',
18: 'Remote control number 18',
19: 'Remote control number 19',
20: 'Remote control number 20',
21: 'Remote control number 21',
22: 'Remote control number 22',
23: 'Remote control number 23',
24: 'Remote control number 24',
25: 'Remote control number 25',
26: 'Remote control number 26',
27: 'Remote control number 27',
28: 'Remote control number 28',
29: 'Remote control number 29',
30: 'Remote control number 30',
31: 'Remote control number 31',
32: 'Remote control number 32',
99: 'Any remote control number'
}
_specialArming = {0: 'Auto-arming (on time/no movement)',
1: 'Late to close',
2: 'No movement arming',
3: 'Partial arming',
4: 'Quick arming',
5: 'Arming through WinLoad',
6: 'Arming with keyswitch',
99: 'Any special arming'
}
_specialDisarming = {0: 'Auto-arm cancelled (on time/no movement)',
1: 'Disarming through WinLoad',
2: 'Disarming through WinLoad after alarm',
3: 'Alarm cancelled through WinLoad',
4: 'Paramedical alarm cancelled',
5: 'Disarm with keyswitch',
6: 'Disarm with keyswitch after an alarm',
7: 'Alarm cancelled with keyswitch',
99: 'Any special disarming'
}
_specialAlarm = {0: 'Panic non-medical emergency',
1: 'Panic medical',
2: 'Panic fire',
3: 'Recent closing',
4: 'Global shutdown',
5: 'Duress alarm',
6: 'Keypad lockout (Partition 1)',
99: 'Any special alarm event'
}
_newTrouble = {0: 'N/A',
1: 'AC failure',
2: 'Battery failure',
3: 'Auxiliary current overload',
4: 'Bell current overload',
5: 'Bell disconnected',
6: 'Clock loss',
7: 'Fire loop trouble',
8: 'Fail to communicate to monitoring station telephone #1',
9: 'Fail to communicate to monitoring station telephone #2',
11: 'Fail to communicate to voice report',
12: 'RF jamming',
13: 'GSM RF jamming',
14: 'GSM no service',
15: 'GSM supervision lost',
16: 'Fail To Communicate IP Receiver 1 (GPRS)',
17: 'Fail To Communicate IP Receiver 2 (GPRS)',
18: 'IP Module No Service',
19: 'IP Module Supervision Loss',
20: 'Fail To Communicate IP Receiver 1 (IP)',
21: 'Fail To Communicate IP Receiver 2 (IP)',
99: 'Any new trouble event'
}
_troubleRestored = {0: 'Telephone line restore',
1: 'AC failure restore',
2: 'Battery failure restore',
3: 'Auxiliary current overload restore',
4: 'Bell current overload restore',
5: 'Bell disconnected restore',
6: 'Clock loss restore',
7: 'Fire loop trouble restore',
8: 'Fail to communicate to monitoring station telephone #1 restore',
9: 'Fail to communicate to monitoring station telephone #2 restore',
11: 'Fail to communicate to voice report restore',
12: 'RF jamming restore',
13: 'GSM RF jamming restore',
14: 'GSM no service restore',
15: 'GSM supervision lost restore',
16: 'Fail To Communicate IP Receiver 1 (GPRS) restore',
17: 'Fail To Communicate IP Receiver 2 (GPRS) restore',
18: 'IP Module No Service restore',
19: 'IP Module Supervision Loss restore',
20: 'Fail To Communicate IP Receiver 1 (IP) restore',
21: 'Fail To Communicate IP Receiver 2 (IP) restore',
99: 'Any trouble event restore'
}
_softwareAccess = {0: 'Non-valid source ID',
1: 'WinLoad direct',
2: 'WinLoad through IP module',
3: 'WinLoad through GSM module',
4: 'WinLoad through modem',
9: 'IP100 direct',
10: 'VDMP3 direct',
11: 'Voice through GSM module',
12: 'Remote access',
13: 'SMS through GSM module',
99: 'Any software access'
}
_outputLabel = {1: 'PGM Number 1',
2: 'PGM Number 2',
3: 'PGM Number 3',
4: 'PGM Number 4',
5: 'PGM Number 5',
6: 'PGM Number 6',
7: 'PGM Number 7',
8: 'PGM Number 8',
9: 'PGM Number 9',
10: 'PGM Number 10',
11: 'PGM Number 11',
12: 'PGM Number 12',
13: 'PGM Number 13',
14: 'PGM Number 14',
15: 'PGM Number 15',
16: 'PGM Number 16'
}
_wirelessRepeater = {1: 'Wireless repeater 1',
2: 'Wireless repeater 2'}
_wirelessKeypad = {1: 'Wireless keypad 1',
2: 'Wireless keypad 2',
3: 'Wireless keypad 3',
4: 'Wireless keypad 4',
5: 'Wireless keypad 5',
6: 'Wireless keypad 6',
7: 'Wireless keypad 7',
8: 'Wireless keypad 8'
}
_wirelessSiren = {1: 'Wireless siren 1',
2: 'Wireless siren 2',
3: 'Wireless siren 3',
4: 'Wireless siren 4'}
_busModuleEvent = {0: 'A bus module was added',
1: 'A bus module was removed',
2: '2-way RF Module Communication Failure',
3: '2-way RF Module Communication Restored'
}
_moduleTrouble = {0: 'Bus / EBus / Wireless module communication fault',
1: 'Tamper trouble',
2: 'Power fail',
3: 'Battery failure',
99: 'Any bus module new trouble event'
}
_moduleTroubleRestore = {0: 'Bus / EBus / Wireless module communication fault restore',
1: 'Tamper trouble restore',
2: 'Power fail restore',
3: 'Battery failure restore',
99: 'Any bus module trouble restored event'
}
_special = {0: 'System power up',
1: 'Reporting test',
2: 'Software log on',
3: 'Software log off',
4: 'Installer in programming mode',
5: 'Installer exited programming mode',
6: 'Maintenance in programming mode',
7: 'Maintenance exited programming mode',
8: 'Closing delinquency delay elapsed',
99: 'Any special event'
}
_systemStatus = {0: 'Follow Arm LED status',
1: 'PGM pulse fast in alarm',
2: 'PGM pulse fast in exit delay below 10 sec.',
3: 'PGM pulse slow in exit delay over 10 sec.',
4: 'PGM steady ON if armed',
5: 'PGM OFF if disarmed',
}
_eventOpt1 = {1: _outputLabel[1],
2: _outputLabel[2],
3: _outputLabel[3],
4: _outputLabel[4],
5: _outputLabel[5],
6: _outputLabel[6],
7: _outputLabel[7],
8: _outputLabel[8],
9: _outputLabel[9],
10: _outputLabel[10],
11: _outputLabel[11],
12: _outputLabel[12],
13: _outputLabel[13],
14: _outputLabel[14],
15: _outputLabel[15],
16: _outputLabel[16],
17: _wirelessRepeater[1],
18: _wirelessRepeater[2],
19: _wirelessKeypad[1],
20: _wirelessKeypad[2],
21: _wirelessKeypad[3],
22: _wirelessKeypad[4],
27: _wirelessSiren[1],
28: _wirelessSiren[2],
29: _wirelessSiren[3],
30: _wirelessSiren[4],
99: 'Any output number'
}
'''dictionary consisting of dictionary'''
subEventGroupMap = {0: _zoneLabel,
1: _zoneLabel,
2: _partitionStatus,
3: _bellStatus,
6: _nonReportableEvents,
8: _remoteLabel,
9: _remoteLabel,
10: _remoteLabel,
11: _remoteLabel,
12: _zoneLabel,
13: _eventOpt1,
14: _zoneLabel,
15: _zoneLabel,
16: _zoneLabel,
17: _zoneLabel,
18: _zoneLabel,
19: _zoneLabel,
20: _zoneLabel,
21: _zoneLabel,
22: _remoteLabel,
23: _remoteLabel,
24: _zoneLabel,
# 25 :_zoneLabel,
26: _softwareAccess,
27: _busModuleEvent,
28: _zoneLabel,
29: _userLabel,
30: _specialArming,
31: _userLabel,
32: _userLabel,
33: _userLabel,
34: _specialDisarming,
35: _zoneLabel,
36: _zoneLabel,
37: _zoneLabel,
38: _zoneLabel,
39: _zoneLabel,
40: _specialAlarm,
41: _zoneLabel,
42: _zoneLabel,
43: _zoneLabel,
44: _newTrouble,
45: _troubleRestored,
46: _moduleTrouble,
47: _moduleTroubleRestore,
48: _special,
49: _zoneLabel,
50: _zoneLabel,
51: _zoneLabel,
52: _zoneLabel,
53: _eventOpt1,
54: _eventOpt1,
55: _eventOpt1,
56: _eventOpt1,
57: _userLabel,
58: _zoneLabel,
59: _zoneLabel,
64: _systemStatus
}
_partitionLabel = {1: 'Partition 1',
2: 'Partition 2'
}
_busModuleLabel = {1: 'Bus Module 1',
2: 'Bus Module 2',
3: 'Bus Module 3',
4: 'Bus Module 4',
5: 'Bus Module 5',
6: 'Bus Module 6',
7: 'Bus Module 7',
8: 'Bus Module 8',
9: 'Bus Module 9',
10: 'Bus Module 10',
11: 'Bus Module 11',
12: 'Bus Module 12',
13: 'Bus Module 13',
14: 'Bus Module 14',
15: 'Bus Module 15'
}
_siteNameLabel = {1: 'Site Name'}
'''mapping of Label Types'''
labelTypeMap = {0: 'Zone Label',
1: 'User Label',
2: 'Partition Label',
3: 'PGM Label',
4: 'Bus Module Label',
5: 'Wireless Repeater Label',
6: 'Wireless Keypad Label'}
@staticmethod
def getEventGroupDescription(eg):
try:
return ParadoxMG5050EventMap.eventGroupMap[eg]
except KeyError:
print "No ParadoxMap for: eg=%d"%(eg)
return "-"
@staticmethod
def getSubEventGroupDescription(eg, seg):
try:
return ParadoxMG5050EventMap.subEventGroupMap[eg][seg]
except KeyError:
print "No ParadoxMap for: eg=%d \t seg=%d"%(eg,seg)
return "-"
@staticmethod
def getEventDescription(eg, seg):
try:
return ParadoxMG5050EventMap.eventGroupMap[eg], ParadoxMG5050EventMap.subEventGroupMap[eg][seg]
except KeyError:
print "No ParadoxMap for: eg=%d \t seg=%d" % (eg, seg)
return "-"
@staticmethod
def getLabelTypeDescription(lt):
return ParadoxMG5050EventMap.labelTypeMap[lt]
@staticmethod
def setzoneLabel(number, value):
ParadoxMG5050EventMap._zoneLabel.update({number: value})
return
@staticmethod
def getzoneLabel(number):
value = ParadoxMG5050EventMap._zoneLabel[number]
return value
@staticmethod
def getAllzoneLabel():
return ParadoxMG5050EventMap._zoneLabel
@staticmethod
def setuserLabel(number, value):
ParadoxMG5050EventMap._userLabel.update({number: value})
return
@staticmethod
def getAlluserLabel():
return ParadoxMG5050EventMap._userLabel
@staticmethod
def setpartitionLabel(number, value):
ParadoxMG5050EventMap._partitionLabel.update({number: value})
return
@staticmethod
def getAllpartitionLabel():
return ParadoxMG5050EventMap._partitionLabel
@staticmethod
def setbusModuleLabel(number, value):
ParadoxMG5050EventMap._busModuleLabel.update({number: value})
return
@staticmethod
def getbusModuleLabel(number):
value = ParadoxMG5050EventMap._busModuleLabel[number]
return value
@staticmethod
def getAllbusModuleLabel():
return ParadoxMG5050EventMap._busModuleLabel
@staticmethod
def setwirelessRepeaterLabel(number, value):
ParadoxMG5050EventMap._wirelessRepeater.update({number: value})
return
@staticmethod
def getwirelessRepeaterLabel(number):
value = ParadoxMG5050EventMap._wirelessRepeater[number]
return value
@staticmethod
def getAllwirelessRepeaterLabel():
return ParadoxMG5050EventMap._wirelessRepeater
@staticmethod
def setwirelessKeypadLabel(number, value):
ParadoxMG5050EventMap._wirelessKeypad.update({number: value})
return
@staticmethod
def getwirelessKeypadLabel(number):
value = ParadoxMG5050EventMap._wirelessKeypad[number]
return value
@staticmethod
def getAllwirelessKeypadLabel():
return ParadoxMG5050EventMap._wirelessKeypad
@staticmethod
def setsiteNameLabel(number, value):
ParadoxMG5050EventMap._siteNameLabel.update({number: value})
return
@staticmethod
def getsiteNameLabel(number):
value = ParadoxMG5050EventMap._siteNameLabel[number]
return value
@staticmethod
def getAllsiteNameLabel():
return ParadoxMG5050EventMap._siteNameLabel
@staticmethod
def setwirelessSirenLabel(number, value):
ParadoxMG5050EventMap._wirelessSiren.update({number: value})
return
@staticmethod
def getwirelessSirenLabel(number):
value = ParadoxMG5050EventMap._wirelessSiren[number]
return value
@staticmethod
def getAllwirelessSirenLabel():
return ParadoxMG5050EventMap._wirelessSiren
@staticmethod
def setoutputLabel(number, value):
ParadoxMG5050EventMap._outputLabel.update({number: value})
return
@staticmethod
def getoutputLabel(number):
value = ParadoxMG5050EventMap._outputLabel[number]
return value
@staticmethod
def getAlloutputLabel():
return ParadoxMG5050EventMap._outputLabel
if __name__ == '__main__':
print "Loaded Paradox Mapping"
#print ParadoxEventMap.getEventGroupDescription(0)
#print ParadoxEventMap.getSubEventGroupDescription(1,22)
#print ParadoxEventMap.getEventDescription(34,2)
#encoding=utf-8
import hashlib
import socket
import time
import lib.client as mqtt
import sys
import array
import random
import ConfigParser
import struct
import importlib
import serial
import logging
import re
import json
import socket
import select
Label_Regex = re.compile('[^a-zA-Z0-9\ \-]')
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
# Alarm controls can be given in payload, e.g. Paradox/C/P1, payl = Disarm
# Do not edit these variables here, use the config.ini file instead.
Zone_Amount = 32
passw = "0000"
user = "1000"
SERIAL_PORT = "/dev/ttyS1"
MQTT_IP = "127.0.0.1"
MQTT_Port = 1883
MQTT_KeepAlive = 60 # Seconds
# Options are Arm, Disarm, Stay, Sleep (case sensitive!)
Topic_Debug = "Paradox/Debug"
Topic_Publish_Battery = "Paradox/Voltage"
Topic_Publish_Events = "Paradox/Events"
Topic_Publish_Status = "Paradox/Status"
Events_Payload_Numeric = False
Topic_Subscribe_Control = "Paradox/Control/" # e.g. To arm partition 1: Paradox/C/P1/Arm
Startup_Publish_All_Info = "True"
Startup_Update_All_Labels = "True"
Topic_Publish_Labels = "Paradox/Labels"
Topic_Publish_AppState = "Paradox/State"
Alarm_Model = "ParadoxMG5050"
Alarm_Registry_Map = "ParadoxMG5050"
Alarm_Event_Map = "ParadoxMG5050"
Socket_Address = "0.0.0.0"
Socket_Port = 2000
# Global variables
Alarm_Control_Action = 0
Alarm_Control_Partition = 0
Alarm_Control_NewState = ""
Output_FControl_Action = 0
Output_FControl_Number = 0
Output_FControl_NewState = ""
Output_PControl_Action = 0
Output_PControl_Number = 0
Output_PControl_NewState = ""
State_Machine = 0
Debug_Mode = 2
Poll_Speed = 1
Debug_Packets = True
Keep_Alive_Interval = 2
Alarm_Data = {}
myAlarm = None
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
logger.debug ("skip: %s" % option)
except:
logger.exception("exception on %s!" % option)
dict1[option] = None
return dict1
def on_connect(client, userdata, flags, rc):
logger.info("Connected to MQTT broker with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# client.subscribe("$SYS/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global Alarm_Control_Partition
global Alarm_Control_NewState
global Alarm_Control_Action
global Output_FControl_Number
global Output_FControl_NewState
global Output_FControl_Action
global Output_PControl_Number
global Output_PControl_NewState
global Output_PControl_Action
global State_Machine
global Debug_Packets
valid_states = ['Arm', 'Disarm', 'Sleep', 'Stay']
logger.debug("MQTT Message: " + msg.topic + " " + str(msg.payload))
topic = msg.topic
if Topic_Subscribe_Control in msg.topic:
if "/Output/" in msg.topic:
try:
Output_FControl_Number = msg.topic.split("/")[-1]
logger.debug("Output force control number: ", Output_FControl_Number)
Output_FControl_NewState = msg.payload.strip()
if len(Output_FControl_NewState) == 0:
logger.warning("No payload for output: e.g. On")
return
logger.debug( "Output force control state: ", Output_FControl_NewState)
client.publish(Topic_Publish_AppState,
"Output: Forcing PGM " + str(Output_FControl_Number) + " to state: " + Output_FControl_NewState, 0, True)
Output_FControl_Action = 1
except:
logger.exception("MQTT message received with incorrect structure")
elif "/Pulse/" in msg.topic:
try:
Output_PControl_Number = msg.topic.split("/")[-1]
logger.debug("Output pulse control number: ", Output_PControl_Number)
Output_PControl_NewState = msg.payload.strip()
if len(Output_PControl_NewState) == 0:
logger.warning("No payload for output: e.g. On")
return
logger.debug( "Output pulse control state: ", Output_PControl_NewState)
client.publish(Topic_Publish_AppState,
"Output: Pulsing PGM " + str(Output_PControl_Number) + " to state: " + Output_PControl_NewState,
0, True)
Output_PControl_Action = 1
except:
logger.exception("MQTT message received with incorrect structure")
elif "/Partition/" in msg.topic:
try:
Alarm_Control_Partition = topic.split("/")[-1]
logger.debug( "Alarm control partition: %s ", Alarm_Control_Partition)
Alarm_Control_NewState = msg.payload.strip()
if len(Alarm_Control_NewState) < 1:
logger.warning('No payload given for alarm control: e.g. Disarm')
return
logger.debug( "Alarm control state: %s", Alarm_Control_NewState)
client.publish(Topic_Publish_AppState,
"Alarm: Control partition " + str(Alarm_Control_Partition) + " to state: " + Alarm_Control_NewState,
0, True)
Alarm_Control_Action = 1
except:
logger.exception("MQTT message received with incorrect structure")
elif msg.topic.split("/")[-1] == "State":
if msg.payload.upper() == "DEBUGPACKETS":
Debug_Packets = True
elif msg.payload.upper() == "NODEBUGPACKETS":
Debug_Packets = False
elif msg.payload.upper() == "NORMAL" and State_Machine == 20:
if myAlarm is not None:
myAlarm.stopSerialPassthrough()
logger.info("Switching to Standard mode")
elif msg.payload.upper() == "PASSTHROUGH":
State_Machine = 20
logger.info("Switching to Passthrough mode")
elif msg.payload.upper() == "RESET":
State_Machine = 0
logger.info("Reseting Paradox Multi MQTT")
else:
logger.warning("Unknown new state: %s", msg.payload)
class CommSerial:
comm = None
def __init__(self, serialport, mqttc):
self.serialport = serialport
self.comm = None
self.client = mqttc
def connect(self, baud=9600, timeout=1):
try:
logger.info( "Opening Serial port: " + self.serialport)
self.comm = serial.Serial()
self.comm.baudrate = baud
self.comm.port = self.serialport
self.comm.timeout = timeout
self.comm.open()
logger.info( "Serial port open!")
except:
return False
return True
def write(self, data):
if Debug_Packets and logger.isEnabledFor(logging.DEBUG):
m = str(len(data)) + " -b- "
for c in data:
m += " %02x" % ord(c)
#logger.debug(m)
self.client.publish(Topic_Debug + "/OUT", m, qos=2)
self.comm.write(data)
def read(self, sz=37, timeout=1):
self.comm.timeout = timeout
data = self.comm.read(sz)
if Debug_Packets and logger.isEnabledFor(logging.DEBUG):
if data is not None and len(data) > 0:
m = str(len(data)) + " -b- "
for c in data:
m += " %02x" % ord(c)
self.client.publish(Topic_Debug+"/INP", m, qos=2)
return data
def disconnect(self):
self.comm.close()
def flush(self):
self.comm.flush()
def getfd(self):
return self.comm.fileno()
# To be implemented. Do dot have the hardware to proceed.
class CommIP150:
def connect():
pass
def write():
pass
def read():
pass
def disconnect():
pass
class Paradox:
loggedin = 0
alarmName = None
zoneTotal = 0
zoneStatus = ['']
zoneNames = ['']
zonePartition = None
partitionStatus = None
partitionName = None
Skip_Update_Labels = 0
mode = 0
def __init__(self, _transport, _encrypted=0, _retries=3, _alarmeventmap="ParadoxMG5050",
_alarmregmap="ParadoxMG5050"):
self.comms = _transport # instance variable unique to each instance
self.retries = _retries
self.encrypted = _encrypted
self.alarmeventmap = _alarmeventmap
self.alarmregmap = _alarmregmap
self.mode = 0
# MyClass = getattr(importlib.import_module("." + self.alarmmodel + "EventMap", __name__))
try:
mod = __import__("ParadoxMap", fromlist=[self.alarmeventmap + "EventMap"])
self.eventmap = getattr(mod, self.alarmeventmap + "EventMap")
except Exception, e:
logger.exception("Failed to load Event Map: ", repr(e))
logger.info("Defaulting to MG5050 Event Map...")
try:
mod = __import__("ParadoxMap", fromlist=["ParadoxMG5050EventMap"])
self.eventmap = getattr(mod, "ParadoxMG5050EventMap")
except Exception, e:
logger.exception( "Failed to load Event Map (exiting): ", repr(e))
sys.exit()
try:
mod = __import__("ParadoxMap", fromlist=[self.alarmregmap + "Registers"])
self.registermap = getattr(mod, self.alarmregmap + "Registers")
except Exception, e:
logger.exception( "Failed to load Register Map (defaulting to not update labels from alarm): ", repr(e))
self.Skip_Update_Labels = 1
# self.eventmap = ParadoxMG5050EventMap # Need to check panel type here and assign correct dictionary!
# self.registermap = ParadoxMG5050Registers # Need to check panel type here and assign correct dictionary!
def skipLabelUpdate(self):
return self.Skip_Update_Labels
def saveState(self):
self.eventmap.save()
def loadState(self):
logger.debug("Loading previous event states and labels from file")
self.eventmap.load()
def login(self, password, Debug_Mode=0):
logger.info("Connecting to Alarm System")
message = '\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
message = '\x50\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
message = '\x5f\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
message = reply
message = self.format37ByteMessage(message)
reply = self.readDataRaw( message, Debug_Mode)
message = '\x50\x00\x1f\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4f'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
message = '\x50\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
message = '\x50\x00\x0e\x52\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
message = self.format37ByteMessage(message)
reply = self.readDataRaw(message, Debug_Mode)
return True
def format37ByteMessage(self, message):
checksum = 0
if len(message) % 37 != 0:
for val in message:
checksum += ord(val)
while checksum > 255:
checksum = checksum - (checksum / 256) * 256
message += bytes(bytearray([checksum])) # Add check to end of message
return message
def updateAllLabels(self, Startup_Publish_All_Info="True", Topic_Publish_Labels="True", Debug_Mode=0):
Alarm_Data['labels'] = dict()
for func in self.registermap.getsupportedItems():
logger.debug("Reading from alarm: " + func)
try:
register_dict = getattr(self.registermap, "get" + func + "Register")()
mapping_dict = getattr(self.eventmap, "set" + func)
total = sum(1 for x in register_dict if isinstance(x, int))
logger.debug("Amount of numeric items in dictionary to read: " + str(total))
skip_next = 0
last_index = False
for x in range(1, total + 1):
message = register_dict[x]["Send"]
try:
next_message = register_dict[x + 1]["Send"]
except KeyError:
skip_next = True
assert isinstance(message, basestring), "Message to be sent is not a string: %r" % message
message = message.ljust(36, '\x00')
reply = self.readDataRaw(self.format37ByteMessage(message), Debug_Mode)
if skip_next:
skip_next = False
continue
start = reply.find('R')
if start == -1 or len(reply) < start + 19:
#logger.warning("Invalid message!")
continue
start += 4
finish = start + 16
label = reply[start:finish].strip()
mapping_dict(x, label)
x += 1
start = finish
finish = start + 16
label = reply[start :finish ].strip()
mapping_dict(x, label)
skip_next = True
try:
completed_dict = getattr(self.eventmap, "getAll" + func)()
if Debug_Mode >= 1:
logger.info("Labels detected for " + func + ": " + str(completed_dict))
except Exception, e:
logger.exception( "Failed to load supported function's completed mappings after updating: " + repr(e))
Alarm_Data['labels'][func] = completed_dict
Alarm_Data[func.replace('Label', '')] = [''] * len(Alarm_Data['labels'][func])
if Startup_Publish_All_Info == "True":
topic = func.split("Label")[0]
client.publish(Topic_Publish_Labels + "/" + topic[0].upper() + topic[1:] + "s",
';'.join('{}{}'.format(key, ":" + val) for key, val in completed_dict.items()))
except Exception, e:
logger.exception( "Failed to load supported function's mapping: " + repr(e))
return
def testForEvents(self, Events_Payload_Numeric=0, Debug_Mode=0, timeout=1):
message = self.comms.read(timeout=timeout)
if message is None or len(message) < 9:
return None
reply = '.'
if len(message) > 0:
if message[0] == '\xe2' or message[0] == '\xe0':
try:
event, subevent = self.eventmap.getEventDescription(ord(message[7]), ord(message[8]))
event = event.strip()
subevent = subevent.strip()
reply = json.dumps({"Event": event, "SubEvent":subevent})
logger.info(reply)
client.publish(Topic_Publish_Events, reply, qos=0, retain=True)
if event.find("Zone ") == 0:
client.publish(Topic_Publish_Status + "/Zones/"+subevent.replace(' ','_').title(), event, qos=1, retain=True)
elif event == 'Partition status':
se = ord(message[8])
if se in [2, 3, 4, 5, 6, 7, 11, 12, 14]:
client.publish(Topic_Publish_Status + "/Partitions/", subevent, qos=1, retain=True)
elif ord(message[7]) in [29, 30, 31, 32, 33, 40, 44, 45] :
client.publish(Topic_Publish_Status + "/System/", event + " -> " + subevent, qos=1, retain=True)
except ValueError:
reply = "No register entry for Event: " + str(ord(message[7])) + ", Sub-Event: " + str(
ord(message[8]))
else:
reply = "Unknown event: " + " ".join(hex(ord(i)) for i in message)
return reply
def readDataRaw(self, request='', Debug_Mode=2, testForEvents=False):
if testForEvents:
self.testForEvents(timeout=0.1) # First check for any pending events received
tries = self.retries
while tries > 0:
try:
if len(request) > 0:
self.comms.write(request)
inc_data = self.comms.read()
if inc_data is None:
if tries > 0:
logger.warning("Error reading data from panel, retrying again... (" + str(tries) + ")")
tries -= 1
time.sleep(0.5)
continue
elif tries == 0:
return ''
else:
break
else:
return inc_data
except Exception, e:
logger.exception("Error reading from panel")
sys.exit(-1)
def readDataStruct37(self, inputData='', Debug_Mode=0): # Sends data, read input data and return the Header and Message
rawdata = self.readDataRaw(inputData, Debug_Mode)
return rawdata
def controlGenericOutput(self, mapping_dict, outputs, state, Debug_Mode=0):
logger.info("Sending generic Output Control: Output: " + str(outputs) + ", State: " + state)
for output in outputs:
message = mapping_dict[output][state]
if not isinstance(message, basestring):
logger.warning("Generic Output: Message to be sent is not a string: %r" % message)
continue
message = message.ljust(36, '\x00')
reply = self.readDataRaw(self.format37ByteMessage(message), Debug_Mode)
return
def controlPGM(self, pgm, state="OFF", Debug_Mode=0):
if isinstance(pgm, basestring):
if pgm == "ALL":
pgm = range(1, 1 + len(Alarm_Data['labels']['outputLabel']))
else:
try:
pgm = json.loads(pgm)
if not isinstance(pgm, list):
logger.warning("PGM must be str, int or list")
return
except:
logger.warning("Could not decode partition list")
return
elif isinstance(pgm, int):
pgm = [pgm]
elif not isinstance(pgm, list):
logger.warning("PGM must be str, int or list")
return
if not state in ["ON", "1", "TRUE", "ENABLE", "OFF", "FALSE", "0", "DISABLE"]:
logger.warning("PGM State is not given correctly: %r" % str(state))
return
for p in pgm:
if not isinstance(p, int) or not (p >= 0 and p <= len(Alarm_Data['labels']['outputLabel'])):
logger.warning("Problem with PGM number: %r" % str(p))
return
self.controlGenericOutput(self.registermap.getcontrolOutputRegister(), pgm, state, Debug_Mode)
return
def controlGenericAlarm(self, mapping_dict, partition, state, Debug_Mode):
registers = mapping_dict
logger.info("Sending generic Alarm Control: Partition: " + str(partition) + ", State: " + state)
for p in partition:
if p not in registers.keys():
logger.warning("Invalid partition: %d", p)
continue
message = registers[p][state]
message = message.ljust(36, '\x00')
reply = self.readDataRaw(self.format37ByteMessage(message), Debug_Mode)
return
def controlAlarm(self, partition=1, state="Disarm", Debug_Mode=0):
state = state.split(' ')[0].upper()
if isinstance(partition, basestring):
if partition == "ALL":
partition = range(1, 1 + len(Alarm_Data['labels']['partitionLabel']))
else:
try:
partition = json.loads(partition)
if not isinstance(partition, list):
logger.warning("Partition must be str, int or list")
return
except:
logger.warning("Could not decode partition list")
return
elif isinstance(partition, int):
partition = [partition]
elif not isinstance(partition, list):
logger.warning("Partition must be str, int or list")
return
for p in partition:
if not p in self.registermap.getcontrolAlarmRegister().keys():
logger.warning("Unkown partition")
return
if not state in self.registermap.getcontrolAlarmRegister()[p].keys():
logger.warning("State is not given correctly: %r for partition %d" % (str(state), p))
return
self.controlGenericAlarm(self.registermap.getcontrolAlarmRegister(), partition, state, Debug_Mode)
return
def disconnect(self, Debug_Mode=0):
message = "\x70\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x76"
self.readDataRaw(self.format37ByteMessage(message))
self.comms.disconnect()
def keepAlive(self, Debug_Mode=0):
global Alarm_Data
aliveSeq = 0
while aliveSeq < 2:
message = "\x50\x00\x80"
message += bytes(bytearray([aliveSeq]))
message += "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
data = self.readDataRaw(self.format37ByteMessage(message), testForEvents=True)
if len(data) != 37:
return
if aliveSeq == 0:
Alarm_Data['date_time'] = {"year": ord(data[9])*100 + ord(data[10]),
"month": ord(data[11]),
"day": ord(data[12]),
"hours": ord(data[13]),
"minutes": ord(data[14])}
voltage = {'vdc': round(ord(data[15])*(20.3-1.4)/255.0+1.4,1) ,
'dc': round(ord(data[16])*22.8/255.0,1),
'battery': round(ord(data[17])*22.8/255.0,1)}
if not 'voltage' in Alarm_Data.keys() or \
abs(voltage['vdc'] - Alarm_Data['voltage']['vdc']) > 0.3 or abs(voltage['dc'] - Alarm_Data['voltage']['dc']) > 0.3 or abs(voltage['battery'] - Alarm_Data['voltage']['battery']) > 0.3:
client.publish(Topic_Publish_Battery, json.dumps(voltage), retain=True)
Alarm_Data['voltage'] = voltage
b = 0
bt = 0
## Ignore the last zone (99 = Any Zone)
for i in range(0, len(Alarm_Data['zone']) - 1 ):
bt = i % 8
if i != 0 and bt == 0:
b += 1
if Alarm_Data['labels']['zoneLabel'][i+1].startswith("Zone "):
continue
state = (ord(data[19 + b]) >> bt) & 0x01
if state == 0:
state = "Zone OK"
else:
state = "Zone open"
if Alarm_Data['zone'][i] != state and ('open' in Alarm_Data['zone'][i] or 'OK' in Alarm_Data['zone'][i] or Alarm_Data['zone'][i] == ''):
Alarm_Data['zone'][i] = state
client.publish(Topic_Publish_Status+"/Zones/"+Alarm_Data['labels']['zoneLabel'][i + 1].replace(' ','_').title(), Alarm_Data['zone'][i], retain=True)
elif aliveSeq == 1:
for i in [0, 1]:
state = 0
if ord(data[18 + i * 4]) == 0x01:
state = "Arming"
elif ord(data[17 + i * 4]) == 0x01:
state = "Armed"
else:
state = "Disarmed"
if Alarm_Data['partition'][i] != state:
Alarm_Data['partition'][i] = state
client.publish(Topic_Publish_Status + "/Partitions/%d/" % (i + 1), state )
aliveSeq += 1
def walker(self, ):
self.zoneTotal = Zone_Amount
logger.debug("Reading (" + str(Zone_Amount) + ") zone names...")
for x in range(16, 65535, 32):
message = "\xe2\x00"
zone = x
zone = list(struct.pack("H", zone))
swop = zone[0]
zone[0] = zone[1]
zone[1] = swop
temp = "".join(zone)
message += temp
message += "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
reply = self.readDataRaw(self.format37ByteMessage(message))
return
def startSerialPassthrough(self):
self.comms.connect()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(0)
try:
s.bind((Socket_Address, Socket_Port))
s.listen(10)
s.settimeout(5)
except socket.error as msg:
s.close()
s = None
logger.warning("Could not bind to socket %s:%s", Socket_Address, Socket_Port)
return
self.mode = 1
while self.mode == 1:
inputs = [self.comms.getfd()]
logger.debug("Passthrough: Waiting for client")
client = None
while self.mode == 1:
try:
client = s.accept()
logger.debug("Passthrough: Client connected: %s", str(client))
except socket.timeout:
pass
inputs.append(client)
while self.mode == 1:
readable, writable, exceptional = select.select(inputs, [] , inputs, 5)
for fin in readable:
if fin == inputs[0]:
client.send(self.comms.read())
else:
self.comms.write(client.recv())
sys.stdout.write(".")
for fex in exceptional:
if fex == inputs[0]:
logger.warning("Could not read from panel!")
return
else:
logger.warning("Client closed connection")
break;
#if client is not None:
# client.close()
self.comms.disconnect()
def stopSerialPassthrough(self):
self.mode = 0
if __name__ == '__main__':
attempts = 3
lastKeepAlive = 0
while True:
# -------------- Read Config file ----------------
if State_Machine <= 0:
logger.info("Reading config.ini file...")
try:
Config = ConfigParser.ConfigParser()
Config.read("config.ini")
Alarm_Model = Config.get("Alarm", "Alarm_Model")
Alarm_Registry_Map = Config.get("Alarm", "Alarm_Registry_Map")
Alarm_Event_Map = Config.get("Alarm", "Alarm_Event_Map")
Zone_Amount = int(Config.get("Alarm", "Zone_Amount"))
if Zone_Amount % 2 != 0:
Zone_Amount += 1
MQTT_IP = Config.get("MQTT Broker", "IP")
MQTT_Port = int(Config.get("MQTT Broker", "Port"))
SERIAL_PORT = Config.get("SERIAL", "SERIAL_PORT")
passw = Config.get("SERIAL", "Password")
Topic_Publish_Events = Config.get("MQTT Topics", "Topic_Publish_Events")
Events_Payload_Numeric = Config.get("MQTT Topics", "Events_Payload_Numeric")
Topic_Subscribe_Control = Config.get("MQTT Topics", "Topic_Subscribe_Control")
Startup_Publish_All_Info = Config.get("MQTT Topics", "Startup_Publish_All_Info")
Topic_Publish_Labels = Config.get("MQTT Topics", "Topic_Publish_Labels")
Topic_Publish_AppState = Config.get("MQTT Topics", "Topic_Publish_AppState")
Startup_Update_All_Labels = Config.get("Application", "Startup_Update_All_Labels")
Debug_Mode = int(Config.get("Application", "Debug_Mode"))
if Debug_Mode == 1:
logger.setLevel(logging.INFO)
elif Debug_Mode == 2:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARN)
logger.info("config.ini file read successfully")
State_Machine += 1
except Exception, e:
logger.exception( "******************* Error reading config.ini file (will use defaults): " + repr(e))
State_Machine = 1
attempts = 3
# -------------- MQTT ----------------
elif State_Machine == 1:
try:
logger.info("Attempting connection to MQTT Broker: " + MQTT_IP + ":" + str(MQTT_Port))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_IP, MQTT_Port, MQTT_KeepAlive)
client.loop_start()
client.subscribe(Topic_Subscribe_Control + "#")
logger.info("MQTT client subscribed to control messages on topic: " + Topic_Subscribe_Control + "#")
client.publish(Topic_Publish_AppState,"State Machine 1, Connected to MQTT Broker",0,True)
State_Machine += 1
except Exception, e:
logger.exception( "MQTT connection error (" + str(attempts) + ": " + repr(e))
time.sleep(attempts * 2)
attempts -= 1
if attempts < 1:
logger.error("Error within State_Machine: " + str(State_Machine) + ": " + repr(e))
State_Machine -= 1
logger.debug("Going to State_Machine: " + str(State_Machine))
attempts = 3
# -------------- Login to Module ----------------
elif State_Machine == 2:
try:
client.publish(Topic_Publish_AppState, "State Machine 2, Connecting to Alarm...", 0, True)
comms = CommSerial(SERIAL_PORT, client)
if not comms.connect():
logger.critical("Error connecting to Alarm")
sys.exit(0)
client.publish(Topic_Publish_AppState,
"State Machine 2, Connected to Alarm, unlocking...",
0, True)
myAlarm = Paradox(comms, 0, 3, Alarm_Event_Map, Alarm_Registry_Map)
if not myAlarm.login(passw, Debug_Mode):
logger.warning("Failed to login & unlock panel, check if another app is using the port. Retrying... ")
client.publish(Topic_Publish_AppState,
"State Machine 2, Failed to login & unlock panel, check if another app is using the port. Retrying... ",
0, True)
comms.close()
time.sleep(Poll_Speed * 20)
else:
client.publish(Topic_Publish_AppState, "State Machine 2, Logged into panel successfully", 0, True)
State_Machine += 1
except Exception, e:
logger.exception("Error attempting connection to panel (" + str(attempts) + ": " + repr(e))
client.publish(Topic_Publish_AppState,
"State Machine 2, Exception, retrying... (" + str(attempts) + ": " + repr(e),
0, True)
time.sleep(Poll_Speed * 5)
attempts -= 1
if attempts < 1:
logger.error("Error within State_Machine: " + str(State_Machine) + ": " + repr(e))
client.publish(Topic_Publish_AppState, "State Machine 2, Error, moving to previous state", 0, True)
State_Machine -= 1
logger.error("Going to State_Machine: " + str(State_Machine))
attempts = 3
# -------------- Reading Labels ----------------
elif State_Machine == 3:
try:
if Startup_Update_All_Labels == "True" and myAlarm.skipLabelUpdate() == 0:
client.publish(Topic_Publish_AppState, "State Machine 3, Reading labels from alarm", 0, True)
logger.info("Updating all labels from alarm")
myAlarm.updateAllLabels(Startup_Publish_All_Info, Topic_Publish_Labels, Debug_Mode)
State_Machine += 1
logger.info("Listening for events...")
client.publish(Topic_Publish_AppState, "State Machine 4, Listening for events...", 0, True)
else:
State_Machine += 1
logger.info("Listening for events...")
client.publish(Topic_Publish_AppState, "State Machine 4, Listening for events...", 0, True)
except Exception, e:
logger.exception( "Error reading labels: " + repr(e))
client.publish(Topic_Publish_AppState, "State Machine 3, Exception: " + repr(e), 0, True)
time.sleep(Poll_Speed * 5)
attempts -= 1
if attempts < 1:
logger.error("Error within State_Machine: " + str(State_Machine) + ": " + repr(e))
client.publish(Topic_Publish_AppState, "State Machine 3, Error, moving to previous state", 0, True)
State_Machine -= 1
logger.debug("Going to State_Machine: " + str(State_Machine))
Alarm_Control_Action = 0
attempts = 3
# -------------- Checking Events & Actioning Controls ----------------
elif State_Machine == 4:
try:
# Test for new events & publish to broker
timeRemaining = time.time() - lastKeepAlive + Keep_Alive_Interval
if lastKeepAlive > 0 and timeRemaining > 0:
myAlarm.testForEvents(Events_Payload_Numeric, Debug_Mode, timeout=timeRemaining)
# Test for pending Alarm Control
if Alarm_Control_Action == 1:
myAlarm.controlAlarm(Alarm_Control_Partition, Alarm_Control_NewState, Debug_Mode)
Alarm_Control_Action = 0
logger.info( "Listening for events...")
client.publish(Topic_Publish_AppState, "State Machine 4, Listening for events...", 0, True)
# Test for pending Force Output Control
if Output_FControl_Action == 1:
myAlarm.controlPGM(Output_FControl_Number, Output_FControl_NewState.upper(), Debug_Mode)
Output_FControl_Action = 0
logger.info("Listening for events...")
client.publish(Topic_Publish_AppState, "State Machine 4, Listening for events...", 0, True)
# Test for pending Pulse Output Control
if Output_PControl_Action == 1:
myAlarm.controlPGM(Output_PControl_Number, Output_PControl_NewState.upper(), Debug_Mode)
time.sleep(0.5)
if Output_PControl_NewState.upper() in ["ON", "1", "TRUE", "ENABLE"]:
myAlarm.controlPGM(Output_PControl_Number, "OFF", Debug_Mode)
else:
myAlarm.controlPGM(Output_PControl_Number, "ON", Debug_Mode)
Output_PControl_Action = 0
logger.info("Listening for events...")
client.publish(Topic_Publish_AppState, "State Machine 4, Listening for events...", 0, True)
if time.time() >= lastKeepAlive + Keep_Alive_Interval:
myAlarm.keepAlive(Debug_Mode)
lastKeepAlive = time.time()
except Exception, e:
logger.exception( "Error during normal poll: " + repr(e) + ", Attempt: " + str(attempts))
client.publish(Topic_Publish_AppState, "State Machine 4, Exception: " + repr(e), 0, True)
time.sleep(Poll_Speed * 5)
attempts -= 1
if attempts < 1:
logger.error("Error within State_Machine: " + str(State_Machine) + ": " + repr(e))
State_Machine -= 1
client.publish(Topic_Publish_AppState, "State Machine 4, Error, moving to previous state", 0, True)
attempts = 3
elif State_Machine == 20:
myAlarm.disconnect()
myAlarm.startSerialPassthrough()
State_Machine = 1
elif State_Machine == 10:
time.sleep(3)
else:
State_Machine = 2
# Ignore everything in this directory
*
# Except this file
!.gitignore
# Ignore everything in this directory
*
# Except this file
!.gitignore
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment