control.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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. },3000)
  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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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,relayId: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. },3000)
  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. if(res.data.errno == 0){
  1186. wx.setStorageSync('lamp', [])
  1187. var lamp = wx.getStorageSync('lamp')
  1188. for(let h = 0;h < 12;h++){
  1189. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1190. }
  1191. setTimeout(function(){
  1192. getData(that.data.deviceId,that.data.deviceKey).then(bind => {
  1193. let Bind = bind.data.data
  1194. if(bind.data.errno == 0){
  1195. for(let r1 = 0;r1 < Bind.length;r1++){
  1196. if(Bind[r1].id == 'R1_RelayStatus'){
  1197. var switchReal = control.switchReal(Bind[r1].current_value,lamp,0)
  1198. }
  1199. else if(Bind[r1].id == 'R2_RelayStatus'){
  1200. var switchReal = control.switchReal(Bind[r1].current_value,lamp,1)
  1201. }
  1202. else if(Bind[r1].id == 'R3_RelayStatus'){
  1203. var switchReal = control.switchReal(Bind[r1].current_value,lamp,2)
  1204. }
  1205. else if(Bind[r1].id == 'R4_RelayStatus'){
  1206. var switchReal = control.switchReal(Bind[r1].current_value,lamp,3)
  1207. }
  1208. else if(Bind[r1].id == 'R5_RelayStatus'){
  1209. var switchReal = control.switchReal(Bind[r1].current_value,lamp,4)
  1210. }
  1211. else if(Bind[r1].id == 'R6_RelayStatus'){
  1212. var switchReal = control.switchReal(Bind[r1].current_value,lamp,5)
  1213. }
  1214. else if(Bind[r1].id == 'R7_RelayStatus'){
  1215. var switchReal = control.switchReal(Bind[r1].current_value,lamp,6)
  1216. }
  1217. else if(Bind[r1].id == 'R8_RelayStatus'){
  1218. var switchReal = control.switchReal(Bind[r1].current_value,lamp,7)
  1219. }
  1220. else if(Bind[r1].id == 'R9_RelayStatus'){
  1221. var switchReal = control.switchReal(Bind[r1].current_value,lamp,8)
  1222. }
  1223. else if(Bind[r1].id == 'R10_RelayStatus'){
  1224. var switchReal = control.switchReal(Bind[r1].current_value,lamp,9)
  1225. }
  1226. else if(Bind[r1].id == 'R11_RelayStatus'){
  1227. var switchReal = control.switchReal(Bind[r1].current_value,lamp,10)
  1228. }
  1229. else if(Bind[r1].id == 'R12_RelayStatus'){
  1230. var switchReal = control.switchReal(Bind[r1].current_value,lamp,11)
  1231. }
  1232. }
  1233. wx.setStorageSync('lamp',switchReal)
  1234. that.setData({lamp:wx.getStorageSync('lamp')})
  1235. }
  1236. })
  1237. },2000)
  1238. }
  1239. else{
  1240. wx.showModal({
  1241. title:'错误',
  1242. content:res.errMsg,
  1243. showCancel:false
  1244. })
  1245. }
  1246. })
  1247. },
  1248. //分组下拉
  1249. changegroup:function(e){
  1250. var that = this
  1251. that.setData({groupIndex:e.detail.value})
  1252. var groupName = that.data.groupName[that.data.groupIndex]//选择的分组名称
  1253. wx.setStorageSync('groupName',groupName)//将修改的分组名称放入缓存
  1254. //获取设备名称下拉
  1255. var group = wx.getStorageSync('group')
  1256. var deviceName = that.data.deviceName
  1257. var deviceName = []
  1258. for(let i = 0;i < group.length;i++){
  1259. if(group[i].groupName == groupName){
  1260. if(group[i].devices != null && group[i].devices.length != 0){
  1261. wx.showToast({
  1262. title:'刷新中',
  1263. icon:'loading',
  1264. duration: 2000
  1265. })
  1266. for(let j = 0;j < group[i].devices.length;j++){
  1267. deviceName.push(group[i].devices[j].deviceName)
  1268. }
  1269. that.setData({deviceName:deviceName})
  1270. var groupNames = that.data.groupName[that.data.groupIndex]//当前分组
  1271. var deviceNames = that.data.deviceName[that.data.deviceIndex]//当前设备
  1272. wx.setStorageSync('deviceName',deviceNames)//将修改设备名放入缓存
  1273. //获取设备信息
  1274. for(let d = 0;d < group.length;d++){
  1275. if(groupNames == group[d].groupName){
  1276. var Groupno = group[d].id//分组id
  1277. for(let g = 0;g < group[d].devices.length;g++){
  1278. if(deviceNames == group[d].devices[g].deviceName){
  1279. //旧设备信息
  1280. if(group[d].devices[g].module == 0){
  1281. var id = group[d].devices[g].id
  1282. var circuitNum = group[d].devices[g].circuitNum
  1283. var circuits = group[d].devices[g].circuits
  1284. var deviceId = group[d].devices[g].deviceId
  1285. var deviceKey = group[d].devices[g].deviceKey
  1286. var moudule = group[d].devices[g].module
  1287. that.setData({play:0})
  1288. }
  1289. //新设备信息
  1290. else{
  1291. var circuitNum = group[d].devices[g].circuitNum
  1292. var circuits = group[d].devices[g].circuits
  1293. var moudule = group[d].devices[g].module
  1294. var id = group[d].devices[g].id
  1295. that.setData({play:1})
  1296. }
  1297. }
  1298. }
  1299. }
  1300. }
  1301. that.setData({moudule:moudule,id:id,loops:circuitNum})
  1302. //灯泡图标缓存
  1303. wx.setStorageSync('lamp', [])
  1304. var lamp = wx.getStorageSync('lamp')
  1305. for(let h = 0;h < 12;h++){
  1306. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1307. }
  1308. //旧设备
  1309. if(moudule == 0){
  1310. //重置状态
  1311. let combination = control.Refresh()
  1312. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1313. //..............................
  1314. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1315. var Circuits = circuits.split(',')
  1316. let loop = that.data.loop
  1317. for(let vr = 0;vr < circuitNum;vr++){
  1318. loop[vr] = Circuits[vr]
  1319. }
  1320. that.setData({loop:loop})
  1321. postData(deviceId,deviceKey,{"Reflash":3}).then(res=>{
  1322. if(res.data.errno == 0){
  1323. setTimeout(function(){
  1324. getBasic(deviceId,deviceKey).then(ever=>{
  1325. if(ever.data.errno == 0){
  1326. if (ever.data.data.online == true) {
  1327. that.setData({status:"在线"})
  1328. }
  1329. else{
  1330. that.setData({status:"离线"})
  1331. }
  1332. getData(deviceId,deviceKey).then(ver =>{
  1333. if(ver.data.errno == 0){
  1334. let data = ver.data.data
  1335. for(let i = 0;i < data.length;i++){
  1336. if (data[i].id == "RelayCount") {
  1337. var quantity = data[i].current_value
  1338. that.setData({loops:quantity})
  1339. }
  1340. else if(data[i].id == "ManualStatus"){
  1341. if(data[i].current_value == 0){
  1342. that.setData({Modecontrol:"自动"})
  1343. }
  1344. else{
  1345. that.setData({Modecontrol:"手动"})
  1346. }
  1347. }
  1348. //回路1
  1349. else if(data[i].id == 'R1_RelayStatus'){
  1350. let police = data[i].current_value
  1351. var lamps = control.switchReal(police,lamp,0)
  1352. }
  1353. else if(data[i].id == 'R2_RelayStatus'){
  1354. let police = data[i].current_value
  1355. var lamps = control.switchReal(police,lamp,1)
  1356. }
  1357. else if(data[i].id == 'R3_RelayStatus'){
  1358. let police = data[i].current_value
  1359. var lamps = control.switchReal(police,lamp,2)
  1360. }
  1361. else if(data[i].id == 'R4_RelayStatus'){
  1362. let police = data[i].current_value
  1363. var lamps = control.switchReal(police,lamp,3)
  1364. }
  1365. else if(data[i].id == 'R5_RelayStatus'){
  1366. let police = data[i].current_value
  1367. var lamps = control.switchReal(police,lamp,4)
  1368. }
  1369. else if(data[i].id == 'R6_RelayStatus'){
  1370. let police = data[i].current_value
  1371. var lamps = control.switchReal(police,lamp,5)
  1372. }
  1373. else if(data[i].id == 'R7_RelayStatus'){
  1374. let police = data[i].current_value
  1375. var lamps = control.switchReal(police,lamp,6)
  1376. }
  1377. else if(data[i].id == 'R8_RelayStatus'){
  1378. let police = data[i].current_value
  1379. var lamps = control.switchReal(police,lamp,7)
  1380. }
  1381. else if(data[i].id == 'R9_RelayStatus'){
  1382. let police = data[i].current_value
  1383. var lamps = control.switchReal(police,lamp,8)
  1384. }
  1385. else if(data[i].id == 'R10_RelayStatus'){
  1386. let police = data[i].current_value
  1387. var lamps = control.switchReal(police,lamp,9)
  1388. }
  1389. else if(data[i].id == 'R11_RelayStatus'){
  1390. let police = data[i].current_value
  1391. var lamps = control.switchReal(police,lamp,10)
  1392. }
  1393. else if(data[i].id == 'R12_RelayStatus'){
  1394. let police = data[i].current_value
  1395. var lamps = control.switchReal(police,lamp,11)
  1396. }
  1397. wx.setStorageSync('lamp', lamps)
  1398. that.setData({lamp:wx.getStorageSync('lamp')})
  1399. }
  1400. for (let i = 0;i < quantity; i++) {
  1401. var Loop = that.data.Loop
  1402. var color = that.data.Color
  1403. var size = that.data.Size
  1404. var Address = that.data.address
  1405. Loop[i] = that.data.loop[i]
  1406. color[i] = "black"
  1407. size[i] = "calc(100vw * 16/375)",
  1408. Address[i] = "/image/modify.png"
  1409. that.setData({Color:color,Size:size,address:Address,Loop:Loop})
  1410. }
  1411. }
  1412. else{
  1413. wx.showModal({
  1414. title:'错误',
  1415. content:ever.data.error,
  1416. showCancel:false
  1417. })
  1418. }
  1419. })
  1420. }
  1421. else{
  1422. wx.showModal({
  1423. title:'错误',
  1424. content:ever.data.error,
  1425. showCancel:false
  1426. })
  1427. }
  1428. })
  1429. },2000)
  1430. }
  1431. else{
  1432. wx.showModal({
  1433. title:'错误',
  1434. content:res.data.error,
  1435. showCancel:false
  1436. })
  1437. that.setData({status:"离线"})
  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. wx.showModal({
  1529. title:'提示',
  1530. content:'工程中不存在设备',
  1531. showCancel:false
  1532. })
  1533. that.setData({status:'离线'})
  1534. wx.setStorageSync('deviceName','')//清空设备名称缓存
  1535. }
  1536. }
  1537. }
  1538. },
  1539. //设备下拉
  1540. changedevice:function(e){
  1541. wx.showToast({
  1542. title:'刷新中',
  1543. icon:'loading',
  1544. duration: 2000
  1545. })
  1546. var that = this
  1547. that.setData({deviceIndex:e.detail.value})
  1548. var group = wx.getStorageSync('group')
  1549. var groupName = that.data.groupName[that.data.groupIndex]
  1550. var deviceName = that.data.deviceName[that.data.deviceIndex]
  1551. wx.setStorageSync('groupName', groupName)//分组名称
  1552. wx.setStorageSync('deviceName', deviceName)//设备名称
  1553. for (let index = 0; index < group.length; index++) {
  1554. if(groupName == group[index].groupName){
  1555. var Groupno = group[index].id//分组id
  1556. for(let t = 0;t < group[index].devices.length;t++){
  1557. if(deviceName == group[index].devices[t].deviceName){
  1558. //旧设备信息
  1559. if(group[index].devices[t].module == 0){
  1560. var id = group[index].devices[t].id
  1561. var circuitNum = group[index].devices[t].circuitNum
  1562. var circuits = group[index].devices[t].circuits
  1563. var deviceId = group[index].devices[t].deviceId
  1564. var deviceKey = group[index].devices[t].deviceKey
  1565. var moudule = group[index].devices[t].module
  1566. that.setData({play:0})
  1567. }
  1568. //新设备信息
  1569. else{
  1570. var circuitNum = group[index].devices[t].circuitNum
  1571. var circuits = group[index].devices[t].circuits
  1572. var moudule = group[index].devices[t].module
  1573. var id = group[index].devices[t].id
  1574. that.setData({play:1})
  1575. }
  1576. }
  1577. }
  1578. }
  1579. }
  1580. that.setData({moudule:moudule,id:id,loops:circuitNum})
  1581. //灯泡图标缓存
  1582. wx.setStorageSync('lamp', [])
  1583. var lamp = wx.getStorageSync('lamp')
  1584. for(let h = 0;h < 12;h++){
  1585. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1586. }
  1587. //旧设备
  1588. if(moudule == 0){
  1589. //重置状态
  1590. let combination = control.Refresh()
  1591. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1592. //..............................
  1593. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1594. var Circuits = circuits.split(',')
  1595. let loop = that.data.loop
  1596. for(let vr = 0;vr < circuitNum;vr++){
  1597. loop[vr] = Circuits[vr]
  1598. }
  1599. that.setData({loop:loop})
  1600. postData(deviceId,deviceKey,{"Reflash":3}).then(res=>{
  1601. if(res.data.errno == 0){
  1602. setTimeout(function(){
  1603. getBasic(deviceId,deviceKey).then(ever=>{
  1604. if(ever.data.errno == 0){
  1605. if (ever.data.data.online == true) {
  1606. that.setData({status:"在线"})
  1607. }
  1608. else{
  1609. that.setData({status:"离线"})
  1610. }
  1611. getData(deviceId,deviceKey).then(ver =>{
  1612. if(ver.data.errno == 0){
  1613. let data = ver.data.data
  1614. for(let i = 0;i < data.length;i++){
  1615. if (data[i].id == "RelayCount") {
  1616. var quantity = data[i].current_value
  1617. that.setData({loops:quantity})
  1618. }
  1619. else if(data[i].id == "ManualStatus"){
  1620. if(data[i].current_value == 0){
  1621. that.setData({Modecontrol:"自动"})
  1622. }
  1623. else{
  1624. that.setData({Modecontrol:"手动"})
  1625. }
  1626. }
  1627. //回路1
  1628. else if(data[i].id == 'R1_RelayStatus'){
  1629. let police = data[i].current_value
  1630. var lamps = control.switchReal(police,lamp,0)
  1631. }
  1632. else if(data[i].id == 'R2_RelayStatus'){
  1633. let police = data[i].current_value
  1634. var lamps = control.switchReal(police,lamp,1)
  1635. }
  1636. else if(data[i].id == 'R3_RelayStatus'){
  1637. let police = data[i].current_value
  1638. var lamps = control.switchReal(police,lamp,2)
  1639. }
  1640. else if(data[i].id == 'R4_RelayStatus'){
  1641. let police = data[i].current_value
  1642. var lamps = control.switchReal(police,lamp,3)
  1643. }
  1644. else if(data[i].id == 'R5_RelayStatus'){
  1645. let police = data[i].current_value
  1646. var lamps = control.switchReal(police,lamp,4)
  1647. }
  1648. else if(data[i].id == 'R6_RelayStatus'){
  1649. let police = data[i].current_value
  1650. var lamps = control.switchReal(police,lamp,5)
  1651. }
  1652. else if(data[i].id == 'R7_RelayStatus'){
  1653. let police = data[i].current_value
  1654. var lamps = control.switchReal(police,lamp,6)
  1655. }
  1656. else if(data[i].id == 'R8_RelayStatus'){
  1657. let police = data[i].current_value
  1658. var lamps = control.switchReal(police,lamp,7)
  1659. }
  1660. else if(data[i].id == 'R9_RelayStatus'){
  1661. let police = data[i].current_value
  1662. var lamps = control.switchReal(police,lamp,8)
  1663. }
  1664. else if(data[i].id == 'R10_RelayStatus'){
  1665. let police = data[i].current_value
  1666. var lamps = control.switchReal(police,lamp,9)
  1667. }
  1668. else if(data[i].id == 'R11_RelayStatus'){
  1669. let police = data[i].current_value
  1670. var lamps = control.switchReal(police,lamp,10)
  1671. }
  1672. else if(data[i].id == 'R12_RelayStatus'){
  1673. let police = data[i].current_value
  1674. var lamps = control.switchReal(police,lamp,11)
  1675. }
  1676. wx.setStorageSync('lamp', lamps)
  1677. that.setData({lamp:wx.getStorageSync('lamp')})
  1678. }
  1679. for (let i = 0;i < quantity; i++) {
  1680. var Loop = that.data.Loop
  1681. var color = that.data.Color
  1682. var size = that.data.Size
  1683. var Address = that.data.address
  1684. Loop[i] = that.data.loop[i]
  1685. color[i] = "black"
  1686. size[i] = "calc(100vw * 16/375)",
  1687. Address[i] = "/image/modify.png"
  1688. that.setData({
  1689. Color:color,
  1690. Size:size,
  1691. address:Address,
  1692. Loop:Loop
  1693. })
  1694. }
  1695. }
  1696. else{
  1697. wx.showModal({
  1698. title:'错误',
  1699. content:ever.data.error,
  1700. showCancel:false
  1701. })
  1702. }
  1703. })
  1704. }
  1705. else{
  1706. wx.showModal({
  1707. title:'错误',
  1708. content:ever.data.error,
  1709. showCancel:false
  1710. })
  1711. }
  1712. })
  1713. },2000)
  1714. }
  1715. else{
  1716. wx.showModal({
  1717. title:'错误',
  1718. content:res.data.error,
  1719. showCancel:false
  1720. })
  1721. that.setData({status:"离线"})
  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. if(res.data.errno == 0){
  1818. that.setData({Modecontrol:'手动'})
  1819. }
  1820. else{
  1821. wx.showModal({
  1822. title:'错误',
  1823. content:res.data.error,
  1824. showCancel:false
  1825. })
  1826. }
  1827. })
  1828. },
  1829. //自动
  1830. automatic:function(e){
  1831. var that = this
  1832. var deviceId = that.data.deviceId
  1833. var deviceKey = that.data.deviceKey
  1834. postData(deviceId,deviceKey,{'ManualStatus':1,'Reflash':3}).then(res => {
  1835. if(res.data.errno == 0){
  1836. that.setData({Modecontrol:'自动'})
  1837. }
  1838. else{
  1839. wx.showModal({
  1840. title:'错误',
  1841. content:res.data.error,
  1842. showCancel:false
  1843. })
  1844. }
  1845. })
  1846. },
  1847. /**
  1848. * 生命周期函数--监听页面加载
  1849. */
  1850. onLoad(options) {
  1851. },
  1852. /**
  1853. * 生命周期函数--监听页面初次渲染完成
  1854. */
  1855. onReady() {
  1856. },
  1857. /**
  1858. * 生命周期函数--监听页面显示
  1859. */
  1860. onShow() {
  1861. wx.showToast({
  1862. title:'加载中',
  1863. icon:'loading',
  1864. duration: 1500
  1865. })
  1866. var that = this
  1867. var group = wx.getStorageSync('group')
  1868. //获取分组下拉
  1869. var groups = []
  1870. for(let i = 0;i < group.length;i++){
  1871. groups.push(group[i].groupName)
  1872. }
  1873. that.setData({
  1874. groupName:groups
  1875. })
  1876. //改变分组下标
  1877. if(wx.getStorageSync('groupName') != ''){
  1878. let GroupName = that.data.groupName
  1879. for(let n = 0;n < GroupName.length;n++){
  1880. if(GroupName[n] == wx.getStorageSync('groupName')){
  1881. that.setData({
  1882. groupIndex:n
  1883. })
  1884. }
  1885. }
  1886. }
  1887. else{
  1888. wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])
  1889. }
  1890. //获取设备下拉
  1891. var device = []
  1892. var groupName = that.data.groupName
  1893. var groupIndex = that.data.groupIndex
  1894. for(let j = 0;j < group.length;j++){
  1895. if(groupName[groupIndex] == group[j].groupName){
  1896. if(group[j].devices != null && group[j].devices.length != 0){
  1897. for(let p = 0;p < group[j].devices.length;p++){
  1898. device.push(group[j].devices[p].deviceName)
  1899. }
  1900. that.setData({deviceName:device})
  1901. //改变设备下标
  1902. if(wx.getStorageSync('deviceName') != ''){
  1903. var DeviceName = that.data.deviceName
  1904. for(let m = 0;m < DeviceName.length;m++){
  1905. if(DeviceName[m] == wx.getStorageSync('deviceName')){
  1906. that.setData({deviceIndex:m})
  1907. }
  1908. }
  1909. }
  1910. else{
  1911. wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex])
  1912. }
  1913. //根据分组名和设备信息
  1914. var groupNames = that.data.groupName[that.data.groupIndex]
  1915. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  1916. for(let d = 0;d < group.length;d++){
  1917. if(groupNames == group[d].groupName){
  1918. var Groupno = group[d].id//分组id
  1919. for(let g = 0;g < group[d].devices.length;g++){
  1920. if(deviceNames == group[d].devices[g].deviceName){
  1921. //旧设备信息
  1922. if(group[d].devices[g].module == 0){
  1923. var id = group[d].devices[g].id//设备的序号
  1924. var circuitNum = group[d].devices[g].circuitNum
  1925. var circuits = group[d].devices[g].circuits
  1926. var deviceId = group[d].devices[g].deviceId
  1927. var deviceKey = group[d].devices[g].deviceKey
  1928. var moudule = group[d].devices[g].module
  1929. that.setData({play:0})
  1930. }
  1931. //新设备信息
  1932. else{
  1933. var circuitNum = group[d].devices[g].circuitNum
  1934. var circuits = group[d].devices[g].circuits
  1935. var moudule = group[d].devices[g].module
  1936. var id = group[d].devices[g].id//设备的序号
  1937. that.setData({play:1})
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. //用于区分两种设备以及记录回路数量
  1944. that.setData({moudule:moudule,id:id,loops:circuitNum})
  1945. //灯泡图标缓存
  1946. wx.setStorageSync('lamp', [])
  1947. var lamp = wx.getStorageSync('lamp')
  1948. for(let h = 0;h < 12;h++){
  1949. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1950. }
  1951. //旧设备
  1952. if(moudule == 0){
  1953. //重置状态
  1954. let combination = control.Refresh()
  1955. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1956. //..............................
  1957. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1958. var Circuits = circuits.split(',')
  1959. let loop = that.data.loop
  1960. for(let vr = 0;vr < circuitNum;vr++){
  1961. loop[vr] = Circuits[vr]
  1962. }
  1963. that.setData({loop:loop})
  1964. postData(deviceId,deviceKey,{"Reflash":3}).then(res=>{
  1965. if(res.data.errno == 0){
  1966. setTimeout(function(){
  1967. getBasic(deviceId,deviceKey).then(ever=>{
  1968. if(ever.data.errno == 0){
  1969. if (ever.data.data.online == true) {
  1970. that.setData({status:"在线"})
  1971. }
  1972. else{
  1973. that.setData({status:"离线"})
  1974. }
  1975. getData(deviceId,deviceKey).then(ver =>{
  1976. if(ver.data.errno == 0){
  1977. let data = ver.data.data
  1978. for(let i = 0;i < data.length;i++){
  1979. if (data[i].id == "RelayCount") {
  1980. var quantity = data[i].current_value
  1981. that.setData({loops:quantity})
  1982. }
  1983. else if(data[i].id == "ManualStatus"){
  1984. if(data[i].current_value == 0){
  1985. that.setData({Modecontrol:"自动"})
  1986. }
  1987. else{
  1988. that.setData({Modecontrol:"手动"})
  1989. }
  1990. }
  1991. //回路1
  1992. else if(data[i].id == 'R1_RelayStatus'){
  1993. let police = data[i].current_value
  1994. var lamps = control.switchReal(police,lamp,0)
  1995. }
  1996. else if(data[i].id == 'R2_RelayStatus'){
  1997. let police = data[i].current_value
  1998. var lamps = control.switchReal(police,lamp,1)
  1999. }
  2000. else if(data[i].id == 'R3_RelayStatus'){
  2001. let police = data[i].current_value
  2002. var lamps = control.switchReal(police,lamp,2)
  2003. }
  2004. else if(data[i].id == 'R4_RelayStatus'){
  2005. let police = data[i].current_value
  2006. var lamps = control.switchReal(police,lamp,3)
  2007. }
  2008. else if(data[i].id == 'R5_RelayStatus'){
  2009. let police = data[i].current_value
  2010. var lamps = control.switchReal(police,lamp,4)
  2011. }
  2012. else if(data[i].id == 'R6_RelayStatus'){
  2013. let police = data[i].current_value
  2014. var lamps = control.switchReal(police,lamp,5)
  2015. }
  2016. else if(data[i].id == 'R7_RelayStatus'){
  2017. let police = data[i].current_value
  2018. var lamps = control.switchReal(police,lamp,6)
  2019. }
  2020. else if(data[i].id == 'R8_RelayStatus'){
  2021. let police = data[i].current_value
  2022. var lamps = control.switchReal(police,lamp,7)
  2023. }
  2024. else if(data[i].id == 'R9_RelayStatus'){
  2025. let police = data[i].current_value
  2026. var lamps = control.switchReal(police,lamp,8)
  2027. }
  2028. else if(data[i].id == 'R10_RelayStatus'){
  2029. let police = data[i].current_value
  2030. var lamps = control.switchReal(police,lamp,9)
  2031. }
  2032. else if(data[i].id == 'R11_RelayStatus'){
  2033. let police = data[i].current_value
  2034. var lamps = control.switchReal(police,lamp,10)
  2035. }
  2036. else if(data[i].id == 'R12_RelayStatus'){
  2037. let police = data[i].current_value
  2038. var lamps = control.switchReal(police,lamp,11)
  2039. }
  2040. wx.setStorageSync('lamp', lamps)
  2041. that.setData({lamp:wx.getStorageSync('lamp')})
  2042. }
  2043. for (let i = 0;i < quantity; i++) {
  2044. var Loop = that.data.Loop
  2045. var color = that.data.Color
  2046. var size = that.data.Size
  2047. var Address = that.data.address
  2048. Loop[i] = that.data.loop[i]
  2049. color[i] = "black"
  2050. size[i] = "calc(100vw * 16/375)",
  2051. Address[i] = "/image/modify.png"
  2052. that.setData({
  2053. Color:color,
  2054. Size:size,
  2055. address:Address,
  2056. Loop:Loop
  2057. })
  2058. }
  2059. }
  2060. else{
  2061. wx.showModal({
  2062. title:'错误',
  2063. content:ever.data.error,
  2064. showCancel:false
  2065. })
  2066. }
  2067. })
  2068. }
  2069. else{
  2070. wx.showModal({
  2071. title:'错误',
  2072. content:ever.data.error,
  2073. showCancel:false
  2074. })
  2075. }
  2076. })
  2077. },2000)
  2078. }
  2079. else{
  2080. wx.showModal({
  2081. title:'错误',
  2082. content:res.data.error,
  2083. showCancel:false
  2084. })
  2085. }
  2086. })
  2087. }
  2088. //新设备
  2089. else{
  2090. //重置状态
  2091. let combination = control.Refresh()
  2092. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  2093. //..............................
  2094. //获取设备在线状态
  2095. getDevice(id,Groupno).then(dve =>{
  2096. if(dve.data.code == 0){
  2097. var state = dve.data.data
  2098. switch(state[0].status){
  2099. case 0:
  2100. that.setData({status:'离线'})
  2101. break;
  2102. case 1:
  2103. that.setData({status:'在线'})
  2104. break;
  2105. }
  2106. }
  2107. else{
  2108. wx.showModal({
  2109. title: '错误',
  2110. content: dve.data.msg,
  2111. showCancel:false
  2112. })
  2113. }
  2114. })
  2115. var Circuits = circuits.split(',')
  2116. let loop = that.data.loop
  2117. for(let vr = 0;vr < circuitNum;vr++){
  2118. loop[vr] = Circuits[vr]
  2119. }
  2120. that.setData({loop:loop})
  2121. getloop(id).then(res =>{
  2122. if(res.data.code == 0){
  2123. let loopState = res.data.data
  2124. //loopState.length可以视为回路数量
  2125. for(let n = 0;n < loopState.length;n++){
  2126. var Loop = that.data.Loop
  2127. var color = that.data.Color
  2128. var size = that.data.Size
  2129. var Address = that.data.address
  2130. Loop[n] = that.data.Loop[n]
  2131. color[n] = "black"
  2132. size[n] = "calc(100vw * 16/375)",
  2133. Address[n] = "/image/modify.png"
  2134. that.setData({Color:color,Size:size,address:Address,Loop:Loop})
  2135. }
  2136. }
  2137. else{
  2138. wx.showModal({
  2139. title: '错误',
  2140. content: res.data.msg,
  2141. showCancel:false
  2142. })
  2143. }
  2144. })
  2145. loopSwitchstate(id,'1,2,3,4,5,6,7,8').then(ret=>{
  2146. if(ret.data.code == 0){
  2147. var relay = ret.data.data
  2148. for(let e = 0;e < relay.length;e++){
  2149. if(relay[e].status == 1){
  2150. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2151. }
  2152. else if(relay[e].status == 0){
  2153. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2154. }
  2155. else{
  2156. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2157. }
  2158. }
  2159. }
  2160. else{
  2161. wx.showModal({
  2162. title:'错误',
  2163. content:ret.data.msg,
  2164. showCancel:false
  2165. })
  2166. }
  2167. })
  2168. setTimeout(function(){
  2169. wx.setStorageSync('lamp',lamp)
  2170. that.setData({lamp:wx.getStorageSync('lamp')})
  2171. },2000)
  2172. }
  2173. }
  2174. else {
  2175. that.setData({status:'离线',deviceName:[]})
  2176. }
  2177. }
  2178. }
  2179. },
  2180. /**
  2181. * 生命周期函数--监听页面隐藏
  2182. */
  2183. onHide() {
  2184. },
  2185. /**
  2186. * 生命周期函数--监听页面卸载
  2187. */
  2188. onUnload() {
  2189. },
  2190. /**
  2191. * 页面相关事件处理函数--监听用户下拉动作
  2192. */
  2193. onPullDownRefresh() {
  2194. },
  2195. /**
  2196. * 页面上拉触底事件的处理函数
  2197. */
  2198. onReachBottom() {
  2199. },
  2200. /**
  2201. * 用户点击右上角分享
  2202. */
  2203. onShareAppMessage() {
  2204. }
  2205. })