Skip to content

Commit

Permalink
Merge pull request #74 from pyonair/feature-stability
Browse files Browse the repository at this point in the history
Feature stability
  • Loading branch information
ossonts authored Jul 21, 2021
2 parents 13b651d + 382cfb8 commit 9fcaa24
Show file tree
Hide file tree
Showing 21 changed files with 821 additions and 497 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/TODO.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
name: Convert TODO to issue
on: ["push"]

on:
#DO NOT CHANGE
workflow_dispatch: ## Manual run possible
push:
branches: [ master , main ]

jobs:
build:
runs-on: "ubuntu-latest" #Fine as we have docker
Expand Down
46 changes: 34 additions & 12 deletions code/lib/Configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import strings as s
from ubinascii import hexlify
from machine import unique_id
from network import LoRa
#from ubinascii import hexlify
#from machine import unique_id
#from network import LoRa
import os
import ujson
from Constants import *
Expand All @@ -24,6 +24,7 @@ def __init__(self,logger):
#RM

self.read_configuration()
print("CONFIG READ########################################")

# Configuration Accessor/Getter
def get_config(self, keys=None):
Expand All @@ -40,6 +41,7 @@ def get_config(self, keys=None):
return self.configuration
if isinstance(keys, list):
return list(self.configuration[k] for k in keys if k in self.configuration)
#TODO remove this multi option
else:
return self.configuration[keys]

Expand All @@ -62,23 +64,42 @@ def save_config(self, new_config):
"""
self.set_config(new_config)

with open(s.root_path + s.config_filename, 'w') as f: # save credentials to sd card
with open(s.root_path + CONFIG_FILE_NAME, 'w') as f: # save credentials to sd card
f.write(ujson.dumps(self.configuration))

# Reads and returns keys and preferences from sd card
def read_configuration(self):
"""
Read config file on SD card and load it to the configuration dict
"""
self.default_configuration = DEFAULT_CONFIG # s.default_configuration #this is from constants/strings
if s.config_filename not in os.listdir('/sd'):
with open('/sd/' + s.config_filename, 'w') as f: # create new config file
f.write(ujson.dumps(DEFAULT_CONFIG))
self.set_config(DEFAULT_CONFIG)
else:
with open('/sd/' + s.config_filename, 'r') as f:
#set to default
self.configuration = DEFAULT_CONFIG


#if no config file
if CONFIG_FILE_NAME not in os.listdir(CONFIG_FILE_DIRECTORY):
with open(CONFIG_FILE_FULL_NAME, 'w') as f: # create new config file
f.write(ujson.dumps(self.configuration))
#self.set_config(str(self.configuration))

#load config
if CONFIG_FILE_NAME in os.listdir(CONFIG_FILE_DIRECTORY):
with open(CONFIG_FILE_FULL_NAME, 'r') as f:
self.set_config(ujson.loads(f.read()))
self.logger.debug(str(self.configuration))


#check for debug force overwrite.


#Override Preferences - DEVELOPER USE ONLY - keep all overwrites here
if DEBUG_CONFIG_FILE_NAME in os.listdir(DEBUG_CONFIG_FILE_DIRECTORY):
self.logger.warning("Overriding configuration with the content of debug_config.json")

with open(DEBUG_CONFIG_FILE_FULL_NAME, 'r') as f:
self.set_config(ujson.loads(f.read()))
print(self.configuration)#TODO logger missing here, use debug logger.
self.logger.warning("Configuration changed to: " + str(self.configuration))


# Returns True if the configuration file is complete
def is_complete(self, logger):
Expand Down Expand Up @@ -111,6 +132,7 @@ def reset_configuration(self, logger):
:type logger: LoggerFactory object
"""
try:
self.logger.warning("This wont work ") #TODO: this method needs sorting
self.configuration.clear() # clear configuration
self.set_config(DEFAULT_CONFIG) # set configuration to default

Expand Down
79 changes: 68 additions & 11 deletions code/lib/Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,79 @@

##Move all constants here.


