pyBMNotify/pyBMNotify.py

98 lines
3.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2020-12-07 17:00:49 +00:00
from socketIO_client import SocketIO
import json
2020-12-07 18:02:56 +00:00
import datetime as dt
2020-12-07 21:33:15 +00:00
import time
import config as cfg
import http.client, urllib
2020-12-07 17:00:49 +00:00
last_TG_activity = {}
last_OM_activity = {}
2020-12-07 21:33:15 +00:00
2020-12-07 17:00:49 +00:00
def on_connect():
print('Connecting to the Brandmeister API')
2020-12-07 17:00:49 +00:00
def on_disconnect():
print('Disconnected')
2020-12-07 17:00:49 +00:00
def on_reconnect():
print('Reconnecting')
def push_message(msg):
if cfg.pushover_token != "" and cfg.pushover_user != "":
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": cfg.pushover_token,
"user": cfg.pushover_user,
"message": msg,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
2020-12-07 17:00:49 +00:00
2020-12-07 21:33:15 +00:00
def construct_message(c):
tg = c["DestinationID"]
2020-12-07 17:00:49 +00:00
out = ""
2020-12-07 21:33:15 +00:00
duration = c["Stop"] - c["Start"]
# convert unix time stamp to human readable format
time = dt.datetime.utcfromtimestamp(c["Start"]).strftime("%Y/%m/%d %H:%M")
2020-12-08 00:32:31 +00:00
# construct text message from various transmission properties
2020-12-07 21:33:15 +00:00
out += c["SourceCall"] + ' (' + c["SourceName"] + ') was active on '
out += str(tg) + ' (' + c["DestinationName"] + ') at '
out += time + ' (' + str(duration) + ' seconds)'
# finally return the text message
return out
2020-12-07 21:33:15 +00:00
def on_mqtt(*args):
2020-12-08 00:32:31 +00:00
# get json data of transmission
2020-12-07 17:00:49 +00:00
call = json.loads(args[0]['payload'])
2020-12-07 21:33:15 +00:00
tg = call["DestinationID"]
callsign = call["SourceCall"]
start_time = call["Start"]
stop_time = call["Stop"]
2020-12-08 11:19:16 +00:00
notify = False
2020-12-08 09:31:05 +00:00
now = int(time.time())
2020-12-08 00:32:31 +00:00
# check if callsign is monitored, the transmission has already been finished
# and the person was inactive for n seconds
if callsign in cfg.callsigns:
2020-12-08 09:31:05 +00:00
if callsign not in last_OM_activity:
last_OM_activity[callsign] = 9999999
inactivity = now - last_OM_activity[callsign]
if callsign not in last_OM_activity or inactivity >= cfg.min_silence:
2020-12-08 00:32:31 +00:00
# If the activity has happened in a monitored TG, remember the transmission start time stamp
if tg in cfg.talkgroups and stop_time > 0:
2020-12-08 09:31:05 +00:00
last_TG_activity[tg] = now
2020-12-08 00:32:31 +00:00
# remember the transmission time stamp of this particular DMR user
2020-12-08 09:31:05 +00:00
last_OM_activity[callsign] = now
2020-12-08 11:19:16 +00:00
#msg = construct_message(call)
notify = True
2020-12-08 00:32:31 +00:00
# Continue if the talkgroup is monitored, the transmission has been finished and there was no activity
# during the last n seconds in this talkgroup
2020-12-08 11:19:16 +00:00
elif tg in cfg.talkgroups and stop_time > 0:# and callsign not in cfg.noisy_calls:
2020-12-08 09:31:05 +00:00
if tg not in last_TG_activity:
last_TG_activity[tg] = 9999999
inactivity = now - last_TG_activity[tg]
# calculate duration of key down
duration = stop_time - start_time
# only proceed if the key down has been long enough
if duration >= cfg.min_duration:
if tg not in last_TG_activity or inactivity >= cfg.min_silence:
2020-12-08 11:19:16 +00:00
#msg = construct_message(call)
notify = True
2020-12-08 09:48:00 +00:00
elif cfg.verbose:
print("ignored activity in TG " + str(tg) + " from " + callsign + ": last action " + str(inactivity) + " seconds ago.")
2020-12-08 09:31:05 +00:00
last_TG_activity[tg] = now
2020-12-08 10:25:09 +00:00
if cfg.verbose and callsign in cfg.noisy_calls:
print("ignored noisy ham " + callsign)
# finally write the message to the console and send a push notification
2020-12-08 11:19:16 +00:00
if notify:
msg = construct_message(call)
print(msg)
push_message(msg)
2020-12-07 17:00:49 +00:00
socket = SocketIO('https://api.brandmeister.network/lh')
socket.on('connect', on_connect)
socket.on('disconnect', on_disconnect)
socket.on('reconnect', on_reconnect)
socket.on('mqtt', on_mqtt)
socket.wait()