|
@@ -7,7 +7,7 @@ import light_cmd from '../../utils/light_cmd.js'
|
|
|
import { _curry } from '../../utils/mjs_curry.js'
|
|
|
import BLE from '../../utils/mjs_wxble.js'
|
|
|
console.log(BLE);
|
|
|
-const ble = new BLE();
|
|
|
+const ble = new BLE(null, ["bleLight"]);
|
|
|
const tmpDevs = [
|
|
|
{
|
|
|
id: 1,
|
|
@@ -61,7 +61,8 @@ Page({
|
|
|
bleDevs: tmpDevs,
|
|
|
},
|
|
|
// 获取蓝牙发送数据对象
|
|
|
- getBleSendHandle(){
|
|
|
+ excuteBleSend(data){
|
|
|
+ console.log('获取蓝牙发送数据对象');
|
|
|
// 固定值
|
|
|
let controlUuid = bleInfo.controlUuid;
|
|
|
let characteristicFirstUuid = bleInfo.characteristicFirstUuid;
|
|
@@ -69,21 +70,26 @@ Page({
|
|
|
let deviceId = this.data.ble.deviceId;
|
|
|
let serverUuid = '';
|
|
|
let characteristicUuid = '';
|
|
|
-
|
|
|
- let server = bleServices.find(s=>s.uuid === controlUuid);
|
|
|
+ let characteristic;
|
|
|
+ let server = bleServices.find(s=>{
|
|
|
+ console.log(`服务uuid ${s.uuid}`);
|
|
|
+ console.log(s);
|
|
|
+ return s.uuid.startsWith(controlUuid);
|
|
|
+ });
|
|
|
if(!server)
|
|
|
{
|
|
|
- // return bleFail('获取服务失败', '该设备不支持该程序');
|
|
|
serverUuid = bleInfo.controlUuid;
|
|
|
}else
|
|
|
{
|
|
|
serverUuid = server.uuid;
|
|
|
+ characteristic = server.characteristics.find(c=>
|
|
|
+ c.uuid.startsWith(characteristicFirstUuid)
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
//获取
|
|
|
- let characteristic = server.characteristics.find(c=>
|
|
|
- c.uuid.startsWith(characteristicFirstUuid)
|
|
|
- );
|
|
|
+
|
|
|
+
|
|
|
if(!characteristic)
|
|
|
{
|
|
|
// return bleFail('获取服务失败', '该设备不支持该程序');
|
|
@@ -93,8 +99,9 @@ Page({
|
|
|
{
|
|
|
characteristicUuid = characteristic.uuid;
|
|
|
}
|
|
|
+ console.log(`服务uuid ${serverUuid} 特征uuid ${characteristicUuid}`);
|
|
|
|
|
|
- return _curry(ble.sendData, deviceId, serverUuid, characteristicUuid);
|
|
|
+ return ble.sendData(deviceId, serverUuid, characteristicUuid, data);
|
|
|
},
|
|
|
// 亮度调节
|
|
|
async brightnessChangeHandle(e){
|
|
@@ -104,72 +111,84 @@ Page({
|
|
|
lamp: {...this.data.lamp, brightness: e.detail.value}
|
|
|
})
|
|
|
this.reComputeBgColor();
|
|
|
- let fn = light_cmd.set_light(
|
|
|
- this.getBleSendHandle(),
|
|
|
- this.data.lamp.brightness
|
|
|
- );
|
|
|
+
|
|
|
+ let fn = this.excuteBleSend(
|
|
|
+ light_cmd.set_light(this.data.lamp.brightness)
|
|
|
+ );
|
|
|
let [_err,_res] = await handle(fn);
|
|
|
if(_err){
|
|
|
+ console.log(_err);
|
|
|
return this.bleFail('发送失败', _err.errMsg);
|
|
|
}
|
|
|
console.log(`亮度调节命令发送成功,亮度:${this.data.lamp.brightness}`);
|
|
|
},
|
|
|
- async colorChangeHandle(e){
|
|
|
- console.log("色温改变");
|
|
|
- console.log(e);
|
|
|
- this.setData({
|
|
|
- lamp: {...this.data.lamp, colorTemperature: e.detail.value}
|
|
|
- })
|
|
|
- this.reComputeBgColor();
|
|
|
+ async colorChangeHandle(e){
|
|
|
+ console.log("色温改变");
|
|
|
+ console.log(e);
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, colorTemperature: e.detail.value}
|
|
|
+ })
|
|
|
+ this.reComputeBgColor();
|
|
|
|
|
|
- let fn = light_cmd.set_temp(
|
|
|
- this.getBleSendHandle(),
|
|
|
- this.data.lamp.colorTemperature
|
|
|
- );
|
|
|
- let [_err,_res] = await handle(fn);
|
|
|
- if(_err){
|
|
|
- return this.bleFail('发送失败', _err.errMsg);
|
|
|
- }
|
|
|
- console.log(`亮度调节命令发送成功,亮度:${this.data.lamp.colorTemperature}`);
|
|
|
- },
|
|
|
- // 计算背景色
|
|
|
- reComputeBgColor(){
|
|
|
- let newBgC;
|
|
|
- let temp = this.data.lamp.colorTemperature;
|
|
|
- let light = this.data.lamp.brightness;
|
|
|
- if(this.data.lamp.fullOpen){
|
|
|
- temp = MAX_COLOR_TEMPERATURE / 2;
|
|
|
- }
|
|
|
- if(this.data.lamp.switch){
|
|
|
- newBgC = calculateColor(temp, light);
|
|
|
- }else{
|
|
|
- newBgC = "#3b3b3b";
|
|
|
- }
|
|
|
- this.setData({
|
|
|
- lamp: {...this.data.lamp, bgc: newBgC}
|
|
|
- })
|
|
|
- },
|
|
|
- async fullModeSwitchHandle(e){
|
|
|
- // 全开模式切换
|
|
|
- console.log("全开模式切换");
|
|
|
- this.setData({
|
|
|
- lamp: {...this.data.lamp, fullOpen: !this.data.lamp.fullOpen}
|
|
|
- })
|
|
|
+ let fn = this.excuteBleSend(
|
|
|
+ light_cmd.set_temp(this.data.lamp.colorTemperature)
|
|
|
+ );
|
|
|
+ let [_err,_res] = await handle(fn);
|
|
|
+ if(_err){
|
|
|
+ return this.bleFail('发送失败', _err.errMsg);
|
|
|
+ }
|
|
|
+ console.log(`亮度调节命令发送成功,亮度:${this.data.lamp.colorTemperature}`);
|
|
|
+ },
|
|
|
+ async executeFullModeSwitch(flag){
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, fullOpen: flag}
|
|
|
+ })
|
|
|
|
|
|
- let fn;
|
|
|
- let nextMode = light_cmd.light_worker_mode.DEFAULT;
|
|
|
- if(this.data.lamp.fullOpen){
|
|
|
- nextMode = light_cmd.light_worker_mode.FULL;
|
|
|
- }
|
|
|
+ let fn;
|
|
|
+ let nextMode = light_cmd.light_worker_mode.DEFAULT;
|
|
|
+ if(flag){
|
|
|
+ nextMode = light_cmd.light_worker_mode.FULL;
|
|
|
+ }
|
|
|
|
|
|
- fn = light_cmd.set_mode(this.getBleSendHandle(), nextMode);
|
|
|
- let [_err,_res] = await handle(fn);
|
|
|
- if(_err){
|
|
|
- return this.bleFail('发送失败', _err.errMsg);
|
|
|
- }
|
|
|
- console.log(`工作模式切换成功:${this.data.lamp.fullOpen}`);
|
|
|
- },
|
|
|
- async switchHandle(e){
|
|
|
+ fn = this.excuteBleSend(
|
|
|
+ light_cmd.set_mode(nextMode)
|
|
|
+ );
|
|
|
+ console.log(fn);
|
|
|
+ [_err,_res] = await handle(fn());
|
|
|
+ if(_err){
|
|
|
+ return this.bleFail('发送失败', _err.errMsg);
|
|
|
+ }
|
|
|
+ console.log(`工作模式切换成功:${this.data.lamp.fullOpen}`);
|
|
|
+ },
|
|
|
+ async fullModeSwitchHandle(e){
|
|
|
+ // 全开模式切换
|
|
|
+ console.log("全开模式切换");
|
|
|
+ let _err,_res;
|
|
|
+ let flag = !this.data.lamp.fullOpen;
|
|
|
+ if(!this.data.lamp.switch){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (flag) {
|
|
|
+ // 弹窗询问是否需要启动全开模式
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '是否启动全开模式',
|
|
|
+ success: (res) => {
|
|
|
+ if (res.confirm) {
|
|
|
+ this.executeFullModeSwitch(flag);
|
|
|
+ } else if (res.cancel) {
|
|
|
+ console.log('用户点击取消')
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ this.executeFullModeSwitch(flag);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ async switchHandle(e){
|
|
|
+ let _err,_res;
|
|
|
// 开关切换
|
|
|
this.setData({
|
|
|
lamp: {...this.data.lamp, switch: !this.data.lamp.switch}
|
|
@@ -177,37 +196,64 @@ Page({
|
|
|
this.setData({
|
|
|
lamp: {...this.data.lamp, fullOpen: false}
|
|
|
})
|
|
|
+
|
|
|
this.reComputeBgColor();
|
|
|
|
|
|
+ console.log(`开关切换:${this.data.lamp.switch}`);
|
|
|
let fn;
|
|
|
if(this.data.lamp.switch){
|
|
|
- fn = light_cmd.open_light(this.getBleSendHandle());
|
|
|
+ fn = this.excuteBleSend(
|
|
|
+ light_cmd.open_light()
|
|
|
+ );
|
|
|
}else{
|
|
|
- fn = light_cmd.close_light(this.getBleSendHandle());
|
|
|
+ fn = this.excuteBleSend(
|
|
|
+ light_cmd.close_light()
|
|
|
+ );
|
|
|
}
|
|
|
- let [_err,_res] = await handle(fn);
|
|
|
+
|
|
|
+ [_err,_res] = await handle(fn);
|
|
|
if(_err){
|
|
|
+ console.log(_err);
|
|
|
return this.bleFail('发送失败', _err.errMsg);
|
|
|
}
|
|
|
console.log(`开关命令发送成功,开关:${this.data.lamp.switch}`);
|
|
|
},
|
|
|
+
|
|
|
+ // 计算背景色
|
|
|
+ reComputeBgColor(){
|
|
|
+ let newBgC;
|
|
|
+ let temp = this.data.lamp.colorTemperature;
|
|
|
+ let light = this.data.lamp.brightness;
|
|
|
+ if(this.data.lamp.fullOpen){
|
|
|
+ temp = MAX_COLOR_TEMPERATURE / 2;
|
|
|
+ }
|
|
|
+ if(this.data.lamp.switch){
|
|
|
+ newBgC = calculateColor(temp, light);
|
|
|
+ }else{
|
|
|
+ newBgC = "#3b3b3b";
|
|
|
+ }
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, bgc: newBgC}
|
|
|
+ })
|
|
|
+ },
|
|
|
|
|
|
bleFail(title,msg){
|
|
|
- this.setData({
|
|
|
- ble: {...this.data.ble,
|
|
|
- state: connectStateTypes.unConnect,
|
|
|
- init: false,
|
|
|
- devName: "",
|
|
|
- deviceId: "",
|
|
|
- },
|
|
|
- bleDevs: []
|
|
|
- });
|
|
|
- bleServices = [];
|
|
|
wx.showModal({
|
|
|
title: title,
|
|
|
content: msg,
|
|
|
- success (res) { }
|
|
|
- })
|
|
|
+ success (res) {
|
|
|
+ this.setData({
|
|
|
+ ble: {...this.data.ble,
|
|
|
+ state: connectStateTypes.unConnect,
|
|
|
+ init: false,
|
|
|
+ devName: "",
|
|
|
+ deviceId: "",
|
|
|
+ },
|
|
|
+ bleDevs: []
|
|
|
+ });
|
|
|
+ bleServices = [];
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
// 蓝牙控制板块
|
|
|
searchDeviceHandle() {
|
|
@@ -274,12 +320,12 @@ Page({
|
|
|
ble:{...this.data.ble, state:connectStateTypes.connected }
|
|
|
});
|
|
|
// 设备服务管理
|
|
|
- this.connectServices();
|
|
|
+ this.connectServices(dev.deviceId);
|
|
|
},
|
|
|
|
|
|
- async connectServices(){
|
|
|
+ async connectServices(deviceId){
|
|
|
// uuid FF01
|
|
|
- let [err,res] = await handle(ble.getBleServices());
|
|
|
+ let [err,res] = await handle(ble.getBleServices(deviceId));
|
|
|
if(err){ return this.bleFail('获取服务失败', err.errMsg);}
|
|
|
// 保存服务信息;
|
|
|
bleServices = res;
|