コード例 #1
0
ファイル: sensor_beacon.py プロジェクト: zyf19930419/Horus
    def __init__(self, bt_address_s, sensor_type_s, gateway_s, pkt):
        self.bt_address = bt_address_s
        self.seq_num = str_util.c2B(pkt[7])

        self.val_temp = str_util.bytes2short(str_util.c2B(pkt[9]),
                                             str_util.c2B(pkt[8])) / 100.0
        self.val_humi = str_util.bytes2ushort(str_util.c2B(pkt[11]),
                                              str_util.c2B(pkt[10])) / 100.0
        self.val_light = str_util.bytes2ushort(str_util.c2B(pkt[13]),
                                               str_util.c2B(pkt[12]))
        self.val_uv = str_util.bytes2ushort(str_util.c2B(pkt[15]),
                                            str_util.c2B(pkt[14])) / 100.0
        self.val_pressure = str_util.bytes2ushort(str_util.c2B(pkt[17]),
                                                  str_util.c2B(pkt[16])) / 10.0
        self.val_noise = str_util.bytes2ushort(str_util.c2B(pkt[19]),
                                               str_util.c2B(pkt[18])) / 100.0
        self.val_battery = (str_util.c2B(pkt[26]) + 100) * 10.0

        if sensor_type_s == "IM":
            self.val_ax = str_util.bytes2short(str_util.c2B(pkt[21]),
                                               str_util.c2B(pkt[20])) / 10.0
            self.val_ay = str_util.bytes2short(str_util.c2B(pkt[23]),
                                               str_util.c2B(pkt[22])) / 10.0
            self.val_az = str_util.bytes2short(str_util.c2B(pkt[25]),
                                               str_util.c2B(pkt[24])) / 10.0
            self.val_di = 0.0
            self.val_heat = 0.0
            self.calc_factor()
        elif sensor_type_s == "EP":
            self.val_ax = 0.0
            self.val_ay = 0.0
            self.val_az = 0.0
            self.val_di = str_util.bytes2short(str_util.c2B(pkt[21]),
                                               str_util.c2B(pkt[20])) / 100.0
            self.val_heat = str_util.bytes2short(str_util.c2B(pkt[23]),
                                                 str_util.c2B(pkt[22])) / 100.0
        else:
            self.val_ax = 0.0
            self.val_ay = 0.0
            self.val_az = 0.0
            self.val_di = 0.0
            self.val_heat = 0.0
            self.calc_factor()

        self.rssi = str_util.c2b(pkt[-1])
        self.distance = self.return_accuracy(self.rssi,
                                             ble.BEACON_MEASURED_POWER)

        self.tick_register = time.strftime('%Y%m%d%H%M%S',
                                           time.localtime(time.time()))
        self.tick_last_update = datetime.datetime.now()
        self.flag_active = True

        self.sensor_type = sensor_type_s
        self.gateway = gateway_s
