首页更新,添加蓝牙电量显示
This commit is contained in:
@ -56,7 +56,7 @@ class BleHelper {
|
||||
isOpenBlue: false, //蓝牙模块是否开启
|
||||
available: false, //蓝牙状态是否可用
|
||||
discovering: false, //蓝牙是否正在搜索
|
||||
searchList: [], //已搜索到的设备列表,
|
||||
|
||||
isSubscribe: false, //是否开启了订阅
|
||||
LinkedList: linkedDevices, //已连接的设备列表
|
||||
platform: systemInfo.uniPlatform,
|
||||
@ -71,9 +71,10 @@ class BleHelper {
|
||||
stateRecoveryCallback: [], //蓝牙适配器恢复可用事件
|
||||
stateBreakCallback: [] //蓝牙适配器不可用事件
|
||||
}
|
||||
this.addReceiveCallback((receive, f, path, recArr) => {
|
||||
recei.ReceiveData(receive, f, path, recArr);
|
||||
}, "BleReceiveData");
|
||||
//蓝牙模块不再订阅,改到首页订阅
|
||||
// this.addReceiveCallback((receive, f, path, recArr) => {
|
||||
// recei.ReceiveData(receive, f, path, recArr);
|
||||
// }, "BleReceiveData");
|
||||
|
||||
setTimeout(() => {
|
||||
this.OpenBlue().then(() => {
|
||||
@ -229,8 +230,12 @@ class BleHelper {
|
||||
}
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg[type].findIndex((v) => {
|
||||
return v.key == key;
|
||||
let f =-1;
|
||||
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
// this.cfg[type][f].callback = callback;
|
||||
@ -253,8 +258,11 @@ class BleHelper {
|
||||
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg[type].findIndex((v) => {
|
||||
return v.key == key;
|
||||
let f =-1;
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
this.cfg[type].splice(f, 1);
|
||||
@ -268,8 +276,31 @@ class BleHelper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//获取某个事件的所有订阅者,但不包含某些订阅者
|
||||
getCfgAllKeys(type,retains){
|
||||
if(!retains){
|
||||
retains=[];
|
||||
}
|
||||
if(!Array.isArray(retains)){
|
||||
retains=[retains];
|
||||
}
|
||||
let keys=null;
|
||||
for (let index = 0; index < this.cfg[type]; index++) {
|
||||
let ele = this.cfg[type];
|
||||
let f=retains.find(v=>{
|
||||
return v.toLowerCase()==ele.key.toLowerCase();
|
||||
});
|
||||
if(!f){
|
||||
if(!keys){
|
||||
keys=[ele.key];
|
||||
}else{
|
||||
keys.push(ele.key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
//设置蓝牙恢复连接的回调
|
||||
addRecoveryCallback(callback, currKey) {
|
||||
@ -280,6 +311,17 @@ class BleHelper {
|
||||
removeRecoveryCallback(currKey) {
|
||||
this.removeCallback(currKey, 'recoveryCallback');
|
||||
}
|
||||
//移除所有蓝牙恢复连接的回调,但不包括currkey
|
||||
removeAllRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('recoveryCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeRecoveryCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙断开连接的回调
|
||||
addDisposeCallback(callback, currKey) {
|
||||
@ -290,6 +332,16 @@ class BleHelper {
|
||||
removeDisposeCallback(currKey) {
|
||||
this.removeCallback(currKey, 'bleDisposeCallback');
|
||||
}
|
||||
removeAllDisposeCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('bleDisposeCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDisposeCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//设置发现新设备的回调
|
||||
@ -301,6 +353,16 @@ class BleHelper {
|
||||
removeDeviceFound(currKey) {
|
||||
this.removeCallback(currKey, 'onDeviceFound');
|
||||
}
|
||||
removeAllDeviceFound(currKey) {
|
||||
let keys=this.getCfgAllKeys('onDeviceFound',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDeviceFound(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//添加接收到数据的回调
|
||||
addReceiveCallback(callback, currKey) {
|
||||
@ -311,6 +373,16 @@ class BleHelper {
|
||||
removeReceiveCallback(currKey) {
|
||||
this.removeCallback(currKey, 'receivDataCallback');
|
||||
}
|
||||
removeAllReceiveCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('receivDataCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeReceiveCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//添加蓝牙不可用的回调
|
||||
addStateBreakCallback(callback, currKey) {
|
||||
@ -321,7 +393,16 @@ class BleHelper {
|
||||
removeStateBreakCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateBreakCallback');
|
||||
}
|
||||
|
||||
removeAllStateBreakCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateBreakCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateBreakCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙适配器恢复可用的回调
|
||||
addStateRecoveryCallback(callback, currKey) {
|
||||
@ -332,6 +413,16 @@ class BleHelper {
|
||||
removeStateRecoveryCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
removeAllStateRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateRecoveryCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateRecoveryCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//清除所有事件回调
|
||||
removeAllCallback(currKey) {
|
||||
@ -342,7 +433,15 @@ class BleHelper {
|
||||
this.removeStateRecoveryCallback(currKey);
|
||||
this.removeStateBreakCallback(currKey);
|
||||
}
|
||||
|
||||
//清除所有事件回调,不包含retain
|
||||
removeAllCallbackByRetain(retain) {
|
||||
this.removeAllDeviceFound(retain)
|
||||
this.removeAllDisposeCallback(retain);
|
||||
this.removeAllReceiveCallback(retain);
|
||||
this.removeAllRecoveryCallback(retain);
|
||||
this.removeAllStateRecoveryCallback(retain);
|
||||
this.removeAllStateBreakCallback(retain);
|
||||
}
|
||||
|
||||
getError(ex) {
|
||||
let code = ex.code;
|
||||
@ -784,21 +883,24 @@ class BleHelper {
|
||||
}
|
||||
|
||||
BleConnChange() {
|
||||
let stateTimeout=null;
|
||||
uni.onBLEConnectionStateChange((res) => {
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
|
||||
// 检查状态是否真的发生了变化
|
||||
let ble = this.data.LinkedList.find(dev => {
|
||||
return res.deviceId === dev.deviceId;
|
||||
});
|
||||
if (ble) {
|
||||
if (ble.Linked === res.connected) {
|
||||
console.error("专业给UniApp填坑,蓝牙连接状态重复回调");
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
clearTimeout(stateTimeout);
|
||||
stateTimeout=setTimeout(() => {
|
||||
if (!res.connected) {
|
||||
console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((
|
||||
@ -851,7 +953,7 @@ class BleHelper {
|
||||
console.log("蓝牙连接已恢复", res);
|
||||
}
|
||||
|
||||
}, 0);
|
||||
}, 50);
|
||||
});
|
||||
}
|
||||
|
||||
@ -881,8 +983,7 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
res.devices = arr;
|
||||
this.data.searchList = this.data.searchList.concat(
|
||||
res);
|
||||
|
||||
if (this.cfg.onDeviceFound) {
|
||||
if (this.cfg.onDeviceFound.length > 0) {
|
||||
this.cfg.onDeviceFound.forEach((found) => {
|
||||
@ -907,7 +1008,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
this.data.searchList = [];
|
||||
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -924,7 +1025,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
this.data.searchList = [];
|
||||
|
||||
resolve();
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -959,20 +1060,15 @@ class BleHelper {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
this.data.searchList = [];
|
||||
|
||||
var these = this;
|
||||
//开始搜索
|
||||
var Search = () => {
|
||||
//只搜索合作供应商的服务id
|
||||
let serviceIds = serviceDic.map(v => {
|
||||
return v.serviceId
|
||||
});
|
||||
|
||||
//搜索一个服务id的设备,循环调用
|
||||
let RunSearch = (serviceId) => {
|
||||
let RunSearch = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services: serviceId ? [serviceId] : [],
|
||||
services:[],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
console.log('开始搜索蓝牙设备成功');
|
||||
@ -992,14 +1088,8 @@ class BleHelper {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let promises = [];
|
||||
// for (let i = 0; i < serviceIds.length; i++) {
|
||||
|
||||
// promises.push(RunSearch(serviceIds[i]));
|
||||
|
||||
// }
|
||||
promises.push(RunSearch());
|
||||
Promise.all(promises).then(resolve).catch(reject);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -1278,7 +1368,6 @@ class BleHelper {
|
||||
|
||||
|
||||
} else
|
||||
|
||||
{
|
||||
console.error("预设的蓝牙服务和特征中找不到");
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
@ -1297,7 +1386,7 @@ class BleHelper {
|
||||
});
|
||||
} else {
|
||||
|
||||
Promise.all(promises)
|
||||
Promise.allSettled(promises)
|
||||
.then(results => {
|
||||
console.log("results= ", results);
|
||||
if (!s) {
|
||||
@ -1970,5 +2059,6 @@ export default {
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
},
|
||||
constService:serviceDic
|
||||
}
|
||||
|
||||
@ -6,13 +6,13 @@ class BleReceive {
|
||||
'/pages/7305/BJQ7305': this.Receive_6155.bind(this),
|
||||
'/pages/650/HBY650': this.Receive_650.bind(this),
|
||||
'/pages/670/HBY670': this.Receive_670.bind(this),
|
||||
'/pages/4877/BJQ4877': this.Receive_4877.bind(this),
|
||||
'/pages/4877/BJQ4877': this.Receive_4877.bind(this),
|
||||
'/pages/100/HBY100': this.Receive_100.bind(this),
|
||||
'/pages/102/HBY102':this.Receive_102.bind(this)
|
||||
'/pages/102/HBY102': this.Receive_102.bind(this)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
setBleFormData(data, f) {
|
||||
if (data) {
|
||||
let linkedList = uni.getStorageSync(this.StorageKey);
|
||||
@ -59,7 +59,7 @@ class BleReceive {
|
||||
|
||||
if (handler) {
|
||||
let data = handler(receive, f, path, recArr);
|
||||
// console.log("设备"+f.device.deviceName+"收到消息,数据:", data);
|
||||
// console.log("设备"+f.device.deviceName+"收到消息,数据:", data);
|
||||
return data;
|
||||
} else {
|
||||
console.error("已收到消息,但无指定处理程序, deviceUrl:", f.device.detailPageUrl, "可用handlers:", keys);
|
||||
@ -99,6 +99,10 @@ class BleReceive {
|
||||
staticLevelText = '低档';
|
||||
modeCurr = "low";
|
||||
break;
|
||||
case 0x68:
|
||||
staticLevelText = '前置';
|
||||
modeCurr = "smalllow";
|
||||
break;
|
||||
case 0x64:
|
||||
staticLevelText = '关闭';
|
||||
modeCurr = "close";
|
||||
@ -162,7 +166,7 @@ class BleReceive {
|
||||
|
||||
if (iswarn) {
|
||||
uni.showModal({
|
||||
content: "'"+f.device.deviceName+"'环境存在漏电电源",
|
||||
content: "'" + f.device.deviceName + "'环境存在漏电电源",
|
||||
title: "警告",
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
@ -258,7 +262,7 @@ class BleReceive {
|
||||
Receive_670(receive, f, path, recArr) {
|
||||
// console.log("pagh=", path);
|
||||
var todo = (bytes) => {
|
||||
console.log("todo",receive);
|
||||
console.log("todo", receive);
|
||||
let receiveData = {};
|
||||
if (bytes[0] == 0x55) {
|
||||
try {
|
||||
@ -319,7 +323,7 @@ class BleReceive {
|
||||
receiveData.staticWarn = staticWarn;
|
||||
receiveData.fourGStrenth = fourGStrenth;
|
||||
receiveData.SOS = sosTxt;
|
||||
receiveData.qzwarn = sosTxt==='sg';
|
||||
receiveData.qzwarn = sosTxt === 'sg';
|
||||
this.setBleFormData(receiveData, f);
|
||||
console.log("recArr=", recArr);
|
||||
let recCnt = recArr.find(v => {
|
||||
@ -327,22 +331,22 @@ class BleReceive {
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
let msgs=[];
|
||||
if(receiveData.qzwarn){
|
||||
let msgs = [];
|
||||
if (receiveData.qzwarn) {
|
||||
msgs.push("声光报警中");
|
||||
}
|
||||
if (staticWarn) { //有静止报警
|
||||
msgs.push("静止报警中");
|
||||
}
|
||||
if(msgs.length>0){
|
||||
msgs="设备'"+f.device.deviceName+msgs.join(";");
|
||||
if (msgs.length > 0) {
|
||||
msgs = "设备'" + f.device.deviceName + msgs.join(";");
|
||||
uni.showModal({
|
||||
title: "警告",
|
||||
content:msgs,
|
||||
content: msgs,
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('数据解析错误:', error);
|
||||
@ -418,44 +422,44 @@ class BleReceive {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Receive_6155(receive, f, path, recArr) {
|
||||
let bytes = receive.bytes;
|
||||
if (bytes[0] == 0xFB && bytes[1] == 0x64 && bytes.length >= 8) {
|
||||
try {
|
||||
|
||||
|
||||
let getName = function(type,) {
|
||||
|
||||
let getName = function(type, ) {
|
||||
let name = "";
|
||||
switch (type) {
|
||||
case 0x01:
|
||||
// name = '强光';
|
||||
name=0;
|
||||
name = 0;
|
||||
break;
|
||||
case 0x02:
|
||||
// name = '弱光';
|
||||
name=1;
|
||||
name = 1;
|
||||
break;
|
||||
case 0x03:
|
||||
// name = '爆闪';
|
||||
name=2;
|
||||
break;
|
||||
// name = '爆闪';
|
||||
name = 2;
|
||||
break;
|
||||
case 0x04:
|
||||
name = 0;//泛光
|
||||
name = 0; //泛光
|
||||
break;
|
||||
case 0x0A:
|
||||
name = 1;//强光&泛光
|
||||
name = 1; //强光&泛光
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
// name = '关闭';
|
||||
name=null;
|
||||
name = null;
|
||||
break;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
let staticLevelText = getName(bytes[2]);
|
||||
|
||||
// 解析照明档位
|
||||
@ -484,26 +488,27 @@ class BleReceive {
|
||||
let mm = Math.max(0, Math.min(100, bytes[7]));
|
||||
lightingTime = HH + "小时" + mm + "分钟";
|
||||
let formData = {};
|
||||
formData.mode = (bytes[2]===0x00 || bytes[2]===0x0B)?'fu':'main';
|
||||
formData.light=formData.mode=='fu'?lightingLevelText:staticLevelText;
|
||||
|
||||
formData.battary = batteryLevel;
|
||||
formData.mode = (bytes[2] === 0x00 || bytes[2] === 0x0B) ? 'fu' : 'main';
|
||||
formData.light = formData.mode == 'fu' ? lightingLevelText : staticLevelText;
|
||||
|
||||
formData.battary = batteryLevel;
|
||||
formData.statu = warn;
|
||||
formData.xuhang = lightingTime;
|
||||
|
||||
|
||||
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
|
||||
return v.key.replace(/\//g, "").toLowerCase() === f.device.detailPageUrl.replace(/\//g, '').toLowerCase();
|
||||
return v.key.replace(/\//g, "").toLowerCase() === f.device.detailPageUrl.replace(/\//g,
|
||||
'').toLowerCase();
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (batteryLevel <= 20) {
|
||||
// 会弹出两个框,暂且注释掉这段代码
|
||||
// 会弹出两个框,暂且注释掉这段代码
|
||||
uni.showModal({
|
||||
content: "设备'"+f.device.deviceName+"'电量低",
|
||||
content: "设备'" + f.device.deviceName + "'电量低",
|
||||
title: "提示"
|
||||
});
|
||||
}
|
||||
@ -520,102 +525,102 @@ class BleReceive {
|
||||
return null;
|
||||
}
|
||||
|
||||
Receive_4877(receive,f,path,recArr){
|
||||
let receiveData={};
|
||||
|
||||
Receive_4877(receive, f, path, recArr) {
|
||||
let receiveData = {};
|
||||
|
||||
try {
|
||||
|
||||
|
||||
// console.log("str=",receive.str);
|
||||
receiveData = JSON.parse(receive.str);
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_PowerPercent<=20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'"+f.device.deviceName+"'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_PowerPercent <= 20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'" + f.device.deviceName + "'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
receiveData = {};
|
||||
console.log("文本解析失败",error)
|
||||
console.log("文本解析失败", error)
|
||||
}
|
||||
return receiveData;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Receive_100(receive,f,path,recArr){
|
||||
let receiveData={};
|
||||
|
||||
|
||||
Receive_100(receive, f, path, recArr) {
|
||||
let receiveData = {};
|
||||
|
||||
try {
|
||||
|
||||
// console.log("str=",receive.str);
|
||||
|
||||
// console.log("str=",receive.str);
|
||||
receiveData = JSON.parse(receive.str);
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_battery<=20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'"+f.device.deviceName+"'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_battery <= 20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'" + f.device.deviceName + "'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
receiveData = {};
|
||||
console.log("文本解析失败",error)
|
||||
console.log("文本解析失败", error)
|
||||
}
|
||||
return receiveData;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Receive_102(receive,f,path,recArr){
|
||||
let receiveData={};
|
||||
|
||||
|
||||
|
||||
Receive_102(receive, f, path, recArr) {
|
||||
let receiveData = {};
|
||||
|
||||
try {
|
||||
|
||||
// console.log("str=",receive.str);
|
||||
|
||||
// console.log("str=",receive.str);
|
||||
receiveData = JSON.parse(receive.str);
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_PowerPercent<=20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'"+f.device.deviceName+"'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
if (receiveData.sta_Intrusion===1) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "闯入报警中",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_PowerPercent <= 20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'" + f.device.deviceName + "'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
if (receiveData.sta_Intrusion === 1) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "闯入报警中",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
receiveData = {};
|
||||
console.log("文本解析失败",error)
|
||||
console.log("文本解析失败", error)
|
||||
}
|
||||
return receiveData;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
|
||||
//地标 转 国测 常量
|
||||
var x_PI = (3.14159265358979324 * 3000.0) / 180.0;
|
||||
var PI = 3.1415926535897932384626;
|
||||
@ -8,115 +7,241 @@ var ee = 0.00669342162296594323; //椭球的偏心率。
|
||||
|
||||
//判断是否在国内,在中国国内的经纬度才需要做偏移
|
||||
function out_of_china(lng, lat) {
|
||||
return (
|
||||
lng < 72.004 ||
|
||||
lng > 137.8347 ||
|
||||
(lat < 0.8293 || lat > 55.8271 || false)
|
||||
);
|
||||
return (
|
||||
lng < 72.004 ||
|
||||
lng > 137.8347 ||
|
||||
(lat < 0.8293 || lat > 55.8271 || false)
|
||||
);
|
||||
}
|
||||
|
||||
//转化经度
|
||||
function transformlng(lng, lat) {
|
||||
var ret =
|
||||
300.0 +
|
||||
lng +
|
||||
2.0 * lat +
|
||||
0.1 * lng * lng +
|
||||
0.1 * lng * lat +
|
||||
0.1 * Math.sqrt(Math.abs(lng));
|
||||
ret +=
|
||||
((20.0 * Math.sin(6.0 * lng * PI) +
|
||||
20.0 * Math.sin(2.0 * lng * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((20.0 * Math.sin(lng * PI) +
|
||||
40.0 * Math.sin((lng / 3.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((150.0 * Math.sin((lng / 12.0) * PI) +
|
||||
300.0 * Math.sin((lng / 30.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
return ret;
|
||||
var ret =
|
||||
300.0 +
|
||||
lng +
|
||||
2.0 * lat +
|
||||
0.1 * lng * lng +
|
||||
0.1 * lng * lat +
|
||||
0.1 * Math.sqrt(Math.abs(lng));
|
||||
ret +=
|
||||
((20.0 * Math.sin(6.0 * lng * PI) +
|
||||
20.0 * Math.sin(2.0 * lng * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((20.0 * Math.sin(lng * PI) +
|
||||
40.0 * Math.sin((lng / 3.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((150.0 * Math.sin((lng / 12.0) * PI) +
|
||||
300.0 * Math.sin((lng / 30.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
//转化纬度
|
||||
function transformlat(lng, lat) {
|
||||
var ret =
|
||||
-100.0 +
|
||||
2.0 * lng +
|
||||
3.0 * lat +
|
||||
0.2 * lat * lat +
|
||||
0.1 * lng * lat +
|
||||
0.2 * Math.sqrt(Math.abs(lng));
|
||||
ret +=
|
||||
((20.0 * Math.sin(6.0 * lng * PI) +
|
||||
20.0 * Math.sin(2.0 * lng * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((20.0 * Math.sin(lat * PI) +
|
||||
40.0 * Math.sin((lat / 3.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((160.0 * Math.sin((lat / 12.0) * PI) +
|
||||
320 * Math.sin((lat * PI) / 30.0)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
return ret;
|
||||
var ret = -100.0 +
|
||||
2.0 * lng +
|
||||
3.0 * lat +
|
||||
0.2 * lat * lat +
|
||||
0.1 * lng * lat +
|
||||
0.2 * Math.sqrt(Math.abs(lng));
|
||||
ret +=
|
||||
((20.0 * Math.sin(6.0 * lng * PI) +
|
||||
20.0 * Math.sin(2.0 * lng * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((20.0 * Math.sin(lat * PI) +
|
||||
40.0 * Math.sin((lat / 3.0) * PI)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
ret +=
|
||||
((160.0 * Math.sin((lat / 12.0) * PI) +
|
||||
320 * Math.sin((lat * PI) / 30.0)) *
|
||||
2.0) /
|
||||
3.0;
|
||||
return ret;
|
||||
}
|
||||
//经纬度互相转换
|
||||
function convertByNet(lng,lat,from){
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if(!from){
|
||||
from='wgs84'
|
||||
}
|
||||
let url = 'https://openapi.lddgo.net/base/gtool/api/v1/CoordinateTransform'
|
||||
uni.request({
|
||||
url: url,
|
||||
method: 'POST',
|
||||
timeout: 10000,
|
||||
header:{
|
||||
'content-type':'application/json;charset=UTF-8'
|
||||
},
|
||||
data:
|
||||
{
|
||||
"from": "wgs84",
|
||||
"coordinateList": [
|
||||
{
|
||||
"sourceLng": lng,
|
||||
"sourceLat": lat
|
||||
}
|
||||
]
|
||||
}
|
||||
,
|
||||
dataType: 'json',
|
||||
success(res) {
|
||||
if (res.statusCode == 200) {
|
||||
|
||||
if (res.data.code===0) {
|
||||
console.log("坐标转换接口成功", res);
|
||||
let lnglag=res.data.data.coordinateList[0];
|
||||
resolve([lnglag.gLng,lnglag.gLat]);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
console.error("坐标转换异常", res);
|
||||
resolve(null)
|
||||
|
||||
},
|
||||
fail(err) {
|
||||
console.error("坐标转换接口出现异常", err);
|
||||
resolve(null);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
//wgs84 to gcj02 地球坐标系 转 火星坐标系
|
||||
function wgs84_to_gcj02(lng, lat) {
|
||||
if (out_of_china(lng, lat)) {
|
||||
return [lng, lat];
|
||||
} else {
|
||||
var dlat = transformlat(lng - 105.0, lat - 35.0);
|
||||
var dlng = transformlng(lng - 105.0, lat - 35.0);
|
||||
var radlat = (lat / 180.0) * PI;
|
||||
var magic = Math.sin(radlat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
var sqrtmagic = Math.sqrt(magic);
|
||||
dlat =
|
||||
(dlat * 180.0) /
|
||||
(((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
|
||||
dlng =
|
||||
(dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
|
||||
var mglat = lat + dlat;
|
||||
var mglng = lng + dlng;
|
||||
function wgs84_to_gcj02_old(lng, lat) {
|
||||
if (out_of_china(lng, lat)) {
|
||||
return [lng, lat];
|
||||
} else {
|
||||
var dlat = transformlat(lng - 105.0, lat - 35.0);
|
||||
var dlng = transformlng(lng - 105.0, lat - 35.0);
|
||||
var radlat = (lat / 180.0) * PI;
|
||||
var magic = Math.sin(radlat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
var sqrtmagic = Math.sqrt(magic);
|
||||
dlat =
|
||||
(dlat * 180.0) /
|
||||
(((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
|
||||
dlng =
|
||||
(dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
|
||||
var mglat = lat + dlat;
|
||||
var mglng = lng + dlng;
|
||||
|
||||
return [mglng, mglat];
|
||||
}
|
||||
return [mglng, mglat];
|
||||
}
|
||||
}
|
||||
//wgs84 to gcj02 wgs84转gcj02,返回Promise
|
||||
function wgs84_to_gcj02(lng, lat) {
|
||||
|
||||
//调用高德的接口进行坐标转换
|
||||
let convertByGD = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let url = 'https://restapi.amap.com/v3/assistant/coordinate/convert?locations=' + lng + ',' +
|
||||
lat + '&coordsys=gps&output=json&key=ca3af8a20d628897020893892bda5ae4'
|
||||
uni.request({
|
||||
url: url,
|
||||
method: 'GET',
|
||||
timeout: 10000,
|
||||
dataType: 'json',
|
||||
success(res) {
|
||||
if (res.statusCode == 200) {
|
||||
|
||||
if (res.data.status == "1" && res.data.infocode=='10000') {
|
||||
console.log("坐标转换接口成功", res);
|
||||
resolve(res.data.locations.split(','));
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
console.error("坐标转换异常", res);
|
||||
resolve(null)
|
||||
|
||||
},
|
||||
fail(err) {
|
||||
console.error("坐标转换接口出现异常", err);
|
||||
resolve(null);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
let convertOrains=()=>{
|
||||
let newLnglat=wgs84_to_gcj02_old(lng,lat);
|
||||
return Promise.resolve(newLnglat);
|
||||
}
|
||||
|
||||
let task=()=>{
|
||||
return new Promise((resolve,reject)=>{
|
||||
|
||||
let promises=[convertByGD(),convertByNet(lng,lat),convertOrains()];
|
||||
Promise.allSettled(promises).then(
|
||||
res=>{
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
let item = res[i];
|
||||
if(item.status==='fulfilled' && item.value){
|
||||
resolve(item.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return task();
|
||||
}
|
||||
|
||||
|
||||
//gcj02 to wgs84 火星坐标系 转 地球坐标系
|
||||
function gcj02_to_wgs84(lng, lat) {
|
||||
if (out_of_china(lng, lat)) {
|
||||
return [lng, lat]
|
||||
}
|
||||
else {
|
||||
var dlat = transformlat(lng - 105.0, lat - 35.0);
|
||||
var dlng = transformlng(lng - 105.0, lat - 35.0);
|
||||
var radlat = lat / 180.0 * PI;
|
||||
var magic = Math.sin(radlat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
var sqrtmagic = Math.sqrt(magic);
|
||||
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
|
||||
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
|
||||
var mglat = lat + dlat;
|
||||
var mglng = lng + dlng;
|
||||
return [lng * 2 - mglng, lat * 2 - mglat]
|
||||
}
|
||||
function gcj02_to_wgs84_old(lng, lat) {
|
||||
if (out_of_china(lng, lat)) {
|
||||
return [lng, lat]
|
||||
} else {
|
||||
var dlat = transformlat(lng - 105.0, lat - 35.0);
|
||||
var dlng = transformlng(lng - 105.0, lat - 35.0);
|
||||
var radlat = lat / 180.0 * PI;
|
||||
var magic = Math.sin(radlat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
var sqrtmagic = Math.sqrt(magic);
|
||||
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
|
||||
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
|
||||
var mglat = lat + dlat;
|
||||
var mglng = lng + dlng;
|
||||
return [lng * 2 - mglng, lat * 2 - mglat]
|
||||
}
|
||||
}
|
||||
|
||||
//gcj02转wgs84
|
||||
function gcj02_to_wgs84(lng, lat){
|
||||
|
||||
let task=()=>{
|
||||
return new Promise((resolve,reject)=>{
|
||||
|
||||
convertByNet(lng,lat,'gcj02').then(res=>{
|
||||
if(res){
|
||||
resolve(res);
|
||||
return;
|
||||
}
|
||||
resolve(gcj02_to_wgs84_old(lng,lat));
|
||||
})
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
return task();
|
||||
}
|
||||
|
||||
export default {
|
||||
gcj02_to_wgs84,
|
||||
wgs84_to_gcj02
|
||||
}
|
||||
|
||||
|
||||
|
||||
gcj02_to_wgs84,
|
||||
wgs84_to_gcj02
|
||||
}
|
||||
Reference in New Issue
Block a user