130 lines
4.5 KiB
Python
130 lines
4.5 KiB
Python
from gs.setup import Setup
|
|
from gs.device_info import DeviceInfo
|
|
from gs.sensor_data_manager import SensorDataManager
|
|
import os
|
|
import ujson
|
|
import machine
|
|
import time
|
|
from gs.classes.sensors.ambilight_sensor import AmbilightSensor
|
|
from gs.classes.sensors.dht22 import TemperatureHumiditySensor
|
|
from gs.classes.sensors.moisture_sensor import MoistureSensor
|
|
|
|
|
|
class GrowSystem:
|
|
|
|
version = "1.0"
|
|
|
|
initial_config = None
|
|
|
|
device_info = DeviceInfo()
|
|
|
|
sensors = []
|
|
|
|
def __init__(self):
|
|
print("Initialize Growsystem", self.version)
|
|
|
|
if not self._is_initial_config_existing():
|
|
print("No config existing. Start setup ...")
|
|
self._setup()
|
|
return
|
|
|
|
from gs.grow_system_api import GrowSystemApi as GSA
|
|
self.gsapi = GSA()
|
|
|
|
if self._is_config_existing():
|
|
print("Skip Setup. Config existing.")
|
|
self._initialize_sensors()
|
|
elif self._is_initial_config_existing():
|
|
print("Initial config only existing (no base config). Start activation ...")
|
|
self._activate()
|
|
|
|
def start(self):
|
|
self.sensor_data_manager = SensorDataManager(self.device_info.get_device_id())
|
|
|
|
print("Start reading sensors ...")
|
|
|
|
read_secs = self.device_info.app_config().read_secs
|
|
|
|
while True:
|
|
# Reset data
|
|
self.most_recent_values = []
|
|
|
|
for sensor in self.sensors:
|
|
print("Read sensor of type " + sensor.type + " at pin " + str(sensor.sensor_pin_int))
|
|
sensor.read()
|
|
for measurement in sensor.most_recent_values:
|
|
print(f"Got {measurement['value']} {measurement['unit']} ({measurement['type']})")
|
|
self.most_recent_values = self.most_recent_values + sensor.most_recent_values
|
|
|
|
self.sensor_data_manager.handleData(self.most_recent_values)
|
|
|
|
time.sleep(read_secs)
|
|
|
|
def _setup(self):
|
|
setup = Setup()
|
|
setup.setup_pico()
|
|
machine.reset
|
|
|
|
def _activate(self):
|
|
print("Start activation!")
|
|
import gs.config.initial_config as ic
|
|
self.initial_config = ic.config
|
|
device_config = self.gsapi.activate(self.initial_config)
|
|
#print("Device Config:", device_config['data'])
|
|
|
|
if True:
|
|
sensors = device_config['data']['sensors']
|
|
sensor_configs = []
|
|
for sensor in sensors:
|
|
sensor_configs.append(sensor['config'])
|
|
print(sensor['config'])
|
|
device_configs = {
|
|
'name': device_config['data']['name'],
|
|
'device_id': device_config['data']['id'],
|
|
'token': device_config['data']['token'],
|
|
'user_id': device_config['data']['user_id'],
|
|
'sensors': sensor_configs
|
|
}
|
|
with open("/gs/config/device_config.json", "w") as f:
|
|
f.write(ujson.dumps(device_configs))
|
|
if self._is_config_existing():
|
|
machine.reset
|
|
|
|
def _is_config_existing(self):
|
|
return self._is_file_existing('/gs/config/device_config.json')
|
|
|
|
def _is_initial_config_existing(self):
|
|
return self._is_file_existing('/gs/config/initial_config.py')
|
|
|
|
def _is_file_existing(self, filepath):
|
|
try:
|
|
f = open(filepath, "r")
|
|
f.close()
|
|
# continue with the file.
|
|
return True
|
|
except OSError: # open failed
|
|
# handle the file open cas
|
|
return False
|
|
|
|
def _initialize_sensors(self):
|
|
# Init sensors
|
|
sensors = self.device_info.config()['sensors']
|
|
for sensor in sensors:
|
|
print("--------------------------------------")
|
|
print("Initialize sensor:")
|
|
print(sensor)
|
|
sensor_type = sensor['type']
|
|
if sensor_type == 'moisture':
|
|
print("Found sensor of type moisture")
|
|
self.sensors.append(MoistureSensor(sensor))
|
|
elif sensor_type == 'dht22':
|
|
print("Found sensor of type DHT22")
|
|
self.sensors.append(TemperatureHumiditySensor(sensor))
|
|
elif sensor_type == 'ambilight':
|
|
print("Found sensor of type GY302/BH1750")
|
|
self.sensors.append(AmbilightSensor(sensor))
|
|
else:
|
|
print("No sensor type configured for: " + sensor['type'])
|
|
|
|
|