cy_car_dev_test.py 2.9 KB

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