control.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const {postData} = require('../api/intell')
  2. const {putDevice,loopSwitch,loopSwitchstate} = require('../api/device')
  3. function getlooplist(){
  4. var group = wx.getStorageSync('group')
  5. //获取分组下拉
  6. var groups = []//分组列表
  7. var device = []//设备列表
  8. for(let i = 0;i < group.length;i++){
  9. groups.push(group[i].groupName)
  10. }
  11. //改变分组下标
  12. if(wx.getStorageSync('groupName') != ''){
  13. for(let n = 0;n < groups.length;n++){
  14. if(groups[n] == wx.getStorageSync('groupName')){
  15. var groupsIndex = n//分组下标
  16. }
  17. }
  18. }
  19. for(let j = 0;j < group.length;j++){
  20. if(groups[groupsIndex] == group[j].groupName){
  21. if(group[j].devices != null && group[j].devices.length != 0){
  22. for(let p = 0;p < group[j].devices.length;p++){
  23. device.push(group[j].devices[p].deviceName)
  24. }
  25. if(wx.getStorageSync('deviceName') != ''){
  26. for(let m = 0;m < device.length;m++){
  27. if(device[m] == wx.getStorageSync('deviceName')){
  28. var deviceIndex = m//设备下标
  29. }
  30. }
  31. }
  32. //根据分组名和设备名获取key和id以及回路名称
  33. for(let d = 0;d < group.length;d++){
  34. if(groups[groupsIndex] == group[d].groupName){
  35. for(let g = 0;g < group[d].devices.length;g++){
  36. if(device[deviceIndex] == group[d].devices[g].deviceName){
  37. var loopnum = group[d].devices[g].circuits
  38. var deviceId = group[d].devices[g].deviceId
  39. var deviceKey = group[d].devices[g].deviceKey
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return [groups,groupsIndex,device,deviceIndex,loopnum,deviceId,deviceKey]
  48. }
  49. //回路开/关(旧设备)
  50. function Switch(deviceId,deviceKey,Rel){
  51. postData(deviceId,deviceKey,Rel).then(res => {
  52. if(res.data.errno == 10){
  53. wx.showModal({
  54. title:'错误',
  55. content:'设备处于离线状态',
  56. showCancel:false
  57. })
  58. }
  59. })
  60. }
  61. //回路开/关读取(旧设备)
  62. function switchReal(police,lamp,index){
  63. switch(police.length){
  64. case 1:
  65. police = '000' + police
  66. break;
  67. case 2:
  68. police = '00' + police
  69. break;
  70. case 3:
  71. police = '0' + police
  72. break;
  73. default:
  74. police = police
  75. break;
  76. }
  77. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  78. lamp[index] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  79. }
  80. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  81. lamp[index] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  82. }
  83. return lamp
  84. }
  85. //回路开/关(新设备)
  86. function newSwitch(swit){
  87. loopSwitch(swit).then(res =>{
  88. if(res.data.code != 0){
  89. wx.showModal({
  90. title:'错误',
  91. content:res.data.msg,
  92. showCancel:false
  93. })
  94. }
  95. })
  96. }
  97. //回路开/关读取(新设备)
  98. function newswitchReal(Device){
  99. loopSwitchstate(Device).then(res=>{
  100. if(res.data.code == 0){
  101. var relay = res.data.data
  102. if(relay[0].status == 1){
  103. let ever = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  104. console.log(ever)
  105. return ever
  106. }
  107. else{
  108. let ever = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  109. console.log(ever)
  110. return ever
  111. }
  112. }
  113. else{
  114. wx.showModal({
  115. title:'错误',
  116. content:res.data.msg,
  117. showCancel:false
  118. })
  119. }
  120. })
  121. }
  122. //分组下拉和设备下拉的获取(主页界面)
  123. function droplist(){
  124. var group = wx.getStorageSync('group')
  125. //获取分组下拉
  126. var groups = []
  127. for(let i = 0;i < group.length;i++){
  128. groups.push(group[i].groupName)
  129. }
  130. //改变分组下标
  131. if(wx.getStorageSync('groupName') != ''){
  132. let GroupName = that.data.groupName
  133. for(let n = 0;n < GroupName.length;n++){
  134. if(GroupName[n] == wx.getStorageSync('groupName')){
  135. var groupIndex = n
  136. }
  137. }
  138. }
  139. else{wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])}
  140. //获取设备下拉
  141. var device = []
  142. var groupName = that.data.groupName
  143. var groupIndex = that.data.groupIndex
  144. for(let j = 0;j < group.length;j++){
  145. if(groupName[groupIndex] == group[j].groupName){
  146. if(group[j].devices != null && group[j].devices.length != 0){
  147. for(let p = 0;p < group[j].devices.length;p++){
  148. device.push(group[j].devices[p].deviceName)
  149. }
  150. that.setData({deviceName:device})
  151. //改变设备下标
  152. if(wx.getStorageSync('deviceName') != ''){
  153. var DeviceName = that.data.deviceName
  154. for(let m = 0;m < DeviceName.length;m++){
  155. if(DeviceName[m] == wx.getStorageSync('deviceName')){
  156. that.setData({
  157. deviceIndex:m
  158. })
  159. }
  160. }
  161. }
  162. else{
  163. wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex])
  164. }
  165. }
  166. }
  167. }
  168. }
  169. //刷新主页界面的显示
  170. function Refresh(){
  171. //重置状态
  172. let loopName = []//回路名称
  173. let loopIcon = []//回路图标
  174. let switchState = []//开关状态
  175. let size = []//字体大小
  176. let fontColor = []//字体颜色
  177. for(let p = 0;p < 12;p++){
  178. loopName.push('回路' + (p+1))
  179. loopIcon.push('')
  180. switchState.push('https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png')
  181. size.push('calc(100vw * 16/375)')
  182. fontColor.push('#d5dbdf')
  183. }
  184. let combination = [loopName,loopIcon,switchState,size,fontColor]
  185. return combination
  186. }
  187. module.exports = {
  188. getlooplist:getlooplist,
  189. Switch:Switch,
  190. switchReal:switchReal,
  191. Refresh:Refresh,
  192. newSwitch:newSwitch,
  193. newswitchReal:newswitchReal,
  194. }