sijiang_dev_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = '863488056550489'
  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": "02", "data": "010A"},
  51. {"funCode": "0C", "data": "00"},
  52. {"funCode": "06", "data": "02"},
  53. {"funCode": "07", "data": "00"},
  54. {"funCode": "09", "data": "0101"},
  55. {"funCode": "0A", "data": "0200"},
  56. {"funCode": "0A", "data": "0201"},
  57. {"funCode": "01", "data": "00"},
  58. {"funCode": "0B", "data": "01"}
  59. ]
  60. count = 0
  61. errCount = 0
  62. dev = Device.get_dev(IMEI)
  63. if not dev:
  64. Device.bind(IMEI, IMEI)
  65. dev = Device.get_dev(IMEI)
  66. dev['server'] = '{}:{}'.format(server, port)
  67. print dev.server
  68. try:
  69. while True:
  70. count = count + 1
  71. if count > 2000:
  72. break
  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'] == 1:
  79. print 'error. cmd = {}'.format(str(cmd))
  80. errCount = errCount + 1
  81. # sys.exit(1)
  82. print('total = {}; err = {}'.format(count, errCount))
  83. # time.sleep(1)
  84. finally:
  85. print 'total = {}; err is: {}'.format(count, errCount)
  86. # for cmd in cmd_list:
  87. # payload = {'cmd': 210, 'IMEI': IMEI}
  88. # payload.update(cmd)
  89. #
  90. # dev = Device.get_dev(IMEI)
  91. #
  92. # result = MessageSender.send(dev, 210, payload)
  93. # if result['rst'] != 0:
  94. # print 'error. cmd = {}'.format(str(cmd))
  95. # exit(1)
  96. # #
  97. # for cmd in cmd_list:
  98. # payload = {'cmd': 210, 'IMEI': IMEI}
  99. # payload.update(cmd)
  100. #
  101. # dev = Device.get_dev(IMEI)
  102. #
  103. # MessageSender.async_send(dev, 210, payload)
  104. #
  105. # while True:
  106. # if event_received == False:
  107. # print('wait event to received.')
  108. # time.sleep(5)
  109. # continue
  110. # else:
  111. # exit(0)
  112. print 'finished'