forked from n8acl/bm_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbm_monitor.py
192 lines (162 loc) · 7.08 KB
/
bm_monitor.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
#################################################################################
# Brandmeister Monitor
# Develped by: Michael Clemens, DK1MI
# Refactored by: Jeff Lehman, N8ACL
# Modified by: Michael Grigutsch, DO3BOX
# Current Version: 1.2
# Original Script: https://codeberg.org/mclemens/pyBMNotify
# Refactored Script: https://github.com/n8acl/bm_monitor
# Questions? Comments? Suggestions? Contact me one of the following ways:
# E-mail: n8acl@qsl.net
# Twitter: @n8acl
# Discord: Ravendos#7364
# Mastodon: @n8acl@mastodon.radio
# Website: https://www.qsl.net/n8acl
################### DO NOT CHANGE BELOW #########################
#############################
##### Import Libraries and configs
import config as cfg
import json
import datetime as dt
import time
import socketio
import http.client, urllib
# libary only needed if Discord is configured in config.py
if cfg.discord:
from discord_webhook import DiscordWebhook
# libraries only needed if Telegram is configured in config.py
# if cfg.telegram:
# import telebot
# from telethon.sync import TelegramClient
# from telethon.tl.types import InputPeerUser, InputPeerChannel
# from telethon import TelegramClient, sync, events
# libraries only needed if dapnet or telegram is configured in config.py
if cfg.dapnet or cfg.telegram:
import requests
from requests.auth import HTTPBasicAuth
#############################
##### Define Variables
sio = socketio.Client()
last_TG_activity = {}
last_OM_activity = {}
#############################
##### Define Functions
# Send push notification via Pushover. Disabled if not configured in config.py
def push_pushover(msg):
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()
def push_telegram(msg):
telegram_url = "https://api.telegram.org/bot" + cfg.telegram_api_hash + "/sendmessage"
response = requests.post(
telegram_url, json = msg, # data=json.dumps(msg),
headers={'Content-Type': 'application/json'}
)
# send pager notification via DAPNET. Disabled if not configured in config.py
def push_dapnet(msg):
dapnet_json = json.dumps({"text": msg, "callSignNames": cfg.dapnet_callsigns, "transmitterGroupNames": [cfg.dapnet_txgroup], "emergency": True})
response = requests.post(cfg.dapnet_url, data=dapnet_json, auth=HTTPBasicAuth(cfg.dapnet_user,cfg.dapnet_pass))
# Send notification to Discord Channel via webhook
def push_discord(wh_url, msg):
webhook = DiscordWebhook(url=wh_url, content=msg)
response = webhook.execute()
def construct_message(c):
tg = c["DestinationID"]
out = ""
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")
# construct text message from various transmission properties
out += c["SourceCall"] + ' (' + c["SourceName"] + ') was active on '
out += str(tg) + ' (' + c["DestinationName"] + ') at '
out += time + ' (' + str(duration) + ' seconds)'
# finally return the text message
print (out)
return out
def construct_long_message(c):
tg = c["DestinationID"]
out = ""
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")
# convert SourceID zu str
source_id = str(c["SourceID"])
# convert TalkerAlias zu str
talker_alias = str(c.get("TalkerAlias", ""))
# convert ContextID zu str
relais = str(c["ContextID"])
# construct text message from various transmission properties
out += 'Call: ' + c["SourceCall"] + ' (' + source_id + ', ' + c["SourceName"] + ', TA:' + talker_alias + '):\n'
out += 'TG: ' + str(tg) + ' (' + c["DestinationName"] + ') at ' + time + ' (' + str(duration) + ' seconds)\n'
out += 'RelaisID:' + str(relais) + ' (' + c["LinkCall"] + ')\n---\n'
# finally return the text message
if cfg.verbose:
print (out)
return out
#############################
##### Define SocketIO Callback Functions
@sio.event
def connect():
print('connection established')
@sio.on("mqtt")
def on_mqtt(data):
call = json.loads(data['payload'])
tg = call["DestinationID"]
rpt = call["LinkCall"]
callsign = call["SourceCall"]
start_time = call["Start"]
stop_time = call["Stop"]
notify = False
now = int(time.time())
if cfg.verbose and callsign in cfg.noisy_calls:
print("ignored noisy ham " + callsign)
else:
# check if callsign is monitored, the transmission has already been finished
# and the person was inactive for n seconds
if rpt in cfg.repeater or callsign in cfg.callsigns:
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:
# If the activity has happened in a monitored TG, remember the transmission start time stamp
if tg in cfg.talkgroups and stop_time > 0:
last_TG_activity[tg] = now
# remember the transmission time stamp of this particular DMR user
last_OM_activity[callsign] = now
notify = True
# Continue if the talkgroup is monitored, the transmission has been
# finished and there was no activity during the last n seconds in this talkgroup
elif tg in cfg.talkgroups and stop_time > 0:# and callsign not in cfg.noisy_calls:
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:
notify = True
elif cfg.verbose:
print("ignored activity in TG " + str(tg) + " from " + callsign + ": last action " + str(inactivity) + " seconds ago.")
last_TG_activity[tg] = now
if notify:
if cfg.pushover:
push_pushover(construct_message(call))
if cfg.telegram:
push_telegram({'text': construct_long_message(call), 'chat_id': cfg.telegram_api_id, "disable_notification": True})
if cfg.dapnet:
push_dapnet(construct_message(call))
if cfg.discord:
push_discord(cfg.discord_wh_url, construct_long_message(call))
@sio.event
def disconnect():
print('disconnected from server')
#############################
##### Main Program
sio.connect(url='https://api.brandmeister.network', socketio_path="/lh/socket.io", transports="websocket")
sio.wait()