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-03-21 11:21:01 +08:00
|
|
|
import ujson
|
2022-03-18 17:53:46 +08:00
|
|
|
import utime
|
|
|
|
import _thread
|
|
|
|
import osTimer
|
|
|
|
|
|
|
|
from aLiYun import aLiYun
|
|
|
|
|
|
|
|
from usr.logging import getLogger
|
|
|
|
from usr.settings import settings
|
2022-03-21 11:21:01 +08:00
|
|
|
from usr.settings import default_values_sys
|
2022-03-18 17:53:46 +08:00
|
|
|
from usr.common import numiter
|
|
|
|
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
PROPERTY = 0x0
|
|
|
|
EVENT = 0x1
|
|
|
|
SERVICE = 0x2
|
|
|
|
|
|
|
|
object_model = {
|
|
|
|
'event': [
|
|
|
|
'sos_alert',
|
|
|
|
'fault_alert',
|
|
|
|
'low_power_alert',
|
2022-03-25 11:56:14 +08:00
|
|
|
'sim_abnormal_alert',
|
2022-03-18 17:53:46 +08:00
|
|
|
'disassemble_alert',
|
|
|
|
'drive_behavior_alert',
|
|
|
|
'over_speed_alert',
|
|
|
|
],
|
|
|
|
'property': [
|
|
|
|
'power_switch',
|
|
|
|
'energy',
|
|
|
|
'phone_num',
|
|
|
|
'loc_method',
|
2022-03-21 19:40:33 +08:00
|
|
|
'work_mode',
|
|
|
|
'work_cycle_period',
|
2022-03-18 17:53:46 +08:00
|
|
|
'local_time',
|
|
|
|
'low_power_alert_threshold',
|
|
|
|
'low_power_shutdown_threshold',
|
|
|
|
'sw_ota',
|
|
|
|
'sw_ota_auto_upgrade',
|
|
|
|
'sw_voice_listen',
|
|
|
|
'sw_voice_record',
|
|
|
|
'sw_fault_alert',
|
|
|
|
'sw_low_power_alert',
|
|
|
|
'sw_over_speed_alert',
|
2022-03-25 11:56:14 +08:00
|
|
|
'sw_sim_abnormal_alert',
|
2022-03-18 17:53:46 +08:00
|
|
|
'sw_disassemble_alert',
|
|
|
|
'sw_drive_behavior_alert',
|
|
|
|
'drive_behavior_code',
|
|
|
|
'power_restart',
|
|
|
|
'over_speed_threshold',
|
2022-03-28 20:29:01 +08:00
|
|
|
'device_module_status',
|
2022-03-18 17:53:46 +08:00
|
|
|
'gps_mode',
|
|
|
|
'user_ota_action',
|
|
|
|
'ota_status',
|
2022-03-21 11:21:01 +08:00
|
|
|
'GeoLocation',
|
2022-03-29 13:24:17 +08:00
|
|
|
'voltage',
|
2022-03-18 17:53:46 +08:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
class AliYunIotError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
2022-03-18 17:53:46 +08:00
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
2022-03-18 17:53:46 +08:00
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
|
|
|
|
class AliYunIot(object):
|
|
|
|
|
|
|
|
def __init__(self, pk, ps, dk, ds, downlink_queue):
|
2022-03-18 17:53:46 +08:00
|
|
|
self.post_res = {}
|
2022-03-25 18:42:11 +08:00
|
|
|
self.breack_flag = 0
|
2022-03-18 17:53:46 +08:00
|
|
|
self.ali_timer = osTimer()
|
2022-03-21 11:21:01 +08:00
|
|
|
self.downlink_queue = downlink_queue
|
2022-03-18 17:53:46 +08:00
|
|
|
|
|
|
|
self.id_iter = numiter()
|
|
|
|
self.id_lock = _thread.allocate_lock()
|
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
self.ica_topic_property_post = '/sys/%s/%s/thing/event/property/post' % (pk, dk)
|
|
|
|
self.ica_topic_property_post_reply = '/sys/%s/%s/thing/event/property/post_reply' % (pk, dk)
|
|
|
|
self.ica_topic_property_set = '/sys/%s/%s/thing/service/property/set' % (pk, dk)
|
|
|
|
self.ica_topic_property_get = '/sys/%s/%s/thing/service/property/get' % (pk, dk)
|
|
|
|
self.ica_topic_property_query = '/sys/%s/%s/thing/service/property/query' % (pk, dk)
|
|
|
|
self.ica_topic_event_post = '/sys/%s/%s/thing/event/{}/post' % (pk, dk)
|
|
|
|
self.ica_topic_event_post_reply = '/sys/%s/%s/thing/event/{}/post_reply' % (pk, dk)
|
|
|
|
|
|
|
|
current_settings = settings.get()
|
|
|
|
if current_settings['sys']['ali_burning_method'] == default_values_sys._ali_burning_method.one_type_one_density:
|
|
|
|
dk = None
|
|
|
|
elif current_settings['sys']['ali_burning_method'] == default_values_sys._ali_burning_method.one_machine_one_density:
|
|
|
|
ps = None
|
|
|
|
self.ali = aLiYun(pk, ps, dk, ds)
|
|
|
|
self.clientID = dk
|
|
|
|
setMqttres = self.ali.setMqtt(self.clientID, clean_session=False, keepAlive=60, reconn=True)
|
|
|
|
if setMqttres == -1:
|
|
|
|
raise AliYunIotError('setMqtt Falied!')
|
|
|
|
self.ali.setCallback(self.ali_sub_cb)
|
|
|
|
self.ali_subcribe_topic()
|
2022-03-18 17:53:46 +08:00
|
|
|
self.ali.start()
|
|
|
|
|
|
|
|
def ali_subcribe_topic(self):
|
2022-03-21 11:21:01 +08:00
|
|
|
if self.ali.subscribe(self.ica_topic_property_post, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % self.ica_topic_property_post)
|
|
|
|
if self.ali.subscribe(self.ica_topic_property_post_reply, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % self.ica_topic_property_post_reply)
|
|
|
|
if self.ali.subscribe(self.ica_topic_property_set, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % self.ica_topic_property_set)
|
|
|
|
if self.ali.subscribe(self.ica_topic_property_get, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % self.ica_topic_property_get)
|
|
|
|
if self.ali.subscribe(self.ica_topic_property_query, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % self.ica_topic_property_query)
|
2022-03-18 17:53:46 +08:00
|
|
|
for tsl_event_identifier in object_model['event']:
|
2022-03-21 11:21:01 +08:00
|
|
|
post_topic = self.ica_topic_event_post.format(tsl_event_identifier)
|
|
|
|
if self.ali.subscribe(post_topic, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % post_topic)
|
|
|
|
|
|
|
|
post_reply_topic = self.ica_topic_event_post_reply.format(tsl_event_identifier)
|
|
|
|
if self.ali.subscribe(post_reply_topic, qos=0) == -1:
|
|
|
|
log.error('Topic [%s] Subscribe Falied.' % post_reply_topic)
|
2022-03-18 17:53:46 +08:00
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
with self.id_lock:
|
|
|
|
try:
|
|
|
|
msg_id = next(self.id_iter)
|
|
|
|
except StopIteration:
|
|
|
|
self.id_iter = numiter()
|
|
|
|
msg_id = next(self.id_iter)
|
|
|
|
|
|
|
|
return str(msg_id)
|
|
|
|
|
|
|
|
def put_post_res(self, msg_id, res):
|
|
|
|
self.post_res[msg_id] = res
|
|
|
|
|
|
|
|
def get_post_res(self, msg_id):
|
|
|
|
current_settings = settings.get()
|
2022-03-25 18:42:11 +08:00
|
|
|
self.ali_timer.start(current_settings['sys']['checknet_timeout'] * 1000, 2, self.ali_timer_cb)
|
2022-03-18 17:53:46 +08:00
|
|
|
while self.post_res.get(msg_id) is None:
|
|
|
|
utime.sleep_ms(200)
|
2022-03-25 18:42:11 +08:00
|
|
|
if self.breack_flag:
|
|
|
|
self.post_res[msg_id] = False
|
|
|
|
break
|
2022-03-18 17:53:46 +08:00
|
|
|
self.ali_timer.stop()
|
2022-03-25 18:42:11 +08:00
|
|
|
self.breack_flag = 0
|
2022-03-18 17:53:46 +08:00
|
|
|
res = self.post_res.pop(msg_id)
|
|
|
|
return res
|
|
|
|
|
2022-03-25 18:42:11 +08:00
|
|
|
def ali_timer_cb(self, args):
|
|
|
|
self.breack_flag = 1
|
|
|
|
|
2022-03-23 14:21:45 +08:00
|
|
|
def post_data(self, data):
|
2022-03-18 17:53:46 +08:00
|
|
|
msg_ids = []
|
|
|
|
if self.ali.getAliyunSta() == 0:
|
|
|
|
try:
|
|
|
|
property_params = {}
|
|
|
|
event_params = {}
|
|
|
|
# Format Publish Params.
|
|
|
|
for k, v in data.items():
|
|
|
|
if k in object_model['property']:
|
|
|
|
property_params[k] = {
|
|
|
|
'value': v,
|
|
|
|
'time': utime.mktime(utime.localtime()) * 1000
|
|
|
|
}
|
|
|
|
elif k in object_model['event']:
|
|
|
|
event_params[k] = {
|
2022-03-28 20:29:01 +08:00
|
|
|
'value': {},
|
2022-03-18 17:53:46 +08:00
|
|
|
'time': utime.mktime(utime.localtime()) * 1000
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
log.error('Publish Key [%s] is not in property and event' % k)
|
|
|
|
# Publish Property Data.
|
|
|
|
if property_params:
|
|
|
|
msg_id = self.get_id()
|
|
|
|
publish_data = {
|
|
|
|
'id': msg_id,
|
|
|
|
'version': '1.0',
|
|
|
|
'sys': {
|
|
|
|
'ack': 1
|
|
|
|
},
|
|
|
|
'params': property_params,
|
|
|
|
'method': 'thing.event.property.post'
|
|
|
|
}
|
2022-03-21 11:21:01 +08:00
|
|
|
self.ali.publish(self.ica_topic_property_post, ujson.dumps(publish_data), qos=0)
|
|
|
|
msg_ids.append(msg_id)
|
2022-03-18 17:53:46 +08:00
|
|
|
# Publish Event Data.
|
|
|
|
if event_params:
|
|
|
|
for event in event_params.keys():
|
|
|
|
topic = self.ica_topic_event_post.format(event)
|
|
|
|
msg_id = self.get_id()
|
|
|
|
publish_data = {
|
|
|
|
'id': msg_id,
|
|
|
|
'version': '1.0',
|
|
|
|
'sys': {
|
|
|
|
'ack': 1
|
|
|
|
},
|
|
|
|
'params': event_params[event],
|
|
|
|
'method': 'thing.event.%s.post' % event
|
|
|
|
}
|
2022-03-21 11:21:01 +08:00
|
|
|
self.ali.publish(topic, ujson.dumps(publish_data), qos=0)
|
|
|
|
msg_ids.append(msg_id)
|
2022-03-18 17:53:46 +08:00
|
|
|
|
|
|
|
pub_res = [self.get_post_res(msg_id) for msg_id in msg_ids]
|
|
|
|
return True if False not in pub_res else False
|
|
|
|
except Exception:
|
|
|
|
log.error('AliYun publish topic %s failed. data: %s' % (data.get('topic'), data.get('data')))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def ali_sub_cb(self, topic, data):
|
2022-03-21 11:21:01 +08:00
|
|
|
# log.info('topic: %s, data: %s' % (topic.decode(), data.decode()))
|
|
|
|
topic = topic.decode()
|
|
|
|
data = ujson.loads(data)
|
2022-03-18 17:53:46 +08:00
|
|
|
if topic.endswith('/post_reply'):
|
2022-03-21 11:21:01 +08:00
|
|
|
log.info('topic: %s, data: %s' % (topic, data))
|
2022-03-18 17:53:46 +08:00
|
|
|
self.put_post_res(data['id'], True if data['code'] == 200 else False)
|
2022-03-21 11:21:01 +08:00
|
|
|
elif topic.endswith('/property/set'):
|
|
|
|
log.info('topic: %s, data: %s' % (topic, data))
|
|
|
|
if data['method'] == 'thing.service.property.set':
|
|
|
|
dl_data = list(zip(data.get("params", {}).keys(), data.get("params", {}).values()))
|
|
|
|
self.downlink_queue.put(('object_model', dl_data))
|
2022-03-18 17:53:46 +08:00
|
|
|
else:
|
|
|
|
pass
|