First commit
This commit is contained in:
160
config_reader.py
Normal file
160
config_reader.py
Normal file
@@ -0,0 +1,160 @@
|
||||
'''
|
||||
# A basic APRS iGate and APRS weather station with additional (optional) PE1RXF telemetry support
|
||||
#
|
||||
# This program reads the registers of the PE1RXF weather station via ModBus RTU and sends it as
|
||||
# an APRS WX report over APRS. Additionally, it sends beacons and forwards received APRS messages
|
||||
# to the APRS-IS network. All configurable via a YAML file called pe1rxf_aprs.yml.
|
||||
#
|
||||
# This program also has a PE1RXF APRS telemetry to MQTT bridge, which is configurable via pe1rxf_telemetry.yml
|
||||
#
|
||||
# Copyright (C) 2023, 2024 M.T. Konstapel https://meezenest.nl/mees
|
||||
#
|
||||
# This file is part of weather_station
|
||||
#
|
||||
# weather_station is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# weather_station is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with weather_station. If not, see <https://www.gnu.org/licenses/>.
|
||||
'''
|
||||
|
||||
import yaml
|
||||
from yaml.loader import SafeLoader
|
||||
|
||||
class config_reader:
|
||||
|
||||
# initiate class: define name configuration files
|
||||
def __init__(self, main_config_file, telemetry_config_file):
|
||||
self.config_file = main_config_file
|
||||
self.telemetry_file = telemetry_config_file
|
||||
self.modbus_settings = []
|
||||
self.aprsis_settings = []
|
||||
self.weather_settings = []
|
||||
self.beacon_settings = []
|
||||
self.telemetry_settings = []
|
||||
|
||||
def read_settings(self):
|
||||
|
||||
if self.read_config_file() == 0:
|
||||
return 0
|
||||
|
||||
if self.read_telemetry_file() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_global_settings() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_modbus_settings() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_aprsis_settings() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_weather_settings() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_beacon_settings() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_telemetry_settings() == 0:
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
def read_config_file (self):
|
||||
try:
|
||||
with open(self.config_file) as f:
|
||||
self.config_file_settings = yaml.load(f, Loader=SafeLoader)
|
||||
except:
|
||||
print ("Configuration file ./" + self.config_file + " not found or syntax error in file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def read_telemetry_file (self):
|
||||
try:
|
||||
with open(self.telemetry_file) as f:
|
||||
self.telemetry_settings = yaml.load(f, Loader=SafeLoader)
|
||||
except:
|
||||
print ("Telemetry configuration file ./" + self.telemetry_file + " not found or syntax error in file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
|
||||
# Test if all settings are pressebt
|
||||
def test_global_settings(self):
|
||||
# Test is all expected settings are present
|
||||
try:
|
||||
tmp = self.config_file_settings['global']['log-rf']
|
||||
except:
|
||||
print ("Error in the global section of the configuration file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
# Test if all settings are pressebt
|
||||
def test_modbus_settings(self):
|
||||
# Test is all expected settings are present
|
||||
try:
|
||||
tmp = self.config_file_settings['modbus']['port']
|
||||
tmp = self.config_file_settings['modbus']['address']
|
||||
except:
|
||||
print ("Error in the modbus section of the configuration file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def test_aprsis_settings(self):
|
||||
try:
|
||||
tmp = self.config_file_settings['aprsis']['call']
|
||||
tmp = self.config_file_settings['aprsis']['passcode']
|
||||
tmp = self.config_file_settings['aprsis']['server']
|
||||
tmp = self.config_file_settings['aprsis']['port']
|
||||
tmp = self.config_file_settings['aprsis']['filter']
|
||||
except:
|
||||
print ("Error in the aprsis section of the configuration file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def test_weather_settings(self):
|
||||
for entry in self.config_file_settings['weather']:
|
||||
try:
|
||||
tmp = entry['port']
|
||||
tmp = entry['call']
|
||||
tmp = entry['destination']
|
||||
tmp = entry['digi_path']
|
||||
tmp = entry['position']
|
||||
tmp = entry['interval']
|
||||
except:
|
||||
print ("Error in the weather section of the configuration file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def test_beacon_settings(self):
|
||||
for entry in self.config_file_settings['beacon']:
|
||||
try:
|
||||
tmp = entry['port']
|
||||
tmp = entry['call']
|
||||
tmp = entry['destination']
|
||||
tmp = entry['digi_path']
|
||||
tmp = entry['position']
|
||||
tmp = entry['interval']
|
||||
tmp = entry['message']
|
||||
except:
|
||||
print ("Error in the beacon section of the configuration file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def test_telemetry_settings(self):
|
||||
return 1
|
Reference in New Issue
Block a user