# server **Repository Path**: ohossmart-home-soulation/server ## Basic Information - **Project Name**: server - **Description**: server code - **Primary Language**: Unknown - **License**: AGPL-3.0 - **Default Branch**: sdkv102 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-09 - **Last Updated**: 2025-03-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # server code ## 接口 ### 在服务器上注册设备 在一个物联网设备连接到服务器的时候分两种情况 #### SLE 链路配网 设备连接入星闪网络后,服务器会记录连接conn_id但是此时设备并没有被注册为一个smarthome设备 smarhome设备需要主动向服务器发送注册指令,向服务器自我介绍,发送一个数据包,来描述设备的基本信息,设备需要向服务器发送格式如下的数据包: ```json { "PkgType":"reg", "Name":"light", "DeviceType":2, "Abilities":1, "DeviceId":0 } ``` 其中DeviceId是可选的 服务器收到注册包后会将设备保存到设备列表,但是还不能使用。服务器会向手机App和家庭智慧屏设备发送认证请求,通过后服务器会给设备分配设备id - SLE 链路设备检查连接状态 设备接入星闪并发送注册请求后,设备需要等待请求通过,通过后服务器会给设备分配id,id分配完成后会向设备发送一个包含deviceId的数据包 ```json {"PkgType":"Command","Command":"auth_reg","ID":122923,"DeviceId":1,"UniqueId":100} ``` 如果返回的DeviceId值为 0 表示连接未建立。设备应该记住DeviceId以方便后面的通信中使用 #### SLE 恢复配网 发送 ```json { "PkgType":"reg", "Name":"light", "DeviceType":2, "Abilities":1, "DeviceId":0, "UniqueId":0 } ``` 返回 ```json {"PkgType":"Command","Command":"restore_reg","ID":122923,"DeviceId":1,"UniqueId":100} ``` ### sle辅助udp配网 在设备连接 sle 链路后可以通过 sle 进行udp网络的辅助配网,设备可以发送一个增量配网包 ```json { "PkgType":"reg_add", "Protocol":"udp", "IP":"192.168.3.1", "DeviceId":1 } ``` 服务端收到设备后会在udp对应设备的8081端口发送udp数据包 ```json { "PkgType":"chk", "Command":"auth_add_udp", "Code":"q1w2e3r4", "ID":1, "DeviceId":1 } ``` sle 链路中需要应答并返回code ```json { "PkgType":"res", "Code":"q1w2e3r4", "ID":1, "ResType":3, // 这里用枚举PACKET_TYPE_SERVER_RES_ADD_REG_UDP_AUTH "DeviceId":1 } ``` 成功应答后服务器udp配网完成 ### sle 增量配网 在设备连接 sle 链路后可以重复注册 sle 设备 ```json { "PkgType":"reg_new", "Protocol":"sle", "Name":"light", "DeviceType":2, "Abilities":1, "FatherDeviceId":1, // 原来的deviceId "DeviceId":0, // 如果之前注册过可以用原来的id恢复状态 } ``` 服务器会发送 ```json {"PkgType":"Command","Command":"auth_new_reg","ID":122923,"DeviceId":1} ``` #### udp链路配网 设备与服务器连接到同一wifi下后,会主动弹出配网请求,根据设备提供的一串配网码(实际上就是ip地址),将配网码输入到服务器键盘上 服务器会根据配网码连接到对应的udp服务器,服务器会通过udp向设备发送一个验证消息 ```json { "PkgType":"chk", "Code":"q1w2e3r4", "ID":1, "DeviceId":1, "UniqueId":2 } ``` udp设备应该响应一个数据包来表示其工作正常 ```json { "PkgType":"res", "Code":"q1w2e3r4", "ID":1, "ResType":3 //PACKET_TYPE_SERVER_RES_ADD_UDP_DEVICE } ``` #### udp 链路配网恢复 发送 ```json { "PkgType":"restore_chk", "Code":"q1w2e3r4", "ID":1, } ``` udp设备应该响应一个数据包来表示其工作正常 ```json { "PkgType":"res", "Code":"q1w2e3r4", "ID":1, "ResType":8, //PACKET_TYPE_SERVER_RES_RESTORE_UDP_DEVICE "DeviceId":1, "UniqueId":2 } ``` ### 获取设备列表 - 链路:udp 请求包 ```json { "PkgType":"Command", "ID":1, "DeviceId":1, "Command":"Query", "Param":"DeviceList" } ``` 返回 ```json {  "PkgType":"res", "ID":1, "ResType":5, //PACKET_TYPE_SMARTSCREEN_QUERY_DEVICELIST "DeviceId":1, "Data": [ { "Name":"light_1", "Type":3, "Abilities":1, "DeviceId":1 } ] } ``` ### 新设备注册流程之智慧屏验证 当有新设备向服务器注册的时候服务器会向授权的智慧屏发送通知 ```json { "PkgType": "req", "Msg": "DeviceAddAuth", "DeviceUniqueId": 2, "ID": 999, "DeviceName": "SmartLight", "DeviceType": 3, "Abilities": 1 } ``` 智慧屏收到通知后向用户询问连接意见,并反馈 ```json { "PkgType":"res", "ID":1, "ResType":6, //PACKET_TYPE_SERVER_RES_AUTH_REQUEST_NEW_DEVICE "DeviceId":1, // 智慧屏的设备id "Msg":"Accepted", // "Accepted"/"Rejected" "DeviceUniqueId": 2, } ``` ### 开灯 - 发送 ```json { "PkgType":"Command", "ID":1, "DeviceId":1, "Command":"light_control", "To":1, //对象deviceid "Status":1, } ``` ### 查询灯状态 - 发送 ```json { "PkgType":"Command", "Command":"Query", "ID": 1234, "param":"status", //on /off /reverse "QueryDeviceId":1, // 查询 "DeviceId":1 } ``` - 接收 ```json { "PkgType":"res", "ID":1, "ResType":4, //PACKET_TYPE_SERVER_RES_QUERY_LIGHT_STATUS "DeviceId":1, // 对应设备的deviceid "Status":1, // on/off "Brightness": 0, // 如果设备没有对应能力视为无效值(switchligh的数据没有这个字段) "Color":[1,1,1], "Motion":0, // 0:无 1:有人 } ``` ### 推送 - 灯状态发生改变会主动向智慧屏推送 ```json { "PkgType":"event", "EventType":1, // "DeviceId":1, // 事件影响的设备id "Status": 1, // 1:on 0:off "Brightness": 0, // 如果设备没有对应能力视为无效值(switchligh的数据没有这个字段) "Color":[1,1,1], "Motion":0, // 0:无 1:有人 } ``` - 传感器状态发生改变会主动向智慧屏推送 ```json { "PkgType": "event", "EventType": 1, "DeviceId": 1234, "T": 25.5, "H": 60.0, "S": 1013.25, "C": 400 } ``` - 烟雾传感器报警会主动向智慧屏推送 ```json { "PkgType": "event", "EventType": 2, "DeviceId": 1234, "S": 1013.25, } ``` ### 传感器设置刷新时间 ```json { "PkgType":"Command", "Command":"setReportTime", "Param":2000, "To":1, // 目标设备id "DeviceId":1, } ``` ### 传感器设置报警阈值 ```json { "PkgType":"Command", "Command":"setAlertLevel", "Param":2000 , "To":1, // 目标设备id "DeviceId":1, } ``` ## 设备类型 |编号|类型| |---|---| |1|服务器| |2|智慧屏| |3|照明灯|