123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os
- import sys
- import time
- import uuid
- import simplejson as json
- from django.conf import settings
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.testing"})
- import django
- django.setup()
- from apps.web.core.networking import MessageSender
- from apps.web.core.mqtt_client import MqttClient
- from apps.web.device.models import Device
- IMEI = '863488058983365'
- server = '211.159.224.10'
- port = 1883
- can_quit = False
- mqttc = MqttClient(client_id = 'webapp_' + str(uuid.uuid1()))
- def on_connect(client, userdata, flags, rc):
- # 订阅设备的120响应事件,用于检查
- device_topic = 'server/{}/120'.format(IMEI)
- client.subscribe(device_topic, qos = 0)
- device_topic = 'server/{}/110'.format(IMEI)
- client.subscribe(device_topic, qos = 0)
- device_topic = 'server/{}/100'.format(IMEI)
- client.subscribe(device_topic, qos = 0)
- device_topic = 'server/{}/301'.format(IMEI)
- client.subscribe(device_topic, qos = 0)
- device_topic = 'server/{}/307'.format(IMEI)
- client.subscribe(device_topic, qos = 0)
- def on_message(mqttc, obj, msg):
- msgDict = json.loads(bytes.decode(msg.payload))
- print('received event msg = {}'.format(msgDict))
- global can_quit
- can_quit = True
- mqttc.loop_stop()
- mqttc.disconnect()
- mqttc.close()
- try:
- mqttc.on_message = on_message
- mqttc.on_connect = on_connect
- mqttc.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
- mqttc.connect(server, port, 60)
- mqttc.loop_start()
- finally:
- pass
- cmd_list = [
- {"funCode": "D0", "data": "00"},
- {"funCode": "D1", "data": "012C00F000B4007800B4012C01E00258145A05"},
- {"funCode": "D2", "data": "1E000003"},
- {"funCode": "D3", "data": "03"},
- {"funCode": "D5", "data": "0000"},
- {"funCode": "D6", "data": "03"},
- {"funCode": "DB", "data": "0102030405"},
- ]
- bind = False
- dev = Device.get_dev(IMEI)
- if not dev:
- Device.bind(IMEI, IMEI)
- bind = True
- dev = Device.get_dev(IMEI)
- dev['server'] = '211.159.224.10:1883'
- for payload in cmd_list:
- result = MessageSender.send(dev, 210, payload)
- if result['rst'] != 0:
- print 'error. cmd = {}'.format(str(cmd_list))
- exit(1)
- for payload in cmd_list:
- result = MessageSender.send(dev, 210, payload)
- if result['rst'] != 0:
- print 'error. cmd = {}'.format(str(cmd_list))
- exit(1)
- #
- # for payload in cmd_list:
- # MessageSender.async_send(dev, 210, payload)
- #
- # for payload in cmd_list:
- # MessageSender.send_no_wait(device = dev,
- # cmd = 220,
- # payload = payload)
- while True:
- if not can_quit:
- print('wait event to received.')
- time.sleep(5)
- continue
- else:
- break
- if bind:
- Device.unbind(dev)
- print 'finished'
|