master
parent
1884eb0f9d
commit
4f8a72936b
|
|
@ -5,25 +5,25 @@
|
|||
<uni-grid :column="4" border-color="#03a9f4">
|
||||
<uni-grid-item :index="0">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ temp1 }} </text>
|
||||
<text class="text">{{ data.temp1 }} </text>
|
||||
<text class="text"> 温度1 ℃</text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="1">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ temp2 }}</text>
|
||||
<text class="text">{{ data.temp2 }}</text>
|
||||
<text class="text"> 温度2 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="2">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ temp3 }}</text>
|
||||
<text class="text">{{ data.temp3 }}</text>
|
||||
<text class="text"> 温度3 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="3">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ temp4 }}</text>
|
||||
<text class="text">{{ data.temp4 }}</text>
|
||||
<text class="text"> 温度4 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
|
|
@ -31,25 +31,25 @@
|
|||
<uni-grid :column="4" border-color="#03a9f4">
|
||||
<uni-grid-item :index="0">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ humi1 }}</text>
|
||||
<text class="text">{{ data.humi1 }}</text>
|
||||
<text class="text"> 湿度1 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="1">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ humi2 }}</text>
|
||||
<text class="text">{{ data.humi2 }}</text>
|
||||
<text class="text"> 湿度2 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="2">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ humi3 }}</text>
|
||||
<text class="text">{{ data.humi3 }}</text>
|
||||
<text class="text"> 湿度3 </text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
<uni-grid-item :index="3">
|
||||
<view class="grid-item-box">
|
||||
<text class="text">{{ humi4 }}</text>
|
||||
<text class="text">{{ data.humi4 }}</text>
|
||||
<text class="text"> 湿度4 %RH</text>
|
||||
</view>
|
||||
</uni-grid-item>
|
||||
|
|
@ -68,14 +68,16 @@ export default {
|
|||
data() {
|
||||
return {
|
||||
connected: false,
|
||||
temp1: null,
|
||||
humi1: null,
|
||||
temp2: null,
|
||||
humi2: null,
|
||||
temp3: null,
|
||||
humi3: null,
|
||||
temp4: null,
|
||||
humi4: null,
|
||||
data: {
|
||||
temp1: null,
|
||||
humi1: null,
|
||||
temp2: null,
|
||||
humi2: null,
|
||||
temp3: null,
|
||||
humi3: null,
|
||||
temp4: null,
|
||||
humi4: null,
|
||||
},
|
||||
lastTopic: "设备" + '',
|
||||
lastRaw: ''
|
||||
}
|
||||
|
|
@ -98,22 +100,33 @@ export default {
|
|||
})
|
||||
|
||||
client.on('message', (topic, payload) => {
|
||||
this.lastTopic = "设备【" + topic+"】"
|
||||
const s = payload.toString()
|
||||
this.lastRaw = s
|
||||
|
||||
// 只处理JSON(混二进制就直接丢)
|
||||
if (!(s.startsWith('{') && s.endsWith('}'))) return
|
||||
let obj
|
||||
try { obj = JSON.parse(s) } catch(e) { return }
|
||||
try {
|
||||
obj = JSON.parse(s)
|
||||
const allKeysNumeric = Object.keys(obj).every(key => /^\d+$/.test(key));
|
||||
if (Object.keys(obj).length <= 0 || !allKeysNumeric) {
|
||||
return;
|
||||
}
|
||||
} catch(e) { return }
|
||||
|
||||
// 你的键:温度101~104、湿度201~204(按你之前约定温度/10、湿度/10)
|
||||
const div10 = (v) => (v == null ? null : Math.round((Number(v)/10)*10)/10)
|
||||
|
||||
this.temp1 = div10(obj["201"]); this.humi1 = div10(obj["101"])
|
||||
this.temp2 = div10(obj["202"]); this.humi2 = div10(obj["102"])
|
||||
this.temp3 = div10(obj["203"]); this.humi3 = div10(obj["103"])
|
||||
this.temp4 = div10(obj["204"]); this.humi4 = div10(obj["104"])
|
||||
this.lastTopic = "设备【" + topic+"】"
|
||||
this.data = {
|
||||
temp1: div10(obj["201"]),
|
||||
humi1: div10(obj["101"]),
|
||||
temp2: div10(obj["202"]),
|
||||
humi2: div10(obj["102"]),
|
||||
temp3: div10(obj["203"]),
|
||||
humi3: div10(obj["103"]),
|
||||
temp4: div10(obj["204"]),
|
||||
humi4: div10(obj["104"])
|
||||
}
|
||||
})
|
||||
|
||||
client.on('close', () => { this.connected = false })
|
||||
|
|
|
|||
Loading…
Reference in New Issue