111 lines
4.1 KiB
Python
111 lines
4.1 KiB
Python
from gs.device_info import DeviceInfo
|
|
import os
|
|
import ujson
|
|
import machine
|
|
import time
|
|
from gs.grow_system_api import GrowSystemApi
|
|
from gs.classes.sensors.ambilight_sensor import AmbilightSensor
|
|
from gs.classes.sensors.dht22 import TemperatureHumiditySensor
|
|
from gs.classes.sensors.moisture_sensor import MoistureSensor
|
|
import gs.config.initial_config as ic
|
|
import helper.token_helper as th
|
|
|
|
class GrowSystem:
|
|
|
|
version = "1.0.0.1"
|
|
|
|
initial_config = None
|
|
|
|
device_info = DeviceInfo()
|
|
|
|
sensors = []
|
|
|
|
def __init__(self):
|
|
print("Initialize Growsystem", self.version)
|
|
self.initial_config = ic
|
|
self.gsapi = GrowSystemApi()
|
|
|
|
def start(self):
|
|
from gs.device_info import DeviceInfo
|
|
di = DeviceInfo()
|
|
from gs.sensor_data_manager import SensorDataManager
|
|
self.sensor_data_manager = SensorDataManager(di.get_device_id())
|
|
|
|
self._initialize_sensors()
|
|
print("Start reading sensors ...")
|
|
|
|
read_secs = self.device_info.app_config().read_secs
|
|
print("Reading and sending every " + str(read_secs) + " seconds")
|
|
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 activate(self):
|
|
print("Start activation!")
|
|
|
|
self.initial_config = ic.config
|
|
device_config = self.gsapi.activate(self.initial_config)
|
|
print("Device Config:", device_config['data'], device_config['data']['token'])
|
|
th.write_token(device_config['data']['token'])
|
|
self.write_device_infos(device_config['data'])
|
|
|
|
def update_device_info(self):
|
|
print("Start Device Info Update!")
|
|
self.initial_config = ic.config
|
|
device_config = self.gsapi.update_device_info(self.initial_config)
|
|
print("Device Config:", device_config['data'])
|
|
self.write_device_infos(device_config['data'])
|
|
|
|
def write_device_infos(self, device_config):
|
|
print("Function received data:", device_config)
|
|
if True:
|
|
sensors = device_config['sensors']
|
|
sensor_configs = []
|
|
for sensor in sensors:
|
|
sensor_configs.append(sensor['config'])
|
|
print(sensor['config'])
|
|
device_configs = {
|
|
'name': device_config['name'],
|
|
'device_id': device_config['id'],
|
|
'token': device_config['token'],
|
|
'user_id': device_config['user_id'],
|
|
'sensors': sensor_configs
|
|
}
|
|
print("Update device_config.json with:", device_configs)
|
|
with open("/gs/config/device_config.json", "w") as f:
|
|
f.write(ujson.dumps(device_configs))
|
|
f.close()
|
|
|
|
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'])
|
|
|
|
|