コード例 #2
0
    def __init__(self, bt_address_s, serial, sensor_type_s, gateway_s, pkt):
        self.bt_address = bt_address_s

        if serial:
            self.serial = serial

        if ((sensor_type_s == "IM") or (sensor_type_s == "EP")):
            self.seq_num = str_util.c2B(pkt[7])

            self.val_temp = str_util.bytes2short(str_util.c2B(pkt[9]),
                                                 str_util.c2B(pkt[8])) / 100.0
            self.val_humi = str_util.bytes2ushort(str_util.c2B(
                pkt[11]), str_util.c2B(pkt[10])) / 100.0
            self.val_light = str_util.bytes2ushort(str_util.c2B(pkt[13]),
                                                   str_util.c2B(pkt[12]))
            self.val_uv = str_util.bytes2ushort(str_util.c2B(pkt[15]),
                                                str_util.c2B(pkt[14])) / 100.0
            self.val_pressure = str_util.bytes2ushort(str_util.c2B(
                pkt[17]), str_util.c2B(pkt[16])) / 10.0
            self.val_noise = str_util.bytes2ushort(str_util.c2B(
                pkt[19]), str_util.c2B(pkt[18])) / 100.0
            self.val_battery = (str_util.c2B(pkt[26]) + 100) * 10.0

            if sensor_type_s == "IM":
                self.val_ax = str_util.bytes2short(str_util.c2B(
                    pkt[21]), str_util.c2B(pkt[20])) / 10.0
                self.val_ay = str_util.bytes2short(str_util.c2B(
                    pkt[23]), str_util.c2B(pkt[22])) / 10.0
                self.val_az = str_util.bytes2short(str_util.c2B(
                    pkt[25]), str_util.c2B(pkt[24])) / 10.0
                self.val_di = 0.0
                self.val_heat = 0.0
                self.calc_factor()
            elif sensor_type_s == "EP":
                self.val_ax = 0.0
                self.val_ay = 0.0
                self.val_az = 0.0
                self.val_di = str_util.bytes2short(str_util.c2B(
                    pkt[21]), str_util.c2B(pkt[20])) / 100.0
                self.val_heat = str_util.bytes2short(str_util.c2B(
                    pkt[23]), str_util.c2B(pkt[22])) / 100.0
            else:
                self.val_ax = 0.0
                self.val_ay = 0.0
                self.val_az = 0.0
                self.val_di = 0.0
                self.val_heat = 0.0
                self.calc_factor()
        else:  # Rbt
            self.seq_num = str_util.c2B(pkt[8])
            if (sensor_type_s == "Rbt 0x01"):
                self.val_temp = str_util.bytes2short(str_util.c2B(
                    pkt[10]), str_util.c2B(pkt[9])) / 100.0
                self.val_humi = str_util.bytes2ushort(str_util.c2B(
                    pkt[12]), str_util.c2B(pkt[11])) / 100.0
                self.val_light = str_util.bytes2ushort(str_util.c2B(pkt[14]),
                                                       str_util.c2B(pkt[13]))
                self.val_pressure = str_util.bytes2uint32(
                    str_util.c2B(pkt[18]), str_util.c2B(pkt[17]),
                    str_util.c2B(pkt[16]), str_util.c2B(pkt[15])) / 1000.0
                self.val_noise = str_util.bytes2ushort(str_util.c2B(
                    pkt[20]), str_util.c2B(pkt[19])) / 100.0
                self.val_etvoc = str_util.bytes2ushort(str_util.c2B(pkt[22]),
                                                       str_util.c2B(pkt[21]))
                self.val_eco2 = str_util.bytes2ushort(str_util.c2B(pkt[24]),
                                                      str_util.c2B(pkt[23]))
                self.calc_factor()
            elif (sensor_type_s == "Rbt 0x02"):
                self.val_di = str_util.bytes2short(str_util.c2B(
                    pkt[10]), str_util.c2B(pkt[9])) / 100.0
                self.val_heat = str_util.bytes2short(str_util.c2B(
                    pkt[12]), str_util.c2B(pkt[11])) / 100.0
                self.val_si = str_util.bytes2ushort(str_util.c2B(
                    pkt[15]), str_util.c2B(pkt[14])) / 10.0
                self.val_pga = str_util.bytes2ushort(str_util.c2B(
                    pkt[17]), str_util.c2B(pkt[16])) / 10.0
                self.val_seismic = str_util.bytes2ushort(
                    str_util.c2B(pkt[19]), str_util.c2B(pkt[18])) / 1000.0
                self.val_ax = str_util.bytes2short(str_util.c2B(
                    pkt[21]), str_util.c2B(pkt[20])) / 10.0
                self.val_ay = str_util.bytes2short(str_util.c2B(
                    pkt[23]), str_util.c2B(pkt[22])) / 10.0
                self.val_az = str_util.bytes2short(str_util.c2B(
                    pkt[25]), str_util.c2B(pkt[24])) / 10.0
                if (str_util.c2B(pkt[13]) == 0x00):
                    self.vibinfo = "NONE"
                elif (str_util.c2B(pkt[13]) == 0x01):
                    self.vibinfo = "VIBRATION"
                elif (str_util.c2B(pkt[13]) == 0x02):
                    self.vibinfo = "EARTHQUAKE"
                else:
                    pass

        self.rssi = str_util.c2b(pkt[-1])
        self.distance = self.return_accuracy(self.rssi,
                                             ble.BEACON_MEASURED_POWER)

        self.tick_register = datetime.datetime.now()
        self.tick_last_update = self.tick_register
        self.flag_active = True

        self.sensor_type = sensor_type_s
        self.gateway = gateway_s
