123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- # -*- coding: utf-8 -*-
- #!/usr/bin/env python
- import os, sys,datetime
- from os.path import abspath, join
- import ConfigParser
- import uuid
- from django.conf import settings
- import simplejson as json
- from apps.web.constant import Const, DeviceCmdCode
- from apps.web.core.mqtt_client import MqttClient
- from apps.web.core.networking import MessageSender
- try:
- configParser = ConfigParser.ConfigParser()
- configParser.read('./settings.ini')
- settingModuleName = configParser.get('CORE', 'setting')
- except Exception:
- settingModuleName = 'staging'
- settingModuleIdentifier = "configs.production"
- PROJECT_ROOT = abspath(os.path.split(os.path.realpath(__file__))[0] + "/..")
- sys.path.insert(0, PROJECT_ROOT)
- sys.path.insert(0, join(PROJECT_ROOT, "apps"))
- os.environ.update({"DJANGO_SETTINGS_MODULE": settingModuleIdentifier})
- from apps.web.device.models import Device
- versionUrlDict = {
- '16':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0016_8955_SSL.bin",
- '15':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0015_8955_SSL.bin",
- '14':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0014_8955_SSL.bin"
- }
- def on_disconnect(client, userdata, rc):
- print('dis conect ........')
-
- def on_connect(client, userdata, flags, rc):
- print('connect ok ........')
- #订阅设备的200响应消息,用于检查
- device_topic = Const.SERVER_TOPIC_PREFIX + '/+' + "/200"
- client.subscribe(device_topic, qos=Const.MQTT_QOS)
-
- def on_message(mqttc, obj, msg):
- global versionUrlDict
-
- try:
- devInfo = json.loads(bytes.decode(msg.payload))
- imei = devInfo['IMEI']
- dev = Device.get_dev(imei)
- if dev is None:
- return
- #串口设备的各家协议不一样,不要进行升级,否则升级完毕,机子就无法联通串口了
- if dev.has_key('devType') and dev['devType'].has_key('code')and dev['devType']['code'] >= '100901':
- return
- if dev.has_key('devType') and (not dev['devType'].has_key('code')):
- return
-
- if dev['devType']['code'] >= Const.DEVICE_TYPE_CODE_PEDICURE:
- return
-
- if devInfo['soft_ver'] >= 'v1.7.6':
- return
-
- devs = Device.get_collection().find({'devNo':imei})
- if devs.count() <= 0:
- return
- dbDev = devs[0]
- upDict = dbDev.get('upDict',{'16':0,'15':0,'14':0,'count':0,'lastTime':None})
- nowTime = datetime.datetime.now()
-
- if upDict.get('lastTime') != None and (nowTime-upDict['lastTime']).seconds < 300:
- return
-
- tVersion = '16'
- if upDict['16'] < 3:
- upDict['16'] += 1
- tVersion = '16'
- elif upDict['15'] < 3:
- upDict['15'] += 1
- tVersion = '15'
- elif upDict['14'] < 3:
- upDict['14'] += 1
- tVersion = '14'
- if upDict['14'] == 3:
- upDict['16'] = 0
- upDict['15'] = 0
- upDict['14'] = 0
- else:
- upDict['result'] = 'failed'
- result = MessageSender.send(device = dev, cmd = DeviceCmdCode.SET_DEVINFO,
- payload = {"IMEI": imei, "ota_set": versionUrlDict[tVersion]})
- if result['rst'] != 0:
- print('send updating cmd error,imei=%s' % imei)
- else:
- print('success for imei=%s,send the message' % imei)
-
- upDict['lastTime'] = datetime.datetime.now()
- upDict['count'] += 1
- Device.get_collection().update({'devNo':imei},{'$set':{'upDict':upDict}})
- except Exception,e:
- return
-
- mqttc = MqttClient(client_id='webapp_'+str(uuid.uuid1()))
- try:
- mqttc.on_message = on_message
- mqttc.on_disconnect = on_disconnect
- mqttc.on_connect = on_connect
- #连接到现网
- mqttc.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
- mqttc.connect(settings.MQTT_HOSTNAME, settings.MQTT_PORT_SECURITY,60)
-
- print('updating process is listening......')
- mqttc.loop_forever()
-
- finally:
- print('finish ok')
- mqttc.disconnect()
- mqttc.close()
|