Ejemplo n.º 1
0
def detect_button_events(interval=5):
    """
    get messages sent from buttons
    :param interval:
    :return:
    """
    if GATEWAY_ID == '' or ORGANIZATION == '' or USERNAME == '' or PASSWORD == '':
        print(
            'Please fill out your username, password, and gateway ID in the script'
        )
        return

    try:
        while True:
            data = get_received_messages(ORGANIZATION, GATEWAY_ID, USERNAME,
                                         PASSWORD, NUM_MINUTES_BACK)
            if data:
                for d in data['events']:
                    eventUUID = d['routerMetadata']['eventUUID']
                    node_id = d['networkMessage']['nodeMetadata']['nodeId']
                    receipt_time = d['routerMetadata']['receiptTime']

                    # change to datatime object
                    receipt_time = receipt_time[:8] + receipt_time[9:15]
                    receipt_time = datetime.datetime.strptime(
                        receipt_time, '%Y%m%d%H%M%S')

                    # 判断是否已经处理过了
                    msg = Message.query.filter_by(eventUUID=eventUUID).first()
                    if not msg:
                        message = Message(eventUUID=eventUUID,
                                          node_id=node_id,
                                          receipt_time=receipt_time)
                        message.save()

                        # 发送微信消息
                        send_wechat(node_id)
                        # 创建订单
                        create_order(node_id)
                        # 发送确认消息
                        send_command(USERNAME, PASSWORD, GATEWAY_ID, node_id,
                                     '0XFF')

            time.sleep(interval)

    except KeyboardInterrupt:
        os.exit()
Ejemplo n.º 2
0
def detect_button_events(interval=5):
    """
    get messages sent from buttons
    :param interval:
    :return:
    """
    if GATEWAY_ID == '' or ORGANIZATION == '' or USERNAME == '' or PASSWORD == '':
        print ('Please fill out your username, password, and gateway ID in the script')
        return

    try:
        while True:
            data = get_received_messages(ORGANIZATION, GATEWAY_ID, USERNAME, PASSWORD, NUM_MINUTES_BACK)
            if data:
                for d in data['events']:
                    eventUUID = d['routerMetadata']['eventUUID']
                    node_id = d['networkMessage']['nodeMetadata']['nodeId']
                    receipt_time = d['routerMetadata']['receiptTime']

                    # change to datatime object
                    receipt_time = receipt_time[:8] + receipt_time[9:15]
                    receipt_time = datetime.datetime.strptime(receipt_time, '%Y%m%d%H%M%S')

                    # 判断是否已经处理过了
                    msg = Message.query.filter_by(eventUUID=eventUUID).first()
                    if not msg:
                        message = Message(eventUUID=eventUUID, node_id=node_id, receipt_time=receipt_time)
                        message.save()

                        # 发送微信消息
                        send_wechat(node_id)
                        # 创建订单
                        create_order(node_id)
                        # 发送确认消息
                        send_command(USERNAME, PASSWORD, GATEWAY_ID, node_id, '0XFF')

            time.sleep(interval)

    except KeyboardInterrupt:
        os.exit()
