Working iGate
This commit is contained in:
@@ -123,6 +123,8 @@ def parsePacket(string):
|
||||
# Split the address and payload separated by APRS PID
|
||||
buffer = string.split(b'\x03\xf0')
|
||||
address = buffer[0]
|
||||
#Define empty list in case packet does not has digipeaters in path
|
||||
digipeaters = []
|
||||
|
||||
# Check if the first byte indicates it is a data packet
|
||||
if address[0] == 0:
|
||||
@@ -207,7 +209,7 @@ def process_aprsis(packet):
|
||||
if rflog_file == 0:
|
||||
return 0
|
||||
|
||||
string = timestamp + ' ' + 'APRSIS ' + ' R ' + str(packet, 'utf-8') + '\n'
|
||||
string = timestamp + ' ' + 'APRSIS ' + ' R ' + str(packet, 'latin-1') + '\n'
|
||||
|
||||
try:
|
||||
with open(rflog_file, "a") as logfile:
|
||||
@@ -236,6 +238,17 @@ def send_aprsis(srcCall, dest, digi, msg):
|
||||
if ('NOGATE' in digi):
|
||||
print(">>> NOGATE, not igated")
|
||||
return 1
|
||||
if msg[0] == '}':
|
||||
if ('TCPIP' in msg) or ('TCPXX' in msg):
|
||||
print(">>> Third party packet, not igated")
|
||||
return 1
|
||||
# TODO: strip third party header and send to igate
|
||||
else:
|
||||
print(">>> Third party packet, not igated")
|
||||
return 1
|
||||
if msg[0] == '?':
|
||||
print(">>> Query, not igated")
|
||||
return 1
|
||||
|
||||
message = srcCall + '>' + dest + ',' + digi + ':' + msg
|
||||
|
||||
@@ -352,12 +365,25 @@ def send_telemetry():
|
||||
|
||||
send_ax25('PE1RXF-3', 'PE1RXF-13', "APZMDM", 0, message)
|
||||
|
||||
def read_weather_station(weather_station):
|
||||
global WxData
|
||||
#print ("Reading registers of weather station.")
|
||||
if weather_station.get_weather_data() == 0:
|
||||
print ('No response from ModBus, even after 5 retries. Keep trying.')
|
||||
else:
|
||||
WxData = weather_station.wx_data
|
||||
|
||||
def check_heater(weather_station):
|
||||
# Check if heater is off, if so, turn it on
|
||||
if weather_station.wx_data['Status bits'] & 0x4 == 0:
|
||||
weather_station.enable_heater()
|
||||
print("Heater was off, turned it back on gain.")
|
||||
|
||||
def check_thread_alive(thr):
|
||||
thr.join(timeout=0.0)
|
||||
# returns True if the thread is still running and False, otherwise
|
||||
return thr.is_alive()
|
||||
|
||||
def run():
|
||||
|
||||
global WxData
|
||||
@@ -396,14 +422,14 @@ def run():
|
||||
else:
|
||||
print("Got response from the weather station. Weather information is available.")
|
||||
|
||||
# NOTE: Should be done periodically! So when the weather station is unplugged and plugged back in, the heater will be enabled again.
|
||||
try:
|
||||
weather_station.enable_heater()
|
||||
except:
|
||||
print("No response from the weather station via the ModBus, even after several retries. Disabling the weather station.")
|
||||
sys.exit(1) # Make program work without weather station!
|
||||
else:
|
||||
print("Enabled the heater function on the weather station.")
|
||||
# NOTE: Is now done periodically. So when the weather station is unplugged and plugged back in, the heater will be enabled again.
|
||||
#try:
|
||||
# weather_station.enable_heater()
|
||||
#except:
|
||||
# print("No response from the weather station via the ModBus, even after several retries. Disabling the weather station.")
|
||||
# sys.exit(1) # Make program work without weather station!
|
||||
#else:
|
||||
# print("Enabled the heater function on the weather station.")
|
||||
|
||||
###
|
||||
# Schedule all periodic transmissions (use a little randomness)
|
||||
@@ -428,12 +454,16 @@ def run():
|
||||
print("Scheduled telemetry transmission.")
|
||||
schedule.every(10).minutes.do(send_telemetry)
|
||||
|
||||
# ScheduleL check if heater is still on
|
||||
# Schedule check if heater is still on
|
||||
# So when the weather station is unplugged and plugged back in, the heater will be enabled again.
|
||||
schedule.every(10).minutes.do(check_heater, weather_station)
|
||||
|
||||
# Schedule readout of weather station
|
||||
print("Scheduled readout of weather station.")
|
||||
schedule.every(1).minutes.do(read_weather_station, weather_station)
|
||||
|
||||
# Connect to incoming APRS-IS feed
|
||||
# by default `raw` is False, then each line is ran through aprslib.parse()
|
||||
|
||||
# Set filter on incomming feed
|
||||
APRSIS.set_filter(Configuration.config_file_settings['aprsis']['filter'])
|
||||
# This is a blocking call, should run as seperate thread
|
||||
@@ -445,22 +475,34 @@ def run():
|
||||
while (1):
|
||||
|
||||
#print ("Reading registers of weather station.")
|
||||
if weather_station.get_weather_data() == 0:
|
||||
print ('No response from ModBus, even after 5 retries. Keep trying.')
|
||||
else:
|
||||
WxData = weather_station.wx_data
|
||||
#if weather_station.get_weather_data() == 0:
|
||||
# print ('No response from ModBus, even after 5 retries. Keep trying.')
|
||||
#else:
|
||||
# WxData = weather_station.wx_data
|
||||
|
||||
# Scheduler
|
||||
schedule.run_pending()
|
||||
|
||||
# Check if APRS-IS thread is still running, if not restart it
|
||||
if check_thread_alive(thread) == False:
|
||||
# Set filter on incomming feed
|
||||
APRSIS.set_filter(Configuration.config_file_settings['aprsis']['filter'])
|
||||
# This is a blocking call, should run as seperate thread
|
||||
# create a thread
|
||||
thread = Thread(target=APRSIS.consumer, args=(process_aprsis, True, True, True))
|
||||
# run the thread
|
||||
thread.start()
|
||||
|
||||
# Listen on all ax25 ports and send to APRS-IS
|
||||
receive = receive_ax25(rx_socket)
|
||||
for port in range(len(axdevice)):
|
||||
if receive[0][1] == axdevice[port]:
|
||||
#print(receive)
|
||||
source, destination, digipeaters, payload = parsePacket(receive[1])
|
||||
#print(receive[1])
|
||||
# bug UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 47: invalid start byte
|
||||
# Convert byte string to normal string
|
||||
try:
|
||||
payload = payload.decode()
|
||||
payload = payload.decode('latin-1')
|
||||
except:
|
||||
payload = 'NOT VALID'
|
||||
|
||||
|
Reference in New Issue
Block a user