demo.tracker-v2/code/tracker.py

215 lines
7.7 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 net
import modem
import utime
import dataCall
2022-04-18 13:55:36 +08:00
from usr.modules.sensor import Sensor
from usr.modules.battery import Battery
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
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)
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:
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-04-14 15:20:22 +08:00
def tracker():
2022-04-22 17:37:08 +08:00
log.info("PROJECT_NAME: %s, PROJECT_VERSION: %s" % (PROJECT_NAME, PROJECT_VERSION))
log.info("DEVICE_FIRMWARE_NAME: %s, DEVICE_FIRMWARE_VERSION: %s" % (DEVICE_FIRMWARE_NAME, DEVICE_FIRMWARE_VERSION))
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
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()
2022-05-10 10:14:34 +08:00
# Add Location to DeviceCheck for checking whether locate is normal or not.
devicecheck.add_module(locator)
2022-05-10 10:14:34 +08:00
# Add Sensor to DeviceCheck for checking whether the sensor is normal or not.
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
)
2022-05-10 10:14:34 +08:00
# Cloud object model init
2022-04-13 15:34:07 +08:00
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
)
2022-05-10 10:14:34 +08:00
# Cloud object model init
2022-04-13 15:34:07 +08:00
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-05-10 10:14:34 +08:00
# Add History to RemotePublish for recording failure data
2022-04-13 15:34:07 +08:00
remote_pub.addObserver(history)
2022-05-10 10:14:34 +08:00
# Add Cloud to RemotePublish for publishing data to cloud
remote_pub.add_cloud(cloud)
2022-04-14 15:20:22 +08:00
# Controller initialization
controller = Controller()
2022-05-10 10:14:34 +08:00
# Add RemotePublish to Controller for publishing data to cloud
2022-04-14 15:20:22 +08:00
controller.add_module(remote_pub)
2022-05-10 10:14:34 +08:00
# Add Settings to Controller for changing settings.
2022-04-14 15:20:22 +08:00
controller.add_module(settings)
2022-05-10 10:14:34 +08:00
# Add LowEnergyManage to Controller for controlling low energy.
controller.add_module(low_energy)
2022-05-10 10:14:34 +08:00
# Add LED to Controller for show device status.
2022-04-14 15:20:22 +08:00
# controller.add_module(energy_led, led_type="energy")
# controller.add_module(running_led, led_type="running")
2022-05-10 10:14:34 +08:00
# Add power_key to Controller for power key callback
2022-04-14 15:20:22 +08:00
controller.add_module(power_key, callback=pwk_callback)
2022-05-10 10:14:34 +08:00
# Add USB to Controller for get usb status
2022-04-14 15:20:22 +08:00
controller.add_module(usb, callback=usb_callback)
2022-05-10 10:14:34 +08:00
# Add dataCall to Controller for get net error callback
2022-04-14 15:20:22 +08:00
controller.add_module(data_call)
# Collector initialization
collector = Collector()
2022-05-10 10:14:34 +08:00
# Add Controller to Collector for puting command to control device.
collector.add_module(controller)
2022-05-10 10:14:34 +08:00
# Add DeviceCheck to Collector for getting device status.
collector.add_module(devicecheck)
2022-05-10 10:14:34 +08:00
# Add Battery to Collector for getting battery info.
collector.add_module(battery)
2022-05-10 10:14:34 +08:00
# Add Sensor to Collector for getting sensor info.
collector.add_module(sensor)
2022-05-10 10:14:34 +08:00
# Add Location to Collector for getting location info.
collector.add_module(locator)
2022-05-10 10:14:34 +08:00
# Add History to Collector for getting history data.
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()
2022-04-22 17:37:08 +08:00
# OTA status init
collector.ota_status_init()
# Device modules status check
collector.device_status_check()
2022-04-22 17:37:08 +08:00
# Device info report to cloud
controller.remote_device_report()
# OTA plain check
controller.remote_ota_check()
# Low energy init
controller.low_energy_init()
# Low energy start
controller.low_energy_start()