add SMS capture

This commit is contained in:
KF7EEL 2021-05-02 09:09:20 -07:00
parent 11a296252d
commit 979fedb218
4 changed files with 243 additions and 148 deletions

View File

@ -112,7 +112,7 @@ __copyright__ = 'Copyright (c) 2016-2019 Cortney T. Buffington, N0MJS and the K
__credits__ = 'Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman, N4IRR; Jonathan Naylor, G4KLX; Hans Barthen, DL5DI; Torsten Shultze, DG1HT, Xavier FRS2013'
__license__ = 'GNU GPLv3'
__maintainer__ = 'Eric Craw, KF7EEL'
__email__ = 'n0mjs@me.com'
__email__ = 'kf7eel@qsl.net'
# Module gobal varaibles
@ -206,6 +206,18 @@ def dashboard_bb_write(call, dmr_id, time, bulletin):
logger.info('User bulletin entry saved.')
#logger.info(dash_bb)
def dashboard_sms_write(snd_call, rcv_call, rcv_dmr_id, snd_dmr_id, sms, time):
#try:
dash_sms = ast.literal_eval(os.popen('cat ' + sms_file).read())
# except:
# dash_entries = []
dash_sms.insert(0, {'snd_call': snd_call, 'rcv_call':rcv_call, 'snd_dmr_id': snd_dmr_id, 'rcv_dmr_id':rcv_dmr_id, 'time': time, 'sms':sms})
with open(sms_file, 'w') as user_sms_file:
user_sms_file.write(str(dash_sms[:25]))
user_sms_file.close()
logger.info('User SMS entry saved.')
def mailbox_write(call, dmr_id, time, message, recipient):
#try:
mail_file = ast.literal_eval(os.popen('cat ' + the_mailbox_file).read())
@ -2343,7 +2355,7 @@ class routerHBP(HBSYSTEM):
#logger.info(_rf_src)
#logger.info((ba2num(bptc_decode(_data)[8:12])))
################################################################3###### CHNGED #########
if int_id(_dst_id) == data_id:
## if int_id(_dst_id) == data_id:
#logger.info(type(_seq))
if type(_seq) is bytes:
pckt_seq = int.from_bytes(_seq, 'big')
@ -2417,6 +2429,7 @@ class routerHBP(HBSYSTEM):
logger.info('User SSID: ' + ssid)
logger.info('User icon: ' + icon_table + icon_icon)
# Attempt to prevent malformed packets from being uploaded.
if int_id(_dst_id) == data_id:
try:
aprslib.parse(aprs_loc_packet)
float(lat_deg) < 91
@ -2482,6 +2495,7 @@ class routerHBP(HBSYSTEM):
nmea_parse = re.sub('.*\$|\n.*|V\*.*', '', final_packet)
loc = pynmea2.parse(nmea_parse, check=False)
logger.info('Latitude: ' + str(loc.lat) + str(loc.lat_dir) + ' Longitude: ' + str(loc.lon) + str(loc.lon_dir) + ' Direction: ' + str(loc.true_course) + ' Speed: ' + str(loc.spd_over_grnd) + '\n')
if int_id(_dst_id) == data_id:
try:
# Begin APRS format and upload
# Disable opening file for reading to reduce "collision" or reading and writing at same time.
@ -2561,7 +2575,23 @@ class routerHBP(HBSYSTEM):
sms = codecs.decode(bytes.fromhex(''.join(sms_hex_string[:-8].split('00'))), 'utf-8', 'ignore')
msg_found = re.sub('.*\n', '', sms)
logger.info('\n\n' + 'Received SMS from ' + str(get_alias(int_id(_rf_src), subscriber_ids)) + ', DMR ID: ' + str(int_id(_rf_src)) + ': ' + str(msg_found) + '\n')
if int_id(_dst_id) == data_id:
process_sms(_rf_src, msg_found, _call_type)
if int_id(_dst_id) != data_id:
if '//' in sms or sms.count(',') > 4 or sms.count('.') > 3 or 'PIN' in sms:
logger.info('Not logging due to possible data error, or double slash.')
else:
if int_id(_dst_id) == data_id:
logger.info('SMS command, not logging')
else:
logger.info('Log SMS to dashboard and subscriber mailbox')
mailbox_write(get_alias(int_id(_rf_src), subscriber_ids), int_id(_rf_src), time(), msg_found, str(get_alias(int_id(_dst_id), subscriber_ids)).upper())
dashboard_sms_write(str(get_alias(int_id(_rf_src), subscriber_ids)), str(get_alias(int_id(_dst_id), subscriber_ids)), str(int_id(_dst_id)), str(int_id(_rf_src)), msg_found, time())
#packet_assembly = ''
pass
#logger.info(bitarray(re.sub("\)|\(|bitarray|'", '', str(bptc_decode(_data)).tobytes().decode('utf-8', 'ignore'))))
@ -2585,14 +2615,14 @@ class routerHBP(HBSYSTEM):
if _call_type == 'group':
self.group_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _frame_type, _dtype_vseq, _stream_id, _data)
# If destination ID = to DATA_DMR_ID, process packet
if int_id(_dst_id) == data_id:
#if int_id(_dst_id) == data_id:
self.data_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
# Used for dev, leave commented out
#self.data_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
elif _call_type == 'unit':
# If destination ID = to DATA_DMR_ID, process packet
if int_id(_dst_id) == data_id:
#if int_id(_dst_id) == data_id:
#logger.info('btf' + str(btf))
self.data_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
# Used for dev, leave commented out
@ -2608,7 +2638,7 @@ class routerHBP(HBSYSTEM):
self.group_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _frame_type, _dtype_vseq, _stream_id, _data)
logger.debug('CSBK recieved, but HBlink does not process them currently. Packets routed to talkgroup.')
# If destination ID = to DATA_DMR_ID, process packet
if int_id(_dst_id) == data_id:
#if int_id(_dst_id) == data_id:
self.data_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
else:
logger.error('Unknown call type recieved -- not processed')
@ -2679,6 +2709,7 @@ if __name__ == '__main__':
loc_file = CONFIG['GPS_DATA']['LOCATION_FILE']
the_mailbox_file = CONFIG['GPS_DATA']['MAILBOX_FILE']
emergency_sos_file = CONFIG['GPS_DATA']['EMERGENCY_SOS_FILE']
sms_file = CONFIG['GPS_DATA']['SMS_FILE']
# User APRS settings
user_settings_file = CONFIG['GPS_DATA']['USER_SETTINGS_FILE']
@ -2707,6 +2738,14 @@ if __name__ == '__main__':
with open(bb_file, 'w') as user_bb_file:
user_bb_file.write("[]")
user_bb_file.close()
if Path(sms_file).is_file():
pass
else:
Path(sms_file).touch()
with open(sms_file, 'w') as user_sms_file:
user_sms_file.write("[]")
user_sms_file.close()
#Only create if API enabled
if use_api == True:
# Generate the apps dictionary

