|
@@ -53,6 +53,7 @@ Page({
|
|
|
fullOpen: false,
|
|
|
// 亮度
|
|
|
brightness: 50,
|
|
|
+ fan: 0,
|
|
|
// 色温
|
|
|
colorTemperature: MAX_COLOR_TEMPERATURE,
|
|
|
// 色彩
|
|
@@ -144,17 +145,17 @@ Page({
|
|
|
lamp: {...this.data.lamp, fullOpen: flag}
|
|
|
})
|
|
|
|
|
|
- let fn;
|
|
|
+
|
|
|
let nextMode = light_cmd.light_worker_mode.DEFAULT;
|
|
|
if(flag){
|
|
|
nextMode = light_cmd.light_worker_mode.FULL;
|
|
|
}
|
|
|
|
|
|
- fn = this.excuteBleSend(
|
|
|
+ let fn = this.excuteBleSend(
|
|
|
light_cmd.set_mode(nextMode)
|
|
|
);
|
|
|
console.log(fn);
|
|
|
- [_err,_res] = await handle(fn());
|
|
|
+ let [_err,_res] = await handle(fn);
|
|
|
if(_err){
|
|
|
return this.bleFail('发送失败', _err.errMsg);
|
|
|
}
|
|
@@ -218,6 +219,100 @@ Page({
|
|
|
}
|
|
|
console.log(`开关命令发送成功,开关:${this.data.lamp.switch}`);
|
|
|
},
|
|
|
+ registerLightCmdAction(){
|
|
|
+ // 灯光信息响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_QUERY, (data)=>{
|
|
|
+ console.log('灯光状态响应');
|
|
|
+ data = data.join('');
|
|
|
+ console.log(data);
|
|
|
+ // 灯光数据 开关状态 模式 亮度 色温 风扇速度
|
|
|
+ let switchState = data.slice(0, 2);
|
|
|
+ let mode = data.slice(2, 4);
|
|
|
+ // 亮度 占1位
|
|
|
+ let light = data.slice(4, 6);
|
|
|
+ // 色温 占2位
|
|
|
+ let temp = data.slice(6, 10);
|
|
|
+ // 风扇速度 占1位
|
|
|
+ let fan = data.slice(10, 12);
|
|
|
+ light = parseInt(light, 16);
|
|
|
+ temp = parseInt(temp, 16);
|
|
|
+ fan = parseInt(fan, 16);
|
|
|
+ switchState = switchState === '01';
|
|
|
+ let infoStr = `设备:${this.data.ble.deviceId} `
|
|
|
+ infoStr += `开关:${switchState ? '开':'关'} `
|
|
|
+ infoStr += `模式:${mode} `
|
|
|
+ infoStr += `色温:${temp} `
|
|
|
+ infoStr += `亮度:${light} `
|
|
|
+ infoStr += `风扇:${fan} `
|
|
|
+ console.log(infoStr);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp,
|
|
|
+ switch: switchState,
|
|
|
+ fullOpen: mode === light_cmd.light_worker_mode.FULL,
|
|
|
+ colorTemperature: temp,
|
|
|
+ brightness: light,
|
|
|
+ fan: fan
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ // 色温响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_TEMP, (data)=>{
|
|
|
+ console.log('色温调节响应');
|
|
|
+ data = data.join('');
|
|
|
+ console.log(data);
|
|
|
+ // hex转10进制
|
|
|
+ let temp = parseInt(data, 16);
|
|
|
+ console.log(temp);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, colorTemperature: temp}
|
|
|
+ })
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ // 亮度响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_LIGTH, (data)=>{
|
|
|
+ console.log('亮度调节响应');
|
|
|
+ data = data.join('');
|
|
|
+ // hex转10进制
|
|
|
+ let light = parseInt(data, 16);
|
|
|
+ console.log(light);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, brightness: light}
|
|
|
+ })
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ // 开灯响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_OPEN, (data)=>{
|
|
|
+ console.log('开灯调节响应');
|
|
|
+ data = data.join('');
|
|
|
+ console.log(data);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, switch: true}
|
|
|
+ });
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ // 关灯响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_CLOSE, (data)=>{
|
|
|
+ console.log('关灯调节响应');
|
|
|
+ data = data.join('');
|
|
|
+ console.log(data);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, switch: false}
|
|
|
+ });
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ // 模式切换响应
|
|
|
+ light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_MODE, (data)=>{
|
|
|
+ console.log('模式调节响应');
|
|
|
+ data = data.join('');
|
|
|
+ console.log(data);
|
|
|
+ let mode = parseInt(data, 16);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, fullOpen: mode === light_cmd.light_worker_mode.FULL}
|
|
|
+ });
|
|
|
+ this.reComputeBgColor();
|
|
|
+ });
|
|
|
+ },
|
|
|
|
|
|
// 计算背景色
|
|
|
reComputeBgColor(){
|
|
@@ -248,20 +343,6 @@ Page({
|
|
|
});
|
|
|
bleServices = [];
|
|
|
// 灯光信息初始化
|
|
|
- this.setData({
|
|
|
- lamp: {
|
|
|
- // 开关状态
|
|
|
- switch: false,
|
|
|
- // 全开状态(无色温调节)
|
|
|
- fullOpen: false,
|
|
|
- // 亮度
|
|
|
- brightness: 50,
|
|
|
- // 色温
|
|
|
- colorTemperature: MAX_COLOR_TEMPERATURE,
|
|
|
- // 色彩
|
|
|
- bgc: "#3b3b3b"
|
|
|
- }
|
|
|
- })
|
|
|
},
|
|
|
// 连接失败
|
|
|
bleFail(title,msg){
|
|
@@ -288,6 +369,7 @@ Page({
|
|
|
if(err){
|
|
|
return this.bleFail('断开连接失败', err.errMsg);
|
|
|
}
|
|
|
+ light_cmd.clearHandle();
|
|
|
wx.showToast({title: `断开连接成功`, })
|
|
|
this.initPageInfo();
|
|
|
},
|
|
@@ -357,14 +439,33 @@ Page({
|
|
|
});
|
|
|
// 设备服务管理
|
|
|
this.connectServices(dev.deviceId);
|
|
|
+ // 注册监听函数
|
|
|
+ ble.registerListenerHandle(this.onNotifyHandle);
|
|
|
+
|
|
|
+ },
|
|
|
+ onNotifyHandle(data){
|
|
|
+ console.log(`收到数据:${data}`);
|
|
|
+ // 解析数据
|
|
|
+ light_cmd.parseCmdData(data);
|
|
|
},
|
|
|
-
|
|
|
async connectServices(deviceId){
|
|
|
// uuid FF01
|
|
|
let [err,res] = await handle(ble.getBleServices(deviceId));
|
|
|
if(err){ return this.bleFail('获取服务失败', err.errMsg);}
|
|
|
// 保存服务信息;
|
|
|
bleServices = res;
|
|
|
+
|
|
|
+ // 注册灯光响应函数
|
|
|
+ this.registerLightCmdAction();
|
|
|
+ // 查询灯光状态
|
|
|
+ let fn = this.excuteBleSend(
|
|
|
+ light_cmd.query_light()
|
|
|
+ );
|
|
|
+ [_err,_res] = await handle(fn);
|
|
|
+ if(_err){
|
|
|
+ console.log(_err);
|
|
|
+ return this.bleFail('发送失败', _err.errMsg);
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
|