demo.tracker-v2/code/tracker.py

979 lines
36 KiB
Python
Raw Normal View History

2022-03-24 16:01:12 +08:00
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sim
import utime
import modem
import checkNet
import dataCall
2022-03-23 14:21:45 +08:00
from misc import Power
from usr.led import LED
from usr.sensor import Sensor
from usr.battery import Battery
from usr.ota import OTAFileClear
2022-04-13 15:34:07 +08:00
from usr.history import History
2022-04-13 11:55:36 +08:00
from usr.logging import getLogger
from usr.mpower import LowEnergyManage
from usr.remote import RemotePublish, RemoteSubscribe
2022-04-13 15:34:07 +08:00
from usr.aliyunIot import AliYunIot, AliObjectModel
from usr.quecthing import QuecThing, QuecObjectModel
from usr.common import Singleton, LOWENERGYMAP
2022-04-14 15:20:22 +08:00
from usr.location import Location, GPSMatch, GPSParse, _loc_method
2022-04-14 17:58:03 +08:00
from usr.settings import PROJECT_NAME, PROJECT_VERSION, \
2022-04-14 15:20:22 +08:00
DEVICE_FIRMWARE_NAME, DEVICE_FIRMWARE_VERSION, settings, UserConfig, \
SYSConfig, Settings
2022-03-03 09:53:51 +08:00
try:
from misc import USB
except ImportError:
USB = None
try:
from misc import PowerKey
except ImportError:
PowerKey = None
log = getLogger(__name__)
2022-04-12 09:13:20 +08:00
sim.setSimDet(1, 0)
2022-04-14 17:58:03 +08:00
ALERTCODE = {
20000: "fault_alert",
30002: "low_power_alert",
30003: "over_speed_alert",
30004: "sim_abnormal_alert",
30005: "disassemble_alert",
40000: "drive_behavior_alert",
50001: "sos_alert",
}
2022-04-12 09:13:20 +08:00
def pwk_callback(status):
if status == 0:
log.info("PowerKey Release.")
elif status == 1:
log.info("PowerKey Press.")
else:
log.warn("Unknown PowerKey Status:", status)
def usb_callback(status):
if status == 0:
log.info("USB is disconnected.")
elif status == 1:
log.info("USB is connected.")
else:
log.warn("Unknown USB Stauts:", status)
def nw_callback(args):
net_check_res = DeviceCheck().net()
if args[1] != 1:
if net_check_res[0] == 1 and net_check_res[1] != 1:
log.warn("SIM abnormal!")
alert_code = 30004
alert_info = {"local_time": Collector().__get_local_time()}
alert_data = Collector().__get_alert_data(alert_code, alert_info)
Controller().device_data_report(event_data=alert_data, msg="sim_abnormal")
2022-04-12 09:13:20 +08:00
else:
if net_check_res == (3, 1):
pass
class Collector(Singleton):
2022-04-12 09:13:20 +08:00
def __init__(self):
self.__controller = None
self.__devicecheck = None
self.__battery = None
self.__sensor = None
self.__locator = None
2022-04-13 18:24:49 +08:00
self.__history = None
self.__gps_match = GPSMatch()
self.__gps_parse = GPSParse()
2022-04-12 09:13:20 +08:00
def __format_loc_method(self, data):
loc_method = "%04d" % int(bin(data)[2:])
gps = bool(int(loc_method[-1]))
cell = bool(int(loc_method[-2]))
wifi = bool(int(loc_method[-3]))
loc_method = {
"gps": gps,
"cell": cell,
"wifi": wifi,
}
return loc_method
def __device_speed_check(self):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
if not self.__locator:
raise TypeError("self.__locator is not registered")
current_settings = self.__controller.settings_get()
2022-04-12 09:13:20 +08:00
alert_data = {
"current_speed": 0.00
}
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["sw_over_speed_alert"] is True:
2022-04-13 15:34:07 +08:00
if self.__locator.gps:
gps_data = self.__locator.gps.read()[1]
vtg_data = self.__gps_match.GxVTG(gps_data)
speed = self.__gps_parse.GxVTG_speed(vtg_data)
2022-04-14 15:20:22 +08:00
if speed and float(speed) >= current_settings["user_cfg"]["over_speed_threshold"]:
2022-04-12 09:13:20 +08:00
alert_code = 30003
alert_info = {"local_time": self.__get_local_time()}
alert_data = self.__get_alert_data(alert_code, alert_info)
if speed:
alert_data["current_speed"] = float(speed)
return alert_data
def __get_local_time(self):
2022-03-28 20:29:01 +08:00
return str(utime.mktime(utime.localtime()) * 1000)
2022-03-25 18:42:11 +08:00
2022-04-12 09:13:20 +08:00
def __get_alert_data(self, alert_code, alert_info):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
current_settings = self.__controller.settings_get()
2022-03-23 14:21:45 +08:00
alert_data = {}
2022-04-12 09:13:20 +08:00
if ALERTCODE.get(alert_code):
2022-04-14 15:20:22 +08:00
alert_status = current_settings.get("user_cfg", {}).get("sw_" + ALERTCODE.get(alert_code))
if alert_status:
2022-04-12 09:13:20 +08:00
alert_data = {ALERTCODE.get(alert_code): alert_info}
else:
2022-04-12 09:13:20 +08:00
log.warn("%s switch is %s" % (ALERTCODE.get(alert_code), alert_status))
else:
2022-04-12 09:13:20 +08:00
log.error("altercode (%s) is not exists. alert info: %s" % (alert_code, alert_info))
2022-03-23 14:21:45 +08:00
return alert_data
def __init_low_energy_method(self, period):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
current_settings = self.__controller.settings_get()
device_model = modem.getDevModel()
support_methds = LOWENERGYMAP.get(device_model, [])
method = "NULL"
if support_methds:
2022-04-14 15:20:22 +08:00
if period >= current_settings["user_cfg"]["work_mode_timeline"]:
if "PSM" in support_methds:
method = "PSM"
elif "POWERDOWN" in support_methds:
method = "POWERDOWN"
elif "PM" in support_methds:
method = "PM"
else:
if "PM" in support_methds:
method = "PM"
log.debug("__init_low_energy_method: %s" % method)
return method
def __get_ali_loc_data(self, loc_method, loc_data):
res = {"GeoLocation": {}}
if loc_method == 0x1:
gga_data = self.__gps_match.GxGGA(loc_data)
data = {}
if gga_data:
Latitude = self.__gps_parse.GxGGA_latitude(gga_data)
if Latitude:
2022-04-13 11:55:36 +08:00
data["Latitude"] = float("%.2f" % float(Latitude))
Longtitude = self.__gps_parse.GxGGA_longtitude(gga_data)
if Longtitude:
2022-04-13 11:55:36 +08:00
data["Longtitude"] = float("%.2f" % float(Longtitude))
Altitude = self.__gps_parse.GxGGA_altitude(gga_data)
if Altitude:
2022-04-13 11:55:36 +08:00
data["Altitude"] = float("%.2f" % float(Altitude))
if data:
data["CoordinateSystem"] = 1
res = {"GeoLocation": data}
elif loc_method in (0x2, 0x4):
if loc_data:
res["GeoLocation"] = {
"Longtitude": round(loc_data[0], 2),
"Latitude": round(loc_data[1], 2),
# "Altitude": 0.0,
"CoordinateSystem": 1
}
return res
def __get_quec_loc_data(self, loc_method, loc_data):
if loc_method == 0x1:
res = {"gps": []}
r = self.__gps_match.GxRMC(loc_data)
if r:
res["gps"].append(r)
r = self.__gps_match.GxGGA(loc_data)
if r:
res["gps"].append(r)
r = self.__gps_match.GxVTG(loc_data)
if r:
res["gps"].append(r)
return res
elif loc_method == 0x2:
return {"non_gps": ["LBS"]}
elif loc_method == 0x4:
return {"non_gps": []}
2022-04-12 09:13:20 +08:00
2022-04-14 15:20:22 +08:00
def add_module(self, module):
if isinstance(module, Controller):
self.__controller = module
2022-04-12 09:13:20 +08:00
return True
2022-04-14 15:20:22 +08:00
elif isinstance(module, DeviceCheck):
self.__devicecheck = module
2022-04-12 09:13:20 +08:00
return True
2022-04-14 15:20:22 +08:00
elif isinstance(module, Battery):
self.__battery = module
2022-04-12 09:13:20 +08:00
return True
2022-04-14 15:20:22 +08:00
elif isinstance(module, Sensor):
self.__sensor = module
2022-04-12 09:13:20 +08:00
return True
2022-04-14 15:20:22 +08:00
elif isinstance(module, Location):
self.__locator = module
2022-04-12 09:13:20 +08:00
return True
2022-04-14 15:20:22 +08:00
elif isinstance(module, History):
self.__history = module
return True
2022-04-13 18:24:49 +08:00
return False
2022-04-13 11:55:36 +08:00
def get_loc_data(self, loc_method, loc_data):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-13 11:55:36 +08:00
current_settings = self.__controller.settings_get()
2022-04-14 15:20:22 +08:00
if current_settings["sys"]["cloud"] & SYSConfig._cloud.quecIot:
2022-04-13 11:55:36 +08:00
return self.__get_quec_loc_data(loc_method, loc_data)
2022-04-14 15:20:22 +08:00
elif current_settings["sys"]["cloud"] & SYSConfig._cloud.AliYun:
2022-04-13 11:55:36 +08:00
return self.__get_ali_loc_data(loc_method, loc_data)
return {}
2022-04-12 09:13:20 +08:00
def device_status_get(self):
2022-04-14 17:58:03 +08:00
if not self.__devicecheck:
raise TypeError("self.__devicecheck is not registered.")
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
device_status_data = {}
device_module_status = {}
alert_code = 20000
net_status = self.__devicecheck.net()
location_status = self.__devicecheck.location()
temp_status = self.__devicecheck.temp()
light_status = self.__devicecheck.light()
triaxial_status = self.__devicecheck.triaxial()
mike_status = self.__devicecheck.mike()
device_module_status["net"] = 1 if net_status == (3, 1) else 0
device_module_status["location"] = 1 if location_status else 0
# TODO: Check Sensor.
if temp_status is not None:
device_module_status["temp_sensor"] = 1 if temp_status else 0
if light_status is not None:
device_module_status["light_sensor"] = 1 if temp_status else 0
if triaxial_status is not None:
device_module_status["move_sensor"] = 1 if temp_status else 0
if mike_status is not None:
device_module_status["mike"] = 1 if temp_status else 0
device_status = True
# TODO: Led Show
if net_status == (3, 1) and location_status is True and \
(temp_status is True or temp_status is None) and \
(light_status is True or light_status is None) and \
(triaxial_status is True or triaxial_status is None) and \
(mike_status is True or mike_status is None):
2022-04-14 17:58:03 +08:00
# self.__controller.running_led_show(0.5)
device_status = True
2022-04-12 09:13:20 +08:00
else:
2022-04-14 17:58:03 +08:00
# self.__controller.running_led_show(2)
2022-04-12 09:13:20 +08:00
device_status = False
if device_status is False:
device_status_data = self.__get_alert_data(alert_code, {"local_time": self.__get_local_time()})
device_status_data.update({"device_module_status": device_module_status})
return device_status_data
def device_status_check(self):
device_status_check_res = self.device_status_get()
self.device_data_report(event_data=device_status_check_res)
def device_data_get(self, power_switch=True):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
if not self.__battery:
raise TypeError("self.__battery is not registered.")
if not self.__locator:
raise TypeError("self.__locator is not registered.")
current_settings = self.__controller.settings_get()
2022-04-12 09:13:20 +08:00
device_data = {
2022-04-12 09:13:20 +08:00
"power_switch": power_switch,
"local_time": self.__get_local_time(),
}
2022-03-23 14:21:45 +08:00
# Get cloud location data
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"].get("loc_method"):
cfg_loc_method = current_settings["user_cfg"].get("loc_method")
elif current_settings["sys"]["base_cfg"]["LocConfig"]:
cfg_loc_method = current_settings["LocConfig"]["loc_method"]
else:
cfg_loc_method = 7
2022-04-14 17:58:03 +08:00
loc_info = self.__locator.read(cfg_loc_method)
2022-03-23 14:21:45 +08:00
if loc_info:
2022-04-14 15:20:22 +08:00
loc_method_dict = {v: k for k, v in _loc_method.__dict__.items()}
2022-04-13 15:34:07 +08:00
loc_data = None
for loc_method in loc_method_dict.keys():
if loc_info.get(loc_method):
log.debug("Location Data loc_method: %s" % loc_method_dict[loc_method])
loc_data = loc_info[loc_method]
if loc_method == _loc_method.gps:
gga_satellite = self.__gps_parse.GxGGA_satellite_num(self.__gps_match.GxGGA(loc_data))
log.debug("GxGGA Satellite Num %s" % gga_satellite)
gsv_satellite = self.__gps_parse.GxGSV_satellite_num(self.__gps_match.GxGSV(loc_data))
log.debug("GxGSV Satellite Num %s" % gsv_satellite)
2022-04-13 15:34:07 +08:00
break
if loc_data:
report_loc_data = None
2022-04-14 15:20:22 +08:00
if current_settings["sys"]["cloud"] & SYSConfig._cloud.quecIot:
2022-04-13 15:34:07 +08:00
report_loc_data = self.__get_quec_loc_data(loc_method, loc_data)
2022-04-14 15:20:22 +08:00
elif current_settings["sys"]["cloud"] & SYSConfig._cloud.AliYun:
2022-04-13 15:34:07 +08:00
report_loc_data = self.__get_ali_loc_data(loc_method, loc_data)
if report_loc_data:
device_data.update(report_loc_data)
# Get gps speed
2022-04-12 09:13:20 +08:00
over_speed_check_res = self.__device_speed_check()
log.debug("over_speed_check_res: %s" % str(over_speed_check_res))
device_data.update(over_speed_check_res)
# Get battery energy
# TODO: Get tempreture from sensor.
self.__battery.set_temp(20)
2022-04-13 15:34:07 +08:00
energy = self.__battery.get_energy()
device_data.update({
2022-04-12 09:13:20 +08:00
"energy": energy,
2022-04-13 15:34:07 +08:00
"voltage": self.__battery.get_voltage(),
})
2022-04-14 15:20:22 +08:00
if energy <= current_settings["user_cfg"]["low_power_alert_threshold"]:
2022-04-12 09:13:20 +08:00
alert_data = self.__get_alert_data(30002, {"local_time": self.__get_local_time()})
2022-03-23 19:42:23 +08:00
device_data.update(alert_data)
# Get ota status & drive behiver code
2022-03-23 14:21:45 +08:00
device_data.update({
2022-04-14 15:20:22 +08:00
"ota_status": current_settings["user_cfg"]["ota_status"],
"drive_behavior_code": current_settings["user_cfg"]["drive_behavior_code"],
2022-03-23 14:21:45 +08:00
})
# Get app settings info
2022-04-14 15:20:22 +08:00
device_data.update(current_settings["user_cfg"])
# Format loc method
2022-04-14 15:20:22 +08:00
device_data.update({"loc_method": self.__format_loc_method(current_settings["user_cfg"]["loc_method"])})
2022-03-25 11:56:14 +08:00
# TODO: Add other machine info.
2022-03-23 14:21:45 +08:00
return device_data
2022-04-12 09:13:20 +08:00
def device_data_report(self, power_switch=True, event_data={}, msg=""):
# TODO: msg to mark post data source
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
device_data = self.device_data_get(power_switch)
2022-03-23 14:21:45 +08:00
if event_data:
device_data.update(event_data)
2022-04-12 09:13:20 +08:00
post_res = self.__controller.remote_post_data(device_data)
2022-03-23 14:21:45 +08:00
# OTA status rst
2022-04-14 17:58:03 +08:00
current_settings = self.__controller.settings_get() if self.__controller else {}
2022-04-14 15:20:22 +08:00
ota_status_info = current_settings["user_cfg"]["ota_status"]
2022-04-12 09:13:20 +08:00
if ota_status_info["upgrade_status"] in (3, 4):
self.ota_status_reset()
2022-04-12 09:13:20 +08:00
return post_res
2022-04-12 09:13:20 +08:00
def ota_status_reset(self):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-14 15:20:22 +08:00
current_settings = self.__controller.settings_get()
ota_status_info = current_settings["user_cfg"]["ota_status"]
2022-04-05 14:56:50 +08:00
ota_info = {}
2022-04-12 09:13:20 +08:00
ota_info["sys_target_version"] = "--"
ota_info["app_target_version"] = "--"
ota_info["upgrade_module"] = 0
ota_info["upgrade_status"] = 0
2022-04-05 14:56:50 +08:00
ota_status_info.update(ota_info)
2022-04-12 09:13:20 +08:00
self.__controller.settings_set("ota_status", ota_status_info)
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["user_ota_action"] != -1:
2022-04-12 09:13:20 +08:00
self.__controller.settings_set("user_ota_action", -1)
2022-04-05 14:56:50 +08:00
2022-04-13 18:24:49 +08:00
def report_history(self):
2022-04-14 17:58:03 +08:00
if not self.__history:
raise TypeError("self.__history is not registered.")
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-13 18:24:49 +08:00
2022-04-14 17:58:03 +08:00
res = True
2022-04-13 18:24:49 +08:00
hist = self.__history.read()
if hist["data"]:
pt_count = 0
for i, data in enumerate(hist["data"]):
pt_count += 1
if not self.__controller.remote_post_data(data):
res = False
break
hist["data"] = hist["data"][pt_count:]
if hist["data"]:
# Flush data in hist-dictionary to tracker_data.hist file.
self.__history.write(hist["data"])
return res
# Do cloud event downlink option by controller
2022-04-12 09:13:20 +08:00
def event_option(self, *args, **kwargs):
# TODO: Data Type Passthrough (Not Support Now).
return False
2022-04-06 19:52:49 +08:00
2022-04-12 09:13:20 +08:00
def event_done(self, *args, **kwargs):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
try:
setting_flag = 0
2022-04-12 09:13:20 +08:00
for arg in args:
2022-04-14 15:20:22 +08:00
if hasattr(UserConfig, arg[0]):
2022-04-12 09:13:20 +08:00
set_res = self.__controller.settings_set(arg[0], arg[1])
if set_res and setting_flag == 0:
setting_flag = 1
if hasattr(self, arg[0]):
getattr(self, arg[0])(arg[1])
2022-04-12 09:13:20 +08:00
if setting_flag:
self.__controller.settings_save()
return True
except:
return False
def event_query(self, *args, **kwargs):
return self.device_data_report()
def event_ota_plain(self, *args, **kwargs):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
current_settings = settings.get()
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["sw_ota"]:
if current_settings["user_cfg"]["sw_ota_auto_upgrade"] or current_settings["user_cfg"]["user_ota_action"] != -1:
if current_settings["user_cfg"]["sw_ota_auto_upgrade"]:
2022-04-12 09:13:20 +08:00
ota_action_val = 1
else:
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["user_ota_action"] != -1:
ota_action_val = current_settings["user_cfg"]["user_ota_action"]
2022-04-12 09:13:20 +08:00
else:
return
2022-04-14 15:20:22 +08:00
if current_settings["sys"]["cloud"] == SYSConfig._cloud.quecIot or \
current_settings["sys"]["cloud"] == SYSConfig._cloud.AliYun:
2022-04-12 09:13:20 +08:00
log.debug("ota_plain args: %s, kwargs: %s" % (str(args), str(kwargs)))
self.__controller.remote_ota_action(action=ota_action_val, module=kwargs.get("module"))
else:
log.error("Current Cloud (0x%X) Not Supported!" % current_settings["sys"]["cloud"])
def event_ota_file_download(self, *args, **kwargs):
# OAT MQTT File Download Is Not Supported Yet.
return False
def power_switch(self, flag=None):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
self.event_query(power_switch=flag)
if flag is False:
self.__controller.power_down()
def user_ota_action(self, action):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
current_settings = self.__controller.settings_get()
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["sw_ota"] and current_settings["user_cfg"]["sw_ota_auto_upgrade"] is False:
ota_status_info = current_settings["user_cfg"]["ota_status"]
if ota_status_info["upgrade_status"] == 1 and current_settings["user_cfg"]["user_ota_action"] == -1:
2022-04-12 09:13:20 +08:00
self.__controller.settings_set("user_ota_action", action)
self.__controller.settings_save()
self.__controller.remote_ota_check()
def ota_status(self, upgrade_info=None):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
current_settings = self.__controller.settings_get()
2022-04-14 15:20:22 +08:00
if upgrade_info and current_settings["user_cfg"]["sw_ota"]:
ota_status_info = current_settings["user_cfg"]["ota_status"]
2022-04-12 09:13:20 +08:00
if ota_status_info["sys_target_version"] == "--" and ota_status_info["app_target_version"] == "--":
ota_info = {}
if upgrade_info[0] == DEVICE_FIRMWARE_NAME:
2022-04-12 09:13:20 +08:00
ota_info["upgrade_module"] = 1
ota_info["sys_target_version"] = upgrade_info[2]
elif upgrade_info[0] == PROJECT_NAME:
ota_info["upgrade_module"] = 2
ota_info["app_target_version"] = upgrade_info[2]
ota_info["upgrade_status"] = upgrade_info[1]
ota_status_info.update(ota_info)
self.__controller.settings_set("ota_status", ota_status_info)
self.__controller.settings_save()
def power_restart(self, flag):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-12 09:13:20 +08:00
self.event_query(power_switch=False)
self.__controller.power_restart()
def work_cycle_period(self, period):
# Reset work_cycle_period & Reset RTC
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
self.__controller.low_energy_stop()
self.__controller.low_energy_set_period(period)
method = self.__init_low_energy_method(period)
self.__controller.low_energy_set_method(method)
self.__controller.low_energy_init()
self.__controller.low_energy_start()
2022-04-12 09:13:20 +08:00
def cloud_init_params(self, params):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-14 15:20:22 +08:00
self.__controller.settings_set("cloud", params)
self.__controller.settings_save()
def low_engery_option(self, low_energy_method):
2022-04-14 17:58:03 +08:00
if not self.__controller:
raise TypeError("self.__controller is not registered.")
2022-04-13 18:24:49 +08:00
self.report_history()
current_settings = self.__controller.settings_get()
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"]["work_mode"] == UserConfig._work_mode.intelligent:
speed_info = self.__device_speed_check()
if speed_info.get("current_speed") > 0:
self.device_data_report()
else:
self.device_data_report()
self.__controller.low_energy_start()
if low_energy_method == "PSM":
# TODO: PSM option.
pass
elif low_energy_method == "POWERDOWN":
self.__controller.power_down()
def update(self, observable, *args, **kwargs):
if isinstance(observable, LowEnergyManage):
2022-04-13 18:24:49 +08:00
log.debug("Low Energy RTC Method: %s" % args[1])
self.low_engery_option(args[1])
2022-04-12 09:13:20 +08:00
class DeviceCheck(object):
2022-04-13 15:34:07 +08:00
def __init__(self):
self.__locator = None
self.__sensor = None
2022-04-13 15:34:07 +08:00
def add_module(self, module):
if isinstance(module, Location):
self.__locator = module
return True
elif isinstance(module, Sensor):
self.__sensor = module
2022-04-13 15:34:07 +08:00
return True
return False
2022-04-12 09:13:20 +08:00
def net(self):
current_settings = settings.get()
2022-04-13 15:34:07 +08:00
checknet = checkNet.CheckNetwork(PROJECT_NAME, PROJECT_VERSION)
2022-04-12 09:13:20 +08:00
timeout = current_settings.get("sys", {}).get("checknet_timeout", 60)
check_res = checknet.wait_network_connected(timeout)
2022-04-12 09:13:20 +08:00
log.debug("DeviceCheck.net res: %s" % str(check_res))
return check_res
2022-04-13 15:34:07 +08:00
def location(self):
# return True if OK
2022-04-14 17:58:03 +08:00
if not self.__locator:
raise TypeError("self.__locator is not registered")
2022-04-13 18:24:49 +08:00
current_settings = settings.get()
retry = 0
gps_data = None
sleep_time = 1
2022-03-28 20:29:01 +08:00
while retry < 5:
2022-04-14 15:20:22 +08:00
if current_settings["user_cfg"].get("loc_method"):
loc_method = current_settings["user_cfg"].get("loc_method")
elif current_settings["sys"]["base_cfg"]["LocConfig"]:
loc_method = current_settings["LocConfig"].get("loc_method")
else:
loc_method = 7
2022-04-14 17:58:03 +08:00
2022-04-14 15:20:22 +08:00
gps_data = self.__locator.read(loc_method)
if gps_data:
break
else:
retry += 1
utime.sleep(sleep_time)
sleep_time *= 2
2022-03-28 20:29:01 +08:00
if gps_data:
return True
return False
2022-04-12 09:13:20 +08:00
def temp(self):
# return True if OK
2022-04-12 09:13:20 +08:00
return None
def light(self):
# return True if OK
return None
def triaxial(self):
# return True if OK
return None
def mike(self):
# return True if OK
return None
class Controller(Singleton):
def __init__(self):
2022-04-13 15:34:07 +08:00
self.__remote_pub = None
2022-04-12 09:13:20 +08:00
self.__settings = None
self.__low_energy = None
2022-04-12 09:13:20 +08:00
self.__energy_led = None
self.__running_led = None
self.__power_key = None
self.__usb = None
self.__data_call = None
self.__ota_file_clear = None
2022-04-12 09:13:20 +08:00
2022-04-14 15:20:22 +08:00
def add_module(self, module, led_type=None, callback=None):
if isinstance(module, RemotePublish):
self.__remote_pub = module
return True
elif isinstance(module, Settings):
self.__settings = module
return True
elif isinstance(module, LowEnergyManage):
self.__low_energy = module
2022-04-14 15:20:22 +08:00
return True
elif isinstance(module, OTAFileClear):
self.__ota_file_clear = module
return True
elif isinstance(module, LED):
if led_type == "energy":
self.__energy_led = module
return True
elif led_type == "running":
self.running_led = module
return True
elif isinstance(module, PowerKey):
self.__power_key = module
if callback:
self.__power_key.powerKeyEventRegister(callback)
return True
elif isinstance(module, USB):
self.__usb = module
if callback:
self.__usb.setCallback(callback)
return True
elif module is dataCall:
self.__data_call = module
if callback:
self.__data_call.setCallback(callback)
return True
return False
2022-04-13 15:34:07 +08:00
def set_remote_pub(self, remote_pub):
if isinstance(remote_pub, RemotePublish):
self.__remote_pub = remote_pub
2022-04-12 09:13:20 +08:00
return True
return False
def set_settings(self, settings):
if isinstance(settings, Settings):
self.__settings = settings
return True
return False
def set_low_energy(self, low_energy_manage):
if isinstance(low_energy_manage, LowEnergyManage):
self.__low_energy = low_energy_manage
2022-04-12 09:13:20 +08:00
return True
return False
def set_energy_led(self, energy_led):
if isinstance(energy_led, LED):
self.__energy_led = energy_led
return True
return False
def set_running_led(self, running_led):
if isinstance(running_led, LED):
self.__running_led = running_led
return True
return False
def set_power_key(self, power_key, power_key_cb):
if isinstance(power_key, PowerKey):
self.__power_key = power_key
2022-04-13 15:34:07 +08:00
if power_key_cb:
self.__power_key.powerKeyEventRegister(power_key_cb)
2022-04-12 09:13:20 +08:00
return True
return False
def set_usb(self, usb, usb_cb):
if isinstance(usb, USB):
self.__usb = usb
if usb_cb:
self.__usb.setCallback(usb_cb)
return True
return False
def set_data_call(self, data_call, data_call_cb):
2022-04-13 15:34:07 +08:00
if data_call is dataCall:
2022-04-12 09:13:20 +08:00
self.__data_call = data_call
if data_call_cb:
self.__data_call.setCallback(data_call_cb)
return True
return False
def set_ota_file_clear(self, ota_file_clear):
if isinstance(ota_file_clear, OTAFileClear):
self.__ota_file_clear = ota_file_clear
return True
return False
2022-04-12 09:13:20 +08:00
def settings_get(self):
2022-04-14 17:58:03 +08:00
if not self.__settings:
raise TypeError("self.__settings is not registered.")
2022-04-12 09:13:20 +08:00
return self.__settings.get()
def settings_set(self, key, value):
2022-04-14 17:58:03 +08:00
if not self.__settings:
raise TypeError("self.__settings is not registered.")
2022-04-12 09:13:20 +08:00
if key == "loc_method":
v = "0b"
v += str(int(value.get(3, 0)))
v += str(int(value.get(2, 0)))
v += str(int(value.get(1, 0)))
value = int(v, 2)
set_res = self.__settings.set(key, value)
log.debug("__settings_set key: %s, val: %s, set_res: %s" % (key, value, set_res))
return set_res
def settings_save(self):
2022-04-14 17:58:03 +08:00
if not self.__settings:
raise TypeError("self.__settings is not registered.")
2022-04-12 09:13:20 +08:00
return self.__settings.save()
def power_restart(self):
Power.powerRestart()
def power_down(self):
Power.powerDown()
def remote_post_data(self, data):
2022-04-14 17:58:03 +08:00
if not self.__remote_pub:
raise TypeError("self.__remote_pub is not registered.")
2022-04-13 15:34:07 +08:00
return self.__remote_pub.post_data(data)
2022-04-12 09:13:20 +08:00
def remote_ota_check(self):
2022-04-14 17:58:03 +08:00
if not self.__remote_pub:
raise TypeError("self.__remote_pub is not registered.")
2022-04-13 15:34:07 +08:00
return self.__remote_pub.cloud_ota_check()
2022-04-12 09:13:20 +08:00
def remote_ota_action(self, action, module):
2022-04-14 17:58:03 +08:00
if not self.__remote_pub:
raise TypeError("self.__remote_pub is not registered.")
2022-04-13 15:34:07 +08:00
return self.__remote_pub.cloud_ota_action(action, module)
2022-04-12 09:13:20 +08:00
def low_energy_set_period(self, period):
if not self.__low_energy:
raise TypeError("self.__low_energy is not registered.")
return self.__low_energy.set_period(period)
def low_energy_set_method(self, method):
if not self.__low_energy:
raise TypeError("self.__low_energy is not registered.")
return self.__low_energy.set_low_energy_method(method)
2022-04-12 09:13:20 +08:00
def low_energy_init(self):
if not self.__low_energy:
raise TypeError("self.__low_energy is not registered.")
return self.__low_energy.low_energy_init()
def low_energy_start(self):
if not self.__low_energy:
raise TypeError("self.__low_energy is not registered.")
return self.__low_energy.start()
def low_energy_stop(self):
if not self.__low_energy:
raise TypeError("self.__low_energy is not registered.")
return self.__low_energy.stop()
def ota_file_clean(self):
2022-04-14 17:58:03 +08:00
if not self.__ota_file_clear:
raise TypeError("self.__ota_file_clear is not registered.")
self.__ota_file_clear.file_clear()
2022-04-13 15:34:07 +08:00
def running_led_show(self, period):
2022-04-14 17:58:03 +08:00
if not self.__running_led:
raise TypeError("self.__running_led is not registered.")
self.__running_led.set_period(period)
return self.__running_led.led_timer_start()
2022-04-13 15:34:07 +08:00
def energy_led_show(self, period):
2022-04-14 17:58:03 +08:00
if not self.energy_led_show:
raise TypeError("self.energy_led_show is not registered.")
self.__energy_led.set_period(period)
return self.__energy_led.led_timer_start()
2022-04-13 15:34:07 +08:00
2022-04-14 15:20:22 +08:00
def tracker():
2022-04-13 15:34:07 +08:00
current_settings = settings.get()
# All device modules initialization
2022-04-17 16:49:25 +08:00
# energy_led = LED()
# running_led = LED()
2022-04-13 15:34:07 +08:00
sensor = Sensor()
2022-04-17 16:49:25 +08:00
history = History()
battery = Battery()
2022-04-17 16:49:25 +08:00
data_call = dataCall
low_energy = LowEnergyManage()
2022-04-17 16:49:25 +08:00
ota_file_clear = OTAFileClear()
usb = USB() if USB is not None else None
power_key = PowerKey() if PowerKey is not None else None
locator = Location(current_settings["LocConfig"]["gps_mode"], current_settings["LocConfig"]["locator_init_params"])
# DeviceCheck initialization
devicecheck = DeviceCheck()
devicecheck.add_module(locator)
devicecheck.add_module(sensor)
2022-04-17 16:49:25 +08:00
# Cloud initialization
2022-04-14 15:20:22 +08:00
cloud_init_params = current_settings["cloud"]
if current_settings["sys"]["cloud"] & SYSConfig._cloud.quecIot:
2022-04-13 15:34:07 +08:00
cloud = QuecThing(
cloud_init_params["PK"],
cloud_init_params["PS"],
cloud_init_params["DK"],
cloud_init_params["DS"],
cloud_init_params["SERVER"],
mcu_name=PROJECT_NAME,
mcu_version=PROJECT_VERSION
)
cloud_om = QuecObjectModel()
cloud.set_object_model(cloud_om)
2022-04-14 15:20:22 +08:00
elif current_settings["sys"]["cloud"] & SYSConfig._cloud.AliYun:
client_id = cloud_init_params["client_id"] if cloud_init_params.get("client_id") else modem.getDevImei()
2022-04-13 15:34:07 +08:00
cloud = AliYunIot(
cloud_init_params["PK"],
cloud_init_params["PS"],
cloud_init_params["DK"],
cloud_init_params["DS"],
cloud_init_params["SERVER"],
2022-04-14 15:20:22 +08:00
client_id,
2022-04-14 15:24:06 +08:00
burning_method=cloud_init_params["burning_method"],
2022-04-13 15:34:07 +08:00
mcu_name=PROJECT_NAME,
mcu_version=PROJECT_VERSION,
firmware_name=DEVICE_FIRMWARE_NAME,
firmware_version=DEVICE_FIRMWARE_VERSION
)
cloud_om = AliObjectModel()
cloud.set_object_model(cloud_om)
2022-04-13 15:34:07 +08:00
else:
raise TypeError("Settings cloud[%s] is not support." % current_settings["sys"]["cloud"])
# RemotePublish initialization
2022-04-17 16:49:25 +08:00
remote_pub = RemotePublish()
2022-04-13 15:34:07 +08:00
remote_pub.addObserver(history)
remote_pub.add_cloud(cloud)
2022-04-14 15:20:22 +08:00
# Controller initialization
controller = Controller()
2022-04-14 15:20:22 +08:00
controller.add_module(remote_pub)
controller.add_module(settings)
controller.add_module(low_energy)
2022-04-14 15:20:22 +08:00
controller.add_module(ota_file_clear)
# controller.add_module(energy_led, led_type="energy")
# controller.add_module(running_led, led_type="running")
controller.add_module(power_key, callback=pwk_callback)
controller.add_module(usb, callback=usb_callback)
controller.add_module(data_call)
# Collector initialization
collector = Collector()
collector.add_module(controller)
collector.add_module(devicecheck)
collector.add_module(battery)
collector.add_module(sensor)
collector.add_module(locator)
collector.add_module(history)
# LowEnergyManage initialization
2022-04-14 15:20:22 +08:00
work_cycle_period = current_settings["user_cfg"]["work_cycle_period"]
low_energy.set_period(work_cycle_period)
low_energy.set_low_energy_method(collector.__init_low_energy_method(work_cycle_period))
low_energy.addObserver(collector)
2022-04-13 15:34:07 +08:00
# RemoteSubscribe initialization
2022-04-17 16:49:25 +08:00
remote_sub = RemoteSubscribe()
remote_sub.add_executor(collector)
2022-04-13 15:34:07 +08:00
cloud.addObserver(remote_sub)
# Business start
# Cloud start
cloud.init()
# OTA upgrade file clean
2022-04-13 15:34:07 +08:00
controller.ota_file_clean()
# Device modules status check
collector.device_status_check()
# Low energy init
controller.low_energy_init()
# Low energy start
controller.low_energy_start()