View File

@ -92,6 +92,52 @@ def get_loc_data():
except:
return str('<h1 style="text-align: center;">No data</h1>')
def get_sms_data():
try:
dash_sms = ast.literal_eval(os.popen('cat ' + sms_file).read())
tmp_sms = ''
sms_hdr = '''
<tr>
<td style="text-align: center;">
<h2><strong>&nbsp;Source <br /></strong></h2>
</td>
<td style="text-align: center;">
<h2>&nbsp;<strong>Destination</strong> </h2>
</td>
<td style="text-align: center;">
<h2>&nbsp;<strong>SMS</strong> </h2>
</td>
<td style="text-align: center;">
<h2>&nbsp;<strong>Local Time</strong>&nbsp;</h2>
</td>
</tr>
'''
for e in dash_sms:
if type(e['time']) == str:
loc_time = str(e['time'])
if type(e['time']) == int or type(e['time']) == float:
loc_time = datetime.fromtimestamp(e['time']).strftime(time_format)
tmp_sms = tmp_sms + '''
<tr>
<td style="text-align: center;">
<p><strong>&nbsp;''' + e['snd_call'] + '''&nbsp;<br /></strong>''' + e['snd_dmr_id'] + '''<strong><br /></strong></p>
</td>
<td style="text-align: center;">
<p><strong>''' + e['rcv_call'] + '''</strong><br />''' + e['rcv_dmr_id'] + '''</p>
</td>
<td style="text-align: center;"><strong>&nbsp;''' + e['sms'] + '''&nbsp;</strong></td>
<td style="text-align: center;">&nbsp;''' + loc_time + '''&nbsp;</td>
</tr>
'''
return str('<h1 style="text-align: center;">SMS</h1>' + tbl_hdr + sms_hdr + tmp_sms + tbl_ftr)
except:
return str('<h1 style="text-align: center;">No data</h1>')
def get_bb_data():
try:
@ -241,6 +287,12 @@ def dash_loc():
def help():
#return get_data()
return render_template('help.html', title = dashboard_title, dashboard_url = dashboard_url, logo = logo, description = description, api = use_api, data_call_type = data_call_type, data_call_id = data_call_id, aprs_ssid = aprs_ssid)
@app.route('/sms/')
def dash_template_sms():
#return get_data()
#return render_template('sms.html', title = dashboard_title, logo = logo)
return render_template('generic.html', title = dashboard_title, dashboard_url = dashboard_url, logo = logo, content = Markup(get_sms_data()))
@app.route('/about/')
def about():
#return get_data()
@ -1001,6 +1053,8 @@ if __name__ == '__main__':
user_settings_file = parser.get('GPS_DATA', 'USER_SETTINGS_FILE')
auth_token_file = parser.get('GPS_DATA', 'AUTHORIZED_TOKENS_FILE')
sms_file = parser.get('GPS_DATA', 'SMS_FILE')
use_api = parser.get('GPS_DATA', 'USE_API')
#access_systems_file = parser.get('GPS_DATA', 'ACCESS_SYSTEMS_FILE')
#authorized_users_file = parser.get('GPS_DATA', 'AUTHORIZED_USERS_FILE')

View File

@ -7,6 +7,7 @@
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/';"> Dashboard Home </button></td>
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/map';"> Station Map </button></td>
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/mailbox';"> The Mailbox </button></td>
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/sms';"> SMS Capture </button></td>
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/user';"> User Settings </button></td>
{% if api %}
<td style="text-align: center;"><button onclick="window.location.href='{{dashboard_url}}/external_apps';"> External Applications </button></td>

View File

@ -168,6 +168,7 @@ def build_config(_config_file):
'LOCATION_FILE': config.get(section, 'LOCATION_FILE'),
'BULLETIN_BOARD_FILE': config.get(section, 'BULLETIN_BOARD_FILE'),
'MAILBOX_FILE': config.get(section, 'MAILBOX_FILE'),
'SMS_FILE': config.get(section, 'SMS_FILE'),
'EMERGENCY_SOS_FILE': config.get(section, 'EMERGENCY_SOS_FILE'),
'USER_SETTINGS_FILE': config.get(section, 'USER_SETTINGS_FILE'),
'USE_API': config.getboolean(section, 'USE_API'),