123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os, sys
- import uuid
- import time
- 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 = '863488056550489'
- server = '211.159.224.10'
- port = 1883
- event_received = 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 event_received
- event_received = 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": "02", "data": "010A"},
- {"funCode": "0C", "data": "00"},
- {"funCode": "06", "data": "02"},
- {"funCode": "07", "data": "00"},
- {"funCode": "09", "data": "0101"},
- {"funCode": "0A", "data": "0200"},
- {"funCode": "0A", "data": "0201"},
- {"funCode": "01", "data": "00"},
- {"funCode": "0B", "data": "01"}
- ]
- count = 0
- errCount = 0
- dev = Device.get_dev(IMEI)
- if not dev:
- Device.bind(IMEI, IMEI)
- dev = Device.get_dev(IMEI)
- dev['server'] = '{}:{}'.format(server, port)
- print dev.server
- try:
- while True:
- count = count + 1
- if count > 2000:
- break
- for cmd in cmd_list:
- payload = {'cmd': 210, 'IMEI': IMEI}
- payload.update(cmd)
- # dev = Device.get_dev(IMEI)
- result = MessageSender.send(dev, 210, payload)
- if result['rst'] == 1:
- print 'error. cmd = {}'.format(str(cmd))
- errCount = errCount + 1
- # sys.exit(1)
- print('total = {}; err = {}'.format(count, errCount))
- # time.sleep(1)
- finally:
- print 'total = {}; err is: {}'.format(count, errCount)
- # for cmd in cmd_list:
- # payload = {'cmd': 210, 'IMEI': IMEI}
- # payload.update(cmd)
- #
- # dev = Device.get_dev(IMEI)
- #
- # result = MessageSender.send(dev, 210, payload)
- # if result['rst'] != 0:
- # print 'error. cmd = {}'.format(str(cmd))
- # exit(1)
- # #
- # for cmd in cmd_list:
- # payload = {'cmd': 210, 'IMEI': IMEI}
- # payload.update(cmd)
- #
- # dev = Device.get_dev(IMEI)
- #
- # MessageSender.async_send(dev, 210, payload)
- #
- # while True:
- # if event_received == False:
- # print('wait event to received.')
- # time.sleep(5)
- # continue
- # else:
- # exit(0)
- print 'finished'
|