forked from scadagenius/obisyslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obihai_syslog.py
195 lines (170 loc) · 9.29 KB
/
obihai_syslog.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
import datetime
import socket
import time
socket_receive_buffer = 4096
class ObihaiSyslog:
def __init__(self, obihelper, obiscan):
self.helper = obihelper
self.obiscan = obiscan
def monitor(self):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:monitor(): enter")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((self.helper.config["syslog"]["ip"], self.helper.config["syslog"]["port"]))
while True:
data, address = sock.recvfrom(socket_receive_buffer)
self.handle_incoming_data(data.decode('utf-8'))
except Exception as e:
self.helper.print(self.helper.log_level_error, "ObihaiSyslog:monitor():Exception: " + str(e))
time.sleep(60)
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:monitor(): exit")
def handle_incoming_data(self, string_data):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:hid(): enter")
time_stamp = datetime.datetime.now().strftime('%H:%M:%S')
string_data = time_stamp + " ::: " + string_data
self.helper.log_data(string_data)
any_change, port_str, hook_status, call_direction, caller_name, caller_number = self.parse_message_data(
string_data)
if any_change:
if hook_status != "":
self.process_hook_change(hook_status, port_str)
else:
self.process_call_change(port_str, time_stamp, call_direction, caller_name, caller_number)
self.helper.print(self.helper.log_level_info, "ObihaiSyslog:hid(): " + time_stamp + ", " + hook_status +
", " + call_direction + ", " + caller_name + ", " + caller_number)
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:hid(): exit")
def parse_message_data(self, message_data):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:pmd(): enter")
call_direction = ""
caller_name = ""
caller_number = ""
any_change, port, hook_status = self.is_hook_change(message_data)
if not any_change:
any_change, port, call_direction, caller_name, caller_number = self.is_inbound_call(message_data)
if not any_change:
any_change, port, call_direction, caller_name, caller_number = self.is_outbound_call(message_data)
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:pmd(): exit")
port_str = "port" + str(port)
return any_change, port_str, hook_status, call_direction, caller_name, caller_number
def get_config_friendly_name(self, port_str):
return self.helper.config["obihai"]["monitor"][port_str]
def process_hook_change(self, hook_status, port_str):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:phc(): enter")
self.helper.update_ha_sensor("sensor.phone_status_" + port_str, hook_status,
{
"friendly_name": "Phone Status " + self.get_config_friendly_name(port_str),
"icon": "mdi:phone-classic"
})
if hook_status == "On Hook":
active_call_direction = "Idle"
self.helper.update_ha_sensor("sensor.active_call_direction_" + port_str, active_call_direction,
{
"friendly_name": "Active Phone Status " +
self.get_config_friendly_name(port_str),
"icon": "mdi:phone-classic"
})
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:phc(): exit")
def process_call_change(self, port_str, time_stamp, call_direction, caller_name, caller_number):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:pcc(): enter")
call_status, call_direction, caller_start_time, caller_name, caller_number = self.obiscan.scan_call_status()
caller_number = self.obiscan.format_phone_number(caller_number)
if call_status != "Idle":
caller_name = self.helper.get_caller_name_from_phone_book(port_str, caller_number, caller_start_time)
self.helper.update_ha_sensor("sensor.active_call_direction_" + port_str, call_direction,
{
"friendly_name": "Active Phone Status " + self.get_config_friendly_name(
port_str),
"icon": "mdi:phone-classic"
})
call_time = time_stamp + " " + datetime.datetime.now().strftime('%m/%d')
self.helper.add_caller_to_history(port_str, caller_number, caller_name, call_direction, call_time)
self.helper.update_ha_phone_history()
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:pcc(): exit")
def get_config_hook_string(self, port_number):
config_str = ""
port_str = "port" + str(port_number)
if port_str in self.helper.config["obihai"]["monitor"]:
config_str = self.helper.config["obihai"][port_str]["onoffhook"]
return config_str
def is_hook_change(self, message_data):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:ihc(): enter")
any_change = False
hook_status = ""
port = 1
while port <= self.helper.max_supported_obihai_ports:
str_to_match = self.get_config_hook_string(port)
if str_to_match != "":
start_index = message_data.find(str_to_match)
if start_index != -1:
if message_data[start_index + 18:].startswith("ONHOOK"):
any_change = True
hook_status = "On Hook"
elif message_data[start_index + 18:].startswith("OFF HOOK"):
any_change = True
hook_status = "Off Hook"
break
port = port + 1
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:ihc(): exit")
return any_change, port, hook_status
def get_config_inbound_string(self, port_number):
config_str = ""
port_str = "port" + str(port_number)
if port_str in self.helper.config["obihai"]["monitor"]:
config_str = self.helper.config["obihai"][port_str]["inbound"]
return config_str
def is_inbound_call(self, message_data):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:iic(): enter")
any_change = False
call_direction = ""
caller_name = ""
caller_number = ""
port = 1
while port <= self.helper.max_supported_obihai_ports:
str_to_match = self.get_config_inbound_string(port)
if str_to_match != "":
start_index = message_data.find(str_to_match)
if start_index != -1:
any_change = True
call_direction = "Inbound"
caller_name = message_data[start_index + 28:]
start_index = caller_name.find("'")
if start_index != -1:
temp_str = caller_name[start_index + 1:].strip()
caller_name = caller_name[:start_index]
caller_name = ' '.join(caller_name.split()) # normalize spaces
for c in temp_str:
if c.isdigit():
caller_number += c
else:
break
caller_number = self.obiscan.format_phone_number(caller_number)
break
port = port + 1
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:iic(): exit")
return any_change, port, call_direction, caller_name, caller_number
def get_config_outbound_string(self, port_number):
config_str = ""
port_str = "port" + str(port_number)
if port_str in self.helper.config["obihai"]["monitor"]:
config_str = self.helper.config["obihai"][port_str]["outbound"]
return config_str
def is_outbound_call(self, message_data):
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:ioc(): enter")
any_change = False
call_direction = ""
caller_name = ""
caller_number = ""
port = 1
while port <= self.helper.max_supported_obihai_ports:
str_to_match = self.get_config_outbound_string(port)
if str_to_match != "":
start_index = message_data.find(str_to_match)
if start_index != -1:
any_change = True
call_direction = "Outbound"
temp_str = message_data[start_index + 32:].split(',')
caller_number = self.obiscan.format_phone_number(temp_str[0])
break
port = port + 1
self.helper.print(self.helper.log_level_debug, "ObihaiSyslog:ioc(): exit")
return any_change, port, call_direction, caller_name, caller_number