From be8ee40a279a7b27d373409995f53afad665f538 Mon Sep 17 00:00:00 2001
From: liuwu <975499773@qq.com>
Date: Tue, 24 Oct 2023 16:15:53 +0800
Subject: [PATCH] V1.0.1
---
html/index1.html | 42 ++++++++-
script/ble.js | 215 +++++++++++++++++++++++++++------------------
script/electric.js | 8 +-
script/index.js | 1 -
4 files changed, 171 insertions(+), 95 deletions(-)
diff --git a/html/index1.html b/html/index1.html
index b530a58..35bae74 100644
--- a/html/index1.html
+++ b/html/index1.html
@@ -119,6 +119,7 @@
+
@@ -168,6 +169,7 @@
var networkStatus = true;
var driverName = '';
var pickData = [];
+ var bleData = [];
var btnArray = ['取消', '确认'];
var sbId = null;
// 初始化echarts Dom
@@ -201,6 +203,7 @@
var allow = 0;
var first = true;
var carListener = null;
+ var bleListener = null;
var resultInterval = null;
var totalInterval = null;
var textInterval = null;
@@ -279,7 +282,7 @@
});
};
- function initService() {
+function initService() {
initManager({single: true});
}
@@ -427,6 +430,7 @@ function getDateStr(date) {
return len;
};
var userPicker;
+ var blePicker;
// 初始化设备下拉
function muiInit() {
(function ($, doc) {
@@ -458,6 +462,42 @@ function getDateStr(date) {
});
})(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() {
api.ajax({
diff --git a/script/ble.js b/script/ble.js
index 3ca8450..bceebb0 100644
--- a/script/ble.js
+++ b/script/ble.js
@@ -17,17 +17,17 @@ var sta = true;
var scanInter;
function scan(param) {
- if (!uuid){
- uuid = api.getPrefs({
- sync: true,
- key: 'uuid'
- });
- }
- if (uuid){
- disconnect({peripheralUUID: uuid});
- connect({peripheralUUID: uuid});
- return false
- }
+ // if (!uuid){
+ // uuid = api.getPrefs({
+ // sync: true,
+ // key: 'uuid'
+ // });
+ // }
+ // if (uuid){
+ // disconnect({peripheralUUID: uuid});
+ // connect({peripheralUUID: uuid});
+ // return false
+ // }
sta = true
clearInterval(scanInter)
times = 5;
@@ -42,7 +42,6 @@ function scan(param) {
});
}else {
if (sta){
- api.hideProgress();
getPeripheral();
sta = false;
}
@@ -54,40 +53,58 @@ function scan(param) {
function getPeripheral() {
ble.getPeripheral(function (ret) {
+ bleData = []
var ret2 = {};
if (ret.peripherals.length > 0) {
ret.peripherals.forEach(function (p) {
if (undefined !== p.name) {
console.log(p.name);
- 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 (p.name.indexOf("TMR_BLE") === -1){
+ return false
}
+ 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){
- mui.confirm('没有扫描到设备信息,继续扫描?', '提示', btnArray, function (e) {
- if (e.index === 0) {} else {
- scan({clean: true});
- }
- });
- }
+ bleInit()
+ // if (undefined === ret2.uuid){
+ // mui.confirm('没有扫描到设备信息,继续扫描?', '提示', btnArray, function (e) {
+ // if (e.index === 0) {} else {
+ // scan({clean: true});
+ // }
+ // });
+ // }
}
+ api.hideProgress();
});
}
@@ -107,17 +124,19 @@ function connect(param) {
wifiDom.innerHTML = '已连接';
document.getElementById('text').innerHTML = '设备已连接';
text = '设备已连接';
- console.log('连接成功')
+ console.log('主机端连接成功')
initWeight();
discoverService({peripheralUUID: param.peripheralUUID});
}else {
connetcedStatus = false
connectTimes ++;
- console.log('连接失败'+connectTimes+' '+uuid)
+ console.log('主机端连接失败'+connectTimes+' '+uuid)
+ wifiDom.innerHTML = '连接失败';
api.hideProgress();
setTimeout(function (){
if (connectTimes < 5 && !connetcedStatus && uuid){
console.log("连接失败!尝试重连" + uuid);
+ wifiDom.innerHTML = '尝试重连';
connect({peripheralUUID: uuid});
}
},3000)
@@ -136,6 +155,46 @@ function disconnect(param) {
}
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) {
ble.discoverService(param, function (ret) {
if (ret.services.length > 0) {
@@ -147,25 +206,9 @@ function discoverService(param) {
serviceUUID: ret4,
peripheralUUID: param.peripheralUUID
}, function (ret5) {
- clearInterval(timer)
- var timer = setInterval(function(){
- if (msgList.length > 0) {
- // 发送报文
- 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)
+ peripheralUUID = param.peripheralUUID;
+ serviceUUID =ret4;
+ characteristicUUID =ret5;
setNotify({
peripheralUUID: param.peripheralUUID,
serviceUUID: ret4,
@@ -208,16 +251,6 @@ function discoverService(param) {
// console.log(JSON.stringify(ret7))
});
}
- // feedMyChart.setOption({
- // dataset: {
- // source: [[1, _feedNum]]
- // }
- // });
- // weightMyChart.setOption({
- // dataset: {
- // source: [[1, nowWeight]]
- // }
- // });
// 计划-饲喂
var feedErr = _planSum-_feedNum;
if (isNaN(feedErr)) {
@@ -237,7 +270,6 @@ function discoverService(param) {
ledStatus = false;
status = '';
statusEle = false;
- msgList = [];
console.log("停止蜂鸣");
control("00", "00");
setTimeout(function () {
@@ -252,7 +284,6 @@ function discoverService(param) {
ledStatus = false;
status = '间隔';
statusEle = true;
- msgList = [];
console.log("开始蜂鸣" + time);
control("60", time);
setTimeout(function () {
@@ -267,7 +298,6 @@ function discoverService(param) {
ledStatus = false;
status = '间隔';
statusEle = true;
- msgList = [];
console.log("开始蜂鸣" + time);
control("60", time);
setTimeout(function () {
@@ -282,7 +312,6 @@ function discoverService(param) {
ledStatus = false;
status = '间隔';
statusEle = true;
- msgList = [];
console.log("开始蜂鸣" + time);
control("60", time);
setTimeout(function () {
@@ -295,7 +324,6 @@ function discoverService(param) {
ledStatus = false;
status = '长鸣';
statusEle = true;
- msgList = [];
console.log("开始长鸣");
control("40", "02");
setTimeout(function () {
@@ -316,7 +344,7 @@ function discoverService(param) {
if (weightDataFilter.dataBuffer.length === 80) {
console.log("重量稳定", currTimeFn(new Date()));
weightDataFilter.dataBuffer = [];
- // document.getElementById('submitData').click();
+ document.getElementById('submitData').click();
}
}
}
@@ -362,15 +390,17 @@ function setNotify(param, callback) {
}
function writeValueForCharacteristic(param, callback) {
- ble.writeValueForCharacteristic(param, function (ret) {
- // console.log(JSON.stringify(ret));
- if (ret.status) {
- // console.log("发送成功");
- } else {
- console.log("发送失败");
- }
- callback(ret);
- });
+ if (param.value && connetcedStatus){
+ ble.writeValueForCharacteristic(param, function (ret) {
+ // console.log(JSON.stringify(ret));
+ if (ret.status) {
+ // console.log("发送成功");
+ } else {
+ console.log("发送失败:"+param.value);
+ }
+ callback(ret);
+ });
+ }
}
var lastCmd = "";
var lastTime = new Date().getSeconds()*1000+ new Date().getMilliseconds();
@@ -423,8 +453,11 @@ function getMess(param, callback) {
param.value = "FF0500" + num.toString(16) + str;
count ++;
if (count >= 5){
- msgList.push(param.value);
- // console.log(param.value + ' '+ new Date().getSeconds() + ' '+new Date().getMilliseconds())
+ // msgList.push(param.value);
+ 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();
renderLoad();
feedMyChart.setOption({
@@ -437,11 +470,21 @@ function getMess(param, callback) {
source: [[1, nowWeight]]
}
});
-
count = 0 ;
}
});
}
+
+function strToHex(str,length){
+ var hex = Number(str).toString(16);
+ var append = '';
+ if (hex.length < length){
+ for (var i=0;i