1
0
forked from dyf/APP
Files
APP/utils/BleReceive.js
2025-08-18 16:30:44 +08:00

225 lines
5.1 KiB
JavaScript

class BleReceive {
constructor() {
this.StorageKey = "linkedDevices";
}
getCurrentPagePath() {
const pages = getCurrentPages();
if (pages.length === 0) {
console.log("pages.length=0");
return "";
}
const currentPage = pages[pages.length - 1];
console.log("currentPage=", currentPage.route);
return currentPage.route;
}
setBleFormData(data,f) {
if(data){
let linkedList=uni.getStorageSync(this.StorageKey);
linkedList.find((v)=>{
if(f.deviceId==v.deviceId){
let keys=Object.keys(data);
keys.forEach((key)=>{
if(!v.formData){
v.formData={};
}
if(!f.formData){
f.formData={};
}
v.formData[key]=data[key];
f.formData[key]=data[key];
});
uni.setStorageSync(this.StorageKey,linkedList);
}
});
}
}
ReceiveData(receive,f,path) {
if(f && f.macAddress && f.id){
if(f.detailPageUrl=='/pages/6155/deviceDetail'){
console.log("该设备是6155");
return this.Receive_6155(receive,f,path);
}
if(f.detailPageUrl=='/pages/650/HBY650'){
console.log("该设备是650");
return this.Receive_650(receive,f,path);
}
}
console.log("已收到该消息,但无法处理",f);
}
Receive_650(receive,f,path) {
console.log("通用程序正在处理650的数据");
var parseData = () => {
let bytes = receive.bytes;
if (bytes[0] == 0x55) {
try {
let staticLevelByte = bytes[1];
let staticLevelText = '未知';
let modeCurr = "";
switch (staticLevelByte) {
case 0x65:
staticLevelText = '高档';
modeCurr = "hight";
break;
case 0x66:
staticLevelText = '中档';
modeCurr = "center";
break;
case 0x67:
staticLevelText = '低档';
modeCurr = "low";
break;
case 0x68:
staticLevelText = '关闭';
modeCurr = "close";
break;
}
// 解析照明档位
let lightingLevelByte = bytes[2];
let lightingLevelText = lightingLevelByte === 0x6e ? '开启' : '关闭';
// 解析剩余照明时间(第三和第四字节,大端序)
let lightingTime = (bytes[3] << 8) | bytes[4];
let hours = Math.floor(lightingTime / 60);
let remainingMinutes = lightingTime % 60;
let xuhang = '0分';
// 处理不同情况的显示
if (hours === 0) {
xuhang = `${remainingMinutes}`;
} else if (remainingMinutes === 0) {
xuhang = `${hours}小时`;
} else {
xuhang = `${hours}小时${remainingMinutes}`;
}
// 解析剩余电量
let batteryLevelByte = bytes[5];
// 电量百分比范围检查
let batteryLevel = Math.max(0, Math.min(100, batteryLevelByte));
let iswarn = false;
let warn = bytes[6];
if (warn == 0x00) {
warn = '无预警';
} else if (warn == 0x01) {
warn = '弱预警';
} else if (warn == 0x02) {
iswarn = true;
warn = '中预警';
} else if (warn == 0x03) {
iswarn = true;
warn = '强预警';
} else if (warn == 0x04) {
iswarn = true;
warn = '非常强预警';
}
let formData={};
formData.battary = batteryLevel;
formData.xuhang = xuhang;
formData.cMode = lightingLevelByte === 0x6e;
formData.modeCurr = modeCurr;
formData.warnLevel = warn;
formData.iswarn = iswarn;
this.setBleFormData(formData,f);
let route=this.getCurrentPagePath();
console.log("f=",f);
console.log("route="+route);
if (iswarn && f.detailPageUrl.indexOf(route)==-1 ) {
uni.showModal({
content:"环境存在漏电电源",
title:"警告",
success(res){
if(res.confirm){
if(f){
uni.navigateTo({
url: f.detailPageUrl,
events: {
ack: function(data) {}
},
success: (res) => {
res.eventChannel.emit('detailData', {
data: f,
deviceType: '',
apiType: 'listA'
});
}
});
}
}
}
})
}else{
console.log("当前全局不处理此消息");
}
return formData;
} catch (error) {
return null;
}
}
if (receive.str) {
try {
let str = receive.str;
if (str.indexOf('mac address:') == 0) {
let formData={};
formData.macAddress = str.split(':')[1];
this.setBleFormData(formData,f);
return formData;
}
return null;
} catch (ex) {
return null;
}
}
}
let data=parseData();
return data;
}
Receive_6155() {
console.log("通用程序正在处理6155的数据");
}
}
let receiveInstance = null;
export default {
getBleReceive: function(found, receive) {
if (!receiveInstance) {
receiveInstance = new BleReceive();
}
return receiveInstance;
}
}