FreeDATA/tnc/helpers.py

191 lines
6.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 25 21:25:14 2020
@author: DJ2LS
"""
import time
2021-03-12 13:14:36 +00:00
import crcengine
import static
2021-09-25 13:24:25 +00:00
2021-08-15 10:34:28 +00:00
def wait(seconds):
2021-08-14 18:00:32 +00:00
timeout = time.time() + seconds
2021-09-25 13:24:25 +00:00
2021-08-14 18:00:32 +00:00
while time.time() < timeout:
time.sleep(0.01)
return True
def get_crc_8(data):
2021-03-12 13:39:36 +00:00
"""
Author: DJ2LS
2021-03-12 13:39:36 +00:00
Get the CRC8 of a byte string
2021-03-12 13:39:36 +00:00
param: data = bytes()
"""
2021-03-12 13:14:36 +00:00
crc_algorithm = crcengine.new('crc8-ccitt') # load crc8 library
crc_data = crc_algorithm(data)
2021-03-12 13:14:36 +00:00
crc_data = crc_data.to_bytes(1, byteorder='big')
return crc_data
2021-03-12 13:14:36 +00:00
def get_crc_16(data):
2021-03-12 13:39:36 +00:00
"""
Author: DJ2LS
2021-03-12 13:39:36 +00:00
Get the CRC16 of a byte string
2021-03-12 13:39:36 +00:00
param: data = bytes()
"""
2021-03-12 13:14:36 +00:00
crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc16 library
crc_data = crc_algorithm(data)
2021-03-12 13:14:36 +00:00
crc_data = crc_data.to_bytes(2, byteorder='big')
return crc_data
def get_crc_32(data):
"""
Author: DJ2LS
2021-09-25 13:24:25 +00:00
Get the CRC32 of a byte string
param: data = bytes()
"""
crc_algorithm = crcengine.new('crc32') # load crc16 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(4, byteorder='big')
return crc_data
2021-09-25 13:24:25 +00:00
def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
2021-03-16 14:21:58 +00:00
# check if buffer empty
if len(static.HEARD_STATIONS) == 0:
static.HEARD_STATIONS.append([dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency])
2021-03-16 14:21:58 +00:00
# if not, we search and update
else:
for i in range(0, len(static.HEARD_STATIONS)):
# update callsign with new timestamp
if static.HEARD_STATIONS[i].count(dxcallsign) > 0:
static.HEARD_STATIONS[i] = [dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency]
2021-03-16 14:21:58 +00:00
break
# insert if nothing found
if i == len(static.HEARD_STATIONS) - 1:
static.HEARD_STATIONS.append([dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency])
2021-03-16 14:21:58 +00:00
break
2021-09-25 13:24:25 +00:00
2021-03-16 14:21:58 +00:00
# for idx, item in enumerate(static.HEARD_STATIONS):
# if dxcallsign in item:
# item = [dxcallsign, int(time.time())]
2021-09-25 13:24:25 +00:00
# static.HEARD_STATIONS[idx] = item
def callsign_to_bytes(callsign):
# http://www.aprs.org/aprs11/SSIDs.txt
#-0 Your primary station usually fixed and message capable
#-1 generic additional station, digi, mobile, wx, etc
#-2 generic additional station, digi, mobile, wx, etc
#-3 generic additional station, digi, mobile, wx, etc
#-4 generic additional station, digi, mobile, wx, etc
#-5 Other networks (Dstar, Iphones, Androids, Blackberry's etc)
#-6 Special activity, Satellite ops, camping or 6 meters, etc
#-7 walkie talkies, HT's or other human portable
#-8 boats, sailboats, RV's or second main mobile
#-9 Primary Mobile (usually message capable)
#-10 internet, Igates, echolink, winlink, AVRS, APRN, etc
#-11 balloons, aircraft, spacecraft, etc
#-12 APRStt, DTMF, RFID, devices, one-way trackers*, etc
#-13 Weather stations
#-14 Truckers or generally full time drivers
#-15 generic additional station, digi, mobile, wx, etc
# try converting to bytestring if possible type string
try:
callsign = bytes(callsign, 'utf-8')
except:
pass
# we need to do this step to reduce the needed paypload by the callsign ( stripping "-" out of the callsign )
callsign = callsign.split(b'-')
try:
ssid = int(callsign[1])
except:
ssid = 0
callsign = callsign[0]
bytestring = bytearray(8)
bytestring[:len(callsign)] = callsign
bytestring[7:8] = bytes([ssid])
return bytes(bytestring)
def bytes_to_callsign(bytestring):
# http://www.aprs.org/aprs11/SSIDs.txt
#-0 Your primary station usually fixed and message capable
#-1 generic additional station, digi, mobile, wx, etc
#-2 generic additional station, digi, mobile, wx, etc
#-3 generic additional station, digi, mobile, wx, etc
#-4 generic additional station, digi, mobile, wx, etc
#-5 Other networks (Dstar, Iphones, Androids, Blackberry's etc)
#-6 Special activity, Satellite ops, camping or 6 meters, etc
#-7 walkie talkies, HT's or other human portable
#-8 boats, sailboats, RV's or second main mobile
#-9 Primary Mobile (usually message capable)
#-10 internet, Igates, echolink, winlink, AVRS, APRN, etc
#-11 balloons, aircraft, spacecraft, etc
#-12 APRStt, DTMF, RFID, devices, one-way trackers*, etc
#-13 Weather stations
#-14 Truckers or generally full time drivers
#-15 generic additional station, digi, mobile, wx, etc
# we need to do this step to reduce the needed paypload by the callsign ( stripping "-" out of the callsign )
callsign = bytes(bytestring[:7])
callsign = callsign.rstrip(b'\x00')
ssid = int.from_bytes(bytes(bytestring[7:8]), "big")
callsign = callsign + b'-'
callsign = callsign.decode('utf-8')
callsign = callsign + str(ssid)
callsign = callsign.encode('utf-8')
return bytes(callsign)
2021-11-18 18:40:22 +00:00
'''
2021-02-16 13:23:57 +00:00
def setup_logging():
2021-03-12 13:39:36 +00:00
"""
Author: DJ2LS
2021-03-12 13:39:36 +00:00
Set the custom logging format so we can use colors
2021-09-25 13:24:25 +00:00
# https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
# 'DEBUG' : 37, # white
# 'INFO' : 36, # cyan
# 'WARNING' : 33, # yellow
# 'ERROR' : 31, # red
# 'CRITICAL': 41, # white on red bg
2021-03-12 13:14:36 +00:00
2021-09-25 13:24:25 +00:00
"""
2021-02-16 13:23:57 +00:00
logging.basicConfig(level=logging.INFO,encoding='utf-8',format='%(asctime)s.%(msecs)03d %(levelname)s:\t%(message)s',datefmt='%H:%M:%S',handlers=[logging.FileHandler("FreeDATA-TNC.log"), logging.StreamHandler()])
2021-09-25 13:24:25 +00:00
2021-09-26 15:51:11 +00:00
logging.addLevelName(logging.DEBUG, "\033[1;36m%s\033[1;0m" % logging.getLevelName(logging.DEBUG))
logging.addLevelName(logging.INFO, "\033[1;37m%s\033[1;0m" % logging.getLevelName(logging.INFO))
logging.addLevelName(logging.WARNING, "\033[1;33m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
2021-03-12 13:14:36 +00:00
logging.addLevelName(logging.ERROR, "\033[1;31m%s\033[1;0m" % "FAILED")
2021-02-16 13:23:57 +00:00
#logging.addLevelName( logging.ERROR, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
2021-09-26 15:51:11 +00:00
logging.addLevelName(logging.CRITICAL, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL))
2021-02-16 13:23:57 +00:00
2021-03-12 13:14:36 +00:00
logging.addLevelName(25, "\033[1;32m%s\033[1;0m" % "SUCCESS")
logging.addLevelName(24, "\033[1;34m%s\033[1;0m" % "DATA")
2021-11-18 18:40:22 +00:00
'''