Ejemplo n.º 3
0
def detect_button_events(interval=5):
    """
    get messages sent from buttons
    :param interval:
    :return:
    """
    if GATEWAY_ID == '' or ORGANIZATION == '' or USERNAME == '' or PASSWORD == '':
        print ('Please fill out your username, password, and gateway ID in the script')
        return

    try:
        while True:
            data = get_received_messages(ORGANIZATION, GATEWAY_ID, USERNAME, PASSWORD, NUM_MINUTES_BACK)
            if data:
                for event in data:
                    for d in event['events']:
                        eventUUID = d['networkMessage']['routerMetadata']['eventUUID']
                        node_id = d['networkMessage']['nodeMetadata']['nodeId']
                        receipt_time_str = d['networkMessage']['routerMetadata']['receiptTime']
                        remark = d["networkMessage"]["payloadHex"]
                        rssi = d["networkMessage"]["signalMetadata"]["rssi"]
                        event_gateway_id = d['networkMessage']['routerMetadata']['routerUUID']

                        # change to datatime object
                        receipt_time = receipt_time_str[:8] + receipt_time_str[9:15]
                        receipt_time = datetime.datetime.strptime(receipt_time, '%Y%m%d%H%M%S') + datetime.timedelta(hours=8)
                        receipt_time_str = receipt_time.strftime('%Y-%m-%d %H:%M:%S')

                        # 判断是否已经处理过了
                        msg = Message.query.filter_by(eventUUID=eventUUID).first()

                        if not msg:
                            message = Message(eventUUID=eventUUID, node_id=node_id, receipt_time=receipt_time)
                            message.save()

                            # 发送微信消息
                            #
                            wechat_data = {
                                'first': {
                                    'value': "恭喜你按下成功!<( ̄︶ ̄)>",
                                    "color": "#173177"
                                },
                                'node_id': {
                                    'value': node_id,
                                    'color': '#173177'
                                },
                                'receipt_time': {
                                    'value': receipt_time_str,
                                    'color': '#173177'
                                },
                                'remark': {
                                    'value': remark,
                                    'color': '#173177'
                                },
                                'rssi': {
                                    'value': rssi,
                                    'color': '173177'
                                },
                                'gateway_sn': {
                                    'value': str(gateway_mac.index(event_gateway_id[-12:]) + 1),
                                    'color': '#173122'
                                },
                                'gateway_id': {
                                    'value': event_gateway_id,
                                    'color': '#173177'
                                }
                            }
                            send_wechat(node_id, wechat_data)
                            # 创建订单username
                            create_order(node_id)
                            # TODO:发送确认消息
                            # send_command(USERNAME, PASSWORD, GATEWAY_ID, node_id, '0XFF')

            time.sleep(interval)

    except Exception:
        detect_button_events()
Ejemplo n.º 4
0
def detect_button_events(interval=5):
    """
    get messages sent from buttons
    :param interval:
    :return:
    """
    if GATEWAY_ID == '' or ORGANIZATION == '' or USERNAME == '' or PASSWORD == '':
        print(
            'Please fill out your username, password, and gateway ID in the script'
        )
        return

    try:
        while True:
            data = get_received_messages(ORGANIZATION, GATEWAY_ID, USERNAME,
                                         PASSWORD, NUM_MINUTES_BACK)
            if data:
                for event in data:
                    for d in event['events']:
                        eventUUID = d['networkMessage']['routerMetadata'][
                            'eventUUID']
                        node_id = d['networkMessage']['nodeMetadata']['nodeId']
                        receipt_time_str = d['networkMessage'][
                            'routerMetadata']['receiptTime']
                        remark = d["networkMessage"]["payloadHex"]
                        rssi = d["networkMessage"]["signalMetadata"]["rssi"]
                        event_gateway_id = d['networkMessage'][
                            'routerMetadata']['routerUUID']

                        # change to datatime object
                        receipt_time = receipt_time_str[:8] + receipt_time_str[
                            9:15]
                        receipt_time = datetime.datetime.strptime(
                            receipt_time,
                            '%Y%m%d%H%M%S') + datetime.timedelta(hours=8)
                        receipt_time_str = receipt_time.strftime(
                            '%Y-%m-%d %H:%M:%S')

                        # 判断是否已经处理过了
                        msg = Message.query.filter_by(
                            eventUUID=eventUUID).first()

                        if not msg:
                            message = Message(eventUUID=eventUUID,
                                              node_id=node_id,
                                              receipt_time=receipt_time)
                            message.save()

                            # 发送微信消息
                            #
                            wechat_data = {
                                'first': {
                                    'value': "恭喜你按下成功!<( ̄︶ ̄)>",
                                    "color": "#173177"
                                },
                                'node_id': {
                                    'value': node_id,
                                    'color': '#173177'
                                },
                                'receipt_time': {
                                    'value': receipt_time_str,
                                    'color': '#173177'
                                },
                                'remark': {
                                    'value': remark,
                                    'color': '#173177'
                                },
                                'rssi': {
                                    'value': rssi,
                                    'color': '173177'
                                },
                                'gateway_sn': {
                                    'value':
                                    str(
                                        gateway_mac.index(
                                            event_gateway_id[-12:]) + 1),
                                    'color':
                                    '#173122'
                                },
                                'gateway_id': {
                                    'value': event_gateway_id,
                                    'color': '#173177'
                                }
                            }
                            send_wechat(node_id, wechat_data)
                            # 创建订单username
                            create_order(node_id)
                            # TODO:发送确认消息
                            # send_command(USERNAME, PASSWORD, GATEWAY_ID, node_id, '0XFF')

            time.sleep(interval)

    except Exception:
        detect_button_events()