未提供项目说明
crownstone-sse的Python项目详细描述
皇冠石Python图书馆
侦听Crownstone SSE事件的异步Python库。在
功能
- 异步:使用asyncio和aiohttp,针对速度进行了优化。在
- 易于使用:只需将您的crownstone电子邮件和密码传递给构造函数,然后启动客户端!在
- 完整:完全集成的事件总线,可用于侦听事件和运行回调。在
- 独立:客户端运行在一个单独的线程中,你的主线程不会被阻塞!在
要求
- Python 3.7或更高版本
- Aiohttp 3.6.2
标准安装
cd到项目文件夹并运行:
$ python setup.py install
在虚拟环境中安装
要安装库,请执行以下命令:
^{pr2}$使用以下方法激活venv:
$source venv/bin/activate
一旦激活,venv将用于执行python文件,并将在venv中安装库。
要安装此库,请cd到项目文件夹并运行:
$ python setup.py install
入门
示例
fromcrownstone_sse.clientimportCrownstoneSSEfromcrownstone_sse.events.switch_state_update_eventimportSwitchStateUpdateEventfromcrownstone_sse.events.system_eventimportSystemEventfromcrownstone_sse.events.presence_eventimportPresenceEventfromcrownstone_sse.events.ability_change_eventimportAbilityChangeEventfromcrownstone_sse.events.data_change_eventimportDataChangeEventfromcrownstone_sse.constimport(EVENT_SYSTEM_STREAM_START,EVENT_SWITCH_STATE_UPDATE,EVENT_PRESENCE_ENTER_LOCATION,EVENT_ABILITY_CHANGE_DIMMING,EVENT_DATA_CHANGE_CROWNSTONE,OPERATION_CREATE,OPERATION_DELETE,OPERATION_UPDATE)importloggingimporttime# enable logginglogging.basicConfig(format='%(levelname)s :%(message)s',level=logging.DEBUG)defswitch_update(event:SwitchStateUpdateEvent):print("Crownstone {} switch state changed to {}".format(event.cloud_id,event.switch_state))defnotify_stream_start(event:SystemEvent):print(event.message)defnotify_presence_changed(event:PresenceEvent):print("User {} has entered location {}".format(event.user_id,event.location_id))defnotify_ability_changed(event:AbilityChangeEvent):print("Ability {} changed to {}".format(event.ability_type,event.ability_enabled))defnotify_data_changed(event:DataChangeEvent):ifevent.operation==OPERATION_CREATE:print("New data is available: {}".format(event.changed_item_name))ifevent.operation==OPERATION_UPDATE:print("Name of id {} has been updated to {}".format(event.changed_item_id,event.changed_item_name))ifevent.operation==OPERATION_DELETE:print("Data {} has been deleted".format(event.changed_item_name))# Create a sse client instance. Pass your crownstone account information.# email and password are required for logging in again when an access token has expired.sse_client=CrownstoneSSE('email','password')# for usage with existing access token you can use this function:# sse_client.set_access_token('myAccessToken')# Start running the clientsse_client.start()# Add listeners for event types of your liking, and the desired callback to be executed. see above.sse_client.add_event_listener(EVENT_SYSTEM_STREAM_START,notify_stream_start)sse_client.add_event_listener(EVENT_SWITCH_STATE_UPDATE,switch_update)sse_client.add_event_listener(EVENT_PRESENCE_ENTER_LOCATION,notify_presence_changed)sse_client.add_event_listener(EVENT_ABILITY_CHANGE_DIMMING,notify_ability_changed)sse_client.add_event_listener(EVENT_DATA_CHANGE_CROWNSTONE,notify_data_changed)# block for 120 seconds (let the client run for 120 second before stopping)time.sleep(120)# stop the clientsse_client.stop()
创建回调
回调是每次特定事件类型的事件发生时都将执行的函数。
回调函数是标准函数,而不是协同程序!
回调的标准格式是:
defcallback(event:EventTypeClass):# do something
每个事件都有自己的字段。建议提供事件类型类提示,以便更好地跟踪它是哪个事件,以及它有哪些字段。
例如:
defcallback(event:PresenceEvent):print(event.user_id)print(event.location_id)
事件类型
目前,有6种不同的事件类型:
- 系统事件
- 命令事件
- 数据更改事件
- 出席活动
- 开关状态更新事件
- 能力变更事件
系统事件
系统事件表示为:
- 代码
- 消息
开关命令事件
开关命令事件表示为:
球体
- 球体编号
皇冠石
- 云逯id
- 唯一的\u id
- 开关值(作为SwitchCommandValue)
多命令切换
用多个命令表示:
球体
- 球体编号
皇冠石列表
- crownstone_列表
列表中的每个皇冠石:
- 云逯id
- 唯一的\u id
- 开关值(作为SwitchCommandValue)
数据更改事件
数据更改事件表示为:
- 操作(更新|删除|创建)
球体
- 球体编号
项目
- 更改了项目编号
- 更改了项目名称
存在事件
存在事件表示为:
球体
- 球体编号
位置
- 位置代号
用户
- 用户?id
开关状态更新事件
开关状态更新事件表示为:
球体
- 球体编号
皇冠石
- 云逯id
- 唯一的\u id
- 开关状态(百分比)
能力变化事件
能力更改事件表示为:
球体
- 球体编号
皇冠石
- 云逯id
- 唯一的\u id
能力
- 能力类型
- 启用能力
- 能力同步到王冠石
测试
- 能力类型
- 启用能力
- 能力同步到王冠石
测试
要使用tox运行测试,请先运行install tox:
$ pip install tox
要将测试cd执行到项目文件夹并运行:
$ tox
要查看测试覆盖了代码的哪些部分,测试成功后会生成覆盖率报告。
要查看覆盖率报告的运行:
$ coverage report
如果您想更好地了解测试,可以生成如下HTML文件:
$ coverage html
查看html文件Linux上的ctly:
$ ./htmlcov/index.html
在Windows上,只需导航到项目文件夹中的htmlcov文件夹,然后双击索引.html. 它将在您选择的浏览器中执行。在
- 项目
标签: