control.js 83 KB

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