123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- # -*- coding: utf-8 -*-
- #!/usr/bin/env python
- import os
- import sys
- import time
- import json
- import logging
- import datetime
- import threading
- import daiquiri
- #: current_dir - 2
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- from common import *
- from apps.web.core.mqtt_client import MqttClient
- daiquiri.setup(
- level=logging.DEBUG,
- outputs=(
- daiquiri.output.Stream(sys.stdout),
- daiquiri.output.File('simdev-errors.log', level=logging.ERROR),
- daiquiri.output.TimedRotatingFile('simdev-everything.log',
- level=logging.DEBUG,
- interval=datetime.timedelta(days=1))
- )
- )
- logger = logging.getLogger(__name__)
- SMART_BOX_PARA = {
- 'ip_addr': "120.27.251.159",
- 'port': 1883,
- 'mny1': 1,
- 'tm1': 6,
- 'mny2': 2,
- 'tm2': 20,
- 'mny3': 3,
- 'tm3': 35,
- 'mny4': 4,
- 'tm4': 45,
- 'pulse_width': 50,
- 'pulse_inter': 500,
- 'pulse_idle': 1,
- 'coin': 0,
- 'coin_yed': 0,
- 'total_coin': 0,
- 'pay_cnt': 0,
- 'cycle': 300,
- 'report_set': 1,
- 'max_coin': 4,
- 'current_date': 100,
- 'imei': "1234567890",
- #: CSQ Values: Received Signal Strength Indication (<rssi>)
- 'csq': 30,
- 'status': 0
- }
- def from_lua_to_py(text):
- result = {}
- for line in text.strip('{}').split(','):
- key, value = line.strip().split('=')
- result[key.strip()] = value.strip()
- return result
- # payload
- def handshake_payload(imei):
- return json.dumps(
- {
- 'cmd': 200,
- #: seq 为遗留版本废弃值, 其余都省略
- 'seq': 23,
- 'IMEI': imei,
- 'mf': "rf-sensing",
- 'soft_ver': "v1.5",
- 'hw_ver': "v2.0",
- 'cycle': SMART_BOX_PARA['cycle'],
- 'coin': SMART_BOX_PARA['coin'],
- 'coin_yed': SMART_BOX_PARA['coin_yed'],
- 'total_coin': SMART_BOX_PARA['total_coin'],
- 'pay_cnt': SMART_BOX_PARA['pay_cnt'],
- 'status': SMART_BOX_PARA['status'],
- 'signal': 22,
- 'max_coin': SMART_BOX_PARA['max_coin'],
- 'pwm_wid': SMART_BOX_PARA['pulse_width'],
- 'pwm_inter': SMART_BOX_PARA['pulse_inter'],
- 'pwm_idle': SMART_BOX_PARA['pulse_idle'],
- 'ip1': SMART_BOX_PARA['ip_addr'],
- 'port1': SMART_BOX_PARA['port'],
- 'mny1': SMART_BOX_PARA['mny1'],
- 'tm1': SMART_BOX_PARA['tm1'],
- 'mny2': SMART_BOX_PARA['mny2'],
- 'tm2': SMART_BOX_PARA['tm2'],
- 'mny3': SMART_BOX_PARA['mny3'],
- 'tm3': SMART_BOX_PARA['tm3'],
- 'mny4': SMART_BOX_PARA['mny4'],
- 'tm4': SMART_BOX_PARA['tm4']
- }
- )
- def get_info_payload(imei):
- return json.dumps(
- {
- 'cmd': 201,
- 'rst': 0,
- 'IMEI': imei,
- 'coin': SMART_BOX_PARA['coin'],
- 'coin_yed': SMART_BOX_PARA['coin_yed'],
- 'cycle': SMART_BOX_PARA['cycle'],
- 'hw_ver': 'v2.0',
- 'ip1': SMART_BOX_PARA['ip_addr'],
- 'max_coin': SMART_BOX_PARA['max_coin'],
- 'mf': 'rf-sensing',
- 'mny1': SMART_BOX_PARA['mny1'],
- 'mny2': SMART_BOX_PARA['mny2'],
- 'mny3': SMART_BOX_PARA['mny3'],
- 'mny4': SMART_BOX_PARA['mny4'],
- 'pay_cnt': SMART_BOX_PARA['pay_cnt'],
- 'port1': SMART_BOX_PARA['port'],
- 'pwm_idle': SMART_BOX_PARA['pulse_idle'],
- 'pwm_inter': SMART_BOX_PARA['pulse_inter'],
- 'pwm_wid': SMART_BOX_PARA['pulse_width'],
- 'signal': SMART_BOX_PARA['csq'],
- 'soft_ver': 'fw_ver',
- 'status': SMART_BOX_PARA['status'],
- 'tm1': SMART_BOX_PARA['tm1'],
- 'tm2': SMART_BOX_PARA['tm2'],
- 'tm3': SMART_BOX_PARA['tm3'],
- 'tm4': SMART_BOX_PARA['tm4'],
- 'total_coin': SMART_BOX_PARA['total_coin']
- }
- )
- def mobile_pay_init_payload(imei):
- return json.dumps(
- {
- 'cmd': 203,
- 'IMEI': imei,
- 'rst': 0
- }
- )
- def mobile_pay_finish_payload(imei, pay):
- return json.dumps(
- {
- 'cmd': 204,
- 'IMEI': imei,
- 'app_pay': pay,
- 'status': 1
- }
- )
- def put_coin_payload(imei):
- return json.dumps(
- {
- 'cmd': 205,
- 'IMEI': SMART_BOX_PARA['imei'],
- 'coin': 1,
- 'status': 1,
- 'today_coin': SMART_BOX_PARA['coin'],
- 'total_coin': SMART_BOX_PARA['total_coin']
- }
- )
- def heartbeat_payload(imei):
- return json.dumps(
- {
- 'cmd': 207,
- 'IMEI': imei,
- 'coin': 0,
- 'total_coin': 0,
- 'pay_cnt': 0,
- 'status': 0,
- 'signal': 20
- }
- )
- def offline_payload(imei):
- return json.dumps(
- {
- 'cmd': 208,
- 'IMEI': imei,
- 'offline': 1
- }
- )
- ## handlers
- class DeviceProtocolHandler(BaseProtocolHandler):
- def __init__(self, client):
- self._client = client # type: MqttClient
- def handle_200(self, payload):
- # type: (dict)->None
- pass
- def handle_201(self, payload):
- # type: (dict)->None
- self._client.publish(
- topic=get_server_topic(imei=payload['IMEI'], cmd=CMD.GET_INFO),
- payload=get_info_payload(imei=payload['IMEI'])
- )
- def handle_202(self, payload):
- # type: (dict)->None
- pass
- def handle_203(self, payload):
- # type: (dict)->None
- self._client.publish(
- topic=get_server_topic(imei=payload['IMEI'], cmd=CMD.MOBILE_PAY_INIT),
- payload=mobile_pay_init_payload(imei=payload['IMEI'])
- )
- def handle_204(self, payload):
- # type: (dict)->None
- pass
- def handle_205(self, payload):
- # type: (dict)->None
- pass
- def handle_206(self, payload):
- # type: (dict)->None
- pass
- def handle_207(self, payload):
- # type: (dict)->None
- pass
- def handle_208(self, request):
- # type: (dict)->None
- pass
- def handle_heartbeat(client):
- while True:
- client.publish(topic=get_server_topic(imei=DEFAULT_DEVICE_IMEI, cmd=CMD.HEARTBEAT),
- payload=heartbeat_payload(imei=DEFAULT_DEVICE_IMEI))
- time.sleep(30)
- def on_connect(client, userdata, flags, rc):
- # type: (MqttClient, dict, int, dict)->None
- logger.info('connected to (%s, %d)' % (MQTT_HOSTNAME, MQTT_PORT))
- logger.info('client: {}'.format(client))
- logger.info('userdata: {}'.format(userdata))
- logger.info('flags: {}'.format(flags))
- logger.info('rc: {}'.format(rc))
- def on_message(client, userdata, message):
- message = json.loads(bytes.decode(message.payload))
- logger.info('simdev client: {}'.format(client))
- logger.info('simdev userdata: {}'.format(userdata))
- logger.info('simdev message: {}'.format(message))
- handler = DeviceProtocolHandler(client)
- handler.handle(payload=message)
- def run():
- client = MqttClient('simdev') # type: MqttClient
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(MQTT_HOSTNAME, MQTT_PORT)
- heartbeat_thread = threading.Thread(target=handle_heartbeat, args=(client,))
- heartbeat_thread.setDaemon(True)
- heartbeat_thread.start()
- client.subscribe(topic=get_device_topic(imei=DEFAULT_DEVICE_IMEI, cmd=MULTI_LEVEL_ALL))
- client.loop_forever()
- if __name__ == '__main__':
- run()
|