control.js 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  1. // pages/control/control.js
  2. const {postGroup,getGroup,putGroup,delGroup} = require('../../api/group')
  3. const {postDevice,getDevice,putDevice,delDevice} = require('../../api/device')
  4. const {getBasic,getData,postData} = require('../../api/intell')
  5. const control = require('../../utils/control')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. statu:false,
  12. status:"离线",
  13. Title:"",
  14. quantity:"",//回路数量
  15. Color:['#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf','#d5dbdf',
  16. '#d5dbdf','#d5dbdf'],//字体颜色
  17. Size:["calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)",
  18. "calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)",
  19. "calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)","calc(100vw * 16/375)"],//字体大小
  20. lamp:['https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  21. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  22. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  23. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  24. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  25. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  26. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  27. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  28. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  29. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  30. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png',
  31. 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'],//灯泡图标
  32. address:["","","","","","","","","","","",""],//修改回路名称图标
  33. loop:['回路1','回路2','回路3','回路4','回路5','回路6',
  34. '回路7','回路8','回路9','回路10','回路11','回路12',],//回路名称
  35. loops:0,//用于记录回路数量
  36. Loop:[],//用于向回路界面传递回路名称
  37. Modecontrol:"",
  38. groupName:[],
  39. deviceName:[],
  40. groupIndex:0,
  41. deviceIndex:0,
  42. deviceId:'',
  43. deviceKey:'',
  44. },
  45. //回路开
  46. Open(e){
  47. var that = this
  48. if(e.target.dataset.open == 1){
  49. control.Switch(that.data.deviceId,that.data.deviceKey,{ 'R1_RelayStatus':13,'Reflash':3})
  50. }
  51. else if (e.target.dataset.open == 2 && that.data.loops == "2") {
  52. control.Switch(that.data.deviceId,that.data.deviceKey,{ 'R2_RelayStatus':13,'Reflash':3})
  53. }
  54. else if (e.target.dataset.open == 3 && that.data.loops == "3") {
  55. control.Switch(that.data.deviceId,that.data.deviceKey,{ 'R3_RelayStatus':13,'Reflash':3})
  56. }
  57. else if (e.target.dataset.open == 4 && that.data.loops == "4") {
  58. control.Switch(that.data.deviceId,that.data.deviceKey,{'R4_RelayStatus':13,'Reflash':3})
  59. }
  60. else if (e.target.dataset.open == 5 && that.data.loops == "5") {
  61. control.Switch(that.data.deviceId,that.data.deviceKey,{'R5_RelayStatus':13,'Reflash':3})
  62. }
  63. else if (e.target.dataset.open == 6 && that.data.loops == "6") {
  64. control.Switch(that.data.deviceId,that.data.deviceKey,{'R6_RelayStatus':13,'Reflash':3})
  65. }
  66. else if (e.target.dataset.open == 7 && that.data.loops == "7") {
  67. control.Switch(that.data.deviceId,that.data.deviceKey,{'R7_RelayStatus':13,'Reflash':3})
  68. }
  69. else if (e.target.dataset.open == 8 && that.data.loops == "8") {
  70. control.Switch(that.data.deviceId,that.data.deviceKey,{'R8_RelayStatus':13,'Reflash':3})
  71. }
  72. else if (e.target.dataset.open == 9 && that.data.loops == "9") {
  73. control.Switch(that.data.deviceId,that.data.deviceKey,{'R9_RelayStatus':13,'Reflash':3})
  74. }
  75. else if (e.target.dataset.open == 10 && that.data.loops == "10") {
  76. control.Switch(that.data.deviceId,that.data.deviceKey,{'R10_RelayStatus':13,'Reflash':3})
  77. }
  78. else if (e.target.dataset.open == 11 && that.data.loops == "11") {
  79. control.Switch(that.data.deviceId,that.data.deviceKey,{'R11_RelayStatus':13,'Reflash':3})
  80. }
  81. else if (e.target.dataset.open == 12 && that.data.loops == "12") {
  82. control.Switch(that.data.deviceId,that.data.deviceKey,{'R12_RelayStatus':13,'Reflash':3})
  83. }
  84. var lamp = wx.getStorageSync('lamp')
  85. setTimeout(function(){
  86. getData(that.data.deviceId,that.data.deviceKey).then(bind => {
  87. let Bind = bind.data.data
  88. if(bind.data.errno == 0){
  89. for(let r1 = 0;r1 < Bind.length;r1++){
  90. if(Bind[r1].id == 'R1_RelayStatus'){
  91. var switchReal = control.switchReal(Bind[r1].current_value,lamp,0)
  92. }
  93. else if(Bind[r1].id == 'R2_RelayStatus'){
  94. var switchReal = control.switchReal(Bind[r1].current_value,lamp,1)
  95. }
  96. else if(Bind[r1].id == 'R3_RelayStatus'){
  97. var switchReal = control.switchReal(Bind[r1].current_value,lamp,2)
  98. }
  99. else if(Bind[r1].id == 'R4_RelayStatus'){
  100. var switchReal = control.switchReal(Bind[r1].current_value,lamp,3)
  101. }
  102. else if(Bind[r1].id == 'R5_RelayStatus'){
  103. var switchReal = control.switchReal(Bind[r1].current_value,lamp,4)
  104. }
  105. else if(Bind[r1].id == 'R6_RelayStatus'){
  106. var switchReal = control.switchReal(Bind[r1].current_value,lamp,5)
  107. }
  108. else if(Bind[r1].id == 'R7_RelayStatus'){
  109. var switchReal = control.switchReal(Bind[r1].current_value,lamp,6)
  110. }
  111. else if(Bind[r1].id == 'R8_RelayStatus'){
  112. var switchReal = control.switchReal(Bind[r1].current_value,lamp,7)
  113. }
  114. else if(Bind[r1].id == 'R9_RelayStatus'){
  115. var switchReal = control.switchReal(Bind[r1].current_value,lamp,8)
  116. }
  117. else if(Bind[r1].id == 'R10_RelayStatus'){
  118. var switchReal = control.switchReal(Bind[r1].current_value,lamp,9)
  119. }
  120. else if(Bind[r1].id == 'R11_RelayStatus'){
  121. var switchReal = control.switchReal(Bind[r1].current_value,lamp,10)
  122. }
  123. else if(Bind[r1].id == 'R12_RelayStatus'){
  124. var switchReal = control.switchReal(Bind[r1].current_value,lamp,11)
  125. }
  126. }
  127. wx.setStorageSync('lamp',switchReal)
  128. that.setData({lamp:wx.getStorageSync('lamp')})
  129. }
  130. })
  131. },2000)
  132. },
  133. //回路关
  134. Shut(e){
  135. var that = this
  136. var lamp = wx.getStorageSync('lamp')
  137. if(e.target.dataset.shut == 1){
  138. control.Switch(that.data.deviceId,that.data.deviceKey,{'R1_RelayStatus':12,'Reflash':3})
  139. }
  140. else if (e.target.dataset.shut == 2 && that.data.loops == "2") {
  141. control.Switch(that.data.deviceId,that.data.deviceKey,{'R2_RelayStatus':12,'Reflash':3})
  142. }
  143. else if (e.target.dataset.shut == 3 && that.data.loops == "3") {
  144. control.Switch(that.data.deviceId,that.data.deviceKey,{'R3_RelayStatus':12,'Reflash':3})
  145. }
  146. else if (e.target.dataset.shut == 4 && that.data.loops == "4") {
  147. control.Switch(that.data.deviceId,that.data.deviceKey,{'R4_RelayStatus':12,'Reflash':3})
  148. }
  149. else if (e.target.dataset.shut == 5 && that.data.loops == "5") {
  150. control.Switch(that.data.deviceId,that.data.deviceKey,{'R5_RelayStatus':12,'Reflash':3})
  151. }
  152. else if (e.target.dataset.shut == 6 && that.data.loops == "6") {
  153. control.Switch(that.data.deviceId,that.data.deviceKey,{'R6_RelayStatus':12,'Reflash':3})
  154. }
  155. else if (e.target.dataset.shut == 7 && that.data.loops == "7") {
  156. control.Switch(that.data.deviceId,that.data.deviceKey,{'R7_RelayStatus':12,'Reflash':3})
  157. }
  158. else if (e.target.dataset.shut == 8 && that.data.loops == "8") {
  159. control.Switch(that.data.deviceId,that.data.deviceKey,{'R8_RelayStatus':12,'Reflash':3})
  160. }
  161. else if (e.target.dataset.shut == 9 && that.data.loops == "9") {
  162. control.Switch(that.data.deviceId,that.data.deviceKey,{'R9_RelayStatus':12,'Reflash':3})
  163. }
  164. else if (e.target.dataset.shut == 10 && that.data.loops == "10") {
  165. control.Switch(that.data.deviceId,that.data.deviceKey,{'R10_RelayStatus':12,'Reflash':3})
  166. }
  167. else if (e.target.dataset.shut == 11 && that.data.loops == "11") {
  168. control.Switch(that.data.deviceId,that.data.deviceKey,{'R11_RelayStatus':12,'Reflash':3})
  169. }
  170. else if (e.target.dataset.shut == 12 && that.data.loops == "12") {
  171. control.Switch(that.data.deviceId,that.data.deviceKey,{'R12_RelayStatus':12,'Reflash':3})
  172. }
  173. var lamp = wx.getStorageSync('lamp')
  174. setTimeout(function(){
  175. getData(that.data.deviceId,that.data.deviceKey).then(bind => {
  176. let Bind = bind.data.data
  177. if(bind.data.errno == 0){
  178. for(let r1 = 0;r1 < Bind.length;r1++){
  179. if(Bind[r1].id == 'R1_RelayStatus'){
  180. var switchReal = control.switchReal(Bind[r1].current_value,lamp,0)
  181. }
  182. else if(Bind[r1].id == 'R2_RelayStatus'){
  183. var switchReal = control.switchReal(Bind[r1].current_value,lamp,1)
  184. }
  185. else if(Bind[r1].id == 'R3_RelayStatus'){
  186. var switchReal = control.switchReal(Bind[r1].current_value,lamp,2)
  187. }
  188. else if(Bind[r1].id == 'R4_RelayStatus'){
  189. var switchReal = control.switchReal(Bind[r1].current_value,lamp,3)
  190. }
  191. else if(Bind[r1].id == 'R5_RelayStatus'){
  192. var switchReal = control.switchReal(Bind[r1].current_value,lamp,4)
  193. }
  194. else if(Bind[r1].id == 'R6_RelayStatus'){
  195. var switchReal = control.switchReal(Bind[r1].current_value,lamp,5)
  196. }
  197. else if(Bind[r1].id == 'R7_RelayStatus'){
  198. var switchReal = control.switchReal(Bind[r1].current_value,lamp,6)
  199. }
  200. else if(Bind[r1].id == 'R8_RelayStatus'){
  201. var switchReal = control.switchReal(Bind[r1].current_value,lamp,7)
  202. }
  203. else if(Bind[r1].id == 'R9_RelayStatus'){
  204. var switchReal = control.switchReal(Bind[r1].current_value,lamp,8)
  205. }
  206. else if(Bind[r1].id == 'R10_RelayStatus'){
  207. var switchReal = control.switchReal(Bind[r1].current_value,lamp,9)
  208. }
  209. else if(Bind[r1].id == 'R11_RelayStatus'){
  210. var switchReal = control.switchReal(Bind[r1].current_value,lamp,10)
  211. }
  212. else if(Bind[r1].id == 'R12_RelayStatus'){
  213. var switchReal = control.switchReal(Bind[r1].current_value,lamp,11)
  214. }
  215. }
  216. wx.setStorageSync('lamp',switchReal)
  217. that.setData({lamp:wx.getStorageSync('lamp')})
  218. }
  219. })
  220. },2000)
  221. },
  222. //回路名称调整
  223. loopname(e){
  224. var that = this
  225. var address = that.data.address//回路名称图标
  226. var loop = that.data.loop//回路名称
  227. var slice//截取数组
  228. var loops = that.data.loops//回路数量
  229. var groupName = that.data.groupName[that.data.groupIndex]//分组名称
  230. var deviceName = that.data.deviceName[that.data.deviceIndex]//设备名称
  231. var token = wx.getStorageSync('token')
  232. var group = wx.getStorageSync('group')
  233. for(let i = 0;i < group.length;i++){
  234. if(groupName == group[i].groupName){
  235. for(let j = 0;j < group[i].devices.length;j++){
  236. if(deviceName == group[i].devices[j].deviceName){
  237. var deviceid = group[i].devices[j].id
  238. var groupid = group[i].devices[j].groupId
  239. }
  240. }
  241. }
  242. }
  243. //回路1
  244. if (e.target.dataset.loopname == 1) {
  245. wx.showModal({
  246. title:'修改回路名称',
  247. editable:true,
  248. placeholderText:'请输入名称',
  249. success(res){
  250. if (res.confirm) {
  251. if(res.content == ''){
  252. wx.showModal({
  253. title:'提示',
  254. content:'请输入名称',
  255. showCancel:false
  256. })
  257. }
  258. else if(res.content.length > 10){
  259. wx.showModal({
  260. title:'提示',
  261. content:'回路名称不能大于10个字符',
  262. showCancel:false
  263. })
  264. }
  265. else{
  266. loop[0] = res.content
  267. slice = loop.slice(0,loops)
  268. slice = slice.toString()
  269. wx.request({
  270. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  271. method:'PUT',
  272. header:{'Content-Type': 'application/json','Authorization': token},
  273. data:{
  274. id:deviceid,
  275. groupId:groupid,
  276. circuits:slice
  277. },
  278. success: res =>{
  279. if(res.data.code == 200){
  280. for(let i = 0;i < group.length;i++){
  281. if(groupName == group[i].groupName){
  282. for(let j = 0;j < group[i].devices.length;j++){
  283. if(deviceName == group[i].devices[j].deviceName){
  284. group[i].devices[j].circuits = slice
  285. }
  286. }
  287. }
  288. }
  289. wx.setStorageSync('group', group)
  290. that.setData({loop:loop})
  291. }
  292. }
  293. })
  294. }
  295. }
  296. }
  297. })
  298. }
  299. //回路2
  300. else if(e.target.dataset.loopname == 2 && address[1] != ""){
  301. wx.showModal({
  302. title:'修改回路名称',
  303. editable:true,
  304. placeholderText:'请输入名称',
  305. success(res){
  306. if (res.confirm) {
  307. if(res.content == ''){
  308. wx.showModal({
  309. title:'提示',
  310. content:'请输入名称',
  311. showCancel:false
  312. })
  313. }
  314. else if(res.content.length > 10){
  315. wx.showModal({
  316. title:'提示',
  317. content:'回路名称不能大于10个字符',
  318. showCancel:false
  319. })
  320. }
  321. else{
  322. loop[1] = res.content
  323. slice = loop.slice(0,loops)
  324. slice = slice.toString()
  325. wx.request({
  326. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  327. method:'PUT',
  328. header:{'Content-Type': 'application/json','Authorization': token},
  329. data:{
  330. id:deviceid,
  331. groupId:groupid,
  332. circuits:slice
  333. },
  334. success: res =>{
  335. if(res.data.code == 200){
  336. for(let i = 0;i < group.length;i++){
  337. if(groupName == group[i].groupName){
  338. for(let j = 0;j < group[i].devices.length;j++){
  339. if(deviceName == group[i].devices[j].deviceName){
  340. group[i].devices[j].circuits = slice
  341. }
  342. }
  343. }
  344. }
  345. wx.setStorageSync('group', group)
  346. that.setData({loop:loop})
  347. }
  348. }
  349. })
  350. }
  351. }
  352. }
  353. })
  354. }
  355. //回路3
  356. else if(e.target.dataset.loopname == 3 && address[2] != ""){
  357. wx.showModal({
  358. title:'修改回路名称',
  359. editable:true,
  360. placeholderText:'请输入名称',
  361. success(res){
  362. if (res.confirm) {
  363. if(res.content == ''){
  364. wx.showModal({
  365. title:'提示',
  366. content:'请输入名称',
  367. showCancel:false
  368. })
  369. }
  370. else if(res.content.length > 10){
  371. wx.showModal({
  372. title:'提示',
  373. content:'回路名称不能大于10个字符',
  374. showCancel:false
  375. })
  376. }
  377. else{
  378. loop[2] = res.content
  379. slice = loop.slice(0,loops)
  380. slice = slice.toString()
  381. wx.request({
  382. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  383. method:'PUT',
  384. header:{'Content-Type': 'application/json','Authorization': token},
  385. data:{
  386. id:deviceid,
  387. groupId:groupid,
  388. circuits:slice
  389. },
  390. success: res =>{
  391. if(res.data.code == 200){
  392. for(let i = 0;i < group.length;i++){
  393. if(groupName == group[i].groupName){
  394. for(let j = 0;j < group[i].devices.length;j++){
  395. if(deviceName == group[i].devices[j].deviceName){
  396. group[i].devices[j].circuits = slice
  397. }
  398. }
  399. }
  400. }
  401. wx.setStorageSync('group', group)
  402. that.setData({loop:loop})
  403. }
  404. }
  405. })
  406. }
  407. }
  408. }
  409. })
  410. }
  411. //回路4
  412. else if(e.target.dataset.loopname == 4 && address[3] != ""){
  413. wx.showModal({
  414. title:'修改回路名称',
  415. editable:true,
  416. placeholderText:'请输入名称',
  417. success(res){
  418. if (res.confirm) {
  419. if(res.content == ''){
  420. wx.showModal({
  421. title:'提示',
  422. content:'请输入名称',
  423. showCancel:false
  424. })
  425. }
  426. else if(res.content.length > 10){
  427. wx.showModal({
  428. title:'提示',
  429. content:'回路名称不能大于10个字符',
  430. showCancel:false
  431. })
  432. }
  433. else{
  434. loop[3] = res.content
  435. slice = loop.slice(0,loops)
  436. slice = slice.toString()
  437. wx.request({
  438. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  439. method:'PUT',
  440. header:{'Content-Type': 'application/json','Authorization': token},
  441. data:{
  442. id:deviceid,
  443. groupId:groupid,
  444. circuits:slice
  445. },
  446. success: res =>{
  447. if(res.data.code == 200){
  448. for(let i = 0;i < group.length;i++){
  449. if(groupName == group[i].groupName){
  450. for(let j = 0;j < group[i].devices.length;j++){
  451. if(deviceName == group[i].devices[j].deviceName){
  452. group[i].devices[j].circuits = slice
  453. }
  454. }
  455. }
  456. }
  457. wx.setStorageSync('group', group)
  458. that.setData({loop:loop})
  459. }
  460. }
  461. })
  462. }
  463. }
  464. }
  465. })
  466. }
  467. //回路5
  468. else if(e.target.dataset.loopname == 5 && address[4] != ""){
  469. wx.showModal({
  470. title:'修改回路名称',
  471. editable:true,
  472. placeholderText:'请输入名称',
  473. success(res){
  474. if (res.confirm) {
  475. if(res.content == ''){
  476. wx.showModal({
  477. title:'提示',
  478. content:'请输入名称',
  479. showCancel:false
  480. })
  481. }
  482. else if(res.content.length > 10){
  483. wx.showModal({
  484. title:'提示',
  485. content:'回路名称不能大于10个字符',
  486. showCancel:false
  487. })
  488. }
  489. else{
  490. loop[4] = res.content
  491. slice = loop.slice(0,loops)
  492. slice = slice.toString()
  493. wx.request({
  494. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  495. method:'PUT',
  496. header:{'Content-Type': 'application/json','Authorization': token},
  497. data:{
  498. id:deviceid,
  499. groupId:groupid,
  500. circuits:slice
  501. },
  502. success: res =>{
  503. if(res.data.code == 200){
  504. for(let i = 0;i < group.length;i++){
  505. if(groupName == group[i].groupName){
  506. for(let j = 0;j < group[i].devices.length;j++){
  507. if(deviceName == group[i].devices[j].deviceName){
  508. group[i].devices[j].circuits = slice
  509. }
  510. }
  511. }
  512. }
  513. wx.setStorageSync('group', group)
  514. that.setData({loop:loop})
  515. }
  516. }
  517. })
  518. }
  519. }
  520. }
  521. })
  522. }
  523. //回路6
  524. else if(e.target.dataset.loopname == 6 && address[5] != ""){
  525. wx.showModal({
  526. title:'修改回路名称',
  527. editable:true,
  528. placeholderText:'请输入名称',
  529. success(res){
  530. if (res.confirm) {
  531. if(res.content == ''){
  532. wx.showModal({
  533. title:'提示',
  534. content:'请输入名称',
  535. showCancel:false
  536. })
  537. }
  538. else if(res.content.length > 10){
  539. wx.showModal({
  540. title:'提示',
  541. content:'回路名称不能大于10个字符',
  542. showCancel:false
  543. })
  544. }
  545. else{
  546. loop[5] = res.content
  547. slice = loop.slice(0,loops)
  548. slice = slice.toString()
  549. wx.request({
  550. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  551. method:'PUT',
  552. header:{'Content-Type': 'application/json','Authorization': token},
  553. data:{
  554. id:deviceid,
  555. groupId:groupid,
  556. circuits:slice
  557. },
  558. success: res =>{
  559. if(res.data.code == 200){
  560. for(let i = 0;i < group.length;i++){
  561. if(groupName == group[i].groupName){
  562. for(let j = 0;j < group[i].devices.length;j++){
  563. if(deviceName == group[i].devices[j].deviceName){
  564. group[i].devices[j].circuits = slice
  565. }
  566. }
  567. }
  568. }
  569. wx.setStorageSync('group', group)
  570. that.setData({loop:loop})
  571. }
  572. }
  573. })
  574. }
  575. }
  576. }
  577. })
  578. }
  579. //回路7
  580. else if(e.target.dataset.loopname == 7 && address[6] != ""){
  581. wx.showModal({
  582. title:'修改回路名称',
  583. editable:true,
  584. placeholderText:'请输入名称',
  585. success(res){
  586. if (res.confirm) {
  587. if(res.content == ''){
  588. wx.showModal({
  589. title:'提示',
  590. content:'请输入名称',
  591. showCancel:false
  592. })
  593. }
  594. else if(res.content.length > 10){
  595. wx.showModal({
  596. title:'提示',
  597. content:'回路名称不能大于10个字符',
  598. showCancel:false
  599. })
  600. }
  601. else{
  602. loop[6] = res.content
  603. slice = loop.slice(0,loops)
  604. slice = slice.toString()
  605. wx.request({
  606. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  607. method:'PUT',
  608. header:{'Content-Type': 'application/json','Authorization': token},
  609. data:{
  610. id:deviceid,
  611. groupId:groupid,
  612. circuits:slice
  613. },
  614. success: res =>{
  615. if(res.data.code == 200){
  616. for(let i = 0;i < group.length;i++){
  617. if(groupName == group[i].groupName){
  618. for(let j = 0;j < group[i].devices.length;j++){
  619. if(deviceName == group[i].devices[j].deviceName){
  620. group[i].devices[j].circuits = slice
  621. }
  622. }
  623. }
  624. }
  625. wx.setStorageSync('group', group)
  626. that.setData({loop:loop})
  627. }
  628. }
  629. })
  630. }
  631. }
  632. }
  633. })
  634. }
  635. //回路8
  636. else if(e.target.dataset.loopname == 8 && address[7] != ""){
  637. wx.showModal({
  638. title:'修改回路名称',
  639. editable:true,
  640. placeholderText:'请输入名称',
  641. success(res){
  642. if (res.confirm) {
  643. if(res.content == ''){
  644. wx.showModal({
  645. title:'提示',
  646. content:'请输入名称',
  647. showCancel:false
  648. })
  649. }
  650. else if(res.content.length > 10){
  651. wx.showModal({
  652. title:'提示',
  653. content:'回路名称不能大于10个字符',
  654. showCancel:false
  655. })
  656. }
  657. else{
  658. loop[7] = res.content
  659. slice = loop.slice(0,loops)
  660. slice = slice.toString()
  661. wx.request({
  662. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  663. method:'PUT',
  664. header:{'Content-Type': 'application/json','Authorization': token},
  665. data:{
  666. id:deviceid,
  667. groupId:groupid,
  668. circuits:slice
  669. },
  670. success: res =>{
  671. if(res.data.code == 200){
  672. for(let i = 0;i < group.length;i++){
  673. if(groupName == group[i].groupName){
  674. for(let j = 0;j < group[i].devices.length;j++){
  675. if(deviceName == group[i].devices[j].deviceName){
  676. group[i].devices[j].circuits = slice
  677. }
  678. }
  679. }
  680. }
  681. wx.setStorageSync('group', group)
  682. that.setData({loop:loop})
  683. }
  684. }
  685. })
  686. }
  687. }
  688. }
  689. })
  690. }
  691. //回路9
  692. else if(e.target.dataset.loopname == 9 && address[8] != ""){
  693. wx.showModal({
  694. title:'修改回路名称',
  695. editable:true,
  696. placeholderText:'请输入名称',
  697. success(res){
  698. if (res.confirm) {
  699. if(res.content == ''){
  700. wx.showModal({
  701. title:'提示',
  702. content:'请输入名称',
  703. showCancel:false
  704. })
  705. }
  706. else if(res.content.length > 10){
  707. wx.showModal({
  708. title:'提示',
  709. content:'回路名称不能大于10个字符',
  710. showCancel:false
  711. })
  712. }
  713. else{
  714. loop[0] = res.content
  715. slice = loop.slice(0,loops)
  716. slice = slice.toString()
  717. wx.request({
  718. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  719. method:'PUT',
  720. header:{'Content-Type': 'application/json','Authorization': token},
  721. data:{
  722. id:deviceid,
  723. groupId:groupid,
  724. circuits:slice
  725. },
  726. success: res =>{
  727. if(res.data.code == 200){
  728. for(let i = 0;i < group.length;i++){
  729. if(groupName == group[i].groupName){
  730. for(let j = 0;j < group[i].devices.length;j++){
  731. if(deviceName == group[i].devices[j].deviceName){
  732. group[i].devices[j].circuits = slice
  733. }
  734. }
  735. }
  736. }
  737. wx.setStorageSync('group', group)
  738. that.setData({loop:loop})
  739. }
  740. }
  741. })
  742. }
  743. }
  744. }
  745. })
  746. }
  747. //回路10
  748. else if(e.target.dataset.loopname == 10 && address[9] != ""){
  749. wx.showModal({
  750. title:'修改回路名称',
  751. editable:true,
  752. placeholderText:'请输入名称',
  753. success(res){
  754. if (res.confirm) {
  755. if(res.content == ''){
  756. wx.showModal({
  757. title:'提示',
  758. content:'请输入名称',
  759. showCancel:false
  760. })
  761. }
  762. else if(res.content.length > 10){
  763. wx.showModal({
  764. title:'提示',
  765. content:'回路名称不能大于10个字符',
  766. showCancel:false
  767. })
  768. }
  769. else{
  770. loop[9] = res.content
  771. slice = loop.slice(0,loops)
  772. slice = slice.toString()
  773. wx.request({
  774. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  775. method:'PUT',
  776. header:{'Content-Type': 'application/json','Authorization': token},
  777. data:{
  778. id:deviceid,
  779. groupId:groupid,
  780. circuits:slice
  781. },
  782. success: res =>{
  783. if(res.data.code == 200){
  784. for(let i = 0;i < group.length;i++){
  785. if(groupName == group[i].groupName){
  786. for(let j = 0;j < group[i].devices.length;j++){
  787. if(deviceName == group[i].devices[j].deviceName){
  788. group[i].devices[j].circuits = slice
  789. }
  790. }
  791. }
  792. }
  793. wx.setStorageSync('group', group)
  794. that.setData({loop:loop})
  795. }
  796. }
  797. })
  798. }
  799. }
  800. }
  801. })
  802. }
  803. //回路11
  804. else if(e.target.dataset.loopname == 11 && address[10] != ""){
  805. wx.showModal({
  806. title:'修改回路名称',
  807. editable:true,
  808. placeholderText:'请输入名称',
  809. success(res){
  810. if (res.confirm) {
  811. if(res.content == ''){
  812. wx.showModal({
  813. title:'提示',
  814. content:'请输入名称',
  815. showCancel:false
  816. })
  817. }
  818. else if(res.content.length > 10){
  819. wx.showModal({
  820. title:'提示',
  821. content:'回路名称不能大于10个字符',
  822. showCancel:false
  823. })
  824. }
  825. else{
  826. loop[10] = res.content
  827. slice = loop.slice(0,loops)
  828. slice = slice.toString()
  829. wx.request({
  830. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  831. method:'PUT',
  832. header:{'Content-Type': 'application/json','Authorization': token},
  833. data:{
  834. id:deviceid,
  835. groupId:groupid,
  836. circuits:slice
  837. },
  838. success: res =>{
  839. if(res.data.code == 200){
  840. for(let i = 0;i < group.length;i++){
  841. if(groupName == group[i].groupName){
  842. for(let j = 0;j < group[i].devices.length;j++){
  843. if(deviceName == group[i].devices[j].deviceName){
  844. group[i].devices[j].circuits = slice
  845. }
  846. }
  847. }
  848. }
  849. wx.setStorageSync('group', group)
  850. that.setData({loop:loop})
  851. }
  852. }
  853. })
  854. }
  855. }
  856. }
  857. })
  858. }
  859. //回路12
  860. else if(e.target.dataset.loopname == 12 && address[11] != ""){
  861. wx.showModal({
  862. title:'修改回路名称',
  863. editable:true,
  864. placeholderText:'请输入名称',
  865. success(res){
  866. if (res.confirm) {
  867. if(res.content == ''){
  868. wx.showModal({
  869. title:'提示',
  870. content:'请输入名称',
  871. showCancel:false
  872. })
  873. }
  874. else if(res.content.length > 10){
  875. wx.showModal({
  876. title:'提示',
  877. content:'回路名称不能大于10个字符',
  878. showCancel:false
  879. })
  880. }
  881. else{
  882. loop[11] = res.content
  883. slice = loop.slice(0,loops)
  884. slice = slice.toString()
  885. wx.request({
  886. url: 'https://cloud.long-chi.com/api/minigrogram/lightcontrol/devices',
  887. method:'PUT',
  888. header:{'Content-Type': 'application/json','Authorization': token},
  889. data:{
  890. id:deviceid,
  891. groupId:groupid,
  892. circuits:slice
  893. },
  894. success: res =>{
  895. if(res.data.code == 200){
  896. for(let i = 0;i < group.length;i++){
  897. if(groupName == group[i].groupName){
  898. for(let j = 0;j < group[i].devices.length;j++){
  899. if(deviceName == group[i].devices[j].deviceName){
  900. group[i].devices[j].circuits = slice
  901. }
  902. }
  903. }
  904. }
  905. wx.setStorageSync('group', group)
  906. that.setData({loop:loop})
  907. }
  908. }
  909. })
  910. }
  911. }
  912. }
  913. })
  914. }
  915. },
  916. //全开
  917. Fullopen:function(){
  918. var that = this
  919. var obj = {'R1_RelayStatus':13,'R2_RelayStatus':13,'R3_RelayStatus':13,'R4_RelayStatus':13,
  920. 'R5_RelayStatus':13,'R6_RelayStatus':13,'R7_RelayStatus':13,'R8_RelayStatus':13,'R9_RelayStatus':13,
  921. 'R10_RelayStatus':13,'R11_RelayStatus':13,'R12_RelayStatus':13,'Reflash':3}
  922. postData(that.data.deviceId,that.data.deviceKey,obj).then(res => {
  923. console.log(res)
  924. if(res.data.errno == 0){
  925. wx.setStorageSync('lamp', [])
  926. var lamp = wx.getStorageSync('lamp')
  927. for(let h = 0;h < 12;h++){
  928. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  929. }
  930. setTimeout(function(){
  931. getData(that.data.deviceId,that.data.deviceKey).then(bind => {
  932. let Bind = bind.data.data
  933. if(bind.data.errno == 0){
  934. for(let r1 = 0;r1 < Bind.length;r1++){
  935. if(Bind[r1].id == 'R1_RelayStatus'){
  936. var switchReal = control.switchReal(Bind[r1].current_value,lamp,0)
  937. }
  938. else if(Bind[r1].id == 'R2_RelayStatus'){
  939. var switchReal = control.switchReal(Bind[r1].current_value,lamp,1)
  940. }
  941. else if(Bind[r1].id == 'R3_RelayStatus'){
  942. var switchReal = control.switchReal(Bind[r1].current_value,lamp,2)
  943. }
  944. else if(Bind[r1].id == 'R4_RelayStatus'){
  945. var switchReal = control.switchReal(Bind[r1].current_value,lamp,3)
  946. }
  947. else if(Bind[r1].id == 'R5_RelayStatus'){
  948. var switchReal = control.switchReal(Bind[r1].current_value,lamp,4)
  949. }
  950. else if(Bind[r1].id == 'R6_RelayStatus'){
  951. var switchReal = control.switchReal(Bind[r1].current_value,lamp,5)
  952. }
  953. else if(Bind[r1].id == 'R7_RelayStatus'){
  954. var switchReal = control.switchReal(Bind[r1].current_value,lamp,6)
  955. }
  956. else if(Bind[r1].id == 'R8_RelayStatus'){
  957. var switchReal = control.switchReal(Bind[r1].current_value,lamp,7)
  958. }
  959. else if(Bind[r1].id == 'R9_RelayStatus'){
  960. var switchReal = control.switchReal(Bind[r1].current_value,lamp,8)
  961. }
  962. else if(Bind[r1].id == 'R10_RelayStatus'){
  963. var switchReal = control.switchReal(Bind[r1].current_value,lamp,9)
  964. }
  965. else if(Bind[r1].id == 'R11_RelayStatus'){
  966. var switchReal = control.switchReal(Bind[r1].current_value,lamp,10)
  967. }
  968. else if(Bind[r1].id == 'R12_RelayStatus'){
  969. var switchReal = control.switchReal(Bind[r1].current_value,lamp,11)
  970. }
  971. }
  972. wx.setStorageSync('lamp',switchReal)
  973. that.setData({lamp:wx.getStorageSync('lamp')})
  974. }
  975. })
  976. },2000)
  977. }
  978. else{
  979. wx.showModal({
  980. title:'错误',
  981. content:res.errMsg,
  982. showCancel:false
  983. })
  984. }
  985. })
  986. },
  987. //全关
  988. Allclosed:function(){
  989. var that = this
  990. var obj = {'R1_RelayStatus':12,'R2_RelayStatus':12,'R3_RelayStatus':12,'R4_RelayStatus':12,
  991. 'R5_RelayStatus':12,'R6_RelayStatus':12,'R7_RelayStatus':12,'R8_RelayStatus':12,'R9_RelayStatus':12,
  992. 'R10_RelayStatus':12,'R11_RelayStatus':12,'R12_RelayStatus':12,'Reflash':3}
  993. postData(that.data.deviceId,that.data.deviceKey,obj).then(res => {
  994. console.log(res)
  995. if(res.data.errno == 0){
  996. wx.setStorageSync('lamp', [])
  997. var lamp = wx.getStorageSync('lamp')
  998. for(let h = 0;h < 12;h++){
  999. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1000. }
  1001. setTimeout(function(){
  1002. getData(that.data.deviceId,that.data.deviceKey).then(bind => {
  1003. let Bind = bind.data.data
  1004. if(bind.data.errno == 0){
  1005. for(let r1 = 0;r1 < Bind.length;r1++){
  1006. if(Bind[r1].id == 'R1_RelayStatus'){
  1007. var switchReal = control.switchReal(Bind[r1].current_value,lamp,0)
  1008. }
  1009. else if(Bind[r1].id == 'R2_RelayStatus'){
  1010. var switchReal = control.switchReal(Bind[r1].current_value,lamp,1)
  1011. }
  1012. else if(Bind[r1].id == 'R3_RelayStatus'){
  1013. var switchReal = control.switchReal(Bind[r1].current_value,lamp,2)
  1014. }
  1015. else if(Bind[r1].id == 'R4_RelayStatus'){
  1016. var switchReal = control.switchReal(Bind[r1].current_value,lamp,3)
  1017. }
  1018. else if(Bind[r1].id == 'R5_RelayStatus'){
  1019. var switchReal = control.switchReal(Bind[r1].current_value,lamp,4)
  1020. }
  1021. else if(Bind[r1].id == 'R6_RelayStatus'){
  1022. var switchReal = control.switchReal(Bind[r1].current_value,lamp,5)
  1023. }
  1024. else if(Bind[r1].id == 'R7_RelayStatus'){
  1025. var switchReal = control.switchReal(Bind[r1].current_value,lamp,6)
  1026. }
  1027. else if(Bind[r1].id == 'R8_RelayStatus'){
  1028. var switchReal = control.switchReal(Bind[r1].current_value,lamp,7)
  1029. }
  1030. else if(Bind[r1].id == 'R9_RelayStatus'){
  1031. var switchReal = control.switchReal(Bind[r1].current_value,lamp,8)
  1032. }
  1033. else if(Bind[r1].id == 'R10_RelayStatus'){
  1034. var switchReal = control.switchReal(Bind[r1].current_value,lamp,9)
  1035. }
  1036. else if(Bind[r1].id == 'R11_RelayStatus'){
  1037. var switchReal = control.switchReal(Bind[r1].current_value,lamp,10)
  1038. }
  1039. else if(Bind[r1].id == 'R12_RelayStatus'){
  1040. var switchReal = control.switchReal(Bind[r1].current_value,lamp,11)
  1041. }
  1042. }
  1043. wx.setStorageSync('lamp',switchReal)
  1044. that.setData({lamp:wx.getStorageSync('lamp')})
  1045. }
  1046. })
  1047. },2000)
  1048. }
  1049. else{
  1050. wx.showModal({
  1051. title:'错误',
  1052. content:res.errMsg,
  1053. showCancel:false
  1054. })
  1055. }
  1056. })
  1057. },
  1058. //分组下拉
  1059. changegroup:function(e){
  1060. var that = this
  1061. that.setData({
  1062. groupIndex:e.detail.value
  1063. })
  1064. wx.setStorageSync('lamp',[])
  1065. var lamp = wx.getStorageSync('lamp')
  1066. for(let h = 0;h < 12;h++){
  1067. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1068. }
  1069. var groupName = that.data.groupName[that.data.groupIndex]//选择的分组名称
  1070. wx.setStorageSync('groupName',groupName)//将修改的分组名称放入缓存
  1071. //获取设备名称下拉
  1072. var group = wx.getStorageSync('group')
  1073. var deviceName = that.data.deviceName
  1074. var deviceName = []
  1075. for(let i = 0;i < group.length;i++){
  1076. if(group[i].groupName == groupName){
  1077. if(group[i].devices != null){
  1078. wx.showToast({
  1079. title:'刷新中',
  1080. icon:'loading',
  1081. duration: 2000
  1082. })
  1083. for(let j = 0;j < group[i].devices.length;j++){
  1084. deviceName.push(group[i].devices[j].deviceName)
  1085. }
  1086. }
  1087. else{
  1088. console.log('主页分组下拉到没有设备的工程')
  1089. wx.showModal({
  1090. title:'提示',
  1091. content:'工程中不存在设备',
  1092. showCancel:false
  1093. })
  1094. var tlamps = []
  1095. var tloops = []
  1096. var tcolors = []
  1097. var taddress = []
  1098. for(let q = 0;q < 12;q++){
  1099. tloops.push('回路' + (q + 1))
  1100. tlamps.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1101. tcolors.push('#d5dbdf')
  1102. taddress.push('')
  1103. that.setData({loop:tloops,lamp:tlamps,Color:tcolors,address:taddress,status:'离线'})
  1104. }
  1105. }
  1106. }
  1107. }
  1108. that.setData({
  1109. deviceName:deviceName
  1110. })
  1111. //重新获取设备id和key
  1112. var groupNames = that.data.groupName[that.data.groupIndex]
  1113. if(that.data.deviceName.length != 0){
  1114. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  1115. for(let d = 0;d < group.length;d++){
  1116. if(groupNames == group[d].groupName){
  1117. for(let g = 0;g < group[d].devices.length;g++){
  1118. if(deviceNames == group[d].devices[g].deviceName){
  1119. var deviceId = group[d].devices[g].deviceId
  1120. var deviceKey = group[d].devices[g].deviceKey
  1121. var circuitNum = group[d].devices[g].circuitNum
  1122. var circuits = group[d].devices[g].circuits
  1123. }
  1124. }
  1125. }
  1126. }
  1127. var Circuits = circuits.split(',')
  1128. let loop = that.data.loop
  1129. for(let vr = 0;vr < circuitNum;vr++){
  1130. loop[vr] = Circuits[vr]
  1131. }
  1132. that.setData({
  1133. loop:loop
  1134. })
  1135. wx.setStorageSync('deviceName',deviceNames)//将修改设备名放入缓存
  1136. that.setData({
  1137. deviceId:deviceId,
  1138. deviceKey:deviceKey,
  1139. })
  1140. wx.request({
  1141. url: 'https://api.heclouds.com/devices/' + deviceId,
  1142. method:'GET',
  1143. header:{"api-key": deviceKey},
  1144. success:(res) =>{
  1145. if (res.data.data.online == true) {
  1146. that.setData({
  1147. status:"在线"
  1148. })
  1149. }
  1150. else{
  1151. that.setData({
  1152. status:"离线"
  1153. })
  1154. }
  1155. }
  1156. })
  1157. wx.request({
  1158. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  1159. header:{"api-key": deviceKey},
  1160. method:'GET',
  1161. success:(res) => {
  1162. let {data} = res.data
  1163. for(let i = 0;i < data.length;i++){
  1164. if (data[i].id == "RelayCount") {
  1165. var quantity = data[i].current_value
  1166. }
  1167. else if(data[i].id == "ManualStatus"){
  1168. if(data[i].current_value == 0){
  1169. that.setData({
  1170. Modecontrol:"自动"
  1171. })
  1172. }
  1173. else{
  1174. that.setData({
  1175. Modecontrol:"手动"
  1176. })
  1177. }
  1178. }
  1179. //回路1
  1180. else if(data[i].id == 'R1_RelayStatus'){
  1181. var police = data[i].current_value
  1182. switch(police.length){
  1183. case 1:
  1184. police = '000' + police
  1185. break;
  1186. case 2:
  1187. police = '00' + police
  1188. break;
  1189. case 3:
  1190. police = '0' + police
  1191. break;
  1192. default:
  1193. police = police
  1194. break;
  1195. }
  1196. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1197. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1198. }
  1199. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1200. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1201. }
  1202. }
  1203. //回路2
  1204. else if(data[i].id == 'R2_RelayStatus'){
  1205. var police = data[i].current_value
  1206. switch(police.length){
  1207. case 1:
  1208. police = '000' + police
  1209. break;
  1210. case 2:
  1211. police = '00' + police
  1212. break;
  1213. case 3:
  1214. police = '0' + police
  1215. break;
  1216. default:
  1217. police = police
  1218. break;
  1219. }
  1220. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1221. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1222. }
  1223. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1224. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1225. }
  1226. }
  1227. //回路3
  1228. else if(data[i].id == 'R3_RelayStatus'){
  1229. var police = data[i].current_value
  1230. switch(police.length){
  1231. case 1:
  1232. police = '000' + police
  1233. break;
  1234. case 2:
  1235. police = '00' + police
  1236. break;
  1237. case 3:
  1238. police = '0' + police
  1239. break;
  1240. default:
  1241. police = police
  1242. break;
  1243. }
  1244. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1245. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1246. }
  1247. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1248. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1249. }
  1250. }
  1251. //回路4
  1252. else if(data[i].id == 'R4_RelayStatus'){
  1253. var police = data[i].current_value
  1254. switch(police.length){
  1255. case 1:
  1256. police = '000' + police
  1257. break;
  1258. case 2:
  1259. police = '00' + police
  1260. break;
  1261. case 3:
  1262. police = '0' + police
  1263. break;
  1264. default:
  1265. police = police
  1266. break;
  1267. }
  1268. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1269. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1270. }
  1271. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1272. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1273. }
  1274. }
  1275. //回路5
  1276. else if(data[i].id == 'R5_RelayStatus'){
  1277. var police = data[i].current_value
  1278. switch(police.length){
  1279. case 1:
  1280. police = '000' + police
  1281. break;
  1282. case 2:
  1283. police = '00' + police
  1284. break;
  1285. case 3:
  1286. police = '0' + police
  1287. break;
  1288. default:
  1289. police = police
  1290. break;
  1291. }
  1292. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1293. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1294. }
  1295. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1296. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1297. }
  1298. }
  1299. //回路6
  1300. else if(data[i].id == 'R6_RelayStatus'){
  1301. var police = data[i].current_value
  1302. switch(police.length){
  1303. case 1:
  1304. police = '000' + police
  1305. break;
  1306. case 2:
  1307. police = '00' + police
  1308. break;
  1309. case 3:
  1310. police = '0' + police
  1311. break;
  1312. default:
  1313. police = police
  1314. break;
  1315. }
  1316. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1317. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1318. }
  1319. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1320. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1321. }
  1322. }
  1323. //回路7
  1324. else if(data[i].id == 'R7_RelayStatus'){
  1325. var police = data[i].current_value
  1326. switch(police.length){
  1327. case 1:
  1328. police = '000' + police
  1329. break;
  1330. case 2:
  1331. police = '00' + police
  1332. break;
  1333. case 3:
  1334. police = '0' + police
  1335. break;
  1336. default:
  1337. police = police
  1338. break;
  1339. }
  1340. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1341. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1342. }
  1343. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1344. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1345. }
  1346. }
  1347. //回路8
  1348. else if(data[i].id == 'R8_RelayStatus'){
  1349. var police = data[i].current_value
  1350. switch(police.length){
  1351. case 1:
  1352. police = '000' + police
  1353. break;
  1354. case 2:
  1355. police = '00' + police
  1356. break;
  1357. case 3:
  1358. police = '0' + police
  1359. break;
  1360. default:
  1361. police = police
  1362. break;
  1363. }
  1364. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1365. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1366. }
  1367. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1368. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1369. }
  1370. }
  1371. //回路9
  1372. else if(data[i].id == 'R9_RelayStatus'){
  1373. var police = data[i].current_value
  1374. switch(police.length){
  1375. case 1:
  1376. police = '000' + police
  1377. break;
  1378. case 2:
  1379. police = '00' + police
  1380. break;
  1381. case 3:
  1382. police = '0' + police
  1383. break;
  1384. default:
  1385. police = police
  1386. break;
  1387. }
  1388. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1389. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1390. }
  1391. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1392. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1393. }
  1394. }
  1395. //回路10
  1396. else if(data[i].id == 'R10_RelayStatus'){
  1397. var police = data[i].current_value
  1398. switch(police.length){
  1399. case 1:
  1400. police = '000' + police
  1401. break;
  1402. case 2:
  1403. police = '00' + police
  1404. break;
  1405. case 3:
  1406. police = '0' + police
  1407. break;
  1408. default:
  1409. police = police
  1410. break;
  1411. }
  1412. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1413. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1414. }
  1415. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1416. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1417. }
  1418. }
  1419. //回路11
  1420. else if(data[i].id == 'R11_RelayStatus'){
  1421. var police = data[i].current_value
  1422. switch(police.length){
  1423. case 1:
  1424. police = '000' + police
  1425. break;
  1426. case 2:
  1427. police = '00' + police
  1428. break;
  1429. case 3:
  1430. police = '0' + police
  1431. break;
  1432. default:
  1433. police = police
  1434. break;
  1435. }
  1436. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1437. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1438. }
  1439. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1440. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1441. }
  1442. }
  1443. //回路12
  1444. else if(data[i].id == 'R12_RelayStatus'){
  1445. var police = data[i].current_value
  1446. switch(police.length){
  1447. case 1:
  1448. police = '000' + police
  1449. break;
  1450. case 2:
  1451. police = '00' + police
  1452. break;
  1453. case 3:
  1454. police = '0' + police
  1455. break;
  1456. default:
  1457. police = police
  1458. break;
  1459. }
  1460. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1461. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1462. }
  1463. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1464. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1465. }
  1466. }
  1467. }
  1468. wx.setStorageSync('lamp',lamp)
  1469. that.setData({
  1470. lamp:wx.getStorageSync('lamp')
  1471. })
  1472. for (let i = 0;i < quantity; i++) {
  1473. var Loop = that.data.Loop
  1474. var color = that.data.Color
  1475. var size = that.data.Size
  1476. var Address = that.data.address
  1477. Loop[i] = that.data.loop[i]
  1478. color[i] = "black"
  1479. size[i] = "calc(100vw * 16/375)",
  1480. Address[i] = "/image/modify.png"
  1481. that.setData({
  1482. Color:color,
  1483. Size:size,
  1484. address:Address,
  1485. loops:quantity,
  1486. Loop:Loop
  1487. })
  1488. }
  1489. }
  1490. })
  1491. }
  1492. else{
  1493. wx.setStorageSync('deviceName','')//清空设备名称缓存
  1494. }
  1495. },
  1496. //设备下拉
  1497. changedevice:function(e){
  1498. wx.showToast({
  1499. title:'刷新中',
  1500. icon:'loading',
  1501. duration: 2000
  1502. })
  1503. var that = this
  1504. that.setData({
  1505. deviceIndex:e.detail.value
  1506. })
  1507. wx.setStorageSync('lamp',[])
  1508. var lamp = wx.getStorageSync('lamp')
  1509. for(let h = 0;h < 12;h++){
  1510. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1511. }
  1512. var group = wx.getStorageSync('group')
  1513. var groupName = that.data.groupName[that.data.groupIndex]
  1514. var deviceName = that.data.deviceName[that.data.deviceIndex]
  1515. wx.setStorageSync('groupName', groupName)//分组名称
  1516. wx.setStorageSync('deviceName', deviceName)//设备名称
  1517. for (let index = 0; index < group.length; index++) {
  1518. if(groupName == group[index].groupName){
  1519. for(let t = 0;t < group[index].devices.length;t++){
  1520. if(deviceName == group[index].devices[t].deviceName){
  1521. var deviceId = group[index].devices[t].deviceId
  1522. var deviceKey = group[index].devices[t].deviceKey
  1523. var circuitNum = group[index].devices[t].circuitNum
  1524. var circuits = group[index].devices[t].circuits
  1525. }
  1526. }
  1527. }
  1528. }
  1529. var Circuits = circuits.split(',')
  1530. let loop = that.data.loop
  1531. for(let vr = 0;vr < circuitNum;vr++){
  1532. loop[vr] = Circuits[vr]
  1533. }
  1534. that.setData({
  1535. deviceId:deviceId,
  1536. deviceKey:deviceKey,
  1537. loop:loop
  1538. })
  1539. wx.request({
  1540. url: 'https://api.heclouds.com/devices/' + deviceId,
  1541. method:'GET',
  1542. header:{"api-key": deviceKey},
  1543. success:(res) =>{
  1544. if (res.data.data.online == true) {
  1545. that.setData({
  1546. status:"在线"
  1547. })
  1548. }
  1549. else{
  1550. that.setData({
  1551. status:"离线"
  1552. })
  1553. }
  1554. }
  1555. })
  1556. wx.request({
  1557. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  1558. header:{"api-key": deviceKey},
  1559. method:'GET',
  1560. success:(res) => {
  1561. let {data} = res.data
  1562. for(let i = 0;i < data.length;i++){
  1563. if (data[i].id == "RelayCount") {
  1564. var quantity = data[i].current_value
  1565. }
  1566. else if(data[i].id == "ManualStatus"){
  1567. if(data[i].current_value == 0){
  1568. that.setData({
  1569. Modecontrol:"自动"
  1570. })
  1571. }
  1572. else{
  1573. that.setData({
  1574. Modecontrol:"手动"
  1575. })
  1576. }
  1577. }
  1578. //回路1
  1579. else if(data[i].id == 'R1_RelayStatus'){
  1580. var police = data[i].current_value
  1581. switch(police.length){
  1582. case 1:
  1583. police = '000' + police
  1584. break;
  1585. case 2:
  1586. police = '00' + police
  1587. break;
  1588. case 3:
  1589. police = '0' + police
  1590. break;
  1591. default:
  1592. police = police
  1593. break;
  1594. }
  1595. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1596. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1597. }
  1598. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1599. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1600. }
  1601. }
  1602. //回路2
  1603. else if(data[i].id == 'R2_RelayStatus'){
  1604. var police = data[i].current_value
  1605. switch(police.length){
  1606. case 1:
  1607. police = '000' + police
  1608. break;
  1609. case 2:
  1610. police = '00' + police
  1611. break;
  1612. case 3:
  1613. police = '0' + police
  1614. break;
  1615. default:
  1616. police = police
  1617. break;
  1618. }
  1619. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1620. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1621. }
  1622. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1623. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1624. }
  1625. }
  1626. //回路3
  1627. else if(data[i].id == 'R3_RelayStatus'){
  1628. var police = data[i].current_value
  1629. switch(police.length){
  1630. case 1:
  1631. police = '000' + police
  1632. break;
  1633. case 2:
  1634. police = '00' + police
  1635. break;
  1636. case 3:
  1637. police = '0' + police
  1638. break;
  1639. default:
  1640. police = police
  1641. break;
  1642. }
  1643. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1644. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1645. }
  1646. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1647. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1648. }
  1649. }
  1650. //回路4
  1651. else if(data[i].id == 'R4_RelayStatus'){
  1652. var police = data[i].current_value
  1653. switch(police.length){
  1654. case 1:
  1655. police = '000' + police
  1656. break;
  1657. case 2:
  1658. police = '00' + police
  1659. break;
  1660. case 3:
  1661. police = '0' + police
  1662. break;
  1663. default:
  1664. police = police
  1665. break;
  1666. }
  1667. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1668. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1669. }
  1670. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1671. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1672. }
  1673. }
  1674. //回路5
  1675. else if(data[i].id == 'R5_RelayStatus'){
  1676. var police = data[i].current_value
  1677. switch(police.length){
  1678. case 1:
  1679. police = '000' + police
  1680. break;
  1681. case 2:
  1682. police = '00' + police
  1683. break;
  1684. case 3:
  1685. police = '0' + police
  1686. break;
  1687. default:
  1688. police = police
  1689. break;
  1690. }
  1691. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1692. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1693. }
  1694. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1695. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1696. }
  1697. }
  1698. //回路6
  1699. else if(data[i].id == 'R6_RelayStatus'){
  1700. var police = data[i].current_value
  1701. switch(police.length){
  1702. case 1:
  1703. police = '000' + police
  1704. break;
  1705. case 2:
  1706. police = '00' + police
  1707. break;
  1708. case 3:
  1709. police = '0' + police
  1710. break;
  1711. default:
  1712. police = police
  1713. break;
  1714. }
  1715. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1716. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1717. }
  1718. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1719. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1720. }
  1721. }
  1722. //回路7
  1723. else if(data[i].id == 'R7_RelayStatus'){
  1724. var police = data[i].current_value
  1725. switch(police.length){
  1726. case 1:
  1727. police = '000' + police
  1728. break;
  1729. case 2:
  1730. police = '00' + police
  1731. break;
  1732. case 3:
  1733. police = '0' + police
  1734. break;
  1735. default:
  1736. police = police
  1737. break;
  1738. }
  1739. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1740. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1741. }
  1742. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1743. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1744. }
  1745. }
  1746. //回路8
  1747. else if(data[i].id == 'R8_RelayStatus'){
  1748. var police = data[i].current_value
  1749. switch(police.length){
  1750. case 1:
  1751. police = '000' + police
  1752. break;
  1753. case 2:
  1754. police = '00' + police
  1755. break;
  1756. case 3:
  1757. police = '0' + police
  1758. break;
  1759. default:
  1760. police = police
  1761. break;
  1762. }
  1763. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1764. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1765. }
  1766. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1767. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1768. }
  1769. }
  1770. //回路9
  1771. else if(data[i].id == 'R9_RelayStatus'){
  1772. var police = data[i].current_value
  1773. switch(police.length){
  1774. case 1:
  1775. police = '000' + police
  1776. break;
  1777. case 2:
  1778. police = '00' + police
  1779. break;
  1780. case 3:
  1781. police = '0' + police
  1782. break;
  1783. default:
  1784. police = police
  1785. break;
  1786. }
  1787. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1788. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1789. }
  1790. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1791. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1792. }
  1793. }
  1794. //回路10
  1795. else if(data[i].id == 'R10_RelayStatus'){
  1796. var police = data[i].current_value
  1797. switch(police.length){
  1798. case 1:
  1799. police = '000' + police
  1800. break;
  1801. case 2:
  1802. police = '00' + police
  1803. break;
  1804. case 3:
  1805. police = '0' + police
  1806. break;
  1807. default:
  1808. police = police
  1809. break;
  1810. }
  1811. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1812. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1813. }
  1814. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1815. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1816. }
  1817. }
  1818. //回路11
  1819. else if(data[i].id == 'R11_RelayStatus'){
  1820. var police = data[i].current_value
  1821. switch(police.length){
  1822. case 1:
  1823. police = '000' + police
  1824. break;
  1825. case 2:
  1826. police = '00' + police
  1827. break;
  1828. case 3:
  1829. police = '0' + police
  1830. break;
  1831. default:
  1832. police = police
  1833. break;
  1834. }
  1835. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1836. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1837. }
  1838. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1839. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1840. }
  1841. }
  1842. //回路12
  1843. else if(data[i].id == 'R12_RelayStatus'){
  1844. var police = data[i].current_value
  1845. switch(police.length){
  1846. case 1:
  1847. police = '000' + police
  1848. break;
  1849. case 2:
  1850. police = '00' + police
  1851. break;
  1852. case 3:
  1853. police = '0' + police
  1854. break;
  1855. default:
  1856. police = police
  1857. break;
  1858. }
  1859. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  1860. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1861. }
  1862. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  1863. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1864. }
  1865. }
  1866. }
  1867. wx.setStorageSync('lamp',lamp)
  1868. that.setData({
  1869. lamp:wx.getStorageSync('lamp')
  1870. })
  1871. for (let i = 0;i < quantity; i++) {
  1872. var Loop = that.data.Loop
  1873. var color = that.data.Color
  1874. var size = that.data.Size
  1875. var Address = that.data.address
  1876. Loop[i] = that.data.loop[i]
  1877. color[i] = "black"
  1878. size[i] = "calc(100vw * 16/375)",
  1879. Address[i] = "/image/modify.png"
  1880. that.setData({
  1881. Color:color,
  1882. Size:size,
  1883. address:Address,
  1884. loops:quantity,
  1885. Loop:Loop
  1886. })
  1887. }
  1888. }
  1889. })
  1890. },
  1891. //手动
  1892. manual:function(e){
  1893. var that = this
  1894. var deviceId = that.data.deviceId
  1895. var deviceKey = that.data.deviceKey
  1896. wx.request({
  1897. url: 'https://api.heclouds.com/cmds?device_id=' + deviceId,
  1898. method:'POST',
  1899. header:{"api-key": deviceKey},
  1900. data:{
  1901. 'ManualStatus':1,
  1902. 'Reflash':3
  1903. },
  1904. success:res =>{
  1905. if(res.statusCode == 200){
  1906. that.setData({
  1907. Modecontrol:'手动'
  1908. })
  1909. }
  1910. else{
  1911. wx.showModal({
  1912. title:'错误',
  1913. content:res.errMsg,
  1914. showCancel:false
  1915. })
  1916. }
  1917. }
  1918. })
  1919. },
  1920. //自动
  1921. automatic:function(e){
  1922. var that = this
  1923. var deviceId = that.data.deviceId
  1924. var deviceKey = that.data.deviceKey
  1925. wx.request({
  1926. url: 'https://api.heclouds.com/cmds?device_id=' + deviceId,
  1927. method:'POST',
  1928. header:{"api-key": deviceKey},
  1929. data:{
  1930. 'ManualStatus':0,
  1931. 'Reflash':3
  1932. },
  1933. success:res =>{
  1934. if(res.statusCode == 200){
  1935. that.setData({
  1936. Modecontrol:'自动'
  1937. })
  1938. }
  1939. else{
  1940. wx.showModal({
  1941. title:'错误',
  1942. content:res.errMsg,
  1943. showCancel:false
  1944. })
  1945. }
  1946. }
  1947. })
  1948. },
  1949. /**
  1950. * 生命周期函数--监听页面加载
  1951. */
  1952. onLoad(options) {
  1953. },
  1954. /**
  1955. * 生命周期函数--监听页面初次渲染完成
  1956. */
  1957. onReady() {
  1958. },
  1959. /**
  1960. * 生命周期函数--监听页面显示
  1961. */
  1962. onShow() {
  1963. wx.showToast({
  1964. title:'加载中',
  1965. icon:'loading',
  1966. duration: 1500
  1967. })
  1968. var that = this
  1969. var group = wx.getStorageSync('group')
  1970. //灯泡图标缓存
  1971. wx.setStorageSync('lamp', [])
  1972. var lamp = wx.getStorageSync('lamp')
  1973. for(let h = 0;h < 12;h++){
  1974. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1975. }
  1976. //获取分组下拉
  1977. var groups = []
  1978. for(let i = 0;i < group.length;i++){
  1979. groups.push(group[i].groupName)
  1980. }
  1981. that.setData({
  1982. groupName:groups
  1983. })
  1984. //改变分组下标
  1985. if(wx.getStorageSync('groupName') != ''){
  1986. let GroupName = that.data.groupName
  1987. for(let n = 0;n < GroupName.length;n++){
  1988. if(GroupName[n] == wx.getStorageSync('groupName')){
  1989. that.setData({
  1990. groupIndex:n
  1991. })
  1992. }
  1993. }
  1994. }
  1995. else{
  1996. console.log('添加分组下标')
  1997. wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])
  1998. }
  1999. //获取设备下拉
  2000. var device = []
  2001. var groupName = that.data.groupName
  2002. var groupIndex = that.data.groupIndex
  2003. for(let j = 0;j < group.length;j++){
  2004. if(groupName[groupIndex] == group[j].groupName){
  2005. console.log(group[j].devices)
  2006. console.log('.................................')
  2007. if(group[j].devices != null && group[j].devices.length != 0){
  2008. for(let p = 0;p < group[j].devices.length;p++){
  2009. device.push(group[j].devices[p].deviceName)
  2010. }
  2011. that.setData({
  2012. deviceName:device
  2013. })
  2014. //改变设备下标
  2015. if(wx.getStorageSync('deviceName') != ''){
  2016. var DeviceName = that.data.deviceName
  2017. for(let m = 0;m < DeviceName.length;m++){
  2018. if(DeviceName[m] == wx.getStorageSync('deviceName')){
  2019. that.setData({
  2020. deviceIndex:m
  2021. })
  2022. }
  2023. }
  2024. }
  2025. else{
  2026. wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex])
  2027. }
  2028. //根据分组名和设备名获取key和id以及回路的数量和名称
  2029. var groupNames = that.data.groupName[that.data.groupIndex]
  2030. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  2031. for(let d = 0;d < group.length;d++){
  2032. if(groupNames == group[d].groupName){
  2033. for(let g = 0;g < group[d].devices.length;g++){
  2034. if(deviceNames == group[d].devices[g].deviceName){
  2035. var circuitNum = group[d].devices[g].circuitNum
  2036. var circuits = group[d].devices[g].circuits
  2037. var deviceId = group[d].devices[g].deviceId
  2038. var deviceKey = group[d].devices[g].deviceKey
  2039. }
  2040. }
  2041. }
  2042. }
  2043. that.setData({
  2044. deviceId:deviceId,
  2045. deviceKey:deviceKey
  2046. })
  2047. var Circuits = circuits.split(',')
  2048. let loop = that.data.loop
  2049. for(let vr = 0;vr < circuitNum;vr++){
  2050. loop[vr] = Circuits[vr]
  2051. }
  2052. that.setData({
  2053. loop:loop
  2054. })
  2055. //发起请求
  2056. wx.request({
  2057. url: 'https://api.heclouds.com/cmds?device_id=' + deviceId,
  2058. method:'POST',
  2059. header:{"api-key": deviceKey},
  2060. data:{
  2061. "Reflash":3
  2062. },
  2063. success:res => {
  2064. wx.request({
  2065. url: 'https://api.heclouds.com/devices/' + deviceId,
  2066. method:'GET',
  2067. header:{"api-key": deviceKey},
  2068. success:(res) =>{
  2069. if (res.data.data.online == true) {
  2070. that.setData({status:"在线"})
  2071. }
  2072. else{
  2073. that.setData({status:"离线"})
  2074. }
  2075. }
  2076. })
  2077. setTimeout(function(){
  2078. wx.request({
  2079. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  2080. header:{"api-key": deviceKey},
  2081. method:'GET',
  2082. success:(res) => {
  2083. let data = res.data.data
  2084. for(let i = 0;i < data.length;i++){
  2085. if (data[i].id == "RelayCount") {
  2086. var quantity = data[i].current_value
  2087. that.setData({
  2088. loops:quantity
  2089. })
  2090. }
  2091. else if(data[i].id == "ManualStatus"){
  2092. if(data[i].current_value == 0){
  2093. that.setData({
  2094. Modecontrol:"自动"
  2095. })
  2096. }
  2097. else{
  2098. that.setData({
  2099. Modecontrol:"手动"
  2100. })
  2101. }
  2102. }
  2103. //回路1
  2104. else if(data[i].id == 'R1_RelayStatus'){
  2105. var police = data[i].current_value
  2106. switch(police.length){
  2107. case 1:
  2108. police = '000' + police
  2109. break;
  2110. case 2:
  2111. police = '00' + police
  2112. break;
  2113. case 3:
  2114. police = '0' + police
  2115. break;
  2116. default:
  2117. police = police
  2118. break;
  2119. }
  2120. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2121. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2122. }
  2123. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2124. lamp[0] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2125. }
  2126. }
  2127. //回路2
  2128. else if(data[i].id == 'R2_RelayStatus'){
  2129. var police = data[i].current_value
  2130. switch(police.length){
  2131. case 1:
  2132. police = '000' + police
  2133. break;
  2134. case 2:
  2135. police = '00' + police
  2136. break;
  2137. case 3:
  2138. police = '0' + police
  2139. break;
  2140. default:
  2141. police = police
  2142. break;
  2143. }
  2144. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2145. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2146. }
  2147. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2148. lamp[1] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2149. }
  2150. }
  2151. //回路3
  2152. else if(data[i].id == 'R3_RelayStatus'){
  2153. var police = data[i].current_value
  2154. switch(police.length){
  2155. case 1:
  2156. police = '000' + police
  2157. break;
  2158. case 2:
  2159. police = '00' + police
  2160. break;
  2161. case 3:
  2162. police = '0' + police
  2163. break;
  2164. default:
  2165. police = police
  2166. break;
  2167. }
  2168. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2169. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2170. }
  2171. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2172. lamp[2] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2173. }
  2174. }
  2175. //回路4
  2176. else if(data[i].id == 'R4_RelayStatus'){
  2177. var police = data[i].current_value
  2178. switch(police.length){
  2179. case 1:
  2180. police = '000' + police
  2181. break;
  2182. case 2:
  2183. police = '00' + police
  2184. break;
  2185. case 3:
  2186. police = '0' + police
  2187. break;
  2188. default:
  2189. police = police
  2190. break;
  2191. }
  2192. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2193. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2194. }
  2195. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2196. lamp[3] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2197. }
  2198. }
  2199. //回路5
  2200. else if(data[i].id == 'R5_RelayStatus'){
  2201. var police = data[i].current_value
  2202. switch(police.length){
  2203. case 1:
  2204. police = '000' + police
  2205. break;
  2206. case 2:
  2207. police = '00' + police
  2208. break;
  2209. case 3:
  2210. police = '0' + police
  2211. break;
  2212. default:
  2213. police = police
  2214. break;
  2215. }
  2216. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2217. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2218. }
  2219. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2220. lamp[4] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2221. }
  2222. }
  2223. //回路6
  2224. else if(data[i].id == 'R6_RelayStatus'){
  2225. var police = data[i].current_value
  2226. switch(police.length){
  2227. case 1:
  2228. police = '000' + police
  2229. break;
  2230. case 2:
  2231. police = '00' + police
  2232. break;
  2233. case 3:
  2234. police = '0' + police
  2235. break;
  2236. default:
  2237. police = police
  2238. break;
  2239. }
  2240. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2241. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2242. }
  2243. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2244. lamp[5] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2245. }
  2246. }
  2247. //回路7
  2248. else if(data[i].id == 'R7_RelayStatus'){
  2249. var police = data[i].current_value
  2250. switch(police.length){
  2251. case 1:
  2252. police = '000' + police
  2253. break;
  2254. case 2:
  2255. police = '00' + police
  2256. break;
  2257. case 3:
  2258. police = '0' + police
  2259. break;
  2260. default:
  2261. police = police
  2262. break;
  2263. }
  2264. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2265. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2266. }
  2267. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2268. lamp[6] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2269. }
  2270. }
  2271. //回路8
  2272. else if(data[i].id == 'R8_RelayStatus'){
  2273. var police = data[i].current_value
  2274. switch(police.length){
  2275. case 1:
  2276. police = '000' + police
  2277. break;
  2278. case 2:
  2279. police = '00' + police
  2280. break;
  2281. case 3:
  2282. police = '0' + police
  2283. break;
  2284. default:
  2285. police = police
  2286. break;
  2287. }
  2288. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2289. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2290. }
  2291. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2292. lamp[7] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2293. }
  2294. }
  2295. //回路9
  2296. else if(data[i].id == 'R9_RelayStatus'){
  2297. var police = data[i].current_value
  2298. switch(police.length){
  2299. case 1:
  2300. police = '000' + police
  2301. break;
  2302. case 2:
  2303. police = '00' + police
  2304. break;
  2305. case 3:
  2306. police = '0' + police
  2307. break;
  2308. default:
  2309. police = police
  2310. break;
  2311. }
  2312. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2313. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2314. }
  2315. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2316. lamp[8] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2317. }
  2318. }
  2319. //回路10
  2320. else if(data[i].id == 'R10_RelayStatus'){
  2321. var police = data[i].current_value
  2322. switch(police.length){
  2323. case 1:
  2324. police = '000' + police
  2325. break;
  2326. case 2:
  2327. police = '00' + police
  2328. break;
  2329. case 3:
  2330. police = '0' + police
  2331. break;
  2332. default:
  2333. police = police
  2334. break;
  2335. }
  2336. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2337. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2338. }
  2339. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2340. lamp[9] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2341. }
  2342. }
  2343. //回路11
  2344. else if(data[i].id == 'R11_RelayStatus'){
  2345. var police = data[i].current_value
  2346. switch(police.length){
  2347. case 1:
  2348. police = '000' + police
  2349. break;
  2350. case 2:
  2351. police = '00' + police
  2352. break;
  2353. case 3:
  2354. police = '0' + police
  2355. break;
  2356. default:
  2357. police = police
  2358. break;
  2359. }
  2360. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2361. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2362. }
  2363. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2364. lamp[10] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2365. }
  2366. }
  2367. //回路12
  2368. else if(data[i].id == 'R12_RelayStatus'){
  2369. var police = data[i].current_value
  2370. switch(police.length){
  2371. case 1:
  2372. police = '000' + police
  2373. break;
  2374. case 2:
  2375. police = '00' + police
  2376. break;
  2377. case 3:
  2378. police = '0' + police
  2379. break;
  2380. default:
  2381. police = police
  2382. break;
  2383. }
  2384. if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){
  2385. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2386. }
  2387. else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){
  2388. lamp[11] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2389. }
  2390. }
  2391. }
  2392. wx.setStorageSync('lamp', lamp)
  2393. that.setData({
  2394. lamp:wx.getStorageSync('lamp')
  2395. })
  2396. for (let i = 0;i < quantity; i++) {
  2397. var Loop = that.data.Loop
  2398. var color = that.data.Color
  2399. var size = that.data.Size
  2400. var Address = that.data.address
  2401. Loop[i] = that.data.loop[i]
  2402. color[i] = "black"
  2403. size[i] = "calc(100vw * 16/375)",
  2404. Address[i] = "/image/modify.png"
  2405. that.setData({
  2406. Color:color,
  2407. Size:size,
  2408. address:Address,
  2409. Loop:Loop
  2410. })
  2411. }
  2412. }
  2413. })
  2414. },1500)
  2415. }
  2416. })
  2417. }
  2418. else {
  2419. that.setData({status:'离线',deviceName:[]})
  2420. }
  2421. }
  2422. }
  2423. },
  2424. /**
  2425. * 生命周期函数--监听页面隐藏
  2426. */
  2427. onHide() {
  2428. },
  2429. /**
  2430. * 生命周期函数--监听页面卸载
  2431. */
  2432. onUnload() {
  2433. },
  2434. /**
  2435. * 页面相关事件处理函数--监听用户下拉动作
  2436. */
  2437. onPullDownRefresh() {
  2438. },
  2439. /**
  2440. * 页面上拉触底事件的处理函数
  2441. */
  2442. onReachBottom() {
  2443. },
  2444. /**
  2445. * 用户点击右上角分享
  2446. */
  2447. onShareAppMessage() {
  2448. }
  2449. })