# -*- 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 = '863488058983381' 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": "0B", "data": "01"}, {"funCode": "01", "data": "00"}, {"funCode": "0C", "data": "00"}, {"funCode": "15", "data": "00"}, {"funCode": "02", "data": "01000001E0"}, {"funCode": "0B", "data": "01"}, {"funCode": "02", "data": "02000001E0"}, {"funCode": "0B", "data": "02"}, {"funCode": "02", "data": "03000001E0"}, {"funCode": "0B", "data": "03"}, {"funCode": "02", "data": "04000001E0"}, {"funCode": "0B", "data": "04"}, {"funCode": "02", "data": "05000001E0"}, {"funCode": "0B", "data": "05"}, {"funCode": "02", "data": "06000001E0"}, {"funCode": "0B", "data": "06"}, {"funCode": "02", "data": "07000001E0"}, {"funCode": "0B", "data": "07"}, {"funCode": "02", "data": "08000001E0"}, {"funCode": "0B", "data": "08"}, {"funCode": "02", "data": "09000001E0"}, {"funCode": "0B", "data": "09"}, {"funCode": "02", "data": "0A000001E0"}, {"funCode": "0B", "data": "0A"}, # {"funCode": "06", "data": "03"}, # {"funCode": "07", "data": "00"}, # {"funCode": "08", "data": "01F40100F000F000F0"}, # {"funCode": "09", "data": "0101"}, # {"funCode": "0A", "data": "0101"}, # {"funCode": "0B", "data": "01"}, # {"funCode": "14", "data": "00C80101900701F405"} ] bind = False dev = Device.get_dev(IMEI) if not dev: Device.bind(IMEI, IMEI) dev = Device.get_dev(IMEI) bind = True 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) i = 0 while i < 1000: for payload in cmd_list: result = MessageSender.send(dev, 210, payload) if result['rst'] != 0: print 'error. cmd = {}'.format(str(cmd_list)) exit(1) i = i + 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'