123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- var util = require("../../utils/util.js");
- var udp;
- var IPAddress = "118.190.37.99";
- var Port = 30;
- const app = getApp()
- Page({
- data: {
- temperature: "00",
- humidity: "00",
- SwitchOn: "/images/switch_button_on.png",
- SwitchOff: "/images/switch_button_off.png",
- SwitchOnCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"1\"}",
- SwitchOffCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"0\"}",
- SwitchQueryCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"-1\"}",
- SwitchTag: false,
- },
- onShow: function () {
- let _this = this;
- console.log("ControlonShow");
-
- this.setData
- ({
- temperaturetext: this.data.temperature,
- humiditytext: this.data.humidity,
- SwitchBackgroundImage: this.data.SwitchOff,
- })
- udp = wx.createUDPSocket()
- udp.bind()
-
- udp.onMessage(function (res) {
- console.log(str)
- let str = util.newAb2Str(res.message);
- console.log('str===' + str)
- if (str != null) {
- let json = JSON.parse(str);
- if (json != null) {
- if (json.data == "TH")
- {
- _this.data.temperature = json.temperature;
- _this.data.humidity = json.humidity;
- if (_this.data.temperature != null && _this.data.humidity != null) {
- _this.setData
- ({
- temperaturetext: _this.data.temperature,
- humiditytext: _this.data.humidity,
- })
- }
- }
- else if (json.data == "switch")
- {
- if (json.status == "1")
- {
- _this.data.DeviceStatusValue = "在线(继电器吸合)"
- _this.setData
- ({
- SwitchBackgroundImage: _this.data.SwitchOn,
- })
- _this.data.SwitchTag = true;
- }
- else if (json.status == "0")
- {
- _this.data.DeviceStatusValue = "在线(继电器断开)"
- _this.setData
- ({
- SwitchBackgroundImage: _this.data.SwitchOff,
- })
- _this.data.SwitchTag = false;
- }
- }
- }
- }
- });
- },
-
- Switch: function () {
- if (this.data.SwitchTag == true)
- {
- this.setData
- ({
- SwitchBackgroundImage: this.data.SwitchOff
- })
- this.data.SwitchTag = false;
- udp.send
- ({
- address: IPAddress,
- port: Port,
- message: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"0\"}"
- });
-
- }
- else
- {
- this.setData
- ({
- SwitchBackgroundImage: this.data.SwitchOn
- })
- this.data.SwitchTag = true;
- udp.send
- ({
- address: IPAddress,
- port: Port,
- message: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"1\"}"
- });
-
- }
- },
- })
|