From ab1b5f183e0c11ff626218d7c3bc2392e7597c7f Mon Sep 17 00:00:00 2001 From: liuwu <975499773@qq.com> Date: Wed, 1 Nov 2023 10:48:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=B7=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E100=E7=9A=84=E9=A5=B2=E6=96=99=E4=B8=8D?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=A0=A1=E5=87=86=E4=B8=8D=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=EF=BC=8C=E7=8F=AD=E6=AC=A1=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=B8=80=E7=9B=B4=E6=98=BE=E7=A4=BA=E9=A5=B2=E5=96=82=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=8C=E8=BD=A6=E5=86=85=E6=80=BB=E9=87=8D=E9=87=8F?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=98=BE=E7=A4=BA=EF=BC=8Cled=E5=B1=8F?= =?UTF-8?q?=E5=B9=95=E4=B8=80=E7=9B=B4=E6=98=BE=E7=A4=BA=E7=8F=AD=E6=AC=A1?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html/index1.html | 7 +- script/ble.js | 304 +++++++++++++++++++++++++++-------------------- script/index.js | 2 + 3 files changed, 178 insertions(+), 135 deletions(-) diff --git a/html/index1.html b/html/index1.html index b01a90c..4122529 100644 --- a/html/index1.html +++ b/html/index1.html @@ -496,10 +496,6 @@ function getDateStr(date) { 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); @@ -621,7 +617,7 @@ function getDateStr(date) { var json = [].concat(_toConsumableArray(e.feedJson), _toConsumableArray(e.cowshedJson)); feedList = json; json.forEach(function (j, index) { - if (index == 0) { + if (index === 0) { html += '' + UNICODE_CHARS[index + 1] + j.name + ':' + Number(j.weight).toFixed(0) + ''; } else { html += '' + UNICODE_CHARS[index + 1] + j.name + ':' + Number(j.weight).toFixed(0) + ''; @@ -641,6 +637,7 @@ function getDateStr(date) { allow = json[0].allow; // 计划 _planSum = json[0].weight.toFixed(0); + allowAuto = _planSum >= 100; document.getElementById('feedNum').innerHTML = ':' + _planSum + '/已装:' + _feedNum + ''; text = feedName + _planSum; batchName = e.className + e.trainNumber; diff --git a/script/ble.js b/script/ble.js index f08c22d..a164b53 100644 --- a/script/ble.js +++ b/script/ble.js @@ -116,6 +116,11 @@ function connect(param) { ble.connect(param, function (ret, err) { if (ret.status) { connetcedStatus = true + // 缓存蓝牙设备uuid方便自动连接 + api.setPrefs({ + key: 'uuid', + value: param.peripheralUUID + }); wifiDom.innerHTML = '已连接'; connectTimes = 0; document.getElementById('text').innerHTML = '设备已连接'; @@ -192,6 +197,8 @@ var timerMsgList = setInterval(function(){ },150) // 未装料状态计数,采集达到10次,发送一次停止蜂鸣指令 var bleConunt=0; +// 默认不自动跳转 +var allowAuto = false; /*扫描蓝牙服务*/ function discoverService(param) { ble.discoverService(param, function (ret) { @@ -214,36 +221,34 @@ function discoverService(param) { serviceUUID: ret4, characteristicUUID: ret5 }, function (ret6) { - if(!feedStatus){ - return false - } if (ret6 !== undefined) { if (isNaN(ret6)) { return false } nowWeight = ret6; - // console.log(ret6) - if (first) { - first = false; - beforeWeight = nowWeight; - } - if (load) { - _feedNum = nowWeight - beforeWeight; - } else { - _feedNum = beforeWeight - nowWeight; - } - text = feedName + _planSum; - if (!isNaN(nowWeight)) { - // 实时重量保存 - totalList.push({ - time: currTimeFn(new Date()), - nowWeight: nowWeight - }); + // 装料状态 + if(feedStatus){ + if (first) { + first = false; + beforeWeight = nowWeight; + } + if (load) { + _feedNum = nowWeight - beforeWeight; + } else { + _feedNum = beforeWeight - nowWeight; + } + text = feedName + _planSum; + if (!isNaN(nowWeight)) { + // 实时重量保存 + totalList.push({ + time: currTimeFn(new Date()), + nowWeight: nowWeight + }); + } + document.getElementById('feedNum').innerHTML = ':' + _planSum + '/已装:' + _feedNum + ''; } serviceUUID = ret4; characteristicUUID = ret5; - // console.log(_feedNum,_weightSum,_planSum); - document.getElementById('feedNum').innerHTML = ':' + _planSum + '/已装:' + _feedNum + ''; // 组装led报文 if (ledStatus && !isNaN(nowWeight)) { getMess({ @@ -252,114 +257,117 @@ function discoverService(param) { characteristicUUID: ret5, value: ret6 }, function (ret7) { - // console.log(JSON.stringify(ret7)) }); } - // 计划-饲喂 - var feedErr = _planSum-_feedNum; - if (isNaN(feedErr)) { - return false - } - // 蜂鸣器校验基础值,大于500的饲料,取500为标准 - var count; - if (_planSum >= 500) { - count = 500 - } else { - count = _planSum - } - // 计算饲料装料量与基础值的误差比例 - var ration = feedErr / count; - // console.log("ration" + ration.toFixed(2)); - // 误差分为100%-20%(停止蜂鸣),10%-20%(间隔05),5%-10%(间隔03),2%-5%(间隔01),-100%-2%(长鸣) - if (ration > 0.2) { - bleConunt ++; - if (statusEle || (bleConunt >= 10)) { - bleConunt=0; - ledStatus = false; - status = ''; - statusEle = false; - msgList = []; - control("00", "00"); - setTimeout(function () { - ledStatus = true; - }, 500); + // 装料状态 + if(feedStatus){ + // 计划-饲喂 + var feedErr = _planSum-_feedNum; + if (isNaN(feedErr)) { + return false } - } else if (0.1 < ration && ration <= 0.2) { - time = "05"; - if (!statusEle || lastTime !== time) { - console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); - lastTime = time; - ledStatus = false; - status = '间隔'; - statusEle = true; - msgList = []; - console.log("开始蜂鸣" + time); - control("60", time); - setTimeout(function () { - ledStatus = true; - }, 500); + // 蜂鸣器校验基础值,大于500的饲料,取500为标准 + var count; + if (_planSum >= 500) { + count = 500 + } else { + count = _planSum } - } else if (0.05 < ration && ration <= 0.1) { - time = "03"; - if (!statusEle || lastTime !== time) { - console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); - lastTime = time; - ledStatus = false; - status = '间隔'; - statusEle = true; - msgList = []; - console.log("开始蜂鸣" + time); - control("60", time); - setTimeout(function () { - ledStatus = true; - }, 500); - } - } else if (0.02 < ration && ration <= 0.05) { - time = "01"; - if (!statusEle || lastTime !== time) { - console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); - lastTime = time; - ledStatus = false; - status = '间隔'; - statusEle = true; - msgList = []; - console.log("开始蜂鸣" + time); - control("60", time); - setTimeout(function () { - ledStatus = true; - }, 500); - } - } else { - if (status === '间隔' || status === '') { - console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); - ledStatus = false; - status = '长鸣'; - statusEle = true; - msgList = []; - console.log("开始长鸣"); - control("40", "02"); - setTimeout(function () { - ledStatus = true; - }, 500); - - setTimeout(function() { - console.log("开始长鸣") - control("40", "02"); - setTimeout(function() { + // 计算饲料装料量与基础值的误差比例 + var ration = feedErr / count; + // console.log("ration" + ration.toFixed(2)); + // 误差分为100%-20%(停止蜂鸣),10%-20%(间隔05),5%-10%(间隔03),2%-5%(间隔01),-100%-2%(长鸣) + if (ration > 0.2) { + bleConunt ++; + if (statusEle || (bleConunt >= 10)) { + bleConunt=0; + ledStatus = false; + status = ''; + statusEle = false; + msgList = []; + control("00", "00"); + setTimeout(function () { ledStatus = true; + }, 500); + } + } else if (0.1 < ration && ration <= 0.2) { + time = "05"; + if (!statusEle || lastTime !== time) { + console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); + lastTime = time; + ledStatus = false; + status = '间隔'; + statusEle = true; + msgList = []; + console.log("开始蜂鸣" + time); + control("60", time); + setTimeout(function () { + ledStatus = true; + }, 500); + } + } else if (0.05 < ration && ration <= 0.1) { + time = "03"; + if (!statusEle || lastTime !== time) { + console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); + lastTime = time; + ledStatus = false; + status = '间隔'; + statusEle = true; + msgList = []; + console.log("开始蜂鸣" + time); + control("60", time); + setTimeout(function () { + ledStatus = true; + }, 500); + } + } else if (0.02 < ration && ration <= 0.05) { + time = "01"; + if (!statusEle || lastTime !== time) { + console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); + lastTime = time; + ledStatus = false; + status = '间隔'; + statusEle = true; + msgList = []; + console.log("开始蜂鸣" + time); + control("60", time); + setTimeout(function () { + ledStatus = true; + }, 500); + } + } else { + if (status === '间隔' || status === '') { + console.log(feedErr + ' ' + allow + ' ' + _feedNum + ' ' + _planSum); + ledStatus = false; + status = '长鸣'; + statusEle = true; + msgList = []; + console.log("开始长鸣"); + control("40", "02"); + setTimeout(function () { + ledStatus = true; + }, 500); + + setTimeout(function() { + console.log("开始长鸣") + control("40", "02"); + setTimeout(function() { + ledStatus = true; + }, 500) }, 500) - }, 500) + } } - } - // 在误差范围内 - if (feedErr <= allow) { - // 重量稳定计数 - var filteredValue = weightDataFilter.filter(nowWeight); - // 重量稳定自动跳转 - if (weightDataFilter.dataBuffer.length === 40) { - console.log("重量稳定", currTimeFn(new Date())); - weightDataFilter.dataBuffer = [] - document.getElementById('submitData').click(); + // console.log(weightStabilization("weightStabilization"+nowWeight)) + // 在误差范围内 + if (feedErr <= allow && allowAuto) { + // 重量稳定计数 + var filteredValue = weightDataFilter.filter(nowWeight); + // 重量稳定自动跳转 + if (weightDataFilter.dataBuffer.length === 40) { + console.log("重量稳定", currTimeFn(new Date())); + weightDataFilter.dataBuffer = [] + document.getElementById('submitData').click(); + } } } } @@ -483,7 +491,23 @@ function getMess(param, callback) { } else { newmsg = feedName + ttr; } - param1.msg = newmsg + Math.abs(Number(_planSum - _feedNum)).toFixed(0) + 'kg'; + // 不是装料状态班次饲喂完成显示 + if(!feedStatus){ + var newmsg = batchName+'次'; + var msglen = newmsg.getByteLen(); + var ttr = ''; + if (msglen < 8) { + for (var i = 0; i < 8 - msglen; i++) { + newmsg = newmsg + ' '; + } + newmsg = newmsg + ttr; + } else { + newmsg = feedName + ttr; + } + param1.msg = newmsg + '饲喂完成'; + }else{ + param1.msg = newmsg + Math.abs(Number(_planSum - _feedNum)).toFixed(0) + 'kg'; + } // param1.msg = newmsg + Math.abs(Number(param.value)).toFixed(0) + 'kg' // console.log(param1.msg) //组装字符卡LED报文 @@ -499,13 +523,16 @@ function getMess(param, callback) { // 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; // 同步刷新图表 - renderUnload(); + // 饲喂状态刷新实时状态,否则不刷新装料信息 + if(feedStatus){ + renderUnload(); + feedMyChart.setOption({ + dataset: { + source: [[1, _feedNum]] + } + }); + } renderLoad(); - feedMyChart.setOption({ - dataset: { - source: [[1, _feedNum]] - } - }); weightMyChart.setOption({ dataset: { source: [[1, nowWeight]] @@ -559,3 +586,20 @@ function initWeight() { document.getElementById('text').innerHTML = 'V' + api.appVersion; api.setGlobalData({key: 'startTime', value: currTimeFn(new Date())}); } + +var lastWeight = 0; +function weightStabilization(weight) { + // 计算当前重量与上一次记录的重量之间的差值 + var diff = weight - lastWeight; + // 如果差值在一定范围内,则认为重量是稳定的 + if (Math.abs(diff) < 2) { + weight = weight; + lastWeight = weight; + return true; + } else { + // 如果差值超过一定范围,则认为重量是不稳定的,重新开始计算 + weight = 0; + lastWeight = weight; + return false; + } + } \ No newline at end of file diff --git a/script/index.js b/script/index.js index 49323b5..ccab0b4 100644 --- a/script/index.js +++ b/script/index.js @@ -172,6 +172,8 @@ function submitData() { document.querySelector('#feed_' + feedIndex).scrollIntoView(true); allow = feedList[feedIndex].allow; _planSum = feedList[feedIndex].weight.toFixed(0); + // 误差小于100必须校准才能自动跳转 + allowAuto = _planSum >= 100; text = feedName + _planSum; document.getElementById('nowFeed').innerHTML = '' + batchName + ',' + feedName + ''; ledStatus = false;