baojia_dev_test.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os, sys
  4. import uuid
  5. import time
  6. import simplejson as json
  7. from django.conf import settings
  8. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  9. sys.path.insert(0, PROJECT_ROOT)
  10. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.testing"})
  11. import django
  12. django.setup()
  13. from apps.web.core.networking import MessageSender
  14. from apps.web.core.mqtt_client import MqttClient
  15. from apps.web.device.models import Device
  16. IMEI = '868956048677870'
  17. server = '211.159.224.10'
  18. port = 1883
  19. event_received = False
  20. mqttc = MqttClient(client_id = 'webapp_' + str(uuid.uuid1()))
  21. def on_connect(client, userdata, flags, rc):
  22. # 订阅设备的120响应事件,用于检查
  23. device_topic = 'server/{}/120'.format(IMEI)
  24. client.subscribe(device_topic, qos = 0)
  25. device_topic = 'server/{}/110'.format(IMEI)
  26. client.subscribe(device_topic, qos = 0)
  27. device_topic = 'server/{}/100'.format(IMEI)
  28. client.subscribe(device_topic, qos = 0)
  29. device_topic = 'server/{}/301'.format(IMEI)
  30. client.subscribe(device_topic, qos = 0)
  31. device_topic = 'server/{}/307'.format(IMEI)
  32. client.subscribe(device_topic, qos = 0)
  33. def on_message(mqttc, obj, msg):
  34. msgDict = json.loads(bytes.decode(msg.payload))
  35. print('received event msg = {}'.format(msgDict))
  36. global event_received
  37. event_received = True
  38. mqttc.loop_stop()
  39. mqttc.disconnect()
  40. mqttc.close()
  41. try:
  42. mqttc.on_message = on_message
  43. mqttc.on_connect = on_connect
  44. mqttc.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
  45. mqttc.connect(server, port, 60)
  46. mqttc.loop_start()
  47. finally:
  48. pass
  49. cmd_list = [
  50. {"funCode": "07", "data": "0000"},
  51. {"funCode": "0F", "data": "00"},
  52. {"funCode": "14", "data": "01000100140001"},
  53. # {"funCode": "05", "data": "0103"},
  54. {"funCode": "15", "data": "01"},
  55. {"funCode": "01", "data": "00"},
  56. {"funCode": "02", "data": "00"},
  57. {"funCode": "18", "data": "001800180000000A00C8012C0190025864500F01011E78FF"},
  58. {"funCode": "1E", "data": "00"}
  59. ]
  60. count = 0
  61. while True:
  62. count = count + 1
  63. if count > 100:
  64. break
  65. for cmd in cmd_list:
  66. payload = {'cmd': 210, 'IMEI': IMEI}
  67. payload.update(cmd)
  68. dev = Device.get_dev(IMEI)
  69. result = MessageSender.send(dev, 210, payload)
  70. if result['rst'] != 0:
  71. print 'error. cmd = {}'.format(str(cmd))
  72. exit(1)
  73. for cmd in cmd_list:
  74. payload = {'cmd': 210, 'IMEI': IMEI}
  75. payload.update(cmd)
  76. dev = Device.get_dev(IMEI)
  77. result = MessageSender.send(dev, 210, payload)
  78. if result['rst'] != 0:
  79. print 'error. cmd = {}'.format(str(cmd))
  80. exit(1)
  81. #
  82. for cmd in cmd_list:
  83. payload = {'cmd': 210, 'IMEI': IMEI}
  84. payload.update(cmd)
  85. dev = Device.get_dev(IMEI)
  86. MessageSender.async_send(dev, 210, payload)
  87. while True:
  88. if event_received == False:
  89. print('wait event to received.')
  90. time.sleep(5)
  91. continue
  92. else:
  93. exit(0)
  94. print 'finished'