1
0
forked from dyf/APP

Compare commits

...

20 Commits

Author SHA1 Message Date
b76a41b624 Merge remote-tracking branch 'origin/new-20250827' 2026-03-24 14:40:58 +08:00
c286660a10 修复102时间加减异常 2026-03-24 12:00:27 +08:00
5163491ea3 merge upstream 2026-03-20 14:49:45 +08:00
0e66d375fc 100J增加物联按键蓝牙同步状态到APP 2026-03-20 09:09:49 +08:00
1fcaf8a9a4 体验性小优化 2026-03-19 17:25:41 +08:00
75b9f27965 merge upstream 2026-03-19 15:49:38 +08:00
bdea9dac4b 100J上传音频格式限制 2026-03-19 15:48:44 +08:00
444b3b35ac 100J加个限制格式提示 2026-03-19 15:46:53 +08:00
3e19646ed7 merge upstream 2026-03-19 15:27:44 +08:00
44607f9b0c 100J更新兼容性 2026-03-19 15:18:59 +08:00
23b72ef9ae 蓝牙按工厂要求修改,app隐藏时断开设备蓝牙,已配对设备显示实际连接的设备
# Conflicts:
#	App.vue
2026-03-19 15:14:32 +08:00
84feb9f1e8 合并代码 2026-03-19 14:37:58 +08:00
3a9de3078c 处理100J设备蓝牙问题 2026-03-19 14:36:17 +08:00
ac59e28281 Merge branch 'main' of http://47.107.152.87:3000/dyf/APP 2026-03-19 12:40:53 +08:00
f943bb9b09 更新100j蓝牙 2026-03-19 12:37:29 +08:00
dyf
a2680fc14d Merge pull request 'new-20250827 解决合并冲突' (#38) from liubiao/APP:new-20250827 into main
Reviewed-on: dyf/APP#38
2026-03-19 11:42:40 +08:00
ebe126d826 完善100J蓝牙 2026-03-19 11:41:17 +08:00
9b6a5e095c merge upstream 2026-02-07 13:53:33 +08:00
7735abc2a1 merge upstream 2026-02-04 09:05:49 +08:00
c626f3766e merge upstream 2026-02-03 13:07:11 +08:00
19 changed files with 2009 additions and 1014 deletions

41
App.vue
View File

@ -1,7 +1,11 @@
<script>
import bleTool from '@/utils/BleHelper.js';
import upgrade from '@/utils/update.js';
// 延迟断开蓝牙:选择文件/录音等会触发 onHide8 秒内返回则不断开
const BLE_DISCONNECT_DELAY = 8000;
let _bleDisconnectTimer = null;
export default {
onLaunch: function() {
@ -27,7 +31,7 @@
// uni.removeStorageSync(val);
// }
// });
uni.clearStorageSync();
// uni.clearStorageSync();
//以上代码仅在开发时使用,否则会出现不可预知的问题。
// #ifdef APP|APP-PLUS
@ -63,10 +67,10 @@
});
}
if (plus.os.name == 'Android') {
if(plus.runtime.isAgreePrivacy()){
initOS();
if (plus.runtime.isAgreePrivacy()) {
initOS();
}
}else{
} else {
initOS();
}
@ -74,7 +78,11 @@
},
onShow: function() {
console.log('App Show');
// 取消延迟断开:用户可能只是选文件/录音后返回,不断开蓝牙
if (_bleDisconnectTimer) {
clearTimeout(_bleDisconnectTimer);
_bleDisconnectTimer = null;
}
//将检查更新换到onshow,因为苹果用户喜欢一直挂着
// #ifdef APP|APP-PLUS
@ -91,11 +99,26 @@
onHide: function() {
console.log('App Hide');
// #ifdef APP|APP-PLUS
// 上传中不主动断开:语音上传进行中则不断开蓝牙
let ble = bleTool.getBleTool();
if (ble) {
console.log("断开所有蓝牙设备");
ble.disconnectDevice();
if (ble && ble.isVoiceUploading && ble.isVoiceUploading()) {
console.log('App Hide: 语音上传中,不启动断开定时器');
return;
}
// 延迟断开:选文件/录音会短暂 onHide8 秒内返回则不断开
if (_bleDisconnectTimer) clearTimeout(_bleDisconnectTimer);
_bleDisconnectTimer = setTimeout(() => {
_bleDisconnectTimer = null;
let ble2 = bleTool.getBleTool();
if (ble2) {
console.log("App隐藏了,断开所有蓝牙设备,停止搜索");
ble2.StopSearch().catch(ex => {});
ble2.disconnectDevice().catch(ex => {});
console.log("断开所有蓝牙设备");
}
}, BLE_DISCONNECT_DELAY);
// #endif
},
onError(ex) {

View File

@ -1,4 +1,5 @@
import request from '@/utils/request'
import Common from '@/utils/Common.js'
// ================== 蓝牙协议封装类 ==================
class HBY100JProtocol {
@ -205,45 +206,111 @@ class HBY100JProtocol {
return reject(new Error('缺少文件地址或本地路径'));
}
const isLocalPath = !/^https?:\/\//i.test(fileUrlOrLocalPath);
if (onProgress) onProgress(1);
const readFromPath = (path) => {
const doSend = (bytes) => {
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
.then(resolve).catch(reject);
};
// App 端 getFileSystemManager 未实现,直接用 plus.io.requestFileSystem+getFile
readFromPathPlus(path, doSend, reject);
};
const readFileEntry = (entry, doSend, reject) => {
entry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = (e) => {
try {
const buf = e.target.result;
const bytes = new Uint8Array(buf);
doSend(bytes);
} catch (err) {
console.error('[100J-蓝牙] 读取ArrayBuffer异常:', err);
reject(err);
}
};
reader.onerror = () => reject(new Error('读取文件失败'));
reader.readAsArrayBuffer(file);
}, (err) => reject(err));
};
const readFromPathPlus = (path, doSend, reject) => {
if (typeof plus === 'undefined' || !plus.io) {
console.error('[100J-蓝牙] 当前环境不支持文件读取(plus.io)');
reject(new Error('当前环境不支持文件读取'));
return;
}
plus.io.resolveLocalFileSystemURL(path, (entry) => {
entry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = (e) => {
try {
const buf = e.target.result;
const bytes = new Uint8Array(buf);
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
.then(resolve).catch(reject);
} catch (err) {
reject(err);
}
};
reader.onerror = () => reject(new Error('读取文件失败'));
reader.readAsArrayBuffer(file);
// _downloads/ 用 requestFileSystem+getFile避免 resolveLocalFileSystemURL 卡住)
if (path && path.startsWith('_downloads/')) {
const fileName = path.replace(/^_downloads\//, '');
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
fs.root.getFile(fileName, {}, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
}, (err) => reject(err));
}, (err) => reject(err));
return;
}
// _doc/ 用 requestFileSystem(PRIVATE_DOC),逐级 getDirectory 再 getFile嵌套路径兼容
if (path && path.startsWith('_doc/')) {
const relPath = path.replace(/^_doc\//, '');
const parts = relPath.split('/');
const fileName = parts.pop();
const dirs = parts;
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
let cur = fs.root;
const next = (i) => {
if (i >= dirs.length) {
cur.getFile(fileName, { create: false }, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
return;
}
cur.getDirectory(dirs[i], { create: false }, (dir) => { cur = dir; next(i + 1); }, (err) => reject(err));
};
next(0);
}, (err) => reject(err));
return;
}
// 其他路径兜底
let resolvePath = path;
if (path && path.startsWith('/') && !path.startsWith('file://')) resolvePath = 'file://' + path;
plus.io.resolveLocalFileSystemURL(resolvePath, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
};
if (isLocalPath) {
// 本地路径:无网络时直接读取
readFromPath(fileUrlOrLocalPath);
} else {
// 网络 URL需下载后读取
uni.downloadFile({
url: fileUrlOrLocalPath,
// 网络 URL优先用 uni.request 直接拉取 ArrayBuffer类似 100 设备,无文件 IO失败再走 downloadFile
let fetchUrl = fileUrlOrLocalPath;
if (fetchUrl.startsWith('http://')) fetchUrl = 'https://' + fetchUrl.slice(7);
if (onProgress) onProgress(2);
uni.request({
url: fetchUrl,
method: 'GET',
responseType: 'arraybuffer',
timeout: 60000,
success: (res) => {
if (res.statusCode !== 200) {
reject(new Error('下载失败: ' + res.statusCode));
return;
if (res.statusCode === 200 && res.data) {
const bytes = res.data instanceof ArrayBuffer ? new Uint8Array(res.data) : new Uint8Array(res.data || []);
if (bytes.length > 0) {
const doSend = (b) => {
this._sendVoiceChunks(b, fileType, CHUNK_SIZE, onProgress).then(resolve).catch(reject);
};
doSend(bytes);
return;
}
}
readFromPath(res.tempFilePath);
fallbackDownload();
},
fail: (err) => reject(err)
fail: () => fallbackDownload()
});
const fallbackDownload = () => {
uni.downloadFile({
url: fetchUrl,
success: (res) => {
if (res.statusCode !== 200 || !res.tempFilePath) {
reject(new Error('下载失败: ' + (res.statusCode || '无路径')));
return;
}
Common.moveFileToDownloads(res.tempFilePath).then((p) => readFromPath(p)).catch(() => readFromPath(res.tempFilePath));
},
fail: (err) => reject(err)
});
};
}
});
}
@ -253,43 +320,56 @@ class HBY100JProtocol {
const ft = (fileType & 0xFF) || 1;
const DELAY_AFTER_START = 80; // 开始包后、等设备响应后再发的缓冲(ms)
const DELAY_PACKET = 80; // 数据包间延时(ms)参考6155
const toHex = (arr) => Array.from(arr).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
console.log('[100J-蓝牙] 语音下发总大小:', total, '字节, fileType=', ft);
if (onProgress) onProgress(1);
const bleToolPromise = import('@/utils/BleHelper.js').then(m => m.default.getBleTool());
const send = (dataBytes) => {
let bleRef = null;
const send = (dataBytes, label = '') => {
const buf = new ArrayBuffer(dataBytes.length + 3);
const v = new Uint8Array(buf);
v[0] = 0xFA;
v[1] = 0x05;
for (let i = 0; i < dataBytes.length; i++) v[2 + i] = dataBytes[i];
v[v.length - 1] = 0xFF;
const hex = toHex(v);
const preview = v.length <= 32 ? hex : hex.slice(0, 96) + '...';
console.log(`[100J-蓝牙] 下发${label}${v.length}字节:`, preview);
return bleToolPromise.then(ble => ble.sendData(this.bleDeviceId, buf, this.SERVICE_UUID, this.WRITE_UUID));
};
const delay = (ms) => new Promise(r => setTimeout(r, ms));
// 开始包: FA 05 [fileType] [phase=0] [size 4B LE] FF
const startData = [ft, 0, total & 0xFF, (total >> 8) & 0xFF, (total >> 16) & 0xFF, (total >> 24) & 0xFF];
const waitPromise = this.waitForFileResponse(1000);
return send(startData)
.then(() => waitPromise)
.then(() => delay(DELAY_AFTER_START))
.then(() => {
let seq = 0;
const sendNext = (offset) => {
if (offset >= total) {
return delay(DELAY_PACKET).then(() => send([ft, 2]));
}
const chunk = bytes.slice(offset, Math.min(offset + chunkSize, total));
const chunkData = [ft, 1, seq & 0xFF, (seq >> 8) & 0xFF, ...chunk];
return send(chunkData).then(() => {
seq++;
if (onProgress) onProgress(Math.min(100, Math.floor((offset + chunk.length) / total * 100)));
return delay(DELAY_PACKET).then(() => sendNext(offset + chunk.length));
});
};
return sendNext(0);
})
.then(() => {
if (onProgress) onProgress(100);
return { code: 200, msg: '语音文件已通过蓝牙上传' };
});
return bleToolPromise.then(ble => {
bleRef = ble;
ble.setVoiceUploading(true);
return send(startData, ' 开始包')
.then(() => { if (onProgress) onProgress(3); return waitPromise; })
.then(() => { if (onProgress) onProgress(5); return delay(DELAY_AFTER_START); })
.then(() => {
let seq = 0;
const sendNext = (offset) => {
if (offset >= total) {
return delay(DELAY_PACKET).then(() => send([ft, 2], ' 结束包'));
}
const chunk = bytes.slice(offset, Math.min(offset + chunkSize, total));
const chunkData = [ft, 1, seq & 0xFF, (seq >> 8) & 0xFF, ...chunk];
return send(chunkData, ` #${seq} 数据包`).then(() => {
seq++;
if (onProgress) onProgress(Math.min(100, Math.floor((offset + chunk.length) / total * 100)));
return delay(DELAY_PACKET).then(() => sendNext(offset + chunk.length));
});
};
return sendNext(0);
})
.then(() => {
if (onProgress) onProgress(100);
return { code: 200, msg: '语音文件已通过蓝牙上传' };
});
}).finally(() => {
if (bleRef) bleRef.setVoiceUploading(false);
});
}
}
@ -303,6 +383,11 @@ export function updateBleStatus(isConnected, bleDeviceId, deviceId) {
console.log('[100J] 蓝牙状态:', isConnected ? '已连接(后续指令走蓝牙)' : '已断开(后续指令走4G)', { bleDeviceId: bleDeviceId || '-', deviceId });
}
// 暴露给页面:获取当前蓝牙连接状态(用于跨页面传递,确保语音管理等子页走蓝牙优先)
export function getBleStatus() {
return { isConnected: protocolInstance.isBleConnected, bleDeviceId: protocolInstance.bleDeviceId, deviceId: protocolInstance.deviceId };
}
// 暴露给页面:解析蓝牙接收到的数据
export function parseBleData(buffer) {
return protocolInstance.parseBleData(buffer);
@ -322,6 +407,29 @@ export function fetchBleLocation() {
return protocolInstance.getLocation();
}
// 等待蓝牙连接扫描中时轮询设备页可能在后台完成连接100J 扫描约 15s
function waitForBleConnection(maxWaitMs = 12000, intervalMs = 500) {
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) return Promise.resolve(true);
return new Promise((resolve) => {
const start = Date.now();
const tick = () => {
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) {
console.log('[100J] 等待蓝牙连接成功');
resolve(true);
return;
}
if (Date.now() - start >= maxWaitMs) {
console.log('[100J] 等待蓝牙连接超时将走4G');
resolve(false);
return;
}
setTimeout(tick, intervalMs);
};
console.log('[100J] 蓝牙未连接,等待扫描/连接中...', maxWaitMs, 'ms');
tick();
});
}
// 暴露给页面:尝试重连蓝牙(优先策略:断线后发指令前先尝试重连)
export function tryReconnectBle(timeoutMs = 2500) {
if (protocolInstance.isBleConnected) return Promise.resolve(true);
@ -382,8 +490,8 @@ export function deviceDeleteAudioFile(params) {
})
}
// 更新语音,使用语音(优先蓝牙:有 fileUrl 或 localPath 且蓝牙连接时通过蓝牙上传,否则走 4G
// localPath无网络时本地文件路径可直接通过蓝牙发送
// 更新语音/使用语音蓝牙优先4G 兜底(不影响原有 4G 音频下发
// 有 fileUrl 或 localPath 且蓝牙可用时走蓝牙;否则或蓝牙失败时走 4G与原先逻辑一致
export function deviceUpdateVoice(data) {
const httpExec = () => request({
url: `/app/hby100j/device/updateVoice`,
@ -396,10 +504,12 @@ export function deviceUpdateVoice(data) {
const hasFileUrl = fileUrl && typeof fileUrl === 'string' && fileUrl.length > 0;
const fileSource = hasLocalPath ? localPath : (hasFileUrl ? fileUrl : null);
if (!fileSource) {
return httpExec(); // 无文件源直接走 4G
console.log('[100J] 语音上传:无 fileUrl/localPath走 4G');
return httpExec(); // 无文件源:直接 4G原有逻辑
}
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress); // fileType=1 语音
return execWithBleFirst(bleExec, httpExec, '语音文件上传');
console.log('[100J] 语音上传:有文件源,蓝牙优先', { isBleConnected: protocolInstance.isBleConnected, bleDeviceId: protocolInstance.bleDeviceId || '-' });
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress);
return execWithBleFirst(bleExec, httpExec, '语音文件上传', data.onWaiting);
}
// 100J信息
export function deviceDetail(id) {
@ -409,31 +519,22 @@ export function deviceDetail(id) {
})
}
// 优先蓝牙:未连接时先尝试重连;蓝牙发送失败时回退4G
function execWithBleFirst(bleExec, httpExec, logName) {
const doBle = () => {
return bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
};
const do4G = () => {
console.log('[100J-4G]', logName, '已通过HTTP发送', '(蓝牙不可用)');
return httpExec().then(res => { res._channel = '4g'; return res; });
};
if (protocolInstance.isBleConnected) {
console.log('[100J-蓝牙]', logName, '(连接正常)');
return doBle().catch(err => {
console.log('[100J] 蓝牙发送失败回退4G', err);
return do4G();
// 蓝牙优先、4G 兜底:未连接时先等待扫描/连接,再尝试重连;蓝牙失败时回退 4G
function execWithBleFirst(bleExec, httpExec, logName, onWaiting) {
const doBle = () => bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
const do4G = () => httpExec().then(res => { res._channel = '4g'; return res; });
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) {
return doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); });
}
// 无 bleDeviceId 时:可能扫描中,先等待连接(设备页在后台可能完成连接)
if (!protocolInstance.bleDeviceId) {
if (typeof onWaiting === 'function') onWaiting();
return waitForBleConnection(12000).then(connected => {
return connected ? doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); }) : do4G();
});
}
return tryReconnectBle(2500).then(reconnected => {
if (reconnected) {
console.log('[100J-蓝牙]', logName, '(重连成功)');
return doBle().catch(err => {
console.log('[100J] 蓝牙发送失败回退4G', err);
return do4G();
});
}
return do4G();
return reconnected ? doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); }) : do4G();
});
}

