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.
|
|
|
|
|
2022-04-05 12:09:23 +08:00
|
|
|
import sim
|
2022-04-20 18:47:47 +08:00
|
|
|
import net
|
2022-04-12 20:16:58 +08:00
|
|
|
import modem
|
2022-04-20 18:47:47 +08:00
|
|
|
import utime
|
2022-03-21 19:40:33 +08:00
|
|
|
import dataCall
|
2022-03-09 16:51:21 +08:00
|
|
|
|
2022-04-18 13:55:36 +08:00
|
|
|
from usr.modules.sensor import Sensor
|
|
|
|
from usr.modules.battery import Battery
|
|
|
|
from usr.modules.ota import OTAFileClear
|
|
|
|
from usr.modules.history import History
|
|
|
|
from usr.modules.logging import getLogger
|
|
|
|
from usr.modules.mpower import LowEnergyManage
|
|
|
|
from usr.modules.remote import RemotePublish, RemoteSubscribe
|
|
|
|
from usr.modules.aliyunIot import AliYunIot, AliObjectModel
|
|
|
|
from usr.modules.quecthing import QuecThing, QuecObjectModel
|
|
|
|
from usr.modules.location import Location
|
2022-04-14 17:58:03 +08:00
|
|
|
from usr.settings import PROJECT_NAME, PROJECT_VERSION, \
|
2022-04-18 13:55:36 +08:00
|
|
|
DEVICE_FIRMWARE_NAME, DEVICE_FIRMWARE_VERSION, settings, SYSConfig
|
|
|
|
from usr.tracker_collector import Collector
|
|
|
|
from usr.tracker_controller import Controller
|
|
|
|
from usr.tracker_devicecheck import DeviceCheck
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-14 11:37:29 +08:00
|
|
|
try:
|
|
|
|
from misc import USB
|
|
|
|
except ImportError:
|
|
|
|
USB = None
|
|
|
|
try:
|
|
|
|
from misc import PowerKey
|
|
|
|
except ImportError:
|
|
|
|
PowerKey = None
|
|
|
|
|
|
|
|
|
2022-03-09 16:51:21 +08:00
|
|
|
log = getLogger(__name__)
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
sim.setSimDet(1, 0)
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2022-04-20 18:47:47 +08:00
|
|
|
net.setModemFun(0)
|
|
|
|
utime.sleep(3)
|
|
|
|
net.setModemFun(1)
|
|
|
|
utime.sleep(3)
|
|
|
|
net_check_res = DeviceCheck().net()
|
|
|
|
# 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
|
|
|
|
|
2022-03-09 16:51:21 +08:00
|
|
|
|
2022-04-14 15:20:22 +08:00
|
|
|
def tracker():
|
2022-04-13 15:34:07 +08:00
|
|
|
current_settings = settings.get()
|
|
|
|
|
2022-04-17 17:33:08 +08:00
|
|
|
# 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()
|
2022-04-17 17:33:08 +08:00
|
|
|
battery = Battery()
|
2022-04-17 16:49:25 +08:00
|
|
|
data_call = dataCall
|
2022-04-17 17:33:08 +08:00
|
|
|
low_energy = LowEnergyManage()
|
2022-04-17 16:49:25 +08:00
|
|
|
ota_file_clear = OTAFileClear()
|
|
|
|
usb = USB() if USB is not None else None
|
2022-04-17 17:33:08 +08:00
|
|
|
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
|
|
|
|
2022-04-17 17:33:08 +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()
|
2022-04-17 17:33:08 +08:00
|
|
|
cloud.set_object_model(cloud_om)
|
2022-04-14 15:20:22 +08:00
|
|
|
elif current_settings["sys"]["cloud"] & SYSConfig._cloud.AliYun:
|
2022-04-15 17:32:56 +08:00
|
|
|
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()
|
2022-04-17 17:33:08 +08:00
|
|
|
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"])
|
|
|
|
|
2022-04-17 17:33:08 +08:00
|
|
|
# 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)
|
2022-04-17 17:33:08 +08:00
|
|
|
remote_pub.add_cloud(cloud)
|
2022-04-14 15:20:22 +08:00
|
|
|
|
2022-04-17 17:33:08 +08:00
|
|
|
# Controller initialization
|
|
|
|
controller = Controller()
|
2022-04-14 15:20:22 +08:00
|
|
|
controller.add_module(remote_pub)
|
|
|
|
controller.add_module(settings)
|
2022-04-15 17:32:56 +08:00
|
|
|
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)
|
|
|
|
|
2022-04-17 17:33:08 +08:00
|
|
|
# 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"]
|
2022-04-15 17:32:56 +08:00
|
|
|
low_energy.set_period(work_cycle_period)
|
2022-04-17 17:33:08 +08:00
|
|
|
low_energy.set_low_energy_method(collector.__init_low_energy_method(work_cycle_period))
|
2022-04-15 17:32:56 +08:00
|
|
|
low_energy.addObserver(collector)
|
2022-04-13 15:34:07 +08:00
|
|
|
|
2022-04-17 17:33:08 +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)
|
|
|
|
|
2022-04-17 17:33:08 +08:00
|
|
|
# Business start
|
|
|
|
# Cloud start
|
|
|
|
cloud.init()
|
|
|
|
# OTA upgrade file clean
|
2022-04-13 15:34:07 +08:00
|
|
|
controller.ota_file_clean()
|
2022-04-17 17:33:08 +08:00
|
|
|
# Device modules status check
|
2022-04-13 20:15:34 +08:00
|
|
|
collector.device_status_check()
|
2022-04-17 17:33:08 +08:00
|
|
|
# Low energy init
|
2022-04-15 17:32:56 +08:00
|
|
|
controller.low_energy_init()
|
2022-04-17 17:33:08 +08:00
|
|
|
# Low energy start
|
2022-04-15 17:32:56 +08:00
|
|
|
controller.low_energy_start()
|