TIME_ISO8601_FMT = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}" # yyyy-mm-ddThh-mm-ss #https://en.wikipedia.org/wiki/ISO_8601

FILENAME_FMT = "{:04d}-{:02d}-{:02d}" # yyyy-mm-ddThh-mm-ss #https://en.wikipedia.org/wiki/ISO_8601

#Just filename not location/path
LOG_FILENAME="PyonAir.log"
LOG_FILENAME="PyonAir"
LOG_EXT=".log"
#The name of the logger instance -- no need to change
DEFAULT_LOG_NAME="Pyonair"


#Str name of the config in the strings file?
DEFAULT_CONFIG = {"device_id": "", "device_name": "NewPyonAir", "password": "newpyonair", "region": "Europe",
"device_eui": "", "application_eui": "", "app_key": "", "SSID": "", "fmt_version": "",
"wifi_password": "", "TEMP": "SHT35", "PM1": "OFF", "code_version": "",
"PM2": "SPS030", "GPS": "OFF", "PM1_id": "002", "PM2_id": "003", "TEMP_id": "001",
"GPS_id": "004", "interval": 15, "TEMP_period": 30, "GPS_period": 12, "PM1_init": 30,
"PM2_init": 30, "logging_lvl": "Warning", "lora_timeout": 20, "GPS_timeout": 20,
"config_timeout": 10, "fair_access": 30, "air_time": 75, "message_count": 0,
"transmission_date": 0, "LORA": "ON", "update": False, "port": 8000,
"server": "10.15.40.51"}
#Names in JSON file (config)
LOG_LEVEL_KEY= "logging_lvl" #TODO : rename to something sensible
AVERAGES_PER_HOUR_INT_KEY = "averagesPerHour" #averages are done at same time(e.g. on the our, min = once per hour)

PM_SENSOR_SAMPELING_RATE = 1 #How many seconds to wait to read the PM sensors
PM_SAMPLE_COUNT_FOR_AVERAGE = 10 # How many samples to take before doing an average?

#config_filename = 'Settings.cfg'
CONFIG_FILE_NAME = "Settings.json" # JSON file with all PYON air setting s -- on flash but can be on SD so public
CONFIG_FILE_DIRECTORY = "/sd/"
CONFIG_FILE_FULL_NAME = CONFIG_FILE_DIRECTORY + CONFIG_FILE_NAME #os.path does not exist, so string concat

RING_BUFFER_DIR = "/sd/"
RING_BUFFER_FILE = "buffer.obj"

#CONFIG_FILE_FULL_NAME =
DEBUG_CONFIG_FILE_NAME = "debug_config.json"
DEBUG_CONFIG_FILE_DIRECTORY = "/flash/" #os.path does not exist, so string concat
DEBUG_CONFIG_FILE_FULL_NAME = DEBUG_CONFIG_FILE_DIRECTORY + DEBUG_CONFIG_FILE_NAME



#Str name of the config in the strings file?
DEFAULT_CONFIG = {
"device_id": "Test",
"device_name": "NewPyonAir",
"password": "newpyonair",
"region": "Europe",
"device_eui": "DONOTUSE",
"application_eui": "DONOTUSE",
"app_key": "DONOTUSE",
"SSID": "SSID",
"fmt_version": "1",
"wifi_password": "1",
"TEMP": "SHT35",
"PM1": "PMS5003",
"code_version": "1",
"PM2": "SPS030",
"GPS": "ON",
"PM1_id": "002",
"PM2_id": "003",
"TEMP_id": "001",
"GPS_id": "004",
"interval": 15,
"TEMP_period": 30,
"GPS_period": 0.1,
"PM1_init": 10,
"PM2_init": 10,
"logging_lvl": "DEBUG",
"lora_timeout": 20,
"GPS_timeout": 20,
"config_timeout": 10,
"fair_access": 30,
"air_time": 75,
"message_count": 0,
"transmission_date": 0,
"LORA": "ON",
"update": False,
"port": 8000,
"averagesPerHour": 60,
"server": "10.15.40.51"
}

#============================Config=================
Loading

0 comments on commit 9fcaa24

Please sign in to comment.