57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from gs.http_client import HTTPClient
|
|
from gs.device_info import DeviceInfo
|
|
from gs.wlan_client import WlanClient
|
|
import json
|
|
import gs.config.initial_config as ic
|
|
|
|
|
|
class GrowSystemApi:
|
|
|
|
http_client = HTTPClient()
|
|
|
|
device_info = DeviceInfo()
|
|
|
|
base_url = ''
|
|
|
|
def __init__(self):
|
|
self.base_url = self.device_info.server_url
|
|
self.connect_wifi(ic.config['ssid'], ic.config['password'])
|
|
|
|
# config = self.device_info.config()
|
|
# print("Config:", config)
|
|
# print("Test", config['test'])
|
|
|
|
def activate(self, config):
|
|
data = self._get_device_data()
|
|
data.update({
|
|
'user_id': 1,
|
|
'pin': config['pin']
|
|
})
|
|
print("activate ...", data)
|
|
response = self.http_client.post(self.base_url + "/api/device/activate", data)
|
|
return self._get_json_encoded(response.text)
|
|
|
|
def say_hello(self):
|
|
response = self.http_client.post(self.base_url + "/api/device", self._get_device_data())
|
|
print(response.text)
|
|
jsonResult = json.loads(response.text)
|
|
print(jsonResult)
|
|
return jsonResult;
|
|
|
|
def send_measurements(self, device_id, data):
|
|
url = self.base_url + "/api/device/" + str(device_id) + "/sensor-log"
|
|
response = self.http_client.post(url, data)
|
|
return json.loads(response.text)
|
|
|
|
def _get_device_data(self):
|
|
return self.device_info.get_all_device_infos()
|
|
|
|
def _get_json_encoded(self, text):
|
|
return json.loads(text)
|
|
|
|
def connect_wifi(self, ssid, password):
|
|
print("Connect WLAN")
|
|
self.wlan_client = WlanClient(ssid, password)
|
|
self.wlan_client.connect()
|
|
|