TMRAppBle/pages/circle.stml

547 lines
13 KiB
Plaintext

<template>
<scroll-view class="main">
<view class="header">
<button type="button" class="header_btn1" @click="handleBackMain">返回</button>
<text class="label_s">{{carName}}</text>
<text class="label_s">{{batch}}{{train}}{{total}}</text>
<text class="label_s" @click="rescan">{{state}}</text>
<button class="header_btnt" @click="bleChange">{bleName}</button>
<!-- <picker class="picker" range={bleArr} range-key="name" mode="selector" onChange={this.bleChange}>
<button class="header_btnt">{bleName}</button>
</picker> -->
</view>
<view class="horizontal">
<!-- <view class="item">
<scroll-view class="section_left" scroll-y>
<view v-for="(item,index) in bleArr" class="left_item" @click="bleConnect(index, item)"
:style="{backgroundColor:index === currCarIndex?'#1482f0':'#ffffff'}">
<text class="label_s"
:style="{color:index === currCarIndex?'#ffffff':'#000000'}">{item.name}</text>
</view>
</scroll-view>
</view> -->
<view class="item2 vertical">
<view class="item">
<scroll-view class="section_right_scroll" scroll-x>
<view v-for="(item,index) in feedArr">
<text class="label_s"
:style="{backgroundColor:index === feedIndex?'purple':'#ffffff'}">{UNICODE_CHARS[index+1]}{item.name}:{item.weight}</text>
</view>
</scroll-view>
</view>
<view class="item2 horizontal">
<view class="item">
<view class="circle">
<text class="label_x">{{name}}</text>
<text class="label_x">{{_feed}}kg</text>
</view>
</view>
<view class="item">
<view class="circle">
<text class="label_x">车内总重</text>
<text class="label_x">{{nowWeight}}kg</text>
</view>
</view>
</view>
<view class="item horizontal">
<view class="item2" v-show="showItems">
<text class="label">{{batch}}{{train}},{{UNICODE_CHARS[feedIndex+1]}}{{name}}</text>
<text class="label">计划:{{plan}}kg,已装:{{feed}}kg</text>
</view>
<view class="item">
<view v-for="(item,index) in carArr">
<button class="car_btnt" :style="{backgroundColor:item.sbId === sbId?'#407ae6':'#ffffff'}" @click="carChange(item)">{{item.name}}</button>
</view>
</view>
<view class="item">
<button class="next_btn_text" @click="nextFeed">下一步</button>
<!-- <text class="next_btn_text" @click="nextFeed">下一步</text> -->
</view>
</view>
</view>
<!-- <view class="item">
<view class="circle">
<text class="text">车内总重</text>
<text class="text">{{nowWeight}}kg</text>
</view>
</view> -->
</view>
</scroll-view>
</template>
<script>
import { POST, GET } from '../script/req.js'
import { bleUtils } from '../script/module/ble.js'
import { Util } from '../script/util.js'
export default {
name: 'circle',
apiready() {//like created
},
data() {
return {
_feed: '0',
showItems:false,
total: '',
bleName: '请选择蓝牙',
state: '未连接',
currCarIndex: 0,
sourceCow: '',
feedArr: [],
dateOf: '',
classCode: '',
sbId: '',
carName:'',
batch: '',
train: '',
carArr: [],
bleArr: [],
ajax: true,
beforeWeight: 0,
nowWeight: '0',
feedIndex: 0,
name: '暂无',
plan: 0,
feed: 0,
websInt: null,
peripheralUUID: null,
serviceUUID: null,
characteristicUUID: null,
UNICODE_CHARS: {
1: "①",
2: "②",
3: "③",
4: "④",
5: "⑤",
6: "⑥",
7: "⑦",
8: "⑧",
9: "⑨",
10: "⑩",
11: "⑪",
12: "⑫",
13: "⑬",
14: "⑭",
15: "⑮",
16: "⑯",
17: "⑰",
18: "⑱",
19: "⑲",
20: "⑳",
21: "㉑",
22: "㉒",
23: "㉓",
24: "㉔",
25: "㉕",
26: "㉖",
27: "㉗",
28: "㉘",
29: "㉙",
30: "㉚"
},
}
},
methods: {
handleBackMain() {
api.closeWin();
},
rescan(){
const that = this;
api.confirm({
title: '确认重新扫描?',
msg: '提示信息',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if(index == 1){
that.initBle()
}
});
},
apiready() {
const that = this;
that.getSbList()
bleUtils.initBle()
var uuid = api.getPrefs({ sync: true, key: 'uuid' })
console.log(uuid)
if (uuid) {
bleUtils.isConnected({ peripheralUUID: uuid }, function (ret) {
console.log('isconnected:' + ret)
if (ret) {
bleUtils.disconnect({ peripheralUUID: uuid }, function (ret, err) {
})
}
that.initBle()
})
} else {
that.initBle()
}
},
/**
* 查询设备列表
*/
getSbList() {
let that = this;
GET('sbList?sourceCow=' + api.getPrefs({ sync: true, key: 'sourceCow' })).then(ret => {
that.data.sbId = ret.data[0].sbId;
api.setGlobalData({ key: 'sbId', value: that.data.sbId });
that.data.carName = ret.data[0].name;
that.data.carArr = ret.data;
api.setPrefs({
key: 'carArr',
value: ret.data
});
}).catch(err => {
api.toast({
msg: JSON.stringify(err)
})
})
},
nextFeed() {
api.showProgress({
title: "切换中,请稍后...",
modal: true
});
setTimeout(() => {
api.hideProgress();
}, 500);
const that = this;
var sbIdHex = that.decimalToHex(that.data.sbId).replace(' ', '');
sbIdHex = sbIdHex.length < 4 ? '00' + sbIdHex : sbIdHex;
var msg = 'FF0A0002' + sbIdHex
let param = {
peripheralUUID: this.data.peripheralUUID,
serviceUUID: this.data.serviceUUID,
characteristicUUID: this.data.characteristicUUID,
value: msg,
writeType: 'withoutResponse'
};
bleUtils.writeValueForCharacteristic(param, function (ret) {
})
},
decimalToHex(decimal) {
let hex = decimal.toString(16);
if (hex.length % 2 !== 0) {
hex = "0" + hex;
}
if (hex.length > 2) {
var str = '';
for (var i = 0; i < hex.length; i += 2) {
str = str + hex.substr(i, 2) + ' '
}
return str.trim()
}
return hex;
},
carChange(item) {
this.data.sbId = item.sbId;
this.data.carName = item.name;
this.resetData()
},
resetData(){
this.data.feedArr = []
this.data.batch = ''
this.data.train = ''
this.data.total = ''
this.data.name = '暂无'
this.data._feed = 0
this.data.total = 0
this.data.showItems = false
},
//选择蓝牙
bleChange(e) {
let that = this;
var bts = [];
that.data.bleArr.forEach(e=>{
bts.push(e.name)
})
api.actionSheet({
title: '已扫描到的蓝牙',
buttons: bts
}, function(ret, err) {
var index = ret.buttonIndex;
if(index>that.data.bleArr.length){
return false
}
that.data.bleName = that.data.bleArr[index-1].name;
that.resetData()
that.bleConnect(that.data.bleArr[index-1])
});
},
initBle() {
const that = this;
bleUtils.initManager({ single: true }, function (ret) {
api.showProgress({
title: "蓝牙扫描中,请稍后...",
modal: true
});
bleUtils.scan({ clean: true }, function (ret) {
setTimeout(() => {
api.hideProgress()
bleUtils.stopScan();
bleUtils.getPeripheral(function (res) {
that.data.bleArr = res
var uuid = api.getPrefs({ sync: true, key: 'uuid' })
res.forEach(r=>{
if(r.id == uuid){
console.log(uuid)
that.bleConnect(r)
}
})
})
}, 3000);
})
})
},
bleConnect(item) {
const that = this;
api.showProgress({
title: "蓝牙连接中,请稍后...",
modal: true
});
bleUtils.connect({ peripheralUUID: item.id }, function (ret) {
api.hideProgress()
if (ret.status) {
api.setPrefs({ key: 'uuid', value: item.id });
console.log(item.name + '连接成功')
that.data.state = item.name + '已连接'
//设置MTU包大小
bleUtils.setMtu({
peripheralUUID: item.id,
mtu:150
},function(ret){
if(ret.status){
bleUtils.discoverService({ peripheralUUID: item.id }, function (res) {
that.data.peripheralUUID = res.peripheralUUID;
that.data.serviceUUID = res.serviceUUID;
that.data.characteristicUUID = res.characteristicUUID;
that.setNotify(res)
})
}else{
api.toast({
msg: JSON.stringify(ret.code)
})
}
});
}
})
},
setNotify(param) {
console.log('setNotify')
const that = this;
// 监听消息
bleUtils.setNotify(param, function (res) {
if (res !== undefined && res != '' && res.startsWith('ff0a')) {
var sbId = parseInt(res.substr(16, 4), 16)
if(sbId != that.data.sbId){
return false
}
that.data.showItems = true;
that.data.nowWeight = parseInt(res.substr(8, 4), 16)
that.data.beforeWeight = parseInt(res.substr(12, 4), 16)
that.data.feed = that.data.nowWeight - that.data.beforeWeight
var _feed = that.data.plan - that.data.feed;
that.data._feed = _feed > 0 ? '少' + Math.abs(_feed) : '多' + Math.abs(_feed);
// that.data.sbId = parseInt(res.substr(16, 4), 16)
var classes = parseInt(res.substr(20, 2), 16)
var train = parseInt(res.substr(22, 2), 16)
var feedIndex = parseInt(res.substr(24, 2), 16)
if (that.data.classCode == classes && that.data.trainNumber == train && that.data.feedIndex == feedIndex && sbId == that.data.sbId) {
return false
}
that.data.feedIndex = feedIndex
if (that.data.feedArr.length > 0) {
that.data.name = that.data.feedArr[feedIndex].name
that.data.plan = that.data.feedArr[feedIndex].weight.toFixed(0)
}
if ((that.data.feedArr.length === 0 || that.data.classCode != classes || that.data.train != train) && that.data.ajax) {
that.data.ajax = false;
var param = {
classCode: classes,
sbId: that.data.sbId,
trainNumber: train,
sourceCow: api.getPrefs({ sync: true, key: 'sourceCow' }),
dateOf: Util.getDateStr(new Date())
}
that.initOne(param)
}
that.data.classCode = classes
that.data.train = train
switch (that.data.classCode) {
case 1:
that.data.batch = '早班'
break;
case 2:
that.data.batch = '中班'
break;
case 3:
that.data.batch = '晚班'
break;
}
}
})
},
/**
* 初始化当前班次每一车
*/
initOne(param) {
let that = this;
that.data.feedArr = []
POST('planDay', param, {}).then(ret => {
if (ret.data.length == 0) {
api.toast({
msg: '该车次暂无计划信息',
location: 'middle'
})
}
ret.data.forEach(e => {
let json = [...e.feedJson, ...e.cowshedJson]
that.data.feedArr = json;
that.data.total = '(' + e.total + 'kg)'
})
that.data.ajax = true
}).catch(err => {
api.toast({
msg: JSON.stringify(err)
})
})
},
StringToNum(hexx) {
var hex = hexx.toString(); //force conversion
var str = '';
var num = '';
for (var i = 0; i < hex.length; i += 2) {
num = String.fromCharCode(parseInt(hex.substr(i, 2), 16));
if ((num >= '0' && num <= '9') || (num === '-')) {
str += num;
}
else {
if (str.length !== 0) {
return str
}
}
}
return str;
},
closeApp() {
api.closeWidget({
id: api.appId, //应用ID
retData: { name: 'closeWidget' },
silent: true
});
}
}
}
</script>
<style>
.main {
width: 100%;
height: 100%;
/* background-color: #407ae6; */
background-color: whitesmoke;
}
.header {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.header_btnt {
background-color: #407ae6;
width: 200px;
color: #ffffff;
font-size: 20px;
height: 60px;
font-weight: 900;
border-radius: 10px;
}
.car_btnt {
margin: 5px;
/* background-color: #407ae6; */
width: 100%;
color: #000000;
font-size: 20px;
height: 50px;
font-weight: 900;
border-radius: 10px;
}
.header_btn1 {
background-color: #e60d0d;
width: 200px;
color: #ffffff;
font-size: 20px;
height: 60px;
font-weight: 900;
border-radius: 10px;
}
.section_right_scroll {
width: 100%;
}
.horizontal {
flex-direction: row;
}
.vertical {
flex-direction: column;
}
.item {
flex: 1;
justify-content: center;
align-items: center;
box-sizing: border-box;
border: 1px solid #a3caef;
}
.item2 {
flex: 2;
box-sizing: border-box;
border: 1px solid #a3caef;
}
.page {
height: 100%;
background-color: gray;
}
.circle {
margin-top: 20px;
width: 450px;
height: 450px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
box-sizing: border-box;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
border-radius: 50%;
background-color: white;
}
.next_btn_text {
margin-top: 40px;
color: #ffffff;
background-color: #407ae6;
font-size: 70px;
font-weight: 900;
border-radius: 20%;
}
.label_s {
color: #407ae6;
font-size: 30px;
font-weight: 900;
border-radius: 10px;
}
.label_x {
font-size: 100px;
font-weight: 900;
border-radius: 10px;
}
.label {
font-size: 60px;
font-weight: 900;
border-radius: 10px;
}
</style>