This commit is contained in:
liuwu 2023-10-24 16:15:53 +08:00
parent 7aa66d199e
commit be8ee40a27
4 changed files with 171 additions and 95 deletions

View File

@ -119,6 +119,7 @@
<button id="driveName" class="btnc mui-btn-info" type="button" onclick="handleBackSys()">机车手</button> <button id="driveName" class="btnc mui-btn-info" type="button" onclick="handleBackSys()">机车手</button>
<button id='wifiDom' class="btnc mui-btn-inline mui-btn-info" <button id='wifiDom' class="btnc mui-btn-inline mui-btn-info"
style="color: seagreen !important;" type='button'>暂未连接</button> style="color: seagreen !important;" type='button'>暂未连接</button>
<button id='showBlePicker' class="btnc mui-btn-inline mui-btn-primary" type='button'>选择蓝牙</button>
</div> </div>
</div> </div>
<!--中间内容开始--> <!--中间内容开始-->
@ -168,6 +169,7 @@
var networkStatus = true; var networkStatus = true;
var driverName = ''; var driverName = '';
var pickData = []; var pickData = [];
var bleData = [];
var btnArray = ['取消', '<b style="font-weight:900">确认</b>']; var btnArray = ['取消', '<b style="font-weight:900">确认</b>'];
var sbId = null; var sbId = null;
// 初始化echarts Dom // 初始化echarts Dom
@ -201,6 +203,7 @@
var allow = 0; var allow = 0;
var first = true; var first = true;
var carListener = null; var carListener = null;
var bleListener = null;
var resultInterval = null; var resultInterval = null;
var totalInterval = null; var totalInterval = null;
var textInterval = null; var textInterval = null;
@ -279,7 +282,7 @@
}); });
}; };
function initService() { function initService() {
initManager({single: true}); initManager({single: true});
} }
@ -427,6 +430,7 @@ function getDateStr(date) {
return len; return len;
}; };
var userPicker; var userPicker;
var blePicker;
// 初始化设备下拉 // 初始化设备下拉
function muiInit() { function muiInit() {
(function ($, doc) { (function ($, doc) {
@ -458,6 +462,42 @@ function getDateStr(date) {
}); });
})(mui, document); })(mui, document);
} }
function bleInit(){
(function ($, doc) {
$.init();
$.ready(function () {
/**
* 获取对象属性的值
* 主要用于过滤三级联动中,可能出现的最低级的数据不存在的情况,实际开发中需要注意这一点;
* @param {Object} obj 对象
* @param {String} param 属性名
*/
var _getParam = function _getParam(obj, param) {
return obj[param] || '';
};
if (blePicker) {} else {
blePicker = new $.PopPicker();
var showBlePickerButton = doc.getElementById('showBlePicker');
bleListener = null;
bleListener = showBlePickerButton.addEventListener('tap', function (event) {
blePicker.show(function (items) {
console.log('蓝牙'+JSON.stringify(items[0]));
showBlePickerButton.innerHTML = items[0].text;
uuid = items[0].value
api.setPrefs({
key: 'uuid',
value: uuid
});
connect({peripheralUUID: items[0].value});
});
}, false);
}
blePicker.setData(bleData);
});
})(mui, document);
}
// 初始化班次信息 // 初始化班次信息
function initTime() { function initTime() {
api.ajax({ api.ajax({

View File

@ -17,17 +17,17 @@ var sta = true;
var scanInter; var scanInter;
function scan(param) { function scan(param) {
if (!uuid){ // if (!uuid){
uuid = api.getPrefs({ // uuid = api.getPrefs({
sync: true, // sync: true,
key: 'uuid' // key: 'uuid'
}); // });
} // }
if (uuid){ // if (uuid){
disconnect({peripheralUUID: uuid}); // disconnect({peripheralUUID: uuid});
connect({peripheralUUID: uuid}); // connect({peripheralUUID: uuid});
return false // return false
} // }
sta = true sta = true
clearInterval(scanInter) clearInterval(scanInter)
times = 5; times = 5;
@ -42,7 +42,6 @@ function scan(param) {
}); });
}else { }else {
if (sta){ if (sta){
api.hideProgress();
getPeripheral(); getPeripheral();
sta = false; sta = false;
} }
@ -54,40 +53,58 @@ function scan(param) {
function getPeripheral() { function getPeripheral() {
ble.getPeripheral(function (ret) { ble.getPeripheral(function (ret) {
bleData = []
var ret2 = {}; var ret2 = {};
if (ret.peripherals.length > 0) { if (ret.peripherals.length > 0) {
ret.peripherals.forEach(function (p) { ret.peripherals.forEach(function (p) {
if (undefined !== p.name) { if (undefined !== p.name) {
console.log(p.name); console.log(p.name);
if (p.name.indexOf("BLUE2USART") !== -1) { if (p.name.indexOf("TMR_BLE") === -1){
uuid = p.uuid; return false
api.setPrefs({
key: 'uuid',
value: uuid
});
ret2 = p;
if (ret2 !== {}) {
var wifiDom = document.getElementById('wifiDom');
wifiDom.innerHTML = "设备:" + ret2.name;
api.hideProgress();
api.showProgress({
title: '设备连接中...',
modal: true
});
document.getElementById('text').innerHTML = '设备连接中';
connect({peripheralUUID: ret2.uuid});
}
} }
var json = { value: p.uuid, text: p.name };
bleData.push(json);
if (!uuid){
uuid = api.getPrefs({
sync: true,
key: 'uuid'
});
}
if (uuid && uuid === p.uuid){
disconnect({peripheralUUID: uuid});
connect({peripheralUUID: uuid});
}
// if (p.name.indexOf("BLUE2USART") !== -1) {
// uuid = p.uuid;
// api.setPrefs({
// key: 'uuid',
// value: uuid
// });
// ret2 = p;
// if (ret2 !== {}) {
// var wifiDom = document.getElementById('wifiDom');
// wifiDom.innerHTML = "设备:" + ret2.name;
// api.hideProgress();
// api.showProgress({
// title: '设备连接中...',
// modal: true
// });
// document.getElementById('text').innerHTML = '设备连接中';
// connect({peripheralUUID: ret2.uuid});
// }
// }
} }
}); });
if (undefined === ret2.uuid){ bleInit()
mui.confirm('没有扫描到设备信息,继续扫描?', '提示', btnArray, function (e) { // if (undefined === ret2.uuid){
if (e.index === 0) {} else { // mui.confirm('没有扫描到设备信息,继续扫描?', '提示', btnArray, function (e) {
scan({clean: true}); // if (e.index === 0) {} else {
} // scan({clean: true});
}); // }
} // });
// }
} }
api.hideProgress();
}); });
} }
@ -107,17 +124,19 @@ function connect(param) {
wifiDom.innerHTML = '已连接'; wifiDom.innerHTML = '已连接';
document.getElementById('text').innerHTML = '设备已连接'; document.getElementById('text').innerHTML = '设备已连接';
text = '设备已连接'; text = '设备已连接';
console.log('连接成功') console.log('主机端连接成功')
initWeight(); initWeight();
discoverService({peripheralUUID: param.peripheralUUID}); discoverService({peripheralUUID: param.peripheralUUID});
}else { }else {
connetcedStatus = false connetcedStatus = false
connectTimes ++; connectTimes ++;
console.log('连接失败'+connectTimes+' '+uuid) console.log('主机端连接失败'+connectTimes+' '+uuid)
wifiDom.innerHTML = '连接失败';
api.hideProgress(); api.hideProgress();
setTimeout(function (){ setTimeout(function (){
if (connectTimes < 5 && !connetcedStatus && uuid){ if (connectTimes < 5 && !connetcedStatus && uuid){
console.log("连接失败!尝试重连" + uuid); console.log("连接失败!尝试重连" + uuid);
wifiDom.innerHTML = '尝试重连';
connect({peripheralUUID: uuid}); connect({peripheralUUID: uuid});
} }
},3000) },3000)
@ -136,6 +155,46 @@ function disconnect(param) {
} }
var i; var i;
var peripheralUUID =''
var serviceUUID =''
var characteristicUUID =''
var msgLed = '';
var msgChan = '';
var msgVoice = '';
var sendStatus = true;
var timerLed = null;
clearInterval(timerLed)
timerLed = setInterval(function(){
if (sendStatus && connetcedStatus && peripheralUUID && serviceUUID && characteristicUUID) {
sendStatus = false;
var param1 = {
peripheralUUID: peripheralUUID,
serviceUUID: serviceUUID,
characteristicUUID: characteristicUUID,
value: msgLed
};
// console.log("msgLed"+new Date().getMilliseconds())
writeValueForCharacteristic(param1, function (ret) {});
setTimeout(function () {
param1.value = msgVoice
// console.log("msgVoice"+new Date().getMilliseconds())
writeValueForCharacteristic(param1, function (ret) {});
setTimeout(function () {
param1.value = msgChan
// console.log("msgChan"+new Date().getMilliseconds())
writeValueForCharacteristic(param1, function (ret) {});
setTimeout(function () {
// console.log("sendStatus"+new Date().getMilliseconds())
sendStatus = true
}, 30);
}, 50);
}, 50);
}
},150)
function discoverService(param) { function discoverService(param) {
ble.discoverService(param, function (ret) { ble.discoverService(param, function (ret) {
if (ret.services.length > 0) { if (ret.services.length > 0) {
@ -147,25 +206,9 @@ function discoverService(param) {
serviceUUID: ret4, serviceUUID: ret4,
peripheralUUID: param.peripheralUUID peripheralUUID: param.peripheralUUID
}, function (ret5) { }, function (ret5) {
clearInterval(timer) peripheralUUID = param.peripheralUUID;
var timer = setInterval(function(){ serviceUUID =ret4;
if (msgList.length > 0) { characteristicUUID =ret5;
// 发送报文
var msg = msgList[0];
msgList.splice(0, 1);
// console.log(msg+ ' ' +new Date().getSeconds()*1000+ new Date().getMilliseconds())
// console.log('设备'+sbId + '班次' + classCode + '车次' + trainNumber +' '+ nowWeight + '/' + _weightSum + ' ' + feedName + _feedNum + '/' + _planSum);
var param1 = {
peripheralUUID: param.peripheralUUID,
serviceUUID: ret4,
characteristicUUID: ret5,
value: msg
};
writeValueForCharacteristic(param1, function (ret) {
// console.log(ret.status)
});
}
},150)
setNotify({ setNotify({
peripheralUUID: param.peripheralUUID, peripheralUUID: param.peripheralUUID,
serviceUUID: ret4, serviceUUID: ret4,
@ -208,16 +251,6 @@ function discoverService(param) {
// console.log(JSON.stringify(ret7)) // console.log(JSON.stringify(ret7))
}); });
} }
// feedMyChart.setOption({
// dataset: {
// source: [[1, _feedNum]]
// }
// });
// weightMyChart.setOption({
// dataset: {
// source: [[1, nowWeight]]
// }
// });
// 计划-饲喂 // 计划-饲喂
var feedErr = _planSum-_feedNum; var feedErr = _planSum-_feedNum;
if (isNaN(feedErr)) { if (isNaN(feedErr)) {
@ -237,7 +270,6 @@ function discoverService(param) {
ledStatus = false; ledStatus = false;
status = ''; status = '';
statusEle = false; statusEle = false;
msgList = [];
console.log("停止蜂鸣"); console.log("停止蜂鸣");
control("00", "00"); control("00", "00");
setTimeout(function () { setTimeout(function () {
@ -252,7 +284,6 @@ function discoverService(param) {
ledStatus = false; ledStatus = false;
status = '间隔'; status = '间隔';
statusEle = true; statusEle = true;
msgList = [];
console.log("开始蜂鸣" + time); console.log("开始蜂鸣" + time);
control("60", time); control("60", time);
setTimeout(function () { setTimeout(function () {
@ -267,7 +298,6 @@ function discoverService(param) {
ledStatus = false; ledStatus = false;
status = '间隔'; status = '间隔';
statusEle = true; statusEle = true;
msgList = [];
console.log("开始蜂鸣" + time); console.log("开始蜂鸣" + time);
control("60", time); control("60", time);
setTimeout(function () { setTimeout(function () {
@ -282,7 +312,6 @@ function discoverService(param) {
ledStatus = false; ledStatus = false;
status = '间隔'; status = '间隔';
statusEle = true; statusEle = true;
msgList = [];
console.log("开始蜂鸣" + time); console.log("开始蜂鸣" + time);
control("60", time); control("60", time);
setTimeout(function () { setTimeout(function () {
@ -295,7 +324,6 @@ function discoverService(param) {
ledStatus = false; ledStatus = false;
status = '长鸣'; status = '长鸣';
statusEle = true; statusEle = true;
msgList = [];
console.log("开始长鸣"); console.log("开始长鸣");
control("40", "02"); control("40", "02");
setTimeout(function () { setTimeout(function () {
@ -316,7 +344,7 @@ function discoverService(param) {
if (weightDataFilter.dataBuffer.length === 80) { if (weightDataFilter.dataBuffer.length === 80) {
console.log("重量稳定", currTimeFn(new Date())); console.log("重量稳定", currTimeFn(new Date()));
weightDataFilter.dataBuffer = []; weightDataFilter.dataBuffer = [];
// document.getElementById('submitData').click(); document.getElementById('submitData').click();
} }
} }
} }
@ -362,15 +390,17 @@ function setNotify(param, callback) {
} }
function writeValueForCharacteristic(param, callback) { function writeValueForCharacteristic(param, callback) {
ble.writeValueForCharacteristic(param, function (ret) { if (param.value && connetcedStatus){
// console.log(JSON.stringify(ret)); ble.writeValueForCharacteristic(param, function (ret) {
if (ret.status) { // console.log(JSON.stringify(ret));
// console.log("发送成功"); if (ret.status) {
} else { // console.log("发送成功");
console.log("发送失败"); } else {
} console.log("发送失败:"+param.value);
callback(ret); }
}); callback(ret);
});
}
} }
var lastCmd = ""; var lastCmd = "";
var lastTime = new Date().getSeconds()*1000+ new Date().getMilliseconds(); var lastTime = new Date().getSeconds()*1000+ new Date().getMilliseconds();
@ -423,8 +453,11 @@ function getMess(param, callback) {
param.value = "FF0500" + num.toString(16) + str; param.value = "FF0500" + num.toString(16) + str;
count ++; count ++;
if (count >= 5){ if (count >= 5){
msgList.push(param.value); // msgList.push(param.value);
// console.log(param.value + ' '+ new Date().getSeconds() + ' '+new Date().getMilliseconds()) msgLed = param.value;
// var ms = strToHex(sbId,4)+strToHex(classCode,2)+strToHex(trainNumber,2)+strToHex(feedIndex,2)+strToHex(_planSum,4)+strToHex(_feedNum,4)+strToHex(nowWeight,4);
// var num1 = ms.length / 2;
// msgChan = "FF0a00"+strToHex(num1,2) + ms
renderUnload(); renderUnload();
renderLoad(); renderLoad();
feedMyChart.setOption({ feedMyChart.setOption({
@ -437,11 +470,21 @@ function getMess(param, callback) {
source: [[1, nowWeight]] source: [[1, nowWeight]]
} }
}); });
count = 0 ; count = 0 ;
} }
}); });
} }
function strToHex(str,length){
var hex = Number(str).toString(16);
var append = '';
if (hex.length < length){
for (var i=0;i<length-hex.length;i++){
append = append + '0';
}
}
return append+hex;
}
function initWeight() { function initWeight() {
api.showProgress({ api.showProgress({
title: '图表数据加装中...', title: '图表数据加装中...',

View File

@ -52,11 +52,5 @@ function getControlCmd(status, time) {
var lastMsg = ''; var lastMsg = '';
function control(status, time) { function control(status, time) {
var msg = "FFFF000D" + getControlCmd(status, time); var msg = "FFFF000D" + getControlCmd(status, time);
msgList=[] msgVoice = msg;
msgList.push(msg);
// queue.enqueue(msg)
// writeValueForCharacteristic({peripheralUUID:uuid,serviceUUID:serviceUUID,characteristicUUID:characteristicUUID,value:msg},function (ret) {
//
// // console.log(JSON.stringify(ret))
// })
} }

View File

@ -142,7 +142,6 @@ var uuid = '';
var serviceUUID = ''; var serviceUUID = '';
var characteristicUUID = ''; var characteristicUUID = '';
var lastTime = ""; var lastTime = "";
var msgList = [];
var time = ""; var time = "";