control.js 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  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. }
  1438. })
  1439. }
  1440. //新设备
  1441. else{
  1442. //重置状态
  1443. let combination = control.Refresh()
  1444. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1445. //..............................
  1446. //获取设备在线状态
  1447. getDevice(id,Groupno).then(dve =>{
  1448. if(dve.data.code == 0){
  1449. var state = dve.data.data
  1450. switch(state[0].status){
  1451. case 0:
  1452. that.setData({status:'离线'})
  1453. break;
  1454. case 1:
  1455. that.setData({status:'在线'})
  1456. break;
  1457. }
  1458. }
  1459. else{
  1460. wx.showModal({
  1461. title: '错误',
  1462. content: dve.data.msg,
  1463. showCancel:false
  1464. })
  1465. }
  1466. })
  1467. var Circuits = circuits.split(',')
  1468. let loop = that.data.loop
  1469. for(let vr = 0;vr < circuitNum;vr++){
  1470. loop[vr] = Circuits[vr]
  1471. }
  1472. that.setData({loop:loop})
  1473. getloop(id).then(res =>{
  1474. if(res.data.code == 0){
  1475. let loopState = res.data.data
  1476. //loopState.length可以视为回路数量
  1477. for(let n = 0;n < loopState.length;n++){
  1478. var Loop = that.data.Loop
  1479. var color = that.data.Color
  1480. var size = that.data.Size
  1481. var Address = that.data.address
  1482. Loop[n] = that.data.Loop[n]
  1483. color[n] = "black"
  1484. size[n] = "calc(100vw * 16/375)",
  1485. Address[n] = "/image/modify.png"
  1486. that.setData({Color:color,Size:size,address:Address,Loop:Loop})
  1487. }
  1488. }
  1489. else{
  1490. wx.showModal({
  1491. title: '错误',
  1492. content: res.data.msg,
  1493. showCancel:false
  1494. })
  1495. }
  1496. })
  1497. loopSwitchstate(id,'1,2,3,4,5,6,7,8').then(res=>{
  1498. if(res.data.code == 0){
  1499. var relay = res.data.data
  1500. for(let e = 0;e < relay.length;e++){
  1501. if(relay[e].status == 1){
  1502. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1503. }
  1504. else if(relay[e].status == 0){
  1505. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1506. }
  1507. else{
  1508. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1509. }
  1510. }
  1511. }
  1512. else{
  1513. wx.showModal({
  1514. title:'错误',
  1515. content:res.data.msg,
  1516. showCancel:false
  1517. })
  1518. }
  1519. })
  1520. setTimeout(function(){
  1521. wx.setStorageSync('lamp',lamp)
  1522. that.setData({lamp:wx.getStorageSync('lamp')})
  1523. },2000)
  1524. }
  1525. }
  1526. else{
  1527. wx.showModal({
  1528. title:'提示',
  1529. content:'工程中不存在设备',
  1530. showCancel:false
  1531. })
  1532. that.setData({status:'离线'})
  1533. wx.setStorageSync('deviceName','')//清空设备名称缓存
  1534. }
  1535. }
  1536. }
  1537. },
  1538. //设备下拉
  1539. changedevice:function(e){
  1540. wx.showToast({
  1541. title:'刷新中',
  1542. icon:'loading',
  1543. duration: 2000
  1544. })
  1545. var that = this
  1546. that.setData({deviceIndex:e.detail.value})
  1547. var group = wx.getStorageSync('group')
  1548. var groupName = that.data.groupName[that.data.groupIndex]
  1549. var deviceName = that.data.deviceName[that.data.deviceIndex]
  1550. wx.setStorageSync('groupName', groupName)//分组名称
  1551. wx.setStorageSync('deviceName', deviceName)//设备名称
  1552. for (let index = 0; index < group.length; index++) {
  1553. if(groupName == group[index].groupName){
  1554. var Groupno = group[index].id//分组id
  1555. for(let t = 0;t < group[index].devices.length;t++){
  1556. if(deviceName == group[index].devices[t].deviceName){
  1557. //旧设备信息
  1558. if(group[index].devices[t].module == 0){
  1559. var id = group[index].devices[t].id
  1560. var circuitNum = group[index].devices[t].circuitNum
  1561. var circuits = group[index].devices[t].circuits
  1562. var deviceId = group[index].devices[t].deviceId
  1563. var deviceKey = group[index].devices[t].deviceKey
  1564. var moudule = group[index].devices[t].module
  1565. that.setData({play:0})
  1566. }
  1567. //新设备信息
  1568. else{
  1569. var circuitNum = group[index].devices[t].circuitNum
  1570. var circuits = group[index].devices[t].circuits
  1571. var moudule = group[index].devices[t].module
  1572. var id = group[index].devices[t].id
  1573. that.setData({play:1})
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. that.setData({moudule:moudule,id:id,loops:circuitNum})
  1580. //灯泡图标缓存
  1581. wx.setStorageSync('lamp', [])
  1582. var lamp = wx.getStorageSync('lamp')
  1583. for(let h = 0;h < 12;h++){
  1584. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1585. }
  1586. //旧设备
  1587. if(moudule == 0){
  1588. //重置状态
  1589. let combination = control.Refresh()
  1590. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1591. //..............................
  1592. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1593. var Circuits = circuits.split(',')
  1594. let loop = that.data.loop
  1595. for(let vr = 0;vr < circuitNum;vr++){
  1596. loop[vr] = Circuits[vr]
  1597. }
  1598. that.setData({loop:loop})
  1599. postData(deviceId,deviceKey,{"Reflash":3}).then(res=>{
  1600. if(res.data.errno == 0){
  1601. setTimeout(function(){
  1602. getBasic(deviceId,deviceKey).then(ever=>{
  1603. if(ever.data.errno == 0){
  1604. if (ever.data.data.online == true) {
  1605. that.setData({status:"在线"})
  1606. }
  1607. else{
  1608. that.setData({status:"离线"})
  1609. }
  1610. getData(deviceId,deviceKey).then(ver =>{
  1611. if(ver.data.errno == 0){
  1612. let data = ver.data.data
  1613. for(let i = 0;i < data.length;i++){
  1614. if (data[i].id == "RelayCount") {
  1615. var quantity = data[i].current_value
  1616. that.setData({loops:quantity})
  1617. }
  1618. else if(data[i].id == "ManualStatus"){
  1619. if(data[i].current_value == 0){
  1620. that.setData({Modecontrol:"自动"})
  1621. }
  1622. else{
  1623. that.setData({Modecontrol:"手动"})
  1624. }
  1625. }
  1626. //回路1
  1627. else if(data[i].id == 'R1_RelayStatus'){
  1628. let police = data[i].current_value
  1629. var lamps = control.switchReal(police,lamp,0)
  1630. }
  1631. else if(data[i].id == 'R2_RelayStatus'){
  1632. let police = data[i].current_value
  1633. var lamps = control.switchReal(police,lamp,1)
  1634. }
  1635. else if(data[i].id == 'R3_RelayStatus'){
  1636. let police = data[i].current_value
  1637. var lamps = control.switchReal(police,lamp,2)
  1638. }
  1639. else if(data[i].id == 'R4_RelayStatus'){
  1640. let police = data[i].current_value
  1641. var lamps = control.switchReal(police,lamp,3)
  1642. }
  1643. else if(data[i].id == 'R5_RelayStatus'){
  1644. let police = data[i].current_value
  1645. var lamps = control.switchReal(police,lamp,4)
  1646. }
  1647. else if(data[i].id == 'R6_RelayStatus'){
  1648. let police = data[i].current_value
  1649. var lamps = control.switchReal(police,lamp,5)
  1650. }
  1651. else if(data[i].id == 'R7_RelayStatus'){
  1652. let police = data[i].current_value
  1653. var lamps = control.switchReal(police,lamp,6)
  1654. }
  1655. else if(data[i].id == 'R8_RelayStatus'){
  1656. let police = data[i].current_value
  1657. var lamps = control.switchReal(police,lamp,7)
  1658. }
  1659. else if(data[i].id == 'R9_RelayStatus'){
  1660. let police = data[i].current_value
  1661. var lamps = control.switchReal(police,lamp,8)
  1662. }
  1663. else if(data[i].id == 'R10_RelayStatus'){
  1664. let police = data[i].current_value
  1665. var lamps = control.switchReal(police,lamp,9)
  1666. }
  1667. else if(data[i].id == 'R11_RelayStatus'){
  1668. let police = data[i].current_value
  1669. var lamps = control.switchReal(police,lamp,10)
  1670. }
  1671. else if(data[i].id == 'R12_RelayStatus'){
  1672. let police = data[i].current_value
  1673. var lamps = control.switchReal(police,lamp,11)
  1674. }
  1675. wx.setStorageSync('lamp', lamps)
  1676. that.setData({lamp:wx.getStorageSync('lamp')})
  1677. }
  1678. for (let i = 0;i < quantity; i++) {
  1679. var Loop = that.data.Loop
  1680. var color = that.data.Color
  1681. var size = that.data.Size
  1682. var Address = that.data.address
  1683. Loop[i] = that.data.loop[i]
  1684. color[i] = "black"
  1685. size[i] = "calc(100vw * 16/375)",
  1686. Address[i] = "/image/modify.png"
  1687. that.setData({
  1688. Color:color,
  1689. Size:size,
  1690. address:Address,
  1691. Loop:Loop
  1692. })
  1693. }
  1694. }
  1695. else{
  1696. wx.showModal({
  1697. title:'错误',
  1698. content:ever.data.error,
  1699. showCancel:false
  1700. })
  1701. }
  1702. })
  1703. }
  1704. else{
  1705. wx.showModal({
  1706. title:'错误',
  1707. content:ever.data.error,
  1708. showCancel:false
  1709. })
  1710. }
  1711. })
  1712. },2000)
  1713. }
  1714. else{
  1715. wx.showModal({
  1716. title:'错误',
  1717. content:res.data.error,
  1718. showCancel:false
  1719. })
  1720. }
  1721. })
  1722. }
  1723. //新设备
  1724. else{
  1725. //重置状态
  1726. let combination = control.Refresh()
  1727. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1728. //..............................
  1729. //获取设备在线状态
  1730. getDevice(id,Groupno).then(dve =>{
  1731. if(dve.data.code == 0){
  1732. var state = dve.data.data
  1733. switch(state[0].status){
  1734. case 0:
  1735. that.setData({status:'离线'})
  1736. break;
  1737. case 1:
  1738. that.setData({status:'在线'})
  1739. break;
  1740. }
  1741. }
  1742. else{
  1743. wx.showModal({
  1744. title: '错误',
  1745. content: dve.data.msg,
  1746. showCancel:false
  1747. })
  1748. }
  1749. })
  1750. var Circuits = circuits.split(',')
  1751. let loop = that.data.loop
  1752. for(let vr = 0;vr < circuitNum;vr++){
  1753. loop[vr] = Circuits[vr]
  1754. }
  1755. that.setData({loop:loop})
  1756. getloop(id).then(res =>{
  1757. if(res.data.code == 0){
  1758. let loopState = res.data.data
  1759. //loopState.length可以视为回路数量
  1760. for(let n = 0;n < loopState.length;n++){
  1761. var Loop = that.data.Loop
  1762. var color = that.data.Color
  1763. var size = that.data.Size
  1764. var Address = that.data.address
  1765. Loop[n] = that.data.Loop[n]
  1766. color[n] = "black"
  1767. size[n] = "calc(100vw * 16/375)",
  1768. Address[n] = "/image/modify.png"
  1769. that.setData({Color:color,Size:size,address:Address,Loop:Loop})
  1770. }
  1771. }
  1772. else{
  1773. wx.showModal({
  1774. title: '错误',
  1775. content: res.data.msg,
  1776. showCancel:false
  1777. })
  1778. }
  1779. })
  1780. loopSwitchstate(id,'1,2,3,4,5,6,7,8').then(res=>{
  1781. if(res.data.code == 0){
  1782. var relay = res.data.data
  1783. for(let e = 0;e < relay.length;e++){
  1784. if(relay[e].status == 1){
  1785. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  1786. }
  1787. else if(relay[e].status == 0){
  1788. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1789. }
  1790. else{
  1791. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  1792. }
  1793. }
  1794. }
  1795. else{
  1796. wx.showModal({
  1797. title:'错误',
  1798. content:res.data.msg,
  1799. showCancel:false
  1800. })
  1801. }
  1802. })
  1803. setTimeout(function(){
  1804. wx.setStorageSync('lamp',lamp)
  1805. that.setData({lamp:wx.getStorageSync('lamp')})
  1806. },2000)
  1807. }
  1808. },
  1809. //手动
  1810. manual:function(e){
  1811. var that = this
  1812. var deviceId = that.data.deviceId
  1813. var deviceKey = that.data.deviceKey
  1814. postData(deviceId,deviceKey,{'ManualStatus':1,'Reflash':3}).then(res => {
  1815. if(res.data.errno == 0){
  1816. that.setData({Modecontrol:'手动'})
  1817. }
  1818. else{
  1819. wx.showModal({
  1820. title:'错误',
  1821. content:res.data.error,
  1822. showCancel:false
  1823. })
  1824. }
  1825. })
  1826. },
  1827. //自动
  1828. automatic:function(e){
  1829. var that = this
  1830. var deviceId = that.data.deviceId
  1831. var deviceKey = that.data.deviceKey
  1832. postData(deviceId,deviceKey,{'ManualStatus':1,'Reflash':3}).then(res => {
  1833. if(res.data.errno == 0){
  1834. that.setData({Modecontrol:'自动'})
  1835. }
  1836. else{
  1837. wx.showModal({
  1838. title:'错误',
  1839. content:res.data.error,
  1840. showCancel:false
  1841. })
  1842. }
  1843. })
  1844. },
  1845. /**
  1846. * 生命周期函数--监听页面加载
  1847. */
  1848. onLoad(options) {
  1849. },
  1850. /**
  1851. * 生命周期函数--监听页面初次渲染完成
  1852. */
  1853. onReady() {
  1854. },
  1855. /**
  1856. * 生命周期函数--监听页面显示
  1857. */
  1858. onShow() {
  1859. wx.showToast({
  1860. title:'加载中',
  1861. icon:'loading',
  1862. duration: 1500
  1863. })
  1864. var that = this
  1865. var group = wx.getStorageSync('group')
  1866. //获取分组下拉
  1867. var groups = []
  1868. for(let i = 0;i < group.length;i++){
  1869. groups.push(group[i].groupName)
  1870. }
  1871. that.setData({
  1872. groupName:groups
  1873. })
  1874. //改变分组下标
  1875. if(wx.getStorageSync('groupName') != ''){
  1876. let GroupName = that.data.groupName
  1877. for(let n = 0;n < GroupName.length;n++){
  1878. if(GroupName[n] == wx.getStorageSync('groupName')){
  1879. that.setData({
  1880. groupIndex:n
  1881. })
  1882. }
  1883. }
  1884. }
  1885. else{
  1886. wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])
  1887. }
  1888. //获取设备下拉
  1889. var device = []
  1890. var groupName = that.data.groupName
  1891. var groupIndex = that.data.groupIndex
  1892. for(let j = 0;j < group.length;j++){
  1893. if(groupName[groupIndex] == group[j].groupName){
  1894. if(group[j].devices != null && group[j].devices.length != 0){
  1895. for(let p = 0;p < group[j].devices.length;p++){
  1896. device.push(group[j].devices[p].deviceName)
  1897. }
  1898. that.setData({deviceName:device})
  1899. //改变设备下标
  1900. if(wx.getStorageSync('deviceName') != ''){
  1901. var DeviceName = that.data.deviceName
  1902. for(let m = 0;m < DeviceName.length;m++){
  1903. if(DeviceName[m] == wx.getStorageSync('deviceName')){
  1904. that.setData({deviceIndex:m})
  1905. }
  1906. }
  1907. }
  1908. else{
  1909. wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex])
  1910. }
  1911. //根据分组名和设备信息
  1912. var groupNames = that.data.groupName[that.data.groupIndex]
  1913. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  1914. for(let d = 0;d < group.length;d++){
  1915. if(groupNames == group[d].groupName){
  1916. var Groupno = group[d].id//分组id
  1917. for(let g = 0;g < group[d].devices.length;g++){
  1918. if(deviceNames == group[d].devices[g].deviceName){
  1919. //旧设备信息
  1920. if(group[d].devices[g].module == 0){
  1921. var id = group[d].devices[g].id//设备的序号
  1922. var circuitNum = group[d].devices[g].circuitNum
  1923. var circuits = group[d].devices[g].circuits
  1924. var deviceId = group[d].devices[g].deviceId
  1925. var deviceKey = group[d].devices[g].deviceKey
  1926. var moudule = group[d].devices[g].module
  1927. that.setData({play:0})
  1928. }
  1929. //新设备信息
  1930. else{
  1931. var circuitNum = group[d].devices[g].circuitNum
  1932. var circuits = group[d].devices[g].circuits
  1933. var moudule = group[d].devices[g].module
  1934. var id = group[d].devices[g].id//设备的序号
  1935. that.setData({play:1})
  1936. }
  1937. }
  1938. }
  1939. }
  1940. }
  1941. //用于区分两种设备以及记录回路数量
  1942. that.setData({moudule:moudule,id:id,loops:circuitNum})
  1943. //灯泡图标缓存
  1944. wx.setStorageSync('lamp', [])
  1945. var lamp = wx.getStorageSync('lamp')
  1946. for(let h = 0;h < 12;h++){
  1947. lamp.push("https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png")
  1948. }
  1949. //旧设备
  1950. if(moudule == 0){
  1951. //重置状态
  1952. let combination = control.Refresh()
  1953. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  1954. //..............................
  1955. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1956. var Circuits = circuits.split(',')
  1957. let loop = that.data.loop
  1958. for(let vr = 0;vr < circuitNum;vr++){
  1959. loop[vr] = Circuits[vr]
  1960. }
  1961. that.setData({loop:loop})
  1962. postData(deviceId,deviceKey,{"Reflash":3}).then(res=>{
  1963. if(res.data.errno == 0){
  1964. setTimeout(function(){
  1965. getBasic(deviceId,deviceKey).then(ever=>{
  1966. if(ever.data.errno == 0){
  1967. if (ever.data.data.online == true) {
  1968. that.setData({status:"在线"})
  1969. }
  1970. else{
  1971. that.setData({status:"离线"})
  1972. }
  1973. getData(deviceId,deviceKey).then(ver =>{
  1974. if(ver.data.errno == 0){
  1975. let data = ver.data.data
  1976. for(let i = 0;i < data.length;i++){
  1977. if (data[i].id == "RelayCount") {
  1978. var quantity = data[i].current_value
  1979. that.setData({loops:quantity})
  1980. }
  1981. else if(data[i].id == "ManualStatus"){
  1982. if(data[i].current_value == 0){
  1983. that.setData({Modecontrol:"自动"})
  1984. }
  1985. else{
  1986. that.setData({Modecontrol:"手动"})
  1987. }
  1988. }
  1989. //回路1
  1990. else if(data[i].id == 'R1_RelayStatus'){
  1991. let police = data[i].current_value
  1992. var lamps = control.switchReal(police,lamp,0)
  1993. }
  1994. else if(data[i].id == 'R2_RelayStatus'){
  1995. let police = data[i].current_value
  1996. var lamps = control.switchReal(police,lamp,1)
  1997. }
  1998. else if(data[i].id == 'R3_RelayStatus'){
  1999. let police = data[i].current_value
  2000. var lamps = control.switchReal(police,lamp,2)
  2001. }
  2002. else if(data[i].id == 'R4_RelayStatus'){
  2003. let police = data[i].current_value
  2004. var lamps = control.switchReal(police,lamp,3)
  2005. }
  2006. else if(data[i].id == 'R5_RelayStatus'){
  2007. let police = data[i].current_value
  2008. var lamps = control.switchReal(police,lamp,4)
  2009. }
  2010. else if(data[i].id == 'R6_RelayStatus'){
  2011. let police = data[i].current_value
  2012. var lamps = control.switchReal(police,lamp,5)
  2013. }
  2014. else if(data[i].id == 'R7_RelayStatus'){
  2015. let police = data[i].current_value
  2016. var lamps = control.switchReal(police,lamp,6)
  2017. }
  2018. else if(data[i].id == 'R8_RelayStatus'){
  2019. let police = data[i].current_value
  2020. var lamps = control.switchReal(police,lamp,7)
  2021. }
  2022. else if(data[i].id == 'R9_RelayStatus'){
  2023. let police = data[i].current_value
  2024. var lamps = control.switchReal(police,lamp,8)
  2025. }
  2026. else if(data[i].id == 'R10_RelayStatus'){
  2027. let police = data[i].current_value
  2028. var lamps = control.switchReal(police,lamp,9)
  2029. }
  2030. else if(data[i].id == 'R11_RelayStatus'){
  2031. let police = data[i].current_value
  2032. var lamps = control.switchReal(police,lamp,10)
  2033. }
  2034. else if(data[i].id == 'R12_RelayStatus'){
  2035. let police = data[i].current_value
  2036. var lamps = control.switchReal(police,lamp,11)
  2037. }
  2038. wx.setStorageSync('lamp', lamps)
  2039. that.setData({lamp:wx.getStorageSync('lamp')})
  2040. }
  2041. for (let i = 0;i < quantity; i++) {
  2042. var Loop = that.data.Loop
  2043. var color = that.data.Color
  2044. var size = that.data.Size
  2045. var Address = that.data.address
  2046. Loop[i] = that.data.loop[i]
  2047. color[i] = "black"
  2048. size[i] = "calc(100vw * 16/375)",
  2049. Address[i] = "/image/modify.png"
  2050. that.setData({
  2051. Color:color,
  2052. Size:size,
  2053. address:Address,
  2054. Loop:Loop
  2055. })
  2056. }
  2057. }
  2058. else{
  2059. wx.showModal({
  2060. title:'错误',
  2061. content:ever.data.error,
  2062. showCancel:false
  2063. })
  2064. }
  2065. })
  2066. }
  2067. else{
  2068. wx.showModal({
  2069. title:'错误',
  2070. content:ever.data.error,
  2071. showCancel:false
  2072. })
  2073. }
  2074. })
  2075. },2000)
  2076. }
  2077. else{
  2078. wx.showModal({
  2079. title:'错误',
  2080. content:res.data.error,
  2081. showCancel:false
  2082. })
  2083. }
  2084. })
  2085. }
  2086. //新设备
  2087. else{
  2088. //重置状态
  2089. let combination = control.Refresh()
  2090. that.setData({loop:combination[0],address:combination[1],lamp:combination[2],size:combination[3],Color:combination[4]})
  2091. //..............................
  2092. //获取设备在线状态
  2093. getDevice(id,Groupno).then(dve =>{
  2094. if(dve.data.code == 0){
  2095. var state = dve.data.data
  2096. switch(state[0].status){
  2097. case 0:
  2098. that.setData({status:'离线'})
  2099. break;
  2100. case 1:
  2101. that.setData({status:'在线'})
  2102. break;
  2103. }
  2104. }
  2105. else{
  2106. wx.showModal({
  2107. title: '错误',
  2108. content: dve.data.msg,
  2109. showCancel:false
  2110. })
  2111. }
  2112. })
  2113. var Circuits = circuits.split(',')
  2114. let loop = that.data.loop
  2115. for(let vr = 0;vr < circuitNum;vr++){
  2116. loop[vr] = Circuits[vr]
  2117. }
  2118. that.setData({loop:loop})
  2119. getloop(id).then(res =>{
  2120. if(res.data.code == 0){
  2121. let loopState = res.data.data
  2122. //loopState.length可以视为回路数量
  2123. for(let n = 0;n < loopState.length;n++){
  2124. var Loop = that.data.Loop
  2125. var color = that.data.Color
  2126. var size = that.data.Size
  2127. var Address = that.data.address
  2128. Loop[n] = that.data.Loop[n]
  2129. color[n] = "black"
  2130. size[n] = "calc(100vw * 16/375)",
  2131. Address[n] = "/image/modify.png"
  2132. that.setData({Color:color,Size:size,address:Address,Loop:Loop})
  2133. }
  2134. }
  2135. else{
  2136. wx.showModal({
  2137. title: '错误',
  2138. content: res.data.msg,
  2139. showCancel:false
  2140. })
  2141. }
  2142. })
  2143. loopSwitchstate(id,'1,2,3,4,5,6,7,8').then(ret=>{
  2144. if(ret.data.code == 0){
  2145. var relay = ret.data.data
  2146. for(let e = 0;e < relay.length;e++){
  2147. if(relay[e].status == 1){
  2148. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png'
  2149. }
  2150. else if(relay[e].status == 0){
  2151. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2152. }
  2153. else{
  2154. lamp[e] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png'
  2155. }
  2156. }
  2157. }
  2158. else{
  2159. wx.showModal({
  2160. title:'错误',
  2161. content:ret.data.msg,
  2162. showCancel:false
  2163. })
  2164. }
  2165. })
  2166. setTimeout(function(){
  2167. wx.setStorageSync('lamp',lamp)
  2168. that.setData({lamp:wx.getStorageSync('lamp')})
  2169. },2000)
  2170. }
  2171. }
  2172. else {
  2173. that.setData({status:'离线',deviceName:[]})
  2174. }
  2175. }
  2176. }
  2177. },
  2178. /**
  2179. * 生命周期函数--监听页面隐藏
  2180. */
  2181. onHide() {
  2182. },
  2183. /**
  2184. * 生命周期函数--监听页面卸载
  2185. */
  2186. onUnload() {
  2187. },
  2188. /**
  2189. * 页面相关事件处理函数--监听用户下拉动作
  2190. */
  2191. onPullDownRefresh() {
  2192. },
  2193. /**
  2194. * 页面上拉触底事件的处理函数
  2195. */
  2196. onReachBottom() {
  2197. },
  2198. /**
  2199. * 用户点击右上角分享
  2200. */
  2201. onShareAppMessage() {
  2202. }
  2203. })