63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
from gs.setup import Setup
|
|
import os
|
|
import ujson
|
|
|
|
|
|
class GrowSystem:
|
|
|
|
version = "1.0"
|
|
|
|
initial_config = None
|
|
|
|
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.")
|
|
elif self._is_initial_config_existing():
|
|
print("Initial config only existing (no base config). Start activation ...")
|
|
self._activate()
|
|
|
|
def _setup(self):
|
|
setup = Setup()
|
|
setup.setup_pico()
|
|
|
|
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'])
|
|
sensors = device_config['data']['sensors']
|
|
sensor_configs = []
|
|
for sensor in sensors:
|
|
sensor_configs.append(sensor['config'])
|
|
print(sensor['config'])
|
|
with open("/gs/config/device_config.py", "w") as f:
|
|
f.write("sensors = " + ujson.dumps(sensor_configs))
|
|
|
|
def _is_config_existing(self):
|
|
return self._is_file_existing('/gs/config/config.py')
|
|
|
|
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
|
|
|