コード例 #3
0
def parse_events(sock, loop_count=10):
    global sensor_list

    pkt = sock.recv(255)

    # Raw avertise packet data from Bluez scan
    # Packet Type (1byte) + BT Event ID (1byte) + Packet Length (1byte) +
    # BLE sub-Event ID (1byte) + Number of Advertising reports (1byte) +
    # Report type ID (1byte) + BT Address Type (1byte) + BT Address (6byte) +
    # Data Length (1byte) + Data ((Data Length)byte) + RSSI (1byte)
    #
    # Packet Type = 0x04
    # BT Event ID = EVT_LE_META_EVENT = 0x3E (BLE events)
    # (All LE commands result in a metaevent, specified by BLE sub-Event ID)
    # BLE sub-Event ID = {
    #                       EVT_LE_CONN_COMPLETE = 0x01
    #                       EVT_LE_ADVERTISING_REPORT = 0x02
    #                       EVT_LE_CONN_UPDATE_COMPLETE = 0x03
    #                       EVT_LE_READ_REMOTE_USED_FEATURES_COMPLETE = 0x04
    #                       EVT_LE_LTK_REQUEST = 0x05
    #                     }
    # Number of Advertising reports = 0x01 (normally)
    # Report type ID = {
    #                       LE_ADV_IND = 0x00
    #                       LE_ADV_DIRECT_IND = 0x01
    #                       LE_ADV_SCAN_IND = 0x02
    #                       LE_ADV_NONCONN_IND = 0x03
    #                       LE_ADV_SCAN_RSP = 0x04
    #                   }
    # BT Address Type = {
    #                       LE_PUBLIC_ADDRESS = 0x00
    #                       LE_RANDOM_ADDRESS = 0x01
    #                    }
    # Data Length = 0x00 - 0x1F
    # * Maximum Data Length of an advertising packet = 0x1F

    parsed_packet = ble.hci_le_parse_response_packet(pkt)
    timestamp = datetime.datetime.now(tz=JST())

    if "bluetooth_le_subevent_name" in parsed_packet and \
            (parsed_packet["bluetooth_le_subevent_name"]
                == 'EVT_LE_ADVERTISING_REPORT'):

        if debug:
            for report in parsed_packet["advertising_reports"]:
                print "----------------------------------------------------"
                print "Found BLE device:", report['peer_bluetooth_address']
                print "Raw Advertising Packet:"
                print ble.packet_as_hex_string(pkt, flag_with_spacing=True,
                                               flag_force_capitalize=True)
                print ""
                for k, v in report.items():
                    if k == "payload_binary":
                        continue
                    print "\t%s: %s" % (k, v)
                print ""

        for report in parsed_packet["advertising_reports"]:
            if (ble.verify_beacon_packet(report)):
                rssi = str_util.c2b(report["payload_binary"][-1])
                serial = None
                if rssi > -90:
                    serial = get_serial(report["peer_bluetooth_address_s"])
                sensor = envsensor.SensorBeacon(
                    report["peer_bluetooth_address_s"],
                    serial,
                    ble.classify_beacon_packet(report),
                    GATEWAY,
                    report["payload_binary"])

                index = find_sensor_in_list(sensor, sensor_list)

                if debug:
                    print ("\t--- sensor data ---")
                    sensor.debug_print()
                    print ""

                lock = threading.Lock()
                lock.acquire()

                if (index != -1):  # BT Address found in sensor_list
                    if sensor.check_diff_seq_num(sensor_list[index]):
                        handling_data(sensor, timestamp)
                    sensor.update(sensor_list[index])
                else:  # new SensorBeacon
                    sensor_list.append(sensor)
                    handling_data(sensor, timestamp)
                lock.release()
            else:
                pass
    else:
        pass
    return