main
commit
e725ebe87e
@ -0,0 +1,40 @@
|
||||
from bleak import BleakScanner, BleakClient
|
||||
import asyncio
|
||||
|
||||
async def get_services(mac_address: str):
|
||||
async with BleakClient(mac_address) as client:
|
||||
svcs = client.services
|
||||
return svcs
|
||||
|
||||
async def find_and_get_services(device_name: str):
|
||||
# 发现所有设备
|
||||
devices = await BleakScanner.discover()
|
||||
|
||||
# 遍历设备列表,查找目标设备
|
||||
for device in devices:
|
||||
if device.name == device_name:
|
||||
print(f"Found device: {device.name}, MAC Address: {device.address}")
|
||||
|
||||
# 获取服务
|
||||
services = await get_services(device.address)
|
||||
if services:
|
||||
last_service = services.services[40]
|
||||
return {
|
||||
"device_name": device.name,
|
||||
"mac_address": device.address,
|
||||
"last_service_uuid": last_service.uuid,
|
||||
}
|
||||
else:
|
||||
print("No services found for the device.")
|
||||
return None
|
||||
|
||||
# 如果没有找到设备
|
||||
print(f"Device with name '{device_name}' not found.")
|
||||
return None
|
||||
|
||||
# 设备名称
|
||||
device_name = "scent08d1f90efa9a"
|
||||
|
||||
# 调用函数并获取返回值
|
||||
result = asyncio.run(find_and_get_services(device_name))
|
||||
print(result)
|
@ -0,0 +1,55 @@
|
||||
'''
|
||||
# Filename: mqtt_sub.py
|
||||
# Author: Cai Gui Lin
|
||||
# Created on: 2024-09-2
|
||||
# Modified on: 2024-09-2
|
||||
# Version: 2.0
|
||||
# Copyright (c) 2024 Cai Gui Lin
|
||||
# License: MIT
|
||||
# Description: mqtt subscriber
|
||||
# Contact: 18166451309@163.com
|
||||
|
||||
'''
|
||||
import random
|
||||
from paho.mqtt import client as mqtt_client
|
||||
import re
|
||||
client_id = f'python-mqtt-{random.randint(0, 100)}'
|
||||
def connect_mqtt(username, password, broker, port) -> mqtt_client:
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
if rc == 0:
|
||||
print("Mqtt ok, Ready to subscribe.")
|
||||
else:
|
||||
print("Failed to connect mqtt, return code %d\n", rc)
|
||||
|
||||
client = mqtt_client.Client(client_id)
|
||||
client.username_pw_set(username=username,password=password)
|
||||
client.on_connect = on_connect
|
||||
client.connect(broker, port)
|
||||
return client
|
||||
|
||||
|
||||
def subscribe(queue, client, sub_topic):
|
||||
queue = queue
|
||||
def on_message(client, userdata, msg):
|
||||
# 解码消息负载为字符串
|
||||
payload_str = msg.payload.decode()
|
||||
# pattern = r'"channelId": (\d+)|"time": (\d+)'
|
||||
# matches = re.findall(pattern, payload_str)
|
||||
# print(payload_str)
|
||||
queue.put(payload_str)
|
||||
client.subscribe(sub_topic)
|
||||
client.on_message = on_message
|
||||
|
||||
def sub_run(queue, username, password, broker, port, sub_topic):
|
||||
client = connect_mqtt(username, password, broker, port)
|
||||
subscribe(queue, client, sub_topic)
|
||||
client.loop_forever()
|
||||
|
||||
if __name__ == '__main__':
|
||||
username = "stark"
|
||||
password = "12345678"
|
||||
broker = "47.120.70.16"
|
||||
port = 1883
|
||||
sub_topic = "/smell/cmd"
|
||||
sub_run(username, password, broker, port, sub_topic)
|
||||
|
Loading…
Reference in New Issue