View File

@ -2,7 +2,7 @@
"name" : "星汉物联",
"appid" : "__UNI__A21EF43",
"description" : "设备管控",
"versionName" : "1.0.22",
"versionName" : "1.0.24",
"versionCode" : 101,
"transformPx" : false,
/* 5+App */
@ -56,7 +56,8 @@
"<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>",
"<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"/>",
"<uses-permission android:name=\"android.permission.a\"/>",
"<uses-permission android:name=\"android.permission.BLUETOOTH_CONNECT\"/>"
"<uses-permission android:name=\"android.permission.BLUETOOTH_CONNECT\"/>",
"<uses-permission android:name=\"android.permission.BLUETOOTH_SCAN\"/>"
],
"minSdkVersion" : 21,
"abiFilters" : [ "armeabi-v7a", "arm64-v8a", "x86" ],

View File

@ -194,7 +194,7 @@
<view class="slider-container">
<slider min="10" max="100" step="10" :disabled="false" :value="formData.sta_LightDimmer"
activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
@change="onBrightnessChanging" @changing="onBrightnessChanging" class="custom-slider" />
@change="onBrightnessChanged" @changing="onBrightnessChanging" class="custom-slider" />
</view>
<view class="line"></view>
@ -206,7 +206,7 @@
<view class="slider-container">
<slider min="0.5" max="10" step="0.5" :disabled="false" :value="formData.sta_LightFreq"
activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
@change="onFreqChanging" @changing="onFreqChanging" class="custom-slider" />
@change="onFreqChanged" @changing="onFreqChanging" class="custom-slider" />
</view>
<view class="line"></view>
@ -218,7 +218,7 @@
<view class="slider-container">
<slider min="1" max="8" step="1" :disabled="false" :value="formData.sta_VoiceVolume"
activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
@change="onVolumeChanging" @changing="onVolumeChanging" class="custom-slider" />
@change="onVolumeChanged" @changing="onVolumeChanging" class="custom-slider" />
</view>
@ -850,9 +850,11 @@
});
},
onFreqChanging(e){
this.formData.sta_LightFreq = e.detail.value;
},
//频率
onFreqChanging(e) {
onFreqChanged(e) {
let f = this.getDevice();
// #ifdef APP-PLUS
if (!f) {
@ -897,8 +899,11 @@
}, 100);
},
onVolumeChanging(e){
this.formData.sta_VoiceVolume = e.detail.value;
},
//音量
onVolumeChanging(e) {
onVolumeChanged(e) {
let f = this.getDevice();
// #ifdef APP-PLUS
if (!f) {
@ -938,8 +943,11 @@
}, 100);
},
onBrightnessChanging(e){
this.formData.sta_LightDimmer = e.detail.value;
},
// 亮度调节
onBrightnessChanging(e) {
onBrightnessChanged(e) {
let f = this.getDevice();
// #ifdef APP-PLUS
if (!f) {

View File

@ -50,7 +50,8 @@
</view>
<view class="item" @click.top="bleStatuToggle">
<text class="lbl">蓝牙状态</text>
<text class="value" :class="formData.bleStatu?'green':'red'">{{device.getbleStatu}}</text>
<text class="value"
:class="(!formData.bleStatu || formData.bleStatu==='err')?'red':'green'">{{getbleStatu}}</text>
</view>
<view class="item">
<text class="lbl">设备状态</text>
@ -243,6 +244,7 @@
deviceUpdateVolume,
deviceVoiceBroadcast,
updateBleStatus,
getBleStatus,
parseBleData,
fetchBlePowerStatus,
fetchBleLocation
@ -468,6 +470,17 @@
onLoad: function() {
const eventChannel = this.getOpenerEventChannel();
var these = this;
this.$watch("deviceInfo.batteryPercentage", (newVal, oldVal) => {
if (newVal <= 20) {
uni.showToast({
title: '设备电量低',
icon: 'none',
duration: 2000
});
}
});
eventChannel.on('detailData', function(data) {
var device = data.data;
these.device = device;
@ -606,9 +619,11 @@
console.log(this.activePermissions, 'this.activePermissions');
these.fetchDeviceDetail(data.data.deviceId)
}
// 尝试连接蓝牙:需先扫描获取 BLE deviceId不能直接用 MAC
// 尝试连接蓝牙:需先扫描获取 BLE deviceId不能直接用 MAC;延迟 500ms 确保蓝牙适配器就绪
if (data.data.deviceMac) {
these.tryConnect100JBle(data.data.deviceMac);
setTimeout(() => {
these.tryConnect100JBle(data.data.deviceMac);
}, 500);
}
});
this.createThrottledFunctions();
@ -640,10 +655,53 @@
this.Status.pageHide = false;
},
computed: {
getbleStatu() {
if (this.formData.bleStatu === true) {
return '已连接';
}
if (this.formData.bleStatu === 'connecting') {
return '连接中';
}
if (this.formData.bleStatu === 'dicconnect') {
return '正在断开';
}
if (this.formData.bleStatu === 'err') {
return '连接异常';
}
return '未连接';
}
},
methods: {
bleStatuToggle() {
let f = bleTool.data.LinkedList.find((v) => {
return v.macAddress == this.device.deviceMac;
});
if (!f) {
this.tryConnect100JBle(this.device.deviceMac);
return;
}
if (this.formData.bleStatu === true) {
this.formData.bleStatu = 'dicconnect';
bleTool.disconnectDevice(f.deviceId).finally(r => {
this.formData.bleStatu = false;
});
return;
}
if (this.formData.bleStatu === false || this.formData.bleStatu === 'err') {
this.formData.bleStatu = 'connecting';
bleTool.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
these.formData.bleStatu = true;
}).catch(ex => {
these.formData.bleStatu = 'err';
});
return;
}
},
createThrottledFunctions() {
// 创建节流函数
this.throttledBrightnessChange = this.throttle(this.handleBrightnessChange, 500);
@ -714,16 +772,17 @@
}
})
},
// 语音管理
// 语音管理(传递蓝牙状态,确保子页走蓝牙优先)
audioManager(item) {
if (this.Status.apiType !== 'listA') {}
const ble = getBleStatus();
uni.navigateTo({
url: '/pages/100J/audioManager/AudioList',
events: {},
success: (res) => {
// 页面跳转成功后的回调函数
res.eventChannel.emit('deviceData', {
data: item
data: item,
ble
});
},
});
@ -989,42 +1048,73 @@
deviceRecovry(res) {},
deviceDispose(res) {},
// 100J 蓝牙连接:先查缓存/尝试直连,失败则扫描createBLEConnection 需要扫描返回的 deviceId
// 100J 蓝牙连接:先查缓存/尝试直连,失败则扫描
// 注意Android 的 createBLEConnection 需要系统返回的 deviceId服务端 MAC(11:22:33:44:55:02) 与 deviceId(02:55:44:33:22:11) 字节序相反
tryConnect100JBle(deviceMac) {
const that = this;
const macNorm = (m) => (m || '').replace(/:/g, '').toUpperCase();
const targetMacNorm = macNorm(deviceMac);
const last6 = targetMacNorm.slice(-6);
// Android BLE deviceId 多为 MAC 字节反序,如 11:22:33:44:55:02 -> 02:55:44:33:22:11
const macToDeviceId = (mac) => {
const parts = (mac || '').split(':').filter(Boolean);
return parts.length === 6 ? parts.reverse().join(':') : mac;
};
// 1. 查缓存:之前连过且 mac 匹配
const cached = bleTool.data.LinkedList.find(v => {
const m = macNorm(v.macAddress);
return m === targetMacNorm || m.slice(-6) === last6;
});
const SVC = '0000AE30-0000-1000-8000-00805F9B34FB';
const WRITE = '0000AE03-0000-1000-8000-00805F9B34FB';
const NOTIFY = '0000AE02-0000-1000-8000-00805F9B34FB';
if (cached && cached.deviceId) {
console.log('[100J] 使用缓存设备连接', cached.deviceId);
bleTool.LinkBlue(cached.deviceId).then(() => {
that.formData.bleStatu = 'connecting';
bleTool.LinkBlue(cached.deviceId, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(缓存)');
that.bleStateRecovry({ deviceId: cached.deviceId });
that.formData.bleStatu = true;
that.bleStateRecovry({
deviceId: cached.deviceId
});
}).catch(err => {
console.log('100J 蓝牙连接失败(缓存),尝试扫描', err);
that.formData.bleStatu = 'err';
that.connect100JByScan(deviceMac, last6);
});
return;
}
// 2. 无缓存:先尝试直连Android 上 deviceId 可能为 MAC
console.log('[100J] 尝试直连', deviceMac);
bleTool.LinkBlue(deviceMac).then(() => {
console.log('100J 蓝牙连接成功(直连)');
that.bleStateRecovry({ deviceId: deviceMac });
}).catch(err => {
console.log('100J 蓝牙直连失败,开始扫描', err);
// 2. 无缓存:先尝试直连Android 上 deviceId 为 MAC 反序(11:22:33:44:55:02->02:55:44:33:22:11)
that.formData.bleStatu = 'connecting';
const tryDirect = (id) => bleTool.LinkBlue(id, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(直连)', id);
that.formData.bleStatu = true;
that.bleStateRecovry({
deviceId: id
});
}).catch(ex => {
that.formData.bleStatu = 'err';
});
const deviceIdReversed = macToDeviceId(deviceMac);
console.log('[100J] 尝试直连', deviceIdReversed, '(MAC反序)');
tryDirect(deviceIdReversed).catch(() => {
if (deviceIdReversed !== deviceMac) {
console.log('[100J] 反序直连失败,尝试原 MAC', deviceMac);
return tryDirect(deviceMac);
}
return Promise.reject();
}).catch(() => {
console.log('[100J] 蓝牙直连失败,开始扫描');
that.connect100JByScan(deviceMac, last6);
});
},
connect100JByScan(deviceMac, last6) {
const that = this;
const SVC = '0000AE30-0000-1000-8000-00805F9B34FB';
const WRITE = '0000AE03-0000-1000-8000-00805F9B34FB';
const NOTIFY = '0000AE02-0000-1000-8000-00805F9B34FB';
let resolved = false;
const timeout = 15000;
const timer = setTimeout(() => {
@ -1048,11 +1138,16 @@
bleTool.StopSearch();
bleTool.removeDeviceFound('HBY100J_SCAN');
console.log('[100J] 扫描到目标设备', match.name, match.deviceId);
bleTool.LinkBlue(match.deviceId).then(() => {
that.formData.bleStatu = 'connecting';
bleTool.LinkBlue(match.deviceId, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(扫描)');
that.bleStateRecovry({ deviceId: match.deviceId });
that.formData.bleStatu = true;
that.bleStateRecovry({
deviceId: match.deviceId
});
}).catch(err => {
console.log('100J 蓝牙连接失败将使用4G', err);
that.formData.bleStatu = 'err';
});
}
}, 'HBY100J_SCAN');
@ -1069,6 +1164,7 @@
});
},
bleStateBreak() {
this.formData.bleStatu = false;
updateBleStatus(false, '', this.deviceInfo.deviceId);
},
bleStateRecovry(res) {
@ -1093,7 +1189,8 @@
previewImg(img) {},
bleValueNotify: function(receive, device, path, recArr) { //订阅消息
// 仅处理当前设备的数据device 为 LinkedList 中匹配 receive.deviceId 的项)
if (device && device.device && this.deviceInfo.deviceId && device.device.id != this.deviceInfo.deviceId) return;
if (device && device.device && this.deviceInfo.deviceId && device.device.id != this.deviceInfo
.deviceId) return;
// 解析蓝牙上报数据 (协议: FC=MAC主动上报, FB=指令响应)
if (!receive.bytes || receive.bytes.length < 3) return;
const parsedData = parseBleData(receive.bytes);
@ -1122,9 +1219,62 @@
this.$set(this.deviceInfo, 'batteryRemainingTime', parsedData.batteryRemainingTime);
}
if (this.deviceInfo.batteryPercentage <= 20) {
this.showMsg("设备电量低");
// 设备物理按键切换:蓝牙响应同步到 APP 页面(与 4G MQTT 逻辑一致)
const fc = parsedData.funcCode;
// 0x0C 强制报警:设备按键切换报警状态
if (fc === 0x0C && parsedData.alarmEnable !== undefined) {
if (parsedData.alarmEnable === 1) {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', 1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
} else {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', -1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
}
}
// 0x0A 爆闪模式:警示灯开关/模式
if (fc === 0x0A && parsedData.strobeEnable !== undefined) {
if (parsedData.strobeEnable === 1) {
this.formData.sta_LightType = (parsedData.strobeMode ?? 0) + '';
} else {
this.formData.sta_LightType = '-1';
}
}
// 0x0E 工作方式:综合状态(设备按键切换后上报)
if (fc === 0x0E) {
if (parsedData.strobeEnable !== undefined) {
if (parsedData.strobeEnable === 1) {
this.formData.sta_LightType = (parsedData.strobeMode ?? 0) + '';
} else {
this.formData.sta_LightType = '-1';
}
}
if (parsedData.alarmEnable !== undefined) {
if (parsedData.alarmEnable === 1) {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', 1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
} else {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', -1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
}
}
if (parsedData.voiceBroadcast !== undefined) {
if (parsedData.voiceBroadcast === 1) this.formData.sta_VoiceType = '7';
else if (this.formData.sta_VoiceType === '7') this.formData.sta_VoiceType = '-1';
}
if (parsedData.volume !== undefined) this.formData.volume = parsedData.volume;
if (parsedData.strobeFrequency !== undefined) this.formData.strobeFrequency = parsedData
.strobeFrequency;
if (parsedData.redBrightness !== undefined) this.formData.lightBrightness = parsedData
.redBrightness;
}
// 0x09 音量、0x0D 亮度:单独响应时同步
if (fc === 0x09 && parsedData.volume !== undefined) this.formData.volume = parsedData.volume;
if (fc === 0x0B && parsedData.strobeFrequency !== undefined) this.formData.strobeFrequency = parsedData
.strobeFrequency;
if (fc === 0x0D && parsedData.redBrightness !== undefined) this.formData.lightBrightness = parsedData
.redBrightness;
},
showBleUnConnect() {},

View File

@ -104,8 +104,10 @@
deviceVoliceList,
videRenameAudioFile,
deviceDeleteAudioFile,
deviceUpdateVoice
deviceUpdateVoice,
updateBleStatus
} from '@/api/100J/HBY100-J.js'
import { baseURL } from '@/utils/request.js'
import {
showLoading,
hideLoading,
@ -217,6 +219,10 @@
console.log(rec, 'ressss');
this.blue = rec.ble;
this.device = rec.data;
// 同步蓝牙状态,确保语音上传走蓝牙优先
if (rec.ble && (rec.ble.isConnected || rec.ble.bleDeviceId)) {
updateBleStatus(!!rec.ble.isConnected, rec.ble.bleDeviceId || '', rec.data?.deviceId || '');
}
this.getinitData(rec.data.deviceId, true)
});
@ -237,7 +243,9 @@
if (!deviceId) return;
const mergeLocal = (serverList) => {
const key = `100J_local_audio_${deviceId}`;
const cacheKey = `100J_local_path_cache_${deviceId}`;
const localList = uni.getStorageSync(key) || [];
const pathCache = uni.getStorageSync(cacheKey) || {};
const localMapped = localList.map(item => ({
...item,
fileNameExt: item.name || '本地语音',
@ -246,7 +254,12 @@
useStatus: 0,
_isLocal: true
}));
return [...localMapped, ...(serverList || [])];
const enriched = (serverList || []).map(item => {
const urlKey = item.fileUrl || item.url || item.filePath || item.audioUrl || item.ossUrl;
const localPath = pathCache[urlKey] || pathCache[item.id];
return localPath ? { ...item, localPath } : item;
});
return [...localMapped, ...enriched];
};
deviceVoliceList({ deviceId }).then((res) => {
if (res.code == 200) {
@ -486,11 +499,22 @@
Apply(item, index) {
this.updateProgress = 0;
this.isUpdating = true;
// 本地项优先用 localPath云端项用 fileUrl兼容多种字段名相对路径补全 baseURL
let fileUrl = '';
let localPath = (item.localPath && typeof item.localPath === 'string') ? item.localPath : '';
if (!item._isLocal) {
const raw = item.fileUrl || item.url || item.filePath || item.audioUrl || item.ossUrl || '';
fileUrl = (typeof raw === 'string' && raw) ? (raw.startsWith('/') ? (baseURL + raw) : raw) : '';
} else {
// 本地项localPath 优先,无则用 fileUrlmergeLocal 中可能只有 fileUrl 存路径)
if (!localPath && item.fileUrl) localPath = item.fileUrl;
}
const data = {
id: item.id,
fileUrl: item._isLocal ? '' : (item.fileUrl || item.url),
localPath: item._isLocal ? item.localPath : '',
onProgress: (p) => { this.updateProgress = p; }
fileUrl,
localPath,
onProgress: (p) => { this.updateProgress = p; },
onWaiting: () => { uni.showToast({ title: '等待蓝牙连接中...', icon: 'none', duration: 2000 }); }
};
// 整体超时 60 秒仅影响蓝牙上传4G HTTP 很快返回)
const overallTimer = setTimeout(() => {
@ -502,11 +526,10 @@
}, 60000);
deviceUpdateVoice(data).then((RES) => {
clearTimeout(overallTimer);
console.log(RES, 'RES');
if (RES.code == 200) {
// 蓝牙上传:进度已由 onProgress 更新,直接完成
if (RES._channel === 'ble') {
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
this.isUpdating = false;
setTimeout(() => { uni.navigateBack(); }, 1500);
return;
@ -514,7 +537,7 @@
// 4G订阅 MQTT 获取设备端进度6 秒超时
this.upgradeTimer = setTimeout(() => {
if (this.isUpdating) {
uni.showToast({ title: '升级进度同步超时', icon: 'none', duration: 2000 });
uni.showToast({ title: '音频进度同步超时', icon: 'none', duration: 2000 });
this.isUpdating = false;
this.updateProgress = 0;
}
@ -530,7 +553,7 @@
this.updateProgress = progress;
if (progress === 100) {
clearTimeout(this.upgradeTimer);
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
this.isUpdating = false;
setTimeout(() => { uni.navigateBack(); }, 1500);
}

View File

@ -422,25 +422,42 @@
}, 1200);
},
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS
// 将临时文件复制到持久化目录 _doc/100J_audio/,避免被系统清理
saveLocalForBle(filePath) {
const deviceId = these.Status.ID;
if (!deviceId) return;
const item = {
...these.cEdit,
localPath: filePath,
fileUrl: '',
deviceId,
id: 'local_' + these.cEdit.Id,
_createTime: these.cEdit.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
_isLocal: true
const doSave = (persistentPath) => {
const item = {
...these.cEdit,
localPath: persistentPath,
fileUrl: '',
deviceId,
id: 'local_' + these.cEdit.Id,
_createTime: these.cEdit.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
_isLocal: true
};
const key = `100J_local_audio_${deviceId}`;
let list = uni.getStorageSync(key) || [];
list.unshift(item);
uni.setStorageSync(key, list);
these.AudioData.tempFilePath = "";
these.Status.isRecord = false;
uni.navigateBack();
};
const key = `100J_local_audio_${deviceId}`;
let list = uni.getStorageSync(key) || [];
list.unshift(item);
uni.setStorageSync(key, list);
these.AudioData.tempFilePath = "";
these.Status.isRecord = false;
uni.navigateBack();
if (typeof plus !== 'undefined' && plus.io) {
const fileName = 'audio_' + (these.cEdit.Id || Date.now()) + '.mp3';
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => {
docEntry.getDirectory('100J_audio', { create: true }, (dirEntry) => {
entry.copyTo(dirEntry, fileName, (newEntry) => {
doSave(newEntry.fullPath);
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
} else {
doSave(filePath);
}
},
// 保存录音并上传(已修复文件格式问题)
uploadLuYin() {
@ -507,6 +524,19 @@
}
const resData = JSON.parse(res.data);
if (resData.code === 200) {
// 缓存本地路径Apply 时优先用本地文件走蓝牙,避免下载失败
const deviceId = these.Status.ID;
if (deviceId) {
const cacheKey = `100J_local_path_cache_${deviceId}`;
const d = resData.data;
const fileUrl = (d && typeof d === 'object' && d.fileUrl) || (typeof d === 'string' ? d : '');
if (filePath) {
let cache = uni.getStorageSync(cacheKey) || {};
if (fileUrl) cache[fileUrl] = filePath;
if (d && typeof d === 'object' && d.id) cache[d.id] = filePath;
uni.setStorageSync(cacheKey, cache);
}
}
// 合并两个存储操作
Promise.all([
new Promise((resolve, reject) => {

View File

@ -60,12 +60,12 @@
</view>
<view class="item" @click.top="bleStatuToggle">
<text class="lbl">蓝牙状态</text>
<text class="value" :class="formData.bleStatu?'green':'red'">{{getbleStatu}}</text>
<text class="value" :class="(!formData.bleStatu || formData.bleStatu==='err')?'red':'green'">{{getbleStatu}}</text>
</view>
<view class="item">
<text class="lbl">信道:{{formData.sta_Channel}}</text>
<text class="value green" @click.stop="ShowChannelEdit()">修改</text>
</view>
<!-- <view class="item">
<text class="lbl">设备状态</text>
<text class="value">{{formData.sta_charge?dic.sta_charge[formData.sta_charge]:"" }}</text>
</view> -->
</view>
<view class="warnnig" v-for="item,index in warnDevices">
@ -177,7 +177,14 @@
:iconUrl="Status.Pop.iconUrl" :message="Status.Pop.message" :buttonText="Status.Pop.buttonText"
@buttonClick="HidePop" :visiblePrompt="Status.Pop.visiblePrompt" :promptTitle="Status.Pop.promptTitle"
v-model="Status.Pop.modelValue" @closePop="closePop" :buttonCancelText="Status.Pop.buttonCancelText"
:showCancel="Status.Pop.showCancel" @cancelPop="closePop" />
:showCancel="Status.Pop.showCancel" @cancelPop="closePop" :showSlot="Status.Pop.showSlot">
<view v-if="Status.ShowEditChannel" class="popup-prompt">
<text class="popup-prompt-title">修改信道</text>
<input class="popup-prompt-input" type="number" placeholder="1-125的整数"
placeholder-class="popup-prompt-input-placeHolder" v-model="formData.ins_Channel" />
</view>
</MessagePopup>
<!-- 下方菜单 -->
@ -221,6 +228,7 @@
curr: 0,
total: 0,
pageHide: false,
ShowEditChannel: false,
Pop: {
showPop: false, //是否显示弹窗
popType: 'custom',
@ -325,8 +333,8 @@
warnTime: '',
sta_sosadd: "",
sta_sosName: '',
sta_IntrusTime: ''
sta_IntrusTime: '',
ins_Channel:23
},
dic: {
sta_LightType: [
@ -519,7 +527,7 @@
});
}
this.getLinkedCnt();
// this.getLinkedCnt();
},
computed: {
Distance: function() {
@ -561,6 +569,73 @@
}
},
methods: {
onChannelChanging() {
let f = this.getDevice();
// #ifdef APP-PLUS
if (!f) {
this.showBleUnConnect();
return;
}
// #endif
let regex = /^([1-9]|[1-7][0-9]|80)$/;
if (!regex.test(this.formData.ins_Channel)) {
uni.showModal({
title: '提示',
content: '只能输入1-80整数'
});
return;
}
var buffer = {
ins_Channel: this.formData.ins_Channel
}
ble.sendString(f.deviceId, buffer);
},
ShowChannelEdit() {
if (!this.permissions.includes('55') && this.Status.apiType !== 'listA') {
this.showPop({
message: '无操作权限',
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
okCallback: null,
buttonText: "确定"
})
return;
}
this.Status.ShowEditChannel = true;
this.showPop({
showPop: true, //是否显示弹窗
popType: 'custom',
bgColor: '#383934bd',
borderColor: '#BBE600',
textColor: '#ffffffde',
buttonBgColor: '#BBE600',
buttonTextColor: '#232323DE',
iconUrl: '',
message: '',
buttonText: '确定',
clickEvt: '',
visiblePrompt: false,
promptTitle: '',
modelValue: '',
visibleClose: false,
okCallback: () => {
this.onChannelChanging();
this.Status.ShowEditChannel = false;
},
showSlot: true,
buttonCancelText: '取消',
showCancel: true,
cancelCallback: () => {
this.Status.ShowEditChannel = false;
}
});
},
getLinkedCnt() { //获取在线设备的数量
let f = this.getDevice();
@ -2266,4 +2341,30 @@
/deep/ .uni-navbar--fixed {
top: 0px;
}
.popup-prompt-input {
width: 100%;
height: 60rpx;
line-height: 60rpx;
color: rgba(255, 255, 255, 0.87);
box-sizing: border-box;
border-bottom: 2rpx solid rgba(255, 255, 255, 0.4);
font-size: 28rpx;
font-weight: 400;
letter-spacing: 0.14px;
}
.popup-prompt-input-placeHolder {
color: rgba(255, 255, 255, 0.4);
font-family: PingFang SC;
font-size: 28rpx;
font-weight: 400;
letter-spacing: 0.14px;
text-align: center;
}
</style>

View File

@ -10,8 +10,8 @@
</uni-nav-bar>
<view class="contentBg">
<view class="row">
<view class="typeItem fleft" :class="{'active':Status.tabType=='link'}" @click.stop="tabChange('link')">
连接记录</view>
<!-- <view class="typeItem fleft" :class="{'active':Status.tabType=='link'}" @click.stop="tabChange('link')">
连接记录</view> -->
<view class="typeItem fleft" :class="{'active':Status.tabType=='warn'}" @click.stop="tabChange('warn')">
报警记录</view>
<view class="filterIco fright" @click.stop="showFilter()">
@ -90,7 +90,7 @@
<view class="mothItem center" :class="{active:Status.filterDayType=='monthThree'}"
@click.stop="mothItemChange('monthThree')">近三个月</view>
<view class="mothItem center" :class="{active:Status.filterDayType=='monthOne'}"
@click.stop="mothItemChange('monthOne')" }>近一个月</view>
@click.stop="mothItemChange('monthOne')" >近一个月</view>
<view class="mothItem center" :class="{active:Status.filterDayType=='customer'}"
@click.stop="mothItemChange('customer')">自定义</view>
@ -204,7 +204,7 @@
filterMode: true, //是否筛选日期
filterDayMode: 'end', //当前筛选的是开始还是结束
filterDayType: 'customer', //当前筛选的日期是哪种类型
tabType: 'link',
tabType: 'warn',
datePickValue: [999, 999, 999] //日期选择控件绑定的值
@ -251,10 +251,10 @@
let eventChannel = this.getOpenerEventChannel();
eventChannel.on('detailData', function(data) {
eventChannel.on('detailData', (data)=> {
var device = data.data;
these.device = device;
these.tabChange('link');
this.device = device;
this.tabChange('warn');
});
@ -304,7 +304,7 @@
let today = new Date();
let end = Common.DateFormat(today, 'yyyy-MM-dd');
let start = Common.DateFormat(today.setMonth(diff), 'yyyy-MM-dd');
let start = Common.DateFormat(today.setMonth(today.getMonth()+ diff), 'yyyy-MM-dd');
this.filter.start = start;
this.filter.end = end;
@ -595,7 +595,9 @@
if (!these.Status.filterMode) {
if (these.filter.MM) {
start = new Date(these.filter.MM + '-01');
end = start.setMonth(1);
end = new Date(these.filter.MM + '-01')
end.setMonth(end.getMonth()+1);
end.setDate(end.getDate()+1);
data = data.filter(v => {
let t = v.linkEqs[0].linkTime
@ -613,7 +615,7 @@
console.log("these.filter.end=",these.filter.end);
start = new Date(these.filter.start);
end = new Date(these.filter.end);
end.setMonth(1);
end.setDate(end.getDate()+1);
console.log("start=",start);
console.log("end=",end);
if(start && end){
@ -643,7 +645,8 @@
return t >= start;
});
} else if (these.filter.end) {
end = new Date(these.filter.end).setMonth(1);
end = new Date(these.filter.end);
end.setDate(end.getDate()+1);
data = data.filter(v => {
@ -680,7 +683,7 @@
if (systemInfo.uniPlatform == 'web') {
for (let i = 0; i < 20; i++) {
const timestamp = 1710072900000 + (i * 3600000); // 每小时递增
const timestamp = new Date().getTime() - (i * 3600000); // 每小时递增
const numItems = Math.floor(Math.random() * 5) + 1; // 1-5条
const item = {
@ -740,7 +743,8 @@
console.log("hese.filter.end=" + these.filter.end);
let start = new Date(these.filter.start);
let end = new Date(these.filter.end);
end.setMonth(1);
end.setDate(end.getDate()+ 1);
data = data.filter(v => {
let t = v.warnTime
@ -748,7 +752,7 @@
t = new Date(v.warnTime);
}
return t >= start && t <= end;
return t >= start && t < end;
});
} else if (these.filter.start) {
let start = new Date(these.filter.start);
@ -762,7 +766,8 @@
return t >= start;
});
} else if (these.filter.end) {
let end = new Date(these.filter.end).setMonth(1);
let end = new Date(these.filter.end);
end.setDate(end.getDate()+ 1);
data = data.filter(v => {
let t = v.warnTime
if (!(t instanceof Date)) {
@ -774,7 +779,9 @@
} else {
if (these.filter.MM) {
let start = new Date(these.filter.MM + '-01');
let end = start.setMonth(1);
let end = new Date(these.filter.MM + '-01');
end.setMonth(end.getMonth()+1);
end.setDate(end.getDate()+1);
data = data.filter((v) => {
let t = v.warnTime
if (!(t instanceof Date)){

View File

@ -74,7 +74,7 @@
<view class="slider-container">
<slider min="1" max="100" step="1" :disabled="false" :value="formData.liangDu" activeColor="#bbe600"
backgroundColor="#00000000" block-size="20" block-color="#ffffffde" @change="sliderChange"
backgroundColor="#00000000" block-size="20" block-color="#ffffffde" @change="sliderChange" @changing="sliderChanging"
class="custom-slider" />
</view>
@ -385,7 +385,7 @@
}
let f = ble.data.LinkedList.find((v) => {
if (v.macAddress == device.deviceMac) {
console.log("找到设备了", v);
// console.log("找到设备了", v);
these.formData.deviceId = v.deviceId;
return true;
}
@ -824,7 +824,7 @@
let packetData = these.rgb565Data.slice(startIndex,
endIndex);
// 构建数据包
let bufferSize = os == 'Android' ? 505 :133; // 5 + packetData.length * 2; // 头部5字节 + 数据部分
let bufferSize = os ==packetSize.length*2+5; // 5 + packetData.length * 2; // 头部5字节 + 数据部分
let buffer = new ArrayBuffer(bufferSize);
let dataView = new DataView(buffer);
@ -1784,6 +1784,9 @@
});
},
sliderChanging(evt){
this.formData.liangDu = evt.detail.value;
},
sliderChange: function(evt) {
if (!this.permissions.includes('1') && this.Status.apiType !== 'listA') {
@ -1834,10 +1837,10 @@
dataView.setUint8(0, 0x55); // 帧头
dataView.setUint8(1, 0x07); // 帧类型:亮度调节
dataView.setUint8(2, 0x01); // 包序号
dataView.setUint8(3, 0x00); // 包序号
dataView.setUint8(4, 0x01); // 数据长度
dataView.setUint8(5, liangDu); // 数据长度
dataView.setUint8(2, 0x01);
dataView.setUint8(3, 0x00);
dataView.setUint8(4, 0x01); //
dataView.setUint8(5, liangDu); // 数据
let f = this.getDevice();
if (f) {

View File

@ -74,7 +74,7 @@
<view class="slider-container">
<slider min="1" max="100" step="1" :disabled="false" :value="formData.liangDu" activeColor="#bbe600"
backgroundColor="#00000000" block-size="20" block-color="#ffffffde" @change="sliderChange"
@changing="sliderChange" class="custom-slider" />
@changing="sliderChanging" class="custom-slider" />
</view>
</view>
@ -1394,6 +1394,10 @@ debugger;
});
},
sliderChanging(evt){
this.formData.liangDu = evt.detail.value;
},
sliderChange: function(evt) {
this.formData.liangDu = evt.detail.value;
clearTimeout(BrighInteval)

View File

@ -53,7 +53,9 @@
Statu: {
bound: null,
timeInteval: null,
isSearch: false
isSearch: false,
pageHide:false,
isBind:false
},
device: {
"deviceId": "",
@ -115,17 +117,32 @@
return "";
}
},
onShow() {
this.Statu.pageHide=false;
},
onHide() {
this.Statu.pageHide=true;
},
onUnload() {
console.log("返回取消订阅");
clearInterval(inteval);
ble.removeAllCallback(pagePath);
if(!this.Statu.isBind && these.device.deviceId){
ble.disconnectDevice(these.device.deviceId).catch(ex=>{
console.error("无法断开蓝牙连接");
});
}
},
onLoad(option) {
these = this;
ble = bleTool.getBleTool();
ble.addStateBreakCallback(res => {
if(this.Statu.pageHide){
return;
}
these.device.linkStatu = false;
hideLoading(these);
uni.showToast({
@ -135,6 +152,9 @@
},pagePath);
ble.addStateRecoveryCallback(res => {
if(this.Statu.pageHide){
return;
}
if (these.device.deviceId) {
showLoading(these, {
text: '蓝牙已恢复正在连接设备'
@ -144,6 +164,9 @@
}
},pagePath);
ble.addDisposeCallback(res => {
if(this.Statu.pageHide){
return;
}
console.log("处理蓝牙断开连接");
these.device.linkStatu = false;
if (these.device.deviceId == res.deviceId) {
@ -156,6 +179,9 @@
},pagePath);
ble.addReceiveCallback((receive, f, path) => {
if(this.Statu.pageHide){
return;
}
console.log("收到设备消息,", receive);
if (these.device.deviceId == receive.deviceId) {
console.log("11111:", receive);
@ -372,6 +398,7 @@
clearTimeout(this.Statu.timeInteval);
this.device.macAddress = null;
this.Statu.timeInteval = null;
this.Statu.isBind=true;
uni.$emit("refreshDeviceList");
setTimeout(() => {
uni.switchTab({

View File

@ -23,9 +23,10 @@
<view class="p100">
<view class="lblTitle">配对设备</view>
<view class="list" style="margin-bottom: 30rpx;">
<view class="item " v-for="item, index in PairEquip" v-show="PairEquip.length>0">
<view class="item " @click.stop="disConnect(item,index)" v-for="item, index in PairEquip" v-show="PairEquip.length>0">
<view class="leftImg ">
<image src="/static/images/common/bluetooth.png" class="titleIco filterNone"
mode="heightFix">
@ -33,9 +34,7 @@
</view>
<view class="centertxt ">
<view class="name" v-text="item.name"></view>
<view class="id">
<text>信号:{{item.RSSI}}dBm</text>
</view>
</view>
<view class="rightIco center">
<image src="/static/images/BLEAdd/linked.png" class="img" mode="aspectFit">
@ -134,6 +133,7 @@
data() {
return {
Status: {
navigateTO: false,
isPageHidden: false,
intval: null,
time: null,
@ -165,11 +165,14 @@
},
search: '', //筛选
PairEquip: [], //已配对设备
tmpLink:[],//本次已配对
EquipMents: [], //搜索出来的设备
device: null,
item: {
deviceId: ''
}
},
}
},
computed: {
@ -189,9 +192,20 @@
if (ble) {
ble.StopSearch();
ble.removeAllCallback(pagePath);
if (!this.device && !this.Status.navigateTO) {
if (this.tmpLink && this.tmpLink.length && this.tmpLink.length > 0) {
console.error("页面卸载时,断开所有连接")
let f = this.tmpLink.forEach((v) => {
ble.disconnectDevice(v.deviceId).catch(ex => {
console.error("无法断开设备连接", ex);
});
});
}
}
}
},
onLoad(option) {
debugger;
eventChannel = this.getOpenerEventChannel();
eventChannel.on('detailData', function(rec) {
@ -211,8 +225,8 @@
const systemInfo = uni.getSystemInfoSync();
ble = bleTool.getBleTool(); // Ensure ble is initialized
// Ensure ble is initialized
if (systemInfo.uniPlatform == 'web') {
@ -236,86 +250,19 @@
"name": "EF4651",
"linkStatu": false,
"isTarget": true
},
{
"RSSI": -69,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "4F0DAC91-4391-CB07-905E-72D7F03EFCD3",
"name": "4877-BF743D",
"linkStatu": false
},
{
"RSSI": -55,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "EBDA4E6F-3A28-FF65-A845-AE8CC7B78375",
"name": "HBY670-BF74EA",
"linkStatu": false
},
{
"RSSI": -61,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "469FB381-B47E-1E40-8073-EF50B5704AAB",
"name": "EF4651",
"linkStatu": false,
"isTarget": true
},
{
"RSSI": -69,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "4F0DAC91-4391-CB07-905E-72D7F03EFCD3",
"name": "4877-BF743D",
"linkStatu": false
}, {
"RSSI": -55,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "EBDA4E6F-3A28-FF65-A845-AE8CC7B78375",
"name": "HBY670-BF74EA",
"linkStatu": false
},
{
"RSSI": -61,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "469FB381-B47E-1E40-8073-EF50B5704AAB",
"name": "EF4651",
"linkStatu": false,
"isTarget": true
},
{
"RSSI": -69,
"advertisData": "",
"advertisServiceUUIDs": [
"0000FFE0-0000-1000-8000-00805F9B34FB"
],
"deviceId": "4F0DAC91-4391-CB07-905E-72D7F03EFCD3",
"name": "4877-BF743D",
"linkStatu": false
}
];
these.PairEquip = [this.EquipMents[0]];
this.$forceUpdate();
console.error("1111111111")
this.PairEquip=[this.EquipMents[0]];
return;
}
ble = bleTool.getBleTool();
this.refreshLinked();
let StartSubsrib = () => {
these.EquipMents = [];
if (!ble) {
ble = bleTool.getBleTool();
@ -327,6 +274,7 @@
}
console.log("处理蓝牙不可用");
hideLoading(these);
console.error("1111111111")
these.PairEquip = [];
these.EquipMents = [];
uni.showToast({
@ -336,13 +284,14 @@
these.showOpenSetting();
}, pagePath);
//蓝牙恢复可用的回调
ble.addStateRecoveryCallback(res=>{
ble.addStateRecoveryCallback(res => {
if (these.Status.isPageHidden) {
return;
}
these.Status.BottomMenu.show = false;
console.error("1111111111")
these.PairEquip = [];
these.EquipMents = [];
uni.showToast({
@ -350,7 +299,7 @@
title: '蓝牙恢复可用'
});
these.refreshBleList();
}),pagePath;
}), pagePath;
//蓝牙断开连接的回调
ble.addDisposeCallback(res => {
@ -360,10 +309,7 @@
// console.log("处理蓝牙断开连接");
these.PairEquip.find(function(v, ind) {
these.PairEquip.splice(ind, 1);
return v.deviceId == res.deviceId;
});
these.refreshLinked();
setTimeout(() => {
hideLoading(these);
@ -422,39 +368,31 @@
these.EquipMents.sort((a, b) => b.RSSI - a.RSSI); //信号好的排前面,一般信号好的是目标设备
}
}, pagePath);
//蓝牙连接已恢复的回调
ble.addRecoveryCallback(res => {
if (these.Status.isPageHidden) {
return;
}
these.refreshLinked();
// hideLoading(these);
if (!these.device) {
hideLoading(these);
}else{
clearInterval(this.Status.intval);
these.DeviceVerdict(res.deviceId);
}
}, pagePath);
}
let startValidDevice = () => {
if (these.device) {
console.log("进入配对模式,启用连接恢复和验证逻辑。");
//蓝牙连接已恢复的回调
ble.addRecoveryCallback(res => {
if (these.Status.isPageHidden) {
return;
}
// hideLoading(these);
these.EquipMents.find(function(v, ind) {
if (v.deviceId == res.deviceId) {
these.PairEquip.push(v);
return true;
}
return false;
});
if (these.device) {
clearInterval(this.Status.intval);
showLoading(these, {
text: '蓝牙连接已恢复,正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(res.deviceId);
}, 0);
} else {
hideLoading(these);
}
}, pagePath);
//收到设备的消息回调
ble.addReceiveCallback((receivData, f, path, arr) => {
@ -476,7 +414,7 @@
showLoading(these, {
text: '正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(f.deviceId);
}, 0);
@ -488,15 +426,33 @@
StartSubsrib();
},
onShow: function() {
debugger;
this.Status.isPageHidden = false;
this.Status.navigateTO = false;
this.refreshBleList();
this.refreshLinked();
},
methods: {
refreshLinked(){
if(ble){
let arr=[];
arr=ble.data.LinkedList.filter(v=>{
return v.Linked;
});
this.PairEquip=arr;
}
},
checkAndRequestLocationPermission() {
return new Promise((resolve) => {
@ -515,16 +471,21 @@
resolve(true);
} else {
// console.warn('定位权限被拒绝');
MsgClear(these);
showPop({headerTxt:'权限提醒',message:'扫描蓝牙设备,需要您开启定位权限',buttonText:'去开启',okCallback:uni.openSetting},these,true);
showPop({
headerTxt: '权限提醒',
message: '扫描蓝牙设备,需要您开启定位权限',
buttonText: '去开启',
okCallback: uni.openSetting
}, these, true);
resolve(false);
}
},
(error) => {
MsgError('请求定位权限失败:'+error.code,'确定',these);
MsgError('请求定位权限失败:' + error.code, '确定', these);
resolve(false);
}
);
@ -565,19 +526,19 @@
time = setTimeout(() => {
these.EquipMents = [];
these.PairEquip = [];
ble.StartSearch().then(result => {
console.log("开始搜索成功", result);
// console.log("开始搜索成功", result);
}).catch(err => {
console.error("开始搜索失败:", err);
if (err.code === 10001) {
these.showOpenSetting();
} else {
MsgClear(these);
MsgError('出现错误:' + err.msg,'确定',these);
MsgError('出现错误:' + err.msg, '确定', these);
}
}).finally(() => {
@ -587,8 +548,8 @@
}
ble.StopSearch().catch(err=>{
console.error("err=",err);
ble.StopSearch().catch(err => {
console.error("err=", err);
}).finally(startSearch);
@ -716,13 +677,14 @@
text: "等待设备上报Mac地址," + these.Status.time + 's'
});
console.log("11111111", this.Status.time);
clearInterval(this.Status.intval);
this.Status.intval = null;
this.Status.intval = setInterval(() => {
this.Status.time = this.Status.time - 1;
if (this.Status.time < 0) {
hideLoading(these);
console.log("停止倒计时", this.Status.time);
clearInterval(this.Status.intval)
this.Status.intval = null;
@ -742,9 +704,12 @@
}
updateLoading(these, {
text: "等待设备上报Mac地址," + these.Status.time + 's'
});
}, 1000);
return undefined;
}
}
@ -760,14 +725,7 @@
let index = 1;
let total = 5;
let linkCallback = (res) => {
let c = these.PairEquip.find(function(v) {
return v.deviceId == item.deviceId;
});
if (!c) {
these.PairEquip.push(item);
}
let linkCallback = (res) => {
console.log("连接成功", these.device);
if (!these.device) {
console.log("跳转到绑定")
@ -778,19 +736,18 @@
},
success(res) {
these.Status.navigateTO = true;
res.eventChannel.emit('LinkItem', item);
}
});
return;
}
// console.log("验证设备")
// these.DeviceVerdict(item.deviceId);
}
let execLink = () => {
return new Promise((resolve, reject) => {
if (index > total) {
reject({
msg: "连接超时"
@ -798,6 +755,7 @@
return;
}
ble.LinkBlue(item.deviceId).then((res) => {
this.tmpLink=[item];
console.log("连接成功");
ble.StopSearch();
resolve(res);
@ -825,14 +783,30 @@
}).catch(ex => {
console.error("ex=", ex)
MsgClear(these);
MsgError("连接失败:" + ex.msg,'确定',these);
MsgError("连接失败:" + ex.msg, '确定', these);
hideLoading(these);
});
},
disConnect:function(item,index){
// #ifdef H5|WEB
this.PairEquip.splice(index,1);
// #endif
// #ifdef APP|APP-PLUS
if(ble){
ble.disconnectDevice(item.deviceId).catch(ex=>{
console.error("无法断开连接",ex);
});
}
// #endif
}
}
}
@ -972,7 +946,7 @@
position: absolute;
top: 240rpx;
left: 0rpx;
/* border: 1px solid #BBE600; */
z-index: 101;
}
@ -1016,7 +990,7 @@
box-sizing: border-box;
padding: 20rpx;
border-radius: 8px;
background: #1a1a1a;
background: #1a1a1a9e;
display: flex;
flex-direction: row;
flex-wrap: nowrap;

View File

@ -304,14 +304,14 @@
},
bleBreak(res) {
console.error("蓝牙断开连接", res);
// console.error("蓝牙断开连接", res);
if (res.deviceId) {
this.updateBleStatu(res.deviceId);
}
},
bleRecovery(res) {
console.log("蓝牙连接成功", res);
// console.log("蓝牙连接成功", res);
if (res.deviceId) {
this.updateBleStatu(res.deviceId);
}

536
temp_hby100j_ebe126d.js Normal file
View File

@ -0,0 +1,536 @@
import request from '@/utils/request'
import Common from '@/utils/Common.js'
// ================== 钃濈墮鍗忚灏佽绫?==================
class HBY100JProtocol {
constructor() {
this.deviceId = ''; // 4G 鎺ュ彛鎵€闇€鐨?deviceId
this.isBleConnected = false;
this.bleDeviceId = ''; // 灏忕▼搴?APP涓繛鎺ヨ摑鐗欑殑 deviceId
// 钃濈墮鏈嶅姟涓庣壒寰佸€?UUID
this.SERVICE_UUID = '0000AE30-0000-1000-8000-00805F9B34FB'; // 0xAE30
this.WRITE_UUID = '0000AE03-0000-1000-8000-00805F9B34FB'; // 0xAE03
this.NOTIFY_UUID = '0000AE02-0000-1000-8000-00805F9B34FB'; // 0xAE02
this.onNotifyCallback = null;
this._fileResponseResolve = null; // 鏂囦欢涓婁紶鏃剁瓑寰呰澶?FB 05 鍝嶅簲
}
// 绛夊緟璁惧 FB 05 鍝嶅簲锛岃秴鏃跺悗浠?resolve锛堣澶囧彲鑳戒笉鍝嶅簲姣忓寘锛? waitForFileResponse(timeoutMs = 2000) {
return new Promise((resolve) => {
const timer = setTimeout(() => {
if (this._fileResponseResolve) {
this._fileResponseResolve = null;
resolve(null);
}
}, timeoutMs);
this._fileResponseResolve = (result) => {
clearTimeout(timer);
this._fileResponseResolve = null;
resolve(result);
};
});
}
setBleConnectionStatus(status, bleDeviceId = '') {
this.isBleConnected = status;
if (bleDeviceId) {
this.bleDeviceId = bleDeviceId;
}
}
onNotify(callback) {
this.onNotifyCallback = callback;
}
parseBleData(buffer) {
const view = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
if (view.length < 3) return null;
const header = view[0];
const tail = view[view.length - 1];
// 5.1 杩炴帴钃濈墮璁惧涓诲姩涓婃姤 MAC 鍦板潃: FC + 6瀛楄妭MAC + FF
if (header === 0xFC && tail === 0xFF && view.length >= 8) {
const macBytes = view.slice(1, 7);
const macAddress = Array.from(macBytes).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(':');
const result = { type: 'mac', macAddress };
console.log('[100J-钃濈墮] 璁惧涓婃姤MAC:', macAddress, '鍘熷:', Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' '));
if (this.onNotifyCallback) this.onNotifyCallback(result);
return result;
}
if (header !== 0xFB || tail !== 0xFF) return null; // 鏍¢獙澶村熬
const funcCode = view[1];
const data = view.slice(2, view.length - 1);
const hexStr = Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
let result = { funcCode, rawData: data };
switch (funcCode) {
case 0x01: result.resetType = data[0]; break;
case 0x02: break;
case 0x03:
// 5.4 鑾峰彇璁惧浣嶇疆锛氱粡搴?B+绾害8B 鍧囦负 float64锛岃澶囦富鍔ㄤ笂鎶?1鍒嗛挓)涓庝富鍔ㄦ煡璇㈠搷搴旀牸寮忕浉鍚? if (data.length >= 16) {
const lonBuf = new ArrayBuffer(8);
const latBuf = new ArrayBuffer(8);
new Uint8Array(lonBuf).set(data.slice(0, 8));
new Uint8Array(latBuf).set(data.slice(8, 16));
result.longitude = new DataView(lonBuf).getFloat64(0, true);
result.latitude = new DataView(latBuf).getFloat64(0, true);
}
break;
case 0x05:
// 05: 鏂囦欢鏇存柊鍝嶅簲 FB 05 [fileType] [status] FF锛宻tatus: 1=鎴愬姛 2=澶辫触
if (data.length >= 1) result.fileType = data[0];
if (data.length >= 2) result.fileStatus = data[1]; // 1=Success, 2=Failure
if (this._fileResponseResolve) this._fileResponseResolve(result);
break;
case 0x04:
// 5.5 鑾峰彇璁惧鐢垫簮鐘舵€? 鐢垫睜瀹归噺8B + 鐢靛帇8B + 鐧惧垎姣?B + 杞﹁浇鐢垫簮1B + 缁埅鏃堕棿2B(鍒嗛挓)
if (data.length >= 20) {
result.batteryPercentage = data[16];
result.vehiclePower = data[17];
result.batteryRemainingTime = data[18] | (data[19] << 8); // 灏忕搴忥紝鍗曚綅鍒嗛挓
}
break;
case 0x06:
// 06: 璇煶鎾姤鍝嶅簲
result.voiceBroadcast = data[0];
break;
case 0x09:
// 09: 淇敼闊抽噺鍝嶅簲
result.volume = data[0];
break;
case 0x0A:
// 0A: 鐖嗛棯妯″紡鍝嶅簲
result.strobeEnable = data[0];
result.strobeMode = data[1];
break;
case 0x0B:
// 0B: 淇敼璀︾ず鐏垎闂鐜囧搷搴? result.strobeFrequency = data[0];
break;
case 0x0C:
// 0C: 寮哄埗澹板厜鎶ヨ鍝嶅簲
result.alarmEnable = data[0];
result.alarmMode = data[1];
break;
case 0x0D:
// 0D: 璀︾ず鐏?LED 浜害璋冭妭鍝嶅簲
result.redBrightness = data[0];
result.blueBrightness = data[1];
result.yellowBrightness = data[2];
break;
case 0x0E:
// 0E: 鑾峰彇褰撳墠宸ヤ綔鏂瑰紡鍝嶅簲
result.voiceBroadcast = data[0];
result.alarmEnable = data[1];
result.alarmMode = data[2];
result.strobeEnable = data[3];
result.strobeMode = data[4];
result.strobeFrequency = data[5];
result.volume = data[6];
result.redBrightness = data[7];
result.blueBrightness = data[8];
result.yellowBrightness = data[9];
break;
}
const funcNames = { 0x01: '澶嶄綅', 0x02: '鍩虹淇℃伅', 0x03: '浣嶇疆', 0x04: '鐢垫簮鐘舵€?, 0x05: '鏂囦欢鏇存柊', 0x06: '煶鎾', 0x09: '闊抽噺', 0x0A: '鐖嗛棯妯', 0x0B: '鐖嗛棯棰戠巼', 0x0C: '寮哄埗鎶ヨ', 0x0D: 'LED浜', 0x0E: '宸ヤ綔鏂瑰紡' };
const name = funcNames[funcCode] || ('0x' + funcCode.toString(16));
console.log('[100J-钃濈墮] 璁惧鍝嶅簲 FB:', name, '瑙f瀽:', JSON.stringify(result), '鍘熷:', hexStr);
if (this.onNotifyCallback) {
this.onNotifyCallback(result);
}
return result;
}
sendBleData(funcCode, dataBytes = []) {
return new Promise((resolve, reject) => {
if (!this.isBleConnected || !this.bleDeviceId) {
return reject(new Error('钃濈墮鏈繛鎺?));
}
const buffer = new ArrayBuffer(dataBytes.length + 3);
const view = new Uint8Array(buffer);
view[0] = 0xFA; // 鏁版嵁澶? view[1] = funcCode; // 鍔熻兘鐮? for (let i = 0; i < dataBytes.length; i++) {
view[2 + i] = dataBytes[i];
}
view[view.length - 1] = 0xFF; // 缁撳熬
const sendHex = Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
console.log('[100J-钃濈墮] 涓嬪彂鎸囦护 FA:', '0x' + funcCode.toString(16).toUpperCase(), sendHex);
// 浣跨敤椤圭洰涓粺涓€鐨?BleHelper 鍙戦€佹暟鎹? import('@/utils/BleHelper.js').then(module => {
const bleTool = module.default.getBleTool();
bleTool.sendData(this.bleDeviceId, buffer, this.SERVICE_UUID, this.WRITE_UUID)
.then(res => resolve(res))
.catch(err => reject(err));
});
});
}
// 绾摑鐗欐寚浠ゅ彂閫佹柟娉? deviceReset(type = 0) { return this.sendBleData(0x01, [type]); }
getBasicInfo() { return this.sendBleData(0x02, []); }
getLocation() { return this.sendBleData(0x03, []); }
getPowerStatus() { return this.sendBleData(0x04, []); }
setVoiceBroadcast(enable) { return this.sendBleData(0x06, [enable]); }
setVolume(volume) { return this.sendBleData(0x09, [volume]); }
setStrobeMode(enable, mode) { return this.sendBleData(0x0A, [enable, mode]); }
setStrobeFrequency(frequency) { return this.sendBleData(0x0B, [frequency]); }
setForceAlarm(enable, mode) { return this.sendBleData(0x0C, [enable, mode]); }
setLightBrightness(red, blue = 0, yellow = 0) { return this.sendBleData(0x0D, [red, blue, yellow]); }
getCurrentWorkMode() { return this.sendBleData(0x0E, []); }
// 0x05 鏂囦欢涓婁紶锛氬垎鐗囦紶杈擄紝鍗忚 FA 05 [fileType] [phase] [data...] FF
// fileType: 1=璇煶 2=鍥剧墖 3=鍔ㄥ浘 4=OTA
// phase: 0=寮€濮?1=鏁版嵁 2=缁撴潫
// 姣忓寘鏈€澶у瓧鑺?钃濈墮锛欳HUNK_SIZE=500
// 鏀寔 fileUrl(闇€缃戠粶涓嬭浇) 鎴?localPath(鏃犵綉缁滄椂鏈湴鏂囦欢)
uploadVoiceFileBle(fileUrlOrLocalPath, fileType = 1, onProgress) {
const CHUNK_SIZE = 500; // 姣忓寘鏈夋晥鏁版嵁锛屽弬鑰?6155 deviceDetail.vue
return new Promise((resolve, reject) => {
if (!this.isBleConnected || !this.bleDeviceId) {
return reject(new Error('钃濈墮鏈繛鎺?));
}
if (!fileUrlOrLocalPath) {
return reject(new Error('缂哄皯鏂囦欢鍦板潃鎴栨湰鍦拌矾寰?));
}
const isLocalPath = !/^https?:\/\//i.test(fileUrlOrLocalPath);
if (onProgress) onProgress(1);
const readFromPath = (path) => {
const doSend = (bytes) => {
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
.then(resolve).catch(reject);
};
// App 绔?getFileSystemManager 鏈疄鐜帮紝鐩存帴鐢?plus.io.requestFileSystem+getFile
readFromPathPlus(path, doSend, reject);
};
const readFileEntry = (entry, doSend, reject) => {
entry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = (e) => {
try {
const buf = e.target.result;
const bytes = new Uint8Array(buf);
doSend(bytes);
} catch (err) {
console.error('[100J-钃濈墮] 璇诲彇ArrayBuffer寮傚父:', err);
reject(err);
}
};
reader.onerror = () => reject(new Error('璇诲彇鏂囦欢澶辫触'));
reader.readAsArrayBuffer(file);
}, (err) => reject(err));
};
const readFromPathPlus = (path, doSend, reject) => {
if (typeof plus === 'undefined' || !plus.io) {
console.error('[100J-钃濈墮] 褰撳墠鐜涓嶆敮鎸佹枃浠惰鍙?plus.io)');
reject(new Error('褰撳墠鐜涓嶆敮鎸佹枃浠惰鍙?));
return;
}
// _downloads/ 鐢?requestFileSystem+getFile锛堥伩鍏?resolveLocalFileSystemURL 鍗′綇锛? if (path && path.startsWith('_downloads/')) {
const fileName = path.replace(/^_downloads\//, '');
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
fs.root.getFile(fileName, {}, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
}, (err) => reject(err));
return;
}
// _doc/ 鐢?requestFileSystem(PRIVATE_DOC)锛岄€愮骇 getDirectory 鍐?getFile锛堝祵濂楄矾寰勫吋瀹癸級
if (path && path.startsWith('_doc/')) {
const relPath = path.replace(/^_doc\//, '');
const parts = relPath.split('/');
const fileName = parts.pop();
const dirs = parts;
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
let cur = fs.root;
const next = (i) => {
if (i >= dirs.length) {
cur.getFile(fileName, { create: false }, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
return;
}
cur.getDirectory(dirs[i], { create: false }, (dir) => { cur = dir; next(i + 1); }, (err) => reject(err));
};
next(0);
}, (err) => reject(err));
return;
}
// 鍏朵粬璺緞鍏滃簳
let resolvePath = path;
if (path && path.startsWith('/') && !path.startsWith('file://')) resolvePath = 'file://' + path;
plus.io.resolveLocalFileSystemURL(resolvePath, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
};
if (isLocalPath) {
// 鏈湴璺緞锛氭棤缃戠粶鏃剁洿鎺ヨ鍙? readFromPath(fileUrlOrLocalPath);
} else {
// 缃戠粶 URL锛氫紭鍏堢敤 uni.request 鐩存帴鎷夊彇 ArrayBuffer锛堢被浼?100 璁惧锛屾棤鏂囦欢 IO锛夛紝澶辫触鍐嶈蛋 downloadFile
let fetchUrl = fileUrlOrLocalPath;
if (fetchUrl.startsWith('http://')) fetchUrl = 'https://' + fetchUrl.slice(7);
uni.request({
url: fetchUrl,
method: 'GET',
responseType: 'arraybuffer',
timeout: 60000,
success: (res) => {
if (res.statusCode === 200 && res.data) {
const bytes = res.data instanceof ArrayBuffer ? new Uint8Array(res.data) : new Uint8Array(res.data || []);
if (bytes.length > 0) {
const doSend = (b) => {
this._sendVoiceChunks(b, fileType, CHUNK_SIZE, onProgress).then(resolve).catch(reject);
};
doSend(bytes);
return;
}
}
fallbackDownload();
},
fail: () => fallbackDownload()
});
const fallbackDownload = () => {
uni.downloadFile({
url: fetchUrl,
success: (res) => {
if (res.statusCode !== 200 || !res.tempFilePath) {
reject(new Error('涓嬭浇澶辫触: ' + (res.statusCode || '鏃犺矾寰?)));
return;
}
Common.moveFileToDownloads(res.tempFilePath).then((p) => readFromPath(p)).catch(() => readFromPath(res.tempFilePath));
},
fail: (err) => reject(err)
});
};
}
});
}
_sendVoiceChunks(bytes, fileType, chunkSize, onProgress) {
const total = bytes.length;
const ft = (fileType & 0xFF) || 1;
const DELAY_AFTER_START = 200; // 寮€濮嬪寘鍚庛€佺瓑璁惧鍝嶅簲鍚庡啀鍙戠殑缂撳啿(ms)
const DELAY_PACKET = 200; // 鏁版嵁鍖呴棿寤舵椂(ms)锛岃澶囨敹涓嶅叏鏃堕€傚綋鍔犲ぇ
const toHex = (arr) => Array.from(arr).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
console.log('[100J-钃濈墮] 煶鏂囦欢鎬诲ぇ灏?', total, '瀛楄妭, fileType=', ft);
if (onProgress) onProgress(0);
const bleToolPromise = import('@/utils/BleHelper.js').then(m => m.default.getBleTool());
const send = (dataBytes, label = '') => {
const buf = new ArrayBuffer(dataBytes.length + 3);
const v = new Uint8Array(buf);
v[0] = 0xFA;
v[1] = 0x05;
for (let i = 0; i < dataBytes.length; i++) v[2 + i] = dataBytes[i];
v[v.length - 1] = 0xFF;
const hex = toHex(v);
const preview = v.length <= 32 ? hex : hex.slice(0, 96) + '...';
console.log(`[100J-钃濈墮] 鍙戦€?{label} 鍏?{v.length}瀛楄妭:`, preview);
return bleToolPromise.then(ble => ble.sendData(this.bleDeviceId, buf, this.SERVICE_UUID, this.WRITE_UUID));
};
const delay = (ms) => new Promise(r => setTimeout(r, ms));
// 寮€濮嬪寘: FA 05 [fileType] [phase=0] [size 4B LE] FF
const startData = [ft, 0, total & 0xFF, (total >> 8) & 0xFF, (total >> 16) & 0xFF, (total >> 24) & 0xFF];
const waitPromise = this.waitForFileResponse(1000);
return send(startData, ' 濮嬪寘')
.then(() => { if (onProgress) onProgress(1); return waitPromise; })
.then(() => { if (onProgress) onProgress(2); return delay(DELAY_AFTER_START); })
.then(() => {
let seq = 0;
const sendNext = (offset) => {
if (offset >= total) {
return delay(DELAY_PACKET).then(() => send([ft, 2], ' 缁撴潫鍖?));
}
const chunk = bytes.slice(offset, Math.min(offset + chunkSize, total));
const chunkData = [ft, 1, seq & 0xFF, (seq >> 8) & 0xFF, ...chunk];
return send(chunkData, ` #${seq} 鏁版嵁鍖卄).then(() => {
seq++;
const pct = Math.round((offset + chunk.length) / total * 100);
if (onProgress) onProgress(Math.min(99, Math.max(3, pct)));
return delay(DELAY_PACKET).then(() => sendNext(offset + chunk.length));
});
};
return sendNext(0);
})
.then(() => delay(DELAY_PACKET))
.then(() => {
if (onProgress) onProgress(100);
return { code: 200, msg: '璇煶鏂囦欢宸查€氳繃钃濈墮涓婁紶' };
});
}
}
// ================== 鍏ㄥ眬鍗曚緥涓庣姸鎬佺鐞?==================
const protocolInstance = new HBY100JProtocol();
// 鏆撮湶缁欓〉闈細鏇存柊钃濈墮杩炴帴鐘舵€?export function updateBleStatus(isConnected, bleDeviceId, deviceId) {
protocolInstance.setBleConnectionStatus(isConnected, bleDeviceId);
protocolInstance.deviceId = deviceId;
console.log('[100J] 钃濈墮鐘舵€?', isConnected ? '宸茶繛鎺?鍚庣画鎸囦护璧拌摑鐗?' : '宸叉柇寮€(鍚庣画鎸囦护璧?G)', { bleDeviceId: bleDeviceId || '-', deviceId });
}
// 鏆撮湶缁欓〉闈細瑙f瀽钃濈墮鎺ユ敹鍒扮殑鏁版嵁
export function parseBleData(buffer) {
return protocolInstance.parseBleData(buffer);
}
// 鏆撮湶缁欓〉闈細钃濈墮杩炴帴鍚庝富鍔ㄦ媺鍙栫數婧愮姸鎬?鐢甸噺銆佺画鑸?
export function fetchBlePowerStatus() {
if (!protocolInstance.isBleConnected) return Promise.reject(new Error('钃濈墮鏈繛鎺?));
console.log('[100J-钃濈墮] 鎷夊彇鐢垫簮鐘舵€?宸查€氳繃钃濈墮鍙戦€?FA 04 FF');
return protocolInstance.getPowerStatus();
}
// 鏆撮湶缁欓〉闈細钃濈墮杩炴帴鍚庝富鍔ㄦ媺鍙栧畾浣?浼樺厛钃濈墮锛岃澶囦篃浼氭瘡1鍒嗛挓涓诲姩涓婃姤)
export function fetchBleLocation() {
if (!protocolInstance.isBleConnected) return Promise.reject(new Error('钃濈墮鏈繛鎺?));
console.log('[100J-钃濈墮] 鎷夊彇瀹氫綅 宸查€氳繃钃濈墮鍙戦€?FA 03 FF');
return protocolInstance.getLocation();
}
// 鏆撮湶缁欓〉闈細灏濊瘯閲嶈繛钃濈墮(浼樺厛绛栫暐锛氭柇绾垮悗鍙戞寚浠ゅ墠鍏堝皾璇曢噸杩?
export function tryReconnectBle(timeoutMs = 2500) {
if (protocolInstance.isBleConnected) return Promise.resolve(true);
if (!protocolInstance.bleDeviceId) return Promise.resolve(false);
return new Promise((resolve) => {
import('@/utils/BleHelper.js').then(module => {
const bleTool = module.default.getBleTool();
const deviceId = protocolInstance.bleDeviceId;
const f = bleTool.data.LinkedList.find(v => v.deviceId === deviceId);
if (!f) {
resolve(false);
return;
}
const svc = f.writeServiceId || '0000AE30-0000-1000-8000-00805F9B34FB';
const write = f.wirteCharactId || '0000AE03-0000-1000-8000-00805F9B34FB';
const notify = f.notifyCharactId || '0000AE02-0000-1000-8000-00805F9B34FB';
const timer = setTimeout(() => {
resolve(protocolInstance.isBleConnected);
}, timeoutMs);
console.log('[100J] 钃濈墮浼樺厛锛氬皾璇曢噸杩?, deviceId);
bleTool.LinkBlue(deviceId, svc, write, notify, 1).then(() => {
clearTimeout(timer);
protocolInstance.setBleConnectionStatus(true, deviceId);
console.log('[100J] 钃濈墮閲嶈繛鎴愬姛');
resolve(true);
}).catch(() => {
clearTimeout(timer);
resolve(false);
});
});
});
}
// ================== API 鎺ュ彛 (鎷︽埅灞? ==================
// 鑾峰彇璇煶绠$悊鍒楄〃
export function deviceVoliceList(params) {
return request({
url: `/app/video/queryAudioFileList`,
method: 'get',
data:params
})
}
// 閲嶅懡鍚?export function videRenameAudioFile(data) {
return request({
url: `/app/video/renameAudioFile`,
method: 'post',
data:data
})
}
// 鍒犻櫎璇煶鏂囦欢鍒楄〃
export function deviceDeleteAudioFile(params) {
return request({
url: `/app/video/deleteAudioFile`,
method: 'get',
data:params
})
}
// 鏇存柊璇煶/浣跨敤璇煶锛氳摑鐗欎紭鍏堬紝4G 鍏滃簳锛堜笉褰卞搷鍘熸湁 4G 闊抽涓嬪彂锛?// 鏈?fileUrl 鎴?localPath 涓旇摑鐗欏彲鐢ㄦ椂璧拌摑鐗欙紱鍚﹀垯鎴栬摑鐗欏け璐ユ椂璧?4G锛堜笌鍘熷厛閫昏緫涓€鑷达級
export function deviceUpdateVoice(data) {
const httpExec = () => request({
url: `/app/hby100j/device/updateVoice`,
method: 'post',
data: { id: data.id }
});
const localPath = data.localPath;
const fileUrl = data.fileUrl;
const hasLocalPath = localPath && typeof localPath === 'string' && localPath.length > 0;
const hasFileUrl = fileUrl && typeof fileUrl === 'string' && fileUrl.length > 0;
const fileSource = hasLocalPath ? localPath : (hasFileUrl ? fileUrl : null);
if (!fileSource) {
return httpExec(); // 鏃犳枃浠舵簮锛氱洿鎺?4G锛堝師鏈夐€昏緫锛? }
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress);
return execWithBleFirst(bleExec, httpExec, '璇煶鏂囦欢涓婁紶');
}
// 100J淇℃伅
export function deviceDetail(id) {
return request({
url: `/app/hby100j/device/${id}`,
method: 'get',
})
}
// 钃濈墮浼樺厛銆?G 鍏滃簳锛氭湭杩炴帴鏃跺皾璇曢噸杩烇紱钃濈墮澶辫触鏃跺洖閫€ 4G锛堜繚鎸佸師鏈?4G 閫氳涓嶅彉锛?function execWithBleFirst(bleExec, httpExec, logName) {
const doBle = () => bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
const do4G = () => httpExec().then(res => { res._channel = '4g'; return res; });
if (protocolInstance.isBleConnected) {
return doBle().catch(() => { console.log('[100J] 钃濈墮澶辫触锛屽洖閫€4G'); return do4G(); });
}
return tryReconnectBle(2500).then(reconnected => {
return reconnected ? doBle().catch(() => { console.log('[100J] 钃濈墮澶辫触锛屽洖閫€4G'); return do4G(); }) : do4G();
});
}
// 鐖嗛棯妯″紡
export function deviceStrobeMode(data) {
return execWithBleFirst(
() => protocolInstance.setStrobeMode(data.enable, data.mode).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/strobeMode`, method: 'post', data }),
'鐖嗛棯妯″紡'
);
}
// 寮哄埗鎶ヨ
export function deviceForceAlarmActivation(data) {
return execWithBleFirst(
() => protocolInstance.setForceAlarm(data.voiceStrobeAlarm, data.mode).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/forceAlarmActivation`, method: 'post', data }),
'寮哄埗鎶ヨ'
);
}
// 鐖嗛棯棰戠巼
export function deviceStrobeFrequency(data) {
return execWithBleFirst(
() => protocolInstance.setStrobeFrequency(data.frequency).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/strobeFrequency`, method: 'post', data }),
'鐖嗛棯棰戠巼'
);
}
// 鐏厜璋冭妭浜害
export function deviceLightAdjustment(data) {
return execWithBleFirst(
() => protocolInstance.setLightBrightness(data.brightness, data.brightness, data.brightness).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/lightAdjustment`, method: 'post', data }),
'鐏厜浜害'
);
}
// 璋冭妭闊抽噺
export function deviceUpdateVolume(data) {
return execWithBleFirst(
() => protocolInstance.setVolume(data.volume).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/updateVolume`, method: 'post', data }),
'璋冭妭闊抽噺'
);
}
// 璇煶鎾斁
export function deviceVoiceBroadcast(data) {
return execWithBleFirst(
() => protocolInstance.setVoiceBroadcast(data.voiceBroadcast).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
() => request({ url: `/app/hby100j/device/voiceBroadcast`, method: 'post', data }),
'璇煶鎾姤'
);
}

View File

@ -1,180 +1,180 @@
// eslint-disable
export const isObject = (obj) => {
return obj
? Object.prototype.toString.call(obj) === "[object Object]"
: false;
};
export const isArray = (arr) => {
return arr ? Array.isArray(arr) : false;
};
/**
* handle async await
* @param {*} promise promise
*/
export const awaitWrap = (promise) =>
promise.then((res) => [null, res]).catch((err) => [err, {}]);
/**
* 深拷贝
* @param {*} source
*/
export const deepClone = (source) => {
if (!isObject(source) && !isArray(source)) return source;
const targetObj = isArray(source) ? [] : {}; // 判断复制的目标是数组还是对象
for (let keys in source) {
// 遍历目标
if (source.hasOwnProperty(keys)) {
if (source[keys] && typeof source[keys] === "object") {
// 如果值是对象,就递归一下
targetObj[keys] = isArray(source[keys]) ? [] : {};
targetObj[keys] = deepClone(source[keys]);
} else {
// 如果不是,就直接赋值
targetObj[keys] = source[keys];
}
}
}
return targetObj;
};
/**
* @description JS对象深度合并
* @param {object} target 需要拷贝的对象
* @param {object} source 拷贝的来源对象
* @returns {object|boolean} 深度合并后的对象或者false入参有不是对象
*/
export const deepMerge = (target = {}, source = {}) => {
target = deepClone(target);
if (typeof target !== "object" || typeof source !== "object") return false;
for (const prop in source) {
if (!source.hasOwnProperty(prop)) continue;
if (prop in target) {
if (typeof target[prop] !== "object") {
target[prop] = source[prop];
} else if (typeof source[prop] !== "object") {
target[prop] = source[prop];
} else if (target[prop].concat && source[prop].concat) {
target[prop] = target[prop].concat(source[prop]);
} else {
target[prop] = deepMerge(target[prop], source[prop]);
}
} else {
target[prop] = source[prop];
}
}
return target;
};
/**
* 将File对象转为 Blob Url
* @param {File} File对象
* @returns Blob Url
*/
export const fileToBlob = (file) => {
if (!file) return;
const fileType = file.type;
const blob = new Blob([file], { type: fileType || 'application/*' });
const blobUrl = window.URL.createObjectURL(blob);
return blobUrl;
};
/**
* 将File对象转为 base64
* @param {File} File对象
* @returns base64
*/
export const fileToBase64 = (file) => {
if (!file) return;
return new Promise((r, j) => {
const reader = new FileReader();
reader.onloadend = () => {
const base64String = reader.result;
r(base64String);
};
reader.onerror = () => {
j({ mode: 'fileToBase64', data: { errMsg: 'File to base64 fail.' } });
};
reader.readAsDataURL(file);
});
};
/**
* base64转临时路径(改自https://github.com/zhetengbiji/image-tools/blob/master/index.js)
* @param base64
* @returns
*/
function dataUrlToBase64(str) {
var array = str.split(',');
return array[array.length - 1];
};
function biggerThan(v1, v2) {
var v1Array = v1.split('.');
var v2Array = v2.split('.');
var update = false;
for (var index = 0; index < v2Array.length; index++) {
var diff = v1Array[index] - v2Array[index];
if (diff !== 0) {
update = diff > 0;
break;
}
}
return update;
};
var index = 0;
function getNewFileId() {
return Date.now() + String(index++);
};
export const base64ToPath = (base64, name = '') => {
return new Promise((r, j) => {
if (typeof plus !== 'object') {
return j(new Error('not support'));
}
var fileName = '';
if (name) {
const names = name.split('.');
const extName = names.splice(-1);
fileName = `${names.join('.')}-${getNewFileId()}.${extName}`;
} else {
const names = base64.split(',')[0].match(/data\:\S+\/(\S+);/);
if (!names) {
j(new Error('base64 error'));
}
const extName = names[1];
fileName = `${getNewFileId()}.${extName}`;
}
var basePath = '_doc';
var dirPath = 'uniapp_temp';
var filePath = `${basePath}/${dirPath}/${fileName}`;
if (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime.innerVersion)) {
plus.io.resolveLocalFileSystemURL(basePath, function (entry) {
entry.getDirectory(dirPath, {
create: true,
exclusive: false,
}, function (entry) {
entry.getFile(fileName, {
create: true,
exclusive: false,
}, function (entry) {
entry.createWriter(function (writer) {
writer.onwrite = function () {
r(filePath);
}
writer.onerror = j;
writer.seek(0);
writer.writeAsBinary(dataUrlToBase64(base64));
}, j)
}, j)
}, j)
}, j)
return;
}
var bitmap = new plus.nativeObj.Bitmap(fileName);
bitmap.loadBase64Data(base64, function () {
bitmap.save(filePath, {}, function () {
bitmap.clear();
r(filePath);
}, function (error) {
bitmap.clear();
j(error);
});
}, function (error) {
bitmap.clear();
j(error);
});
});
};
// eslint-disable
export const isObject = (obj) => {
return obj
? Object.prototype.toString.call(obj) === "[object Object]"
: false;
};
export const isArray = (arr) => {
return arr ? Array.isArray(arr) : false;
};
/**
* handle async await
* @param {*} promise promise
*/
export const awaitWrap = (promise) =>
promise.then((res) => [null, res]).catch((err) => [err, {}]);
/**
* 深拷贝
* @param {*} source
*/
export const deepClone = (source) => {
if (!isObject(source) && !isArray(source)) return source;
const targetObj = isArray(source) ? [] : {}; // 判断复制的目标是数组还是对象
for (let keys in source) {
// 遍历目标
if (source.hasOwnProperty(keys)) {
if (source[keys] && typeof source[keys] === "object") {
// 如果值是对象,就递归一下
targetObj[keys] = isArray(source[keys]) ? [] : {};
targetObj[keys] = deepClone(source[keys]);
} else {
// 如果不是,就直接赋值
targetObj[keys] = source[keys];
}
}
}
return targetObj;
};
/**
* @description JS对象深度合并
* @param {object} target 需要拷贝的对象
* @param {object} source 拷贝的来源对象
* @returns {object|boolean} 深度合并后的对象或者false入参有不是对象
*/
export const deepMerge = (target = {}, source = {}) => {
target = deepClone(target);
if (typeof target !== "object" || typeof source !== "object") return false;
for (const prop in source) {
if (!source.hasOwnProperty(prop)) continue;
if (prop in target) {
if (typeof target[prop] !== "object") {
target[prop] = source[prop];
} else if (typeof source[prop] !== "object") {
target[prop] = source[prop];
} else if (target[prop].concat && source[prop].concat) {
target[prop] = target[prop].concat(source[prop]);
} else {
target[prop] = deepMerge(target[prop], source[prop]);
}
} else {
target[prop] = source[prop];
}
}
return target;
};
/**
* 将File对象转为 Blob Url
* @param {File} File对象
* @returns Blob Url
*/
export const fileToBlob = (file) => {
if (!file) return;
const fileType = file.type;
const blob = new Blob([file], { type: fileType || 'application/*' });
const blobUrl = window.URL.createObjectURL(blob);
return blobUrl;
};
/**
* 将File对象转为 base64
* @param {File} File对象
* @returns base64
*/
export const fileToBase64 = (file) => {
if (!file) return;
return new Promise((r, j) => {
const reader = new FileReader();
reader.onloadend = () => {
const base64String = reader.result;
r(base64String);
};
reader.onerror = () => {
j({ mode: 'fileToBase64', data: { errMsg: 'File to base64 fail.' } });
};
reader.readAsDataURL(file);
});
};
/**
* base64转临时路径(改自https://github.com/zhetengbiji/image-tools/blob/master/index.js)
* @param base64
* @returns
*/
function dataUrlToBase64(str) {
var array = str.split(',');
return array[array.length - 1];
};
function biggerThan(v1, v2) {
var v1Array = v1.split('.');
var v2Array = v2.split('.');
var update = false;
for (var index = 0; index < v2Array.length; index++) {
var diff = v1Array[index] - v2Array[index];
if (diff !== 0) {
update = diff > 0;
break;
}
}
return update;
};
var index = 0;
function getNewFileId() {
return Date.now() + String(index++);
};
export const base64ToPath = (base64, name = '') => {
return new Promise((r, j) => {
if (typeof plus !== 'object') {
return j(new Error('not support'));
}
var fileName = '';
if (name) {
const names = name.split('.');
const extName = names.splice(-1);
fileName = `${names.join('.')}-${getNewFileId()}.${extName}`;
} else {
const names = base64.split(',')[0].match(/data\:\S+\/(\S+);/);
if (!names) {
j(new Error('base64 error'));
}
const extName = names[1];
fileName = `${getNewFileId()}.${extName}`;
}
var basePath = '_doc';
var dirPath = 'uniapp_temp';
var filePath = `${basePath}/${dirPath}/${fileName}`;
if (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime.innerVersion)) {
plus.io.resolveLocalFileSystemURL(basePath, function (entry) {
entry.getDirectory(dirPath, {
create: true,
exclusive: false,
}, function (entry) {
entry.getFile(fileName, {
create: true,
exclusive: false,
}, function (entry) {
entry.createWriter(function (writer) {
writer.onwrite = function () {
r(filePath);
}
writer.onerror = j;
writer.seek(0);
writer.writeAsBinary(dataUrlToBase64(base64));
}, j)
}, j)
}, j)
}, j)
return;
}
var bitmap = new plus.nativeObj.Bitmap(fileName);
bitmap.loadBase64Data(base64, function () {
bitmap.save(filePath, {}, function () {
bitmap.clear();
r(filePath);
}, function (error) {
bitmap.clear();
j(error);
});
}, function (error) {
bitmap.clear();
j(error);
});
});
};

View File

@ -36,7 +36,7 @@ class BleHelper {
linkedDevices = uni.getStorageSync(this.StorageKey);
}
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
console.log("111111", linkedDevices);
// console.log("111111", linkedDevices);
linkedDevices = linkedDevices.filter((v) => {
if (v) {
v.Linked = false;
@ -61,7 +61,8 @@ class BleHelper {
LinkedList: linkedDevices, //已连接的设备列表
platform: systemInfo.uniPlatform,
Disconnect: [], //主动断开的设备
connectingDevices: {} //正在连接的设备
connectingDevices: {}, //正在连接的设备
voiceUploading: false //语音上传中(上传中不主动断开蓝牙)
}
this.cfg = {
onDeviceFound: [], //发现新设备的事件
@ -561,7 +562,7 @@ class BleHelper {
return;
}
if (!this.data.isOpenBlue) {
console.error("蓝牙模块未打开");
console.log("蓝牙模块未打开,即将初始化");
resolve({
available: false,
discovering: false
@ -613,13 +614,13 @@ class BleHelper {
if (this.data.isSubscribe) { //整个App生命周期只订阅一次
return;
}
console.error("开始订阅各类变化消息");
// console.error("开始订阅各类变化消息");
this.data.isSubscribe = true;
this.BleStateChange(); //蓝牙适配器变化
this.BleConnChange(); //蓝牙连接变化
this.BleDeviceFound(); //发现新设备
this.BleReceive(); //收到消息
console.error("订阅各类变化消息完成");
// console.error("订阅各类变化消息完成");
},
fail: (ex2) => {
console.error("蓝牙模块启动失败", ex2);
@ -962,7 +963,7 @@ class BleHelper {
}
} else {
console.log("蓝牙连接已恢复", res);
// console.log("蓝牙连接已恢复", res);
// 系统级连接恢复:更新 LinkedList 并通知业务层,避免 sendData 误判未连接而重复 createBLEConnection
let f = this.data.LinkedList.find(v => v.deviceId == res.deviceId);
if (f) {
@ -994,15 +995,15 @@ class BleHelper {
// console.log("发现新设备",item.name+" "+item.RSSI);
// }
let f = serviceDic.find(v => {
return item.advertisServiceUUIDs
.includes(v.serviceId);
});
if (f) {
let f = serviceDic.find(v => {
return item.advertisServiceUUIDs
.includes(v.serviceId);
});
if (f) {
// console.log("发现目标设备:", item);
arr.push(item);
}
}
}
if (arr.length === 0) {
@ -1098,7 +1099,7 @@ class BleHelper {
services: [],
allowDuplicatesKey: true,
success: (res) => {
console.log('开始搜索蓝牙设备成功');
// console.log('开始搜索蓝牙设备成功');
resolve(res);
},
@ -1237,11 +1238,11 @@ class BleHelper {
characteristicId: characteristicId,
state: state,
success: (res) => {
if (state) {
console.log("订阅消息成功", res);
} else {
console.log("取消订阅成功", res);
}
// if (state) {
// console.log("订阅消息成功", res);
// } else {
// console.log("取消订阅成功", res);
// }
this.data.LinkedList.find((v) => {
if (v.deviceId == deviceId) {
@ -1311,13 +1312,13 @@ class BleHelper {
results.forEach((result, index) => {
if (result.status === "fulfilled") {
console.log(`订阅消息操作${index + 1}成功:`, result.value);
// console.log(`订阅消息操作${index + 1}成功:`, result.value);
} else {
console.error(`订阅消息操作${index + 1}失败:`, result
.reason);
}
});
console.log("订阅消息完成deviceId:", deviceId);
// console.log("订阅消息完成deviceId:", deviceId);
resolve();
}).catch((ex) => {
console.error("异常,ex=", ex);
@ -1624,7 +1625,7 @@ class BleHelper {
// console.log("正在连接" + deviceId);
uni.createBLEConnection({
deviceId: deviceId,
timeout: 15000,
timeout: 20000,
success: (info) => {
//释放连接锁
@ -1808,6 +1809,13 @@ class BleHelper {
});
return prom;
}
// 语音上传中不主动断开:设置/查询上传状态App onHide 时检查)
setVoiceUploading(flag) {
this.data.voiceUploading = !!flag;
}
isVoiceUploading() {
return !!this.data.voiceUploading;
}
//断开连接
disconnectDevice(deviceId) {
if (this.data.platform == 'web') {

View File

@ -14,8 +14,7 @@ class BleReceive {
'/pages/100/HBY100': this.Receive_100.bind(this),
'/pages/102/HBY102': this.Receive_102.bind(this),
'/pages/6170/deviceControl/index':this.Receive_6170.bind(this),
'/pages/100J/HBY100-J': this.Receive_100J.bind(this),
'/pages/102/HBY102': this.Receive_102.bind(this)
'/pages/100J/HBY100-J': this.Receive_100J.bind(this)
};
}

View File

@ -1,491 +1,491 @@
import request from "@/utils/request.js";
export default {
audioStorageKey: "audioStorageKey",
pcmStorageKey: "pcmStorageKey",
guid: function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0;
let v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
},
alert: function(title, content, callback) {
if (!title) {
title = '提示'
}
if (!content) {
content = title;
}
uni.showModal({
title: title,
content: content,
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
if (callback) {
callback(res);
}
}
});
},
showLoading: function(title, mask) {
uni.showLoading({
title: title,
mask: mask,
})
},
hideLoading: function() {
uni.hideLoading();
},
showToast: function(title, mask, duration, callback) {
if (!duration) {
duration = 1500;
}
if (mask == undefined) {
mask = false;
}
uni.showToast({
title: title,
mask: mask,
duration: duration,
callback: callback,
icon: 'none'
})
},
GetData: function(url, data, method, contentType, succ, err, complete) {
var these = this;
if (!url) {
console.error("url为空");
return;
}
if (url.toLowerCase().indexOf('http://') == -1 || url.toLowerCase().indexOf('https://') == -1) {
if (url.indexOf('/') == 0) {
url = url.substr(1, url.length - 1);
}
let ServerPath = these.DevApi;
if (these.Version === 'Dev') {
ServerPath = these.DevApi;
} else if (these.Version === 'Uat') {
ServerPath = these.UatApi;
} else if (these.Version === 'Relese') {
ServerPath = these.ReleseApi;
} else {
these.DevApi
}
url = ServerPath + url;
}
var these = this;
if (!method) {
method = 'POST';
}
method = method.toUpperCase();
if (!contentType) {
contentType = 'application/json;charset=UTF-8';
}
these.checkLAN(
function() {
these.showLoading('请稍候..', true);
setTimeout(function() {
uni.request({
url: url,
data: data,
header: {
"Content-Type": contentType
},
method: method,
timeout: 60000,
dataType: 'json',
success: function(json) {
if (succ) {
succ(json);
}
},
fail: function(ex) {
if (err) {
err(ex);
}
},
complete: function() {
if (complete) {
complete();
}
}
});
}, 0);
}
,
function() {
these.showToast('无网络连接');
});
},
checkLAN: function(succ, error) {
uni.getNetworkType({
success: (res) => {
let networkType = res.networkType;
// 判断网络是否连接
if (networkType === 'none') {
console.error('无网络连接')
if (error) {
error();
}
} else {
if (succ) {
succ();
}
}
},
fail: (err) => {
console.error('获取网络状态失败:', err);
if (error) {
error();
}
}
});
},
DateFormat: function(date, format) {
if (!date) {
date = new Date();
}
if (!format) {
format = 'yyyy-MM-dd HH:mm:ss';
}
// 处理参数默认值
if (typeof date === 'string' || typeof date === 'number') {
date = new Date(date);
}
date = date instanceof Date ? date : new Date();
format = format || 'yyyy-MM-dd';
// 检查日期是否有效
if (isNaN(date.getTime())) {
return 'Invalid Date';
}
// 定义格式化映射
let formatMap = {
'yyyy': date.getFullYear(),
'MM': String(date.getMonth() + 1).padStart(2, '0'),
'dd': String(date.getDate()).padStart(2, '0'),
'HH': String(date.getHours()).padStart(2, '0'),
'mm': String(date.getMinutes()).padStart(2, '0'),
'ss': String(date.getSeconds()).padStart(2, '0'),
'SSS': String(date.getMilliseconds()).padStart(3, '0'),
'M': date.getMonth() + 1,
'd': date.getDate(),
'H': date.getHours(),
'm': date.getMinutes(),
's': date.getSeconds(),
'S': date.getMilliseconds()
};
// 替换格式字符串中的占位符
return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => {
return formatMap[match];
});
},
getdeviceShareId(id) { //获取设备分享信息
return request({
url: `/app/deviceShare/${id}`,
method: 'get',
})
},
getPermissions(type) {
if (!type) {
type = '6170';
}
let array = [{
value: "1",
label: "灯光模式",
checked: false,
type: ['6170', '670','102','6155','650','7305','6075']
},
{
value: "2",
label: "激光模式",
checked: false,
type: ['6170','6075']
},
{
value: "3",
label: "开机画面",
checked: false,
type: ['210', '6170', '670','6155','650','7305','6075']
},
{
value: "4",
label: "人员信息登记",
checked: false,
type: ['210', '6170', '670','6155','650','7305','6075']
},
{
value: "5",
label: "发送信息",
checked: false,
type: ['210', '6170', '670','6075']
},
{
value: "6",
label: "产品信息",
checked: false,
type: ['210', '6170', '670']
}, {
value: "41",
label: "静电探测",
checked: false,
type: ['670','650']
}, {
value: "42",
label: "SOS",
checked: false,
type: ['670','4877','6075']
},
{
value: "43",
label: "联机设备",
checked: false,
type: ['210']
},
{
value: "44",
label: "报警声音",
checked: false,
type: ['210']
},
{
value: "45",
label: "自动报警",
checked: false,
type: ['210']
},
{
value: "46",
label: "手动报警",
checked: false,
type: ['210','102']
},
{
value: "47",
label: "报警时长",
checked: false,
type: ['210']
},
{
value: "48",
label: "物体感应",
checked: false,
type: ['102']
},
{
value: "49",
label: "联机模式",
checked: false,
type: ['102']
},
{
value: "50",
label: "报警模式",
checked: false,
type: ['100','100J']
},
{
value: "51",
label: "警示灯",
checked: false,
type: ['100','100J']
},
{
value: "52",
label: "语音管理",
checked: false,
type: ['100','100J']
},
{
value: "53",
label: "箭头模式",
checked: false,
type: ['4877']
},
{
value: "54",
label: "配组设置",
checked: false,
type: ['4877']
},
{
value: "55",
label: "修改信道",
checked: false,
type: ['4877']
},
{
value: "56",
label: "灯光类型设置",
checked: false,
type: ['100J']
},
]
let arr = [];
for (let i = 0; i < array.length; i++) {
let item = array[i];
if (!item) {
continue;
}
if (!item.type) {
continue;
}
let typeContais = item.type.find(v => {
return v.includes(type);
});
if (typeContais) {
let json = {};
Object.assign(json, item);
arr.push(json);
}
}
return arr;
},
//10进制转换为16进制字符串
decimalToHexLittleEndian(num, byteCount, revers) {
// 处理负数(如果需要支持负数,可先转为补码)
if (num < 0) {
num = 0xFFFFFFFF + num + 1;
}
// 转为16进制去除前缀0x转为大写
let hex = num.toString(16).toUpperCase();
// 计算需要补充的0的数量确保每个字节占2位
let padLength = (byteCount || Math.ceil(hex.length / 2) * 2) - hex.length;
if (padLength > 0) {
hex = '0'.repeat(padLength) + hex;
}
// 分割为字节数组每2位一个字节
let bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(hex.substr(i, 2));
}
// 是否反转字节顺序(低位在前)并拼接
if (revers) {
return bytes.reverse().join('');
}
return bytes.join('');
},
//将相对路径的文件移动到_downloads文件夹中
moveFileToDownloads(tempFilePath) {
return new Promise((resolve, reject) => {
if(!tempFilePath){
console.log("无文件需要移动");
resolve(tempFilePath);
return;
}
//本来就在此目录时直接返回
if (tempFilePath.indexOf("_downloads") === 0) {
console.log("文件已存在,无需移动");
resolve(tempFilePath);
return;
}
//不是app直接返回
if (!uni.getSystemInfoSync().uniPlatform.includes('app')) {
resolve('仅支持 App 端操作');
return;
}
// console.log("tempFilePath=", tempFilePath);
var srcPath = plus.io.convertLocalFileSystemURL(tempFilePath);
// console.log("srcPath=", srcPath);
plus.io.resolveLocalFileSystemURL(srcPath,
(fileEntry) => {
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
// console.log("fs=",fs.name);
// console.log("fs=",fs.root.fullPath);
fileEntry.moveTo(fs.root, fileEntry.name, (entry) => {
console.log("entry=", entry);
let relativePath = `_downloads/${entry.name}`;
resolve(relativePath);
}, (ex) => {
reject(ex)
});
}, (e) => {
console.error("请求download目录失败 " + e);
reject(e);
});
},
(error) => {
console.log('文件不存在/路径错误:', error.message); // 核心问题!
}
);
});
},
getOSAndUpload(){
let os=uni.getSystemInfoSync().platform;
let url=''
if(os==='ios'){
url='https://apps.apple.com/cn/app/星汉物联/id6752555460'
}
else if(os==='android'){
url='https://www.pgyer.com/xhwl';
}
return {os:os,url:url};
},
//将点阵数据转换成RGB565
convertToRGB565(pixels, type) {
if (!type) {
type = 'rgb';
}
const result = new Uint16Array(pixels.length / 4);
let index = 0;
for (let i = 0; i < pixels.length; i += 4) {
let r = pixels[i];
let g = pixels[i + 1];
let b = pixels[i + 2];
let a = pixels[i + 3];
if (type == 'bgr') {
result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
} else {
result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
}
return result;
}
import request from "@/utils/request.js";
export default {
audioStorageKey: "audioStorageKey",
pcmStorageKey: "pcmStorageKey",
guid: function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0;
let v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
},
alert: function(title, content, callback) {
if (!title) {
title = '提示'
}
if (!content) {
content = title;
}
uni.showModal({
title: title,
content: content,
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
if (callback) {
callback(res);
}
}
});
},
showLoading: function(title, mask) {
uni.showLoading({
title: title,
mask: mask,
})
},
hideLoading: function() {
uni.hideLoading();
},
showToast: function(title, mask, duration, callback) {
if (!duration) {
duration = 1500;
}
if (mask == undefined) {
mask = false;
}
uni.showToast({
title: title,
mask: mask,
duration: duration,
callback: callback,
icon: 'none'
})
},
GetData: function(url, data, method, contentType, succ, err, complete) {
var these = this;
if (!url) {
console.error("url为空");
return;
}
if (url.toLowerCase().indexOf('http://') == -1 || url.toLowerCase().indexOf('https://') == -1) {
if (url.indexOf('/') == 0) {
url = url.substr(1, url.length - 1);
}
let ServerPath = these.DevApi;
if (these.Version === 'Dev') {
ServerPath = these.DevApi;
} else if (these.Version === 'Uat') {
ServerPath = these.UatApi;
} else if (these.Version === 'Relese') {
ServerPath = these.ReleseApi;
} else {
these.DevApi
}
url = ServerPath + url;
}
var these = this;
if (!method) {
method = 'POST';
}
method = method.toUpperCase();
if (!contentType) {
contentType = 'application/json;charset=UTF-8';
}
these.checkLAN(
function() {
these.showLoading('请稍候..', true);
setTimeout(function() {
uni.request({
url: url,
data: data,
header: {
"Content-Type": contentType
},
method: method,
timeout: 60000,
dataType: 'json',
success: function(json) {
if (succ) {
succ(json);
}
},
fail: function(ex) {
if (err) {
err(ex);
}
},
complete: function() {
if (complete) {
complete();
}
}
});
}, 0);
}
,
function() {
these.showToast('无网络连接');
});
},
checkLAN: function(succ, error) {
uni.getNetworkType({
success: (res) => {
let networkType = res.networkType;
// 判断网络是否连接
if (networkType === 'none') {
console.error('无网络连接')
if (error) {
error();
}
} else {
if (succ) {
succ();
}
}
},
fail: (err) => {
console.error('获取网络状态失败:', err);
if (error) {
error();
}
}
});
},
DateFormat: function(date, format) {
if (!date) {
date = new Date();
}
if (!format) {
format = 'yyyy-MM-dd HH:mm:ss';
}
// 处理参数默认值
if (typeof date === 'string' || typeof date === 'number') {
date = new Date(date);
}
date = date instanceof Date ? date : new Date();
format = format || 'yyyy-MM-dd';
// 检查日期是否有效
if (isNaN(date.getTime())) {
return 'Invalid Date';
}
// 定义格式化映射
let formatMap = {
'yyyy': date.getFullYear(),
'MM': String(date.getMonth() + 1).padStart(2, '0'),
'dd': String(date.getDate()).padStart(2, '0'),
'HH': String(date.getHours()).padStart(2, '0'),
'mm': String(date.getMinutes()).padStart(2, '0'),
'ss': String(date.getSeconds()).padStart(2, '0'),
'SSS': String(date.getMilliseconds()).padStart(3, '0'),
'M': date.getMonth() + 1,
'd': date.getDate(),
'H': date.getHours(),
'm': date.getMinutes(),
's': date.getSeconds(),
'S': date.getMilliseconds()
};
// 替换格式字符串中的占位符
return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => {
return formatMap[match];
});
},
getdeviceShareId(id) { //获取设备分享信息
return request({
url: `/app/deviceShare/${id}`,
method: 'get',
})
},
getPermissions(type) {
if (!type) {
type = '6170';
}
let array = [{
value: "1",
label: "灯光模式",
checked: false,
type: ['6170', '670','102','6155','650','7305','6075']
},
{
value: "2",
label: "激光模式",
checked: false,
type: ['6170','6075']
},
{
value: "3",
label: "开机画面",
checked: false,
type: ['210', '6170', '670','6155','650','7305','6075']
},
{
value: "4",
label: "人员信息登记",
checked: false,
type: ['210', '6170', '670','6155','650','7305','6075']
},
{
value: "5",
label: "发送信息",
checked: false,
type: ['210', '6170', '670','6075']
},
{
value: "6",
label: "产品信息",
checked: false,
type: ['210', '6170', '670']
}, {
value: "41",
label: "静电探测",
checked: false,
type: ['670','650']
}, {
value: "42",
label: "SOS",
checked: false,
type: ['670','4877','6075']
},
{
value: "43",
label: "联机设备",
checked: false,
type: ['210']
},
{
value: "44",
label: "报警声音",
checked: false,
type: ['210']
},
{
value: "45",
label: "自动报警",
checked: false,
type: ['210']
},
{
value: "46",
label: "手动报警",
checked: false,
type: ['210','102']
},
{
value: "47",
label: "报警时长",
checked: false,
type: ['210']
},
{
value: "48",
label: "物体感应",
checked: false,
type: ['102']
},
{
value: "49",
label: "联机模式",
checked: false,
type: ['102']
},
{
value: "50",
label: "报警模式",
checked: false,
type: ['100','100J']
},
{
value: "51",
label: "警示灯",
checked: false,
type: ['100','100J']
},
{
value: "52",
label: "语音管理",
checked: false,
type: ['100','100J']
},
{
value: "53",
label: "箭头模式",
checked: false,
type: ['4877']
},
{
value: "54",
label: "配组设置",
checked: false,
type: ['4877']
},
{
value: "55",
label: "修改信道",
checked: false,
type: ['4877','102']
},
{
value: "56",
label: "灯光类型设置",
checked: false,
type: ['100J']
},
]
let arr = [];
for (let i = 0; i < array.length; i++) {
let item = array[i];
if (!item) {
continue;
}
if (!item.type) {
continue;
}
let typeContais = item.type.find(v => {
return v.includes(type);
});
if (typeContais) {
let json = {};
Object.assign(json, item);
arr.push(json);
}
}
return arr;
},
//10进制转换为16进制字符串
decimalToHexLittleEndian(num, byteCount, revers) {
// 处理负数(如果需要支持负数,可先转为补码)
if (num < 0) {
num = 0xFFFFFFFF + num + 1;
}
// 转为16进制去除前缀0x转为大写
let hex = num.toString(16).toUpperCase();
// 计算需要补充的0的数量确保每个字节占2位
let padLength = (byteCount || Math.ceil(hex.length / 2) * 2) - hex.length;
if (padLength > 0) {
hex = '0'.repeat(padLength) + hex;
}
// 分割为字节数组每2位一个字节
let bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(hex.substr(i, 2));
}
// 是否反转字节顺序(低位在前)并拼接
if (revers) {
return bytes.reverse().join('');
}
return bytes.join('');
},
//将相对路径的文件移动到_downloads文件夹中
moveFileToDownloads(tempFilePath) {
return new Promise((resolve, reject) => {
if(!tempFilePath){
console.log("无文件需要移动");
resolve(tempFilePath);
return;
}
//本来就在此目录时直接返回
if (tempFilePath.indexOf("_downloads") === 0) {
console.log("文件已存在,无需移动");
resolve(tempFilePath);
return;
}
//不是app直接返回
if (!uni.getSystemInfoSync().uniPlatform.includes('app')) {
resolve('仅支持 App 端操作');
return;
}
// console.log("tempFilePath=", tempFilePath);
var srcPath = plus.io.convertLocalFileSystemURL(tempFilePath);
// console.log("srcPath=", srcPath);
plus.io.resolveLocalFileSystemURL(srcPath,
(fileEntry) => {
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
// console.log("fs=",fs.name);
// console.log("fs=",fs.root.fullPath);
fileEntry.moveTo(fs.root, fileEntry.name, (entry) => {
console.log("entry=", entry);
let relativePath = `_downloads/${entry.name}`;
resolve(relativePath);
}, (ex) => {
reject(ex)
});
}, (e) => {
console.error("请求download目录失败 " + e);
reject(e);
});
},
(error) => {
console.log('文件不存在/路径错误:', error.message); // 核心问题!
}
);
});
},
getOSAndUpload(){
let os=uni.getSystemInfoSync().platform;
let url=''
if(os==='ios'){
url='https://apps.apple.com/cn/app/星汉物联/id6752555460'
}
else if(os==='android'){
url='https://www.pgyer.com/xhwl';
}
return {os:os,url:url};
},
//将点阵数据转换成RGB565
convertToRGB565(pixels, type) {
if (!type) {
type = 'rgb';
}
const result = new Uint16Array(pixels.length / 4);
let index = 0;
for (let i = 0; i < pixels.length; i += 4) {
let r = pixels[i];
let g = pixels[i + 1];
let b = pixels[i + 2];
let a = pixels[i + 3];
if (type == 'bgr') {
result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
} else {
result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
}
return result;
}
}