index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //index.js
  2. //获取应用实例
  3. var util = require("../../utils/util.js");
  4. var udp;
  5. var IPAddress = "118.190.37.99";
  6. //var IPAddress = "192.168.0.104";
  7. var Port = 30;
  8. const app = getApp()
  9. Page({
  10. data: {
  11. temperature: "00",//温度
  12. humidity: "00", //湿度
  13. SwitchOn: "/images/switch_button_on.png", //开关ON图片
  14. SwitchOff: "/images/switch_button_off.png",//开关OFF图片
  15. SwitchOnCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"1\"}", //控制继电器吸合
  16. SwitchOffCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"0\"}", //控制继电器断开
  17. SwitchQueryCmd: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"-1\"}",//查询继电器状态
  18. SwitchTag: false,
  19. },
  20. onShow: function () {
  21. let _this = this;
  22. console.log("ControlonShow");
  23. //初始化显示温湿度:00,按钮状态:关,设备状态:离线
  24. this.setData
  25. ({
  26. temperaturetext: this.data.temperature,
  27. humiditytext: this.data.humidity,
  28. SwitchBackgroundImage: this.data.SwitchOff,
  29. })
  30. udp = wx.createUDPSocket()//启用UDP
  31. udp.bind()
  32. //UDP接收到消息
  33. udp.onMessage(function (res) {
  34. console.log(str)
  35. let str = util.newAb2Str(res.message);//接收消息
  36. console.log('str===' + str)
  37. if (str != null) {
  38. let json = JSON.parse(str);//解析JSON数据
  39. if (json != null) {
  40. if (json.data == "TH")//是温湿度数据
  41. {
  42. _this.data.temperature = json.temperature;
  43. _this.data.humidity = json.humidity;
  44. if (_this.data.temperature != null && _this.data.humidity != null) {
  45. _this.setData
  46. ({
  47. temperaturetext: _this.data.temperature,
  48. humiditytext: _this.data.humidity,
  49. })
  50. }
  51. }
  52. else if (json.data == "switch")//接收的是开关数据
  53. {
  54. if (json.status == "1")//开关接通
  55. {
  56. _this.data.DeviceStatusValue = "在线(继电器吸合)"
  57. _this.setData
  58. ({
  59. SwitchBackgroundImage: _this.data.SwitchOn,
  60. })
  61. _this.data.SwitchTag = true;
  62. }
  63. else if (json.status == "0")//开关断开
  64. {
  65. _this.data.DeviceStatusValue = "在线(继电器断开)"
  66. _this.setData
  67. ({
  68. SwitchBackgroundImage: _this.data.SwitchOff,
  69. })
  70. _this.data.SwitchTag = false;
  71. }
  72. }
  73. }
  74. }
  75. });
  76. },
  77. /**
  78. * 页面控件--控制开关按钮
  79. */
  80. Switch: function () {
  81. if (this.data.SwitchTag == true) //开关状态是开
  82. {
  83. this.setData
  84. ({
  85. SwitchBackgroundImage: this.data.SwitchOff //设置显示开关OFF图片
  86. })
  87. this.data.SwitchTag = false;//记录图片的状态
  88. udp.send
  89. ({
  90. address: IPAddress,
  91. port: Port,
  92. message: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"0\"}"
  93. });
  94. //发送关指令
  95. }
  96. else //开关状态是关
  97. {
  98. this.setData
  99. ({
  100. SwitchBackgroundImage: this.data.SwitchOn //设置显示开关ON图片
  101. })
  102. this.data.SwitchTag = true;//记录图片的状态
  103. udp.send
  104. ({
  105. address: IPAddress,
  106. port: Port,
  107. message: "{\"data\":\"switch\",\"bit\":\"1\",\"status\":\"1\"}"
  108. });
  109. //发送开指令
  110. }
  111. },
  112. })