system.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. // pages/system/system.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. dates:"",//日期
  8. times:"",//时间
  9. Illuminance:"",//光照度
  10. Firealarmtext:"",//消防报警文字
  11. Firealarmcolor:"",//消防报警颜色
  12. longitude:"",//经度
  13. latitude:"",//纬度
  14. Sunrisetime:"",//日出时间
  15. Sunsettime:"",//日落时间
  16. array: ['0', '1', '2', '3','4','5','6','7','8','9','10',
  17. '11', '12', '13', '14','15','16','17','18','19','20'],//开关延时
  18. index: 0,
  19. switchtime:0,
  20. multiItems: [[], []],//经度显示
  21. multiIndex: [0, 0],
  22. multiItems2:[[],[]],//纬度显示
  23. multiIndex2:[0,0],
  24. Title:"",
  25. statu:"false",
  26. status:"离线",
  27. loops:"",//记录回路数量
  28. Storage:[],//储存发送数据
  29. Modecontrol:"",
  30. groupName:[],
  31. deviceName:[],
  32. groupIndex:0,
  33. deviceIndex:0,
  34. deviceId:'',
  35. deviceKey:'',
  36. },
  37. //开关延时
  38. bindPickerChange: function(e) {
  39. this.setData({
  40. index: e.detail.value,
  41. })
  42. },
  43. //分组下拉
  44. changegroup:function(e){
  45. var that = this
  46. that.setData({
  47. groupIndex:e.detail.value
  48. })
  49. wx.setStorageSync('groupIndex', e.detail.value)
  50. var groupName = that.data.groupName[that.data.groupIndex]//选择的分组名称
  51. wx.setStorageSync('groupName',groupName)//将修改的分组名称放入缓存
  52. //获取设备名称下拉
  53. var group = wx.getStorageSync('group')
  54. var deviceName = that.data.deviceName
  55. var deviceName = []
  56. for(let i = 0;i < group.length;i++){
  57. if(group[i].groupName == groupName){
  58. if(group[i].devices != null){
  59. wx.showToast({
  60. title:'刷新中',
  61. icon:'loading',
  62. duration: 2000
  63. })
  64. for(let j = 0;j < group[i].devices.length;j++){
  65. deviceName.push(group[i].devices[j].deviceName)
  66. }
  67. }
  68. else{
  69. wx.showModal({
  70. title:'提示',
  71. content:'工程中不存在设备',
  72. showCancel:false
  73. })
  74. console.log('系统分组下拉到没有设备的工程')
  75. that.setData({status:'离线'})
  76. }
  77. }
  78. }
  79. this.setData({
  80. deviceName:deviceName
  81. })
  82. //重新获取设备id和key
  83. if(that.data.deviceName.length != 0){
  84. var groupNames = that.data.groupName[that.data.groupIndex]
  85. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  86. for(let d = 0;d < group.length;d++){
  87. if(groupNames == group[d].groupName){
  88. for(let g = 0;g < group[d].devices.length;g++){
  89. if(deviceNames == group[d].devices[g].deviceName){
  90. var deviceId = group[d].devices[g].deviceId
  91. var deviceKey = group[d].devices[g].deviceKey
  92. }
  93. }
  94. }
  95. }
  96. wx.setStorageSync('deviceName',deviceNames)//将修改设备名放入缓存
  97. that.setData({
  98. deviceId:deviceId,
  99. deviceKey:deviceKey
  100. })
  101. wx.request({
  102. url: 'https://api.heclouds.com/devices/' + deviceId,
  103. method:'GET',
  104. header:{"api-key": deviceKey},
  105. success:(res) =>{
  106. if (res.data.data.online == true) {
  107. that.setData({
  108. status:"在线"
  109. })
  110. }
  111. else{
  112. that.setData({
  113. status:"离线"
  114. })
  115. }
  116. }
  117. })
  118. wx.request({
  119. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  120. header:{"api-key": deviceKey},
  121. method:'GET',
  122. success: function(res){
  123. let {data} = res.data
  124. for(let i = 0; i < data.length; i++){
  125. //时间
  126. if (data[i].id == "SysTime") {
  127. var times = data[i].current_value
  128. }
  129. //日期
  130. else if(data[i].id == "SysDate"){
  131. var dates = data[i].current_value
  132. }
  133. //光照度
  134. else if(data[i].id == "lightLuxValue"){
  135. var Illuminance = data[i].current_value
  136. }
  137. //日出时间
  138. else if(data[i].id == "RiseTime"){
  139. var Sunrisetime = data[i].current_value
  140. }
  141. //日落时间
  142. else if(data[i].id == "SetTime"){
  143. var Sunsettime = data[i].current_value
  144. }
  145. //经度
  146. else if(data[i].id == "Longitude"){
  147. var longitude = data[i].current_value
  148. }
  149. //纬度
  150. else if(data[i].id == "Latitude"){
  151. var latitude = data[i].current_value
  152. }
  153. //消防报警
  154. else if(data[i].id == "AlarmState"){
  155. var Firealarm = data[i].current_value
  156. }
  157. //回路数量
  158. else if(data[i].id == "RelayCount"){
  159. var loops = data[i].current_value
  160. }
  161. //开关延时
  162. else if (data[i].id == "DeviceID") {
  163. var device = data[i].current_value
  164. that.setData({index:device,switchtime:device})
  165. }
  166. }
  167. //时间
  168. if(times.length == 5){
  169. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  170. }
  171. else if(times.length == 6){
  172. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  173. }
  174. else{
  175. a = times
  176. }
  177. //日期
  178. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  179. //日出时间
  180. var c = Sunrisetime.substring(0,1) + ':' + Sunrisetime.substring(1,3)
  181. //日落时间
  182. var d = Sunsettime.substring(0,2) + ':' + Sunsettime.substring(2,4)
  183. //经度
  184. var multiIndex =that.data.multiIndex
  185. if (longitude.length == 5) {
  186. var e = longitude.substring(0,3) + "," + longitude.substring(3,5) + "'"
  187. multiIndex[0] = parseInt(longitude.substring(0,3))
  188. multiIndex[1] = parseInt(longitude.substring(3,5))
  189. that.setData({
  190. multiIndex:multiIndex
  191. })
  192. }
  193. else if(longitude.length == 4){
  194. var e = longitude.substring(0,2) + "," + longitude.substring(2,4) + "'"
  195. multiIndex[0] = parseInt(longitude.substring(0,2))
  196. multiIndex[1] = parseInt(longitude.substring(2,4))
  197. that.setData({
  198. multiIndex:multiIndex
  199. })
  200. }
  201. else if(longitude.length == 3){
  202. var e = longitude.substring(0,1) + "," + longitude.substring(1,3) + "'"
  203. multiIndex[0] = parseInt(longitude.substring(0,1))
  204. multiIndex[1] = parseInt(longitude.substring(1,3))
  205. that.setData({
  206. multiIndex:multiIndex
  207. })
  208. }
  209. else if(longitude.length == 2){
  210. var e = 0 + "," + longitude.substring(0,2) + "'"
  211. multiIndex[0] = 0
  212. multiIndex[1] = parseInt(longitude.substring(0,2))
  213. that.setData({
  214. multiIndex:multiIndex
  215. })
  216. }
  217. else{
  218. var e = 0 + "," + longitude.substring(0,1) + "'"
  219. multiIndex[0] = 0
  220. multiIndex[1] = parseInt(longitude.substring(0,1))
  221. that.setData({
  222. multiIndex:multiIndex
  223. })
  224. }
  225. //纬度
  226. var multiIndex2 = that.data.multiIndex2
  227. if (latitude.length == 5) {
  228. var f = latitude.substring(0,3) + "," + latitude.substring(3,5) + "'"
  229. multiIndex2[0] = parseInt(latitude.substring(0,3))
  230. multiIndex2[1] = parseInt(latitude.substring(3,5))
  231. that.setData({
  232. multiIndex2:multiIndex2
  233. })
  234. }
  235. else if(latitude.length == 4){
  236. var f = latitude.substring(0,2) + "," + latitude.substring(2,4) + "'"
  237. multiIndex2[0] = parseInt(latitude.substring(0,2))
  238. multiIndex2[1] = parseInt(latitude.substring(2,4))
  239. that.setData({
  240. multiIndex2:multiIndex2
  241. })
  242. }
  243. else if(latitude.length == 3){
  244. var f = latitude.substring(0,1) + "," + latitude.substring(1,3) + "'"
  245. multiIndex2[0] = parseInt(latitude.substring(0,1))
  246. multiIndex2[1] = parseInt(latitude.substring(1,3))
  247. that.setData({
  248. multiIndex2:multiIndex2
  249. })
  250. }
  251. else if(latitude.length == 2){
  252. var f = 0 + "," + latitude.substring(0,2) + "'"
  253. multiIndex2[0] = 0
  254. multiIndex2[1] = parseInt(latitude.substring(0,2))
  255. that.setData({
  256. multiIndex2:multiIndex2
  257. })
  258. }
  259. else{
  260. var f = 0 + "," + latitude.substring(0,1) + "'"
  261. multiIndex2[0] = 0
  262. multiIndex2[1] = parseInt(latitude.substring(0,1))
  263. that.setData({
  264. multiIndex2:multiIndex2
  265. })
  266. }
  267. //光照度
  268. var g = Illuminance
  269. //消防报警
  270. if(Firealarm == "0"){
  271. that.setData({
  272. Firealarmtext:"正常",
  273. Firealarmcolor:"#5cfe03"
  274. })
  275. }
  276. else{
  277. that.setData({
  278. Firealarmtext:"异常",
  279. Firealarmcolor:"red"
  280. })
  281. }
  282. that.setData({
  283. times:a,
  284. dates:b,
  285. Sunrisetime:c,
  286. Sunsettime:d,
  287. longitude:e,
  288. latitude:f,
  289. Illuminance:g,
  290. loops:loops,
  291. })
  292. }
  293. })
  294. }
  295. else{
  296. wx.setStorageSync('deviceName','')//清空设备名称缓存
  297. }
  298. },
  299. //设备下拉
  300. changedevice:function(e){
  301. var that = this
  302. wx.showToast({
  303. title:'刷新中',
  304. icon:'loading',
  305. duration: 2000
  306. })
  307. that.setData({
  308. deviceIndex:e.detail.value
  309. })
  310. var group = wx.getStorageSync('group')
  311. var groupName = that.data.groupName[that.data.groupIndex]
  312. var deviceName = that.data.deviceName[that.data.deviceIndex]
  313. wx.setStorageSync('groupName', groupName)//分组名称
  314. wx.setStorageSync('deviceName', deviceName)//设备名称
  315. for (let index = 0; index < group.length; index++) {
  316. if(groupName == group[index].groupName){
  317. for(let t = 0;t < group[index].devices.length;t++){
  318. if(deviceName == group[index].devices[t].deviceName){
  319. var deviceId = group[index].devices[t].deviceId
  320. var deviceKey = group[index].devices[t].deviceKey
  321. }
  322. }
  323. }
  324. }
  325. that.setData({
  326. deviceId:deviceId,
  327. deviceKey:deviceKey
  328. })
  329. wx.request({
  330. url: 'https://api.heclouds.com/devices/' + deviceId,
  331. method:'GET',
  332. header:{"api-key": deviceKey},
  333. success:(res) =>{
  334. if (res.data.data.online == true) {
  335. that.setData({
  336. status:"在线"
  337. })
  338. }
  339. else{
  340. that.setData({
  341. status:"离线"
  342. })
  343. }
  344. }
  345. })
  346. wx.request({
  347. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  348. header:{"api-key": deviceKey},
  349. method:'GET',
  350. success: function(res){
  351. let {data} = res.data
  352. for(let i = 0; i < data.length; i++){
  353. //时间
  354. if (data[i].id == "SysTime") {
  355. var times = data[i].current_value
  356. }
  357. //日期
  358. else if(data[i].id == "SysDate"){
  359. var dates = data[i].current_value
  360. }
  361. //光照度
  362. else if(data[i].id == "lightLuxValue"){
  363. var Illuminance = data[i].current_value
  364. }
  365. //日出时间
  366. else if(data[i].id == "RiseTime"){
  367. var Sunrisetime = data[i].current_value
  368. }
  369. //日落时间
  370. else if(data[i].id == "SetTime"){
  371. var Sunsettime = data[i].current_value
  372. }
  373. //经度
  374. else if(data[i].id == "Longitude"){
  375. var longitude = data[i].current_value
  376. }
  377. //纬度
  378. else if(data[i].id == "Latitude"){
  379. var latitude = data[i].current_value
  380. }
  381. //消防报警
  382. else if(data[i].id == "AlarmState"){
  383. var Firealarm = data[i].current_value
  384. }
  385. //回路数量
  386. else if(data[i].id == "RelayCount"){
  387. var loops = data[i].current_value
  388. }
  389. //开关延时
  390. else if (data[i].id == "DeviceID") {
  391. var device = data[i].current_value
  392. that.setData({index:device,switchtime:device})
  393. }
  394. }
  395. //时间
  396. if(times.length == 5){
  397. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  398. }
  399. else if(times.length == 6){
  400. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  401. }
  402. else{
  403. a = times
  404. }
  405. //日期
  406. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  407. //日出时间
  408. var c = Sunrisetime.substring(0,1) + ':' + Sunrisetime.substring(1,3)
  409. //日落时间
  410. var d = Sunsettime.substring(0,2) + ':' + Sunsettime.substring(2,4)
  411. //经度
  412. var multiIndex =that.data.multiIndex
  413. if (longitude.length == 5) {
  414. var e = longitude.substring(0,3) + "," + longitude.substring(3,5) + "'"
  415. multiIndex[0] = parseInt(longitude.substring(0,3))
  416. multiIndex[1] = parseInt(longitude.substring(3,5))
  417. that.setData({
  418. multiIndex:multiIndex
  419. })
  420. }
  421. else if(longitude.length == 4){
  422. var e = longitude.substring(0,2) + "," + longitude.substring(2,4) + "'"
  423. multiIndex[0] = parseInt(longitude.substring(0,2))
  424. multiIndex[1] = parseInt(longitude.substring(2,4))
  425. that.setData({
  426. multiIndex:multiIndex
  427. })
  428. }
  429. else if(longitude.length == 3){
  430. var e = longitude.substring(0,1) + "," + longitude.substring(1,3) + "'"
  431. multiIndex[0] = parseInt(longitude.substring(0,1))
  432. multiIndex[1] = parseInt(longitude.substring(1,3))
  433. that.setData({
  434. multiIndex:multiIndex
  435. })
  436. }
  437. else if(longitude.length == 2){
  438. var e = 0 + "," + longitude.substring(0,2) + "'"
  439. multiIndex[0] = 0
  440. multiIndex[1] = parseInt(longitude.substring(0,2))
  441. that.setData({
  442. multiIndex:multiIndex
  443. })
  444. }
  445. else{
  446. var e = 0 + "," + longitude.substring(0,1) + "'"
  447. multiIndex[0] = 0
  448. multiIndex[1] = parseInt(longitude.substring(0,1))
  449. that.setData({
  450. multiIndex:multiIndex
  451. })
  452. }
  453. //纬度
  454. var multiIndex2 = that.data.multiIndex2
  455. if (latitude.length == 5) {
  456. var f = latitude.substring(0,3) + "," + latitude.substring(3,5) + "'"
  457. multiIndex2[0] = parseInt(latitude.substring(0,3))
  458. multiIndex2[1] = parseInt(latitude.substring(3,5))
  459. that.setData({
  460. multiIndex2:multiIndex2
  461. })
  462. }
  463. else if(latitude.length == 4){
  464. var f = latitude.substring(0,2) + "," + latitude.substring(2,4) + "'"
  465. multiIndex2[0] = parseInt(latitude.substring(0,2))
  466. multiIndex2[1] = parseInt(latitude.substring(2,4))
  467. that.setData({
  468. multiIndex2:multiIndex2
  469. })
  470. }
  471. else if(latitude.length == 3){
  472. var f = latitude.substring(0,1) + "," + latitude.substring(1,3) + "'"
  473. multiIndex2[0] = parseInt(latitude.substring(0,1))
  474. multiIndex2[1] = parseInt(latitude.substring(1,3))
  475. that.setData({
  476. multiIndex2:multiIndex2
  477. })
  478. }
  479. else if(latitude.length == 2){
  480. var f = 0 + "," + latitude.substring(0,2) + "'"
  481. multiIndex2[0] = 0
  482. multiIndex2[1] = parseInt(latitude.substring(0,2))
  483. that.setData({
  484. multiIndex2:multiIndex2
  485. })
  486. }
  487. else{
  488. var f = 0 + "," + latitude.substring(0,1) + "'"
  489. multiIndex2[0] = 0
  490. multiIndex2[1] = parseInt(latitude.substring(0,1))
  491. that.setData({
  492. multiIndex2:multiIndex2
  493. })
  494. }
  495. //光照度
  496. var g = Illuminance
  497. //消防报警
  498. if(Firealarm == "0"){
  499. that.setData({
  500. Firealarmtext:"正常",
  501. Firealarmcolor:"#5cfe03"
  502. })
  503. }
  504. else{
  505. that.setData({
  506. Firealarmtext:"异常",
  507. Firealarmcolor:"red"
  508. })
  509. }
  510. that.setData({
  511. times:a,
  512. dates:b,
  513. Sunrisetime:c,
  514. Sunsettime:d,
  515. longitude:e,
  516. latitude:f,
  517. Illuminance:g,
  518. loops:loops,
  519. })
  520. }
  521. })
  522. },
  523. //校准时间
  524. Calibrationtime:function(){
  525. wx.showToast({
  526. title:'校准中',
  527. icon:'loading',
  528. duration: 2000
  529. })
  530. var that = this
  531. var myDate = new Date();
  532. //年
  533. var year = myDate.getFullYear().toString()
  534. year = year.slice(2,4)
  535. //月
  536. var month = myDate.getMonth() + 1
  537. month = month.toString()
  538. if(month.length == 1){
  539. month = '0' + month
  540. }
  541. //日
  542. var date = myDate.getDate()
  543. date = date.toString()
  544. if(date.length == 1){
  545. date = '0' + date
  546. }
  547. //小时
  548. var hour = myDate.getHours()
  549. hour = hour.toString()
  550. if(hour.length == 1){
  551. hour = '0' + hour
  552. }
  553. //分钟
  554. var minutes = myDate.getMinutes()
  555. minutes = minutes.toString()
  556. if(minutes.length == 1){
  557. minutes = '0' + minutes
  558. }
  559. //秒
  560. var seconds = myDate.getSeconds()
  561. seconds = seconds.toString()
  562. if(seconds.length == 1){
  563. seconds = '0' + seconds
  564. }
  565. var datas = year + month + date
  566. datas = parseInt(datas)
  567. var time = hour + minutes + seconds
  568. time = parseInt(time)
  569. wx.request({
  570.   url: "https://api.heclouds.com/cmds?device_id=" + that.data.deviceId,
  571.   method: 'POST',
  572.   header: {
  573.     "api-key": that.data.deviceKey
  574.   },
  575.   data: {
  576. "SysTime":time,
  577. "SysDate":datas,
  578.     "Reflash": 4 //刷新设备当前时间,并且将设备当前时间上传
  579.   },
  580.   success(res) {
  581. setTimeout(function(){
  582. wx.request({
  583. url: 'https://api.heclouds.com/devices/' + that.data.deviceId + '/datastreams',
  584. method:'GET',
  585. header:{"api-key": that.data.deviceKey},
  586. success:ever =>{
  587. let {data} = ever.data
  588. for(let i = 0; i < data.length; i++){
  589. //时间
  590. if (data[i].id == "SysTime") {
  591. var times = data[i].current_value
  592. }
  593. //日期
  594. else if(data[i].id == "SysDate"){
  595. var dates = data[i].current_value
  596. }
  597. }
  598. if(times.length == 5){
  599. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  600. }
  601. else if(times.length == 6){
  602. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  603. }
  604. else{
  605. a = times
  606. }
  607. //日期
  608. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  609. that.setData({
  610. times:a,
  611. dates:b
  612. })
  613. }
  614. })
  615. },2000)
  616.   },
  617. })
  618. },
  619. //经度调整
  620. handleMulti(e){
  621. this.setData({
  622. multiIndex : e.detail.value
  623. })
  624. },
  625. //纬度调整
  626. handleMulti2(e){
  627. this.setData({
  628. multiIndex2 : e.detail.value
  629. })
  630. },
  631. //刷新页面
  632. RefreshPage:function(){
  633. wx.showToast({
  634. title:'刷新中',
  635. icon:'loading',
  636. duration: 2000
  637. })
  638. var that = this
  639. wx.request({
  640. url: 'https://api.heclouds.com/devices/' + that.data.deviceId + '/datastreams',
  641. header: {"api-key": that.data.deviceKey},
  642. method:'GET',
  643. success: function(res){
  644. let {data} = res.data
  645. for(let i = 0; i < data.length; i++){
  646. //时间
  647. if (data[i].id == "SysTime") {
  648. var times = data[i].current_value
  649. }
  650. //日期
  651. else if(data[i].id == "SysDate"){
  652. var dates = data[i].current_value
  653. }
  654. //光照度
  655. else if(data[i].id == "lightLuxValue"){
  656. var Illuminance = data[i].current_value
  657. }
  658. //日出时间
  659. else if(data[i].id == "RiseTime"){
  660. var Sunrisetime = data[i].current_value
  661. }
  662. //日落时间
  663. else if(data[i].id == "SetTime"){
  664. var Sunsettime = data[i].current_value
  665. }
  666. //经度
  667. else if(data[i].id == "Longitude"){
  668. var longitude = data[i].current_value
  669. }
  670. //纬度
  671. else if(data[i].id == "Latitude"){
  672. var latitude = data[i].current_value
  673. }
  674. //消防报警
  675. else if(data[i].id == "AlarmState"){
  676. var Firealarm = data[i].current_value
  677. }
  678. //回路数量
  679. else if(data[i].id == "RelayCount"){
  680. var loops = data[i].current_value
  681. }
  682. //开关延时
  683. else if (data[i].id == "DeviceID") {
  684. var device = data[i].current_value
  685. that.setData({index:device,switchtime:device})
  686. }
  687. }
  688. //时间
  689. if(times.length == 5){
  690. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  691. }
  692. else if(times.length == 6){
  693. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  694. }
  695. else{
  696. a = times
  697. }
  698. //日期
  699. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  700. //日出时间
  701. var c = Sunrisetime.substring(0,1) + ':' + Sunrisetime.substring(1,3)
  702. //日落时间
  703. var d = Sunsettime.substring(0,2) + ':' + Sunsettime.substring(2,4)
  704. //经度
  705. var multiIndex =that.data.multiIndex
  706. if (longitude.length == 5) {
  707. var e = longitude.substring(0,3) + "," + longitude.substring(3,5) + "'"
  708. multiIndex[0] = parseInt(longitude.substring(0,3))
  709. multiIndex[1] = parseInt(longitude.substring(3,5))
  710. that.setData({
  711. multiIndex:multiIndex
  712. })
  713. }
  714. else if(longitude.length == 4){
  715. var e = longitude.substring(0,2) + "," + longitude.substring(2,4) + "'"
  716. multiIndex[0] = parseInt(longitude.substring(0,2))
  717. multiIndex[1] = parseInt(longitude.substring(2,4))
  718. that.setData({
  719. multiIndex:multiIndex
  720. })
  721. }
  722. else if(longitude.length == 3){
  723. var e = longitude.substring(0,1) + "," + longitude.substring(1,3) + "'"
  724. multiIndex[0] = parseInt(longitude.substring(0,1))
  725. multiIndex[1] = parseInt(longitude.substring(1,3))
  726. that.setData({
  727. multiIndex:multiIndex
  728. })
  729. }
  730. else if(longitude.length == 2){
  731. var e = 0 + "," + longitude.substring(0,2) + "'"
  732. multiIndex[0] = 0
  733. multiIndex[1] = parseInt(longitude.substring(0,2))
  734. that.setData({
  735. multiIndex:multiIndex
  736. })
  737. }
  738. else{
  739. var e = 0 + "," + longitude.substring(0,1) + "'"
  740. multiIndex[0] = 0
  741. multiIndex[1] = parseInt(longitude.substring(0,1))
  742. that.setData({
  743. multiIndex:multiIndex
  744. })
  745. }
  746. //纬度
  747. var multiIndex2 = that.data.multiIndex2
  748. if (latitude.length == 5) {
  749. var f = latitude.substring(0,3) + "," + latitude.substring(3,5) + "'"
  750. multiIndex2[0] = parseInt(latitude.substring(0,3))
  751. multiIndex2[1] = parseInt(latitude.substring(3,5))
  752. that.setData({
  753. multiIndex2:multiIndex2
  754. })
  755. }
  756. else if(latitude.length == 4){
  757. var f = latitude.substring(0,2) + "," + latitude.substring(2,4) + "'"
  758. multiIndex2[0] = parseInt(latitude.substring(0,2))
  759. multiIndex2[1] = parseInt(latitude.substring(2,4))
  760. that.setData({
  761. multiIndex2:multiIndex2
  762. })
  763. }
  764. else if(latitude.length == 3){
  765. var f = latitude.substring(0,1) + "," + latitude.substring(1,3) + "'"
  766. multiIndex2[0] = parseInt(latitude.substring(0,1))
  767. multiIndex2[1] = parseInt(latitude.substring(1,3))
  768. that.setData({
  769. multiIndex2:multiIndex2
  770. })
  771. }
  772. else if(latitude.length == 2){
  773. var f = 0 + "," + latitude.substring(0,2) + "'"
  774. multiIndex2[0] = 0
  775. multiIndex2[1] = parseInt(latitude.substring(0,2))
  776. that.setData({
  777. multiIndex2:multiIndex2
  778. })
  779. }
  780. else{
  781. var f = 0 + "," + latitude.substring(0,1) + "'"
  782. multiIndex2[0] = 0
  783. multiIndex2[1] = parseInt(latitude.substring(0,1))
  784. that.setData({
  785. multiIndex2:multiIndex2
  786. })
  787. }
  788. //光照度
  789. var g = Illuminance
  790. //消防报警
  791. if(Firealarm == "0"){
  792. that.setData({
  793. Firealarmtext:"正常",
  794. Firealarmcolor:"#5cfe03"
  795. })
  796. }
  797. else{
  798. that.setData({
  799. Firealarmtext:"异常",
  800. Firealarmcolor:"red"
  801. })
  802. }
  803. that.setData({
  804. times:a,
  805. dates:b,
  806. Sunrisetime:c,
  807. Sunsettime:d,
  808. longitude:e,
  809. latitude:f,
  810. Illuminance:g,
  811. loops:loops,
  812. })
  813. }
  814. })
  815. wx.request({
  816.   url: "https://api.heclouds.com/cmds?device_id=" + that.data.deviceId,
  817.   method: 'POST',
  818.   header: {
  819.     "api-key": that.data.deviceKey
  820.   },
  821.   data: {
  822.     "Reflash": 4 //刷新设备当前时间,并且将设备当前时间上传
  823.   },
  824.   success(res) {
  825. setTimeout(function(){
  826. wx.request({
  827. url: 'https://api.heclouds.com/devices/' + that.data.deviceId + '/datastreams',
  828. method:'GET',
  829. header:{"api-key": that.data.deviceKey},
  830. success:ever =>{
  831. let {data} = ever.data
  832. for(let i = 0; i < data.length; i++){
  833. //时间
  834. if (data[i].id == "SysTime") {
  835. var times = data[i].current_value
  836. }
  837. //日期
  838. else if(data[i].id == "SysDate"){
  839. var dates = data[i].current_value
  840. }
  841. }
  842. if(times.length == 5){
  843. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  844. }
  845. else if(times.length == 6){
  846. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  847. }
  848. else{
  849. a = times
  850. }
  851. //日期
  852. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  853. that.setData({
  854. times:a,
  855. dates:b
  856. })
  857. }
  858. })
  859. },2000)
  860.   },
  861. })
  862. },
  863. //存储数据
  864. Storedata: function(){
  865. wx.showToast({
  866. title:'存储数据中',
  867. icon:'success',
  868. duration: 2000
  869. })
  870. var that = this
  871. var switchtime = that.data.array[that.data.index]
  872. switchtime = parseInt(switchtime)
  873. //经度
  874. var longitude
  875. var longitude1 = that.data.multiItems[0][that.data.multiIndex[0]]
  876. var longitude2 = that.data.multiItems[1][that.data.multiIndex[1]]
  877. if(longitude1 == 0){
  878. longitude = longitude2
  879. }
  880. else{
  881. longitude1 = longitude1 + ''
  882. longitude2 = longitude2 + ''
  883. if(longitude2.length == 2){
  884. longitude = longitude1 + longitude2
  885. longitude =parseInt(longitude)
  886. }
  887. else{
  888. longitude = longitude1 + '0' +longitude2
  889. longitude =parseInt(longitude)
  890. }
  891. }
  892. //纬度
  893. var latitude
  894. var latitude1 = that.data.multiItems2[0][that.data.multiIndex2[0]]
  895. var latitude2 = that.data.multiItems2[1][that.data.multiIndex2[1]]
  896. if(latitude1 == 0){
  897. latitude = latitude2
  898. }
  899. else{
  900. latitude1 = latitude1 + ''
  901. latitude2 = latitude2 + ''
  902. if(latitude2.length == 2){
  903. latitude = latitude1 + latitude2
  904. latitude =parseInt(latitude)
  905. }
  906. else{
  907. latitude = latitude1 + '0' +latitude2
  908. latitude =parseInt(latitude)
  909. }
  910. }
  911. if(that.data.status == "在线"){
  912. wx.request({
  913. url: 'https://api.heclouds.com/cmds?device_id=' + that.data.deviceId,
  914. method:'POST',
  915. header:{"api-key":that.data.deviceKey},
  916. data:{
  917. "DeviceID":switchtime,
  918. "Longitude":longitude,
  919. "Latitude":latitude,
  920. "Reflash":0,
  921. },
  922. success:function(res){
  923. that.setData({switchtime:switchtime})
  924. }
  925. })
  926. }
  927. else{
  928. wx.showModal({
  929. title:'提示',
  930. content:'存储数据失败,请检查设备是否在线或者刷新页面',
  931. showCancel:false,
  932. })
  933. }
  934. },
  935. /**
  936. * 生命周期函数--监听页面加载
  937. */
  938. onLoad(options) {
  939. },
  940. /**
  941. * 生命周期函数--监听页面初次渲染完成
  942. */
  943. onReady() {
  944. },
  945. /**
  946. * 生命周期函数--监听页面显示
  947. */
  948. onShow() {
  949. wx.showToast({
  950. title:'刷新数据中',
  951. icon:'loading',
  952. duration: 2000
  953. })
  954. var that = this
  955. //经度显示
  956. var pt = [[],[]]
  957. for(let i = 0;i <180;i++){
  958. pt[0].push(i)
  959. }
  960. for(let j = 0;j < 59;j++){
  961. pt[1].push(j)
  962. }
  963. that.setData({
  964. multiItems:pt
  965. })
  966. //纬度显示
  967. var pt1 = [[],[]]
  968. for(let i = 0;i <180;i++){
  969. pt1[0].push(i)
  970. }
  971. for(let j = 0;j < 59;j++){
  972. pt1[1].push(j)
  973. }
  974. that.setData({
  975. multiItems2:pt
  976. })
  977. var group = wx.getStorageSync('group')
  978. //获取分组下拉
  979. var groups = []
  980. for(let i = 0;i < group.length;i++){
  981. groups.push(group[i].groupName)
  982. }
  983. that.setData({
  984. groupName:groups
  985. })
  986. //改变分组下标
  987. if(wx.getStorageSync('groupName') != ''){
  988. let GroupName = that.data.groupName
  989. for(let n = 0;n < GroupName.length;n++){
  990. if(GroupName[n] == wx.getStorageSync('groupName')){
  991. that.setData({
  992. groupIndex:n
  993. })
  994. }
  995. }
  996. }
  997. else{
  998. wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])
  999. }
  1000. //获取设备下拉
  1001. var groupName = that.data.groupName
  1002. var groupIndex = that.data.groupIndex
  1003. var device = []
  1004. for(let j = 0;j < group.length;j++){
  1005. if(groupName[groupIndex] == group[j].groupName){
  1006. if(group[j].devices != null && group[j].devices.length != 0){
  1007. for(let p = 0;p < group[j].devices.length;p++){
  1008. device.push(group[j].devices[p].deviceName)
  1009. }
  1010. that.setData({deviceName:device})
  1011. if(wx.getStorageSync('deviceName') != ''){
  1012. var DeviceName = that.data.deviceName
  1013. for(let m = 0;m < DeviceName.length;m++){
  1014. if(DeviceName[m] == wx.getStorageSync('deviceName')){
  1015. that.setData({
  1016. deviceIndex:m
  1017. })
  1018. }
  1019. }
  1020. }
  1021. else{
  1022. wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex])
  1023. }
  1024. //根据分组名和设备名获取key和id
  1025. var groupNames = that.data.groupName[that.data.groupIndex]
  1026. var deviceNames = that.data.deviceName[that.data.deviceIndex]
  1027. for(let d = 0;d < group.length;d++){
  1028. if(groupNames == group[d].groupName){
  1029. for(let g = 0;g < group[d].devices.length;g++){
  1030. if(deviceNames == group[d].devices[g].deviceName){
  1031. var deviceId = group[d].devices[g].deviceId
  1032. var deviceKey = group[d].devices[g].deviceKey
  1033. }
  1034. }
  1035. }
  1036. }
  1037. that.setData({deviceId:deviceId,deviceKey:deviceKey})
  1038. wx.request({
  1039. url: 'https://api.heclouds.com/devices/' + deviceId,
  1040. method:'GET',
  1041. header:{"api-key": deviceKey},
  1042. success:(res) =>{
  1043. if (res.data.data.online == true) {
  1044. that.setData({
  1045. status:"在线"
  1046. })
  1047. }
  1048. else{
  1049. that.setData({
  1050. status:"离线"
  1051. })
  1052. }
  1053. }
  1054. })
  1055. wx.request({
  1056. url: 'https://api.heclouds.com/devices/' + deviceId + '/datastreams',
  1057. header: {"api-key": deviceKey},
  1058. method:'GET',
  1059. success: function(res){
  1060. let {data} = res.data
  1061. for(let i = 0; i < data.length; i++){
  1062. //时间
  1063. if (data[i].id == "SysTime") {
  1064. var times = data[i].current_value
  1065. }
  1066. else if (data[i].id == "RelayCount") {
  1067. var quantity = data[i].current_value
  1068. that.setData({
  1069. loops:quantity
  1070. })
  1071. }
  1072. //日期
  1073. else if(data[i].id == "SysDate"){
  1074. var dates = data[i].current_value
  1075. }
  1076. //光照度
  1077. else if(data[i].id == "lightLuxValue"){
  1078. var Illuminance = data[i].current_value
  1079. }
  1080. //日出时间
  1081. else if(data[i].id == "RiseTime"){
  1082. var Sunrisetime = data[i].current_value
  1083. }
  1084. //日落时间
  1085. else if(data[i].id == "SetTime"){
  1086. var Sunsettime = data[i].current_value
  1087. }
  1088. //经度
  1089. else if(data[i].id == "Longitude"){
  1090. var longitude = data[i].current_value
  1091. }
  1092. //纬度
  1093. else if(data[i].id == "Latitude"){
  1094. var latitude = data[i].current_value
  1095. }
  1096. //消防报警
  1097. else if(data[i].id == "AlarmState"){
  1098. var Firealarm = data[i].current_value
  1099. }
  1100. //回路数量
  1101. else if(data[i].id == "RelayCount"){
  1102. var loops = data[i].current_value
  1103. }
  1104. //开关延时
  1105. else if (data[i].id == "DeviceID") {
  1106. var device = data[i].current_value
  1107. that.setData({index:device,switchtime:device})
  1108. }
  1109. }
  1110. //时间
  1111. if(times.length == 5){
  1112. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  1113. }
  1114. else if(times.length == 6){
  1115. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  1116. }
  1117. else{
  1118. a = times
  1119. }
  1120. //日期
  1121. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  1122. //日出时间
  1123. var c = Sunrisetime.substring(0,1) + ':' + Sunrisetime.substring(1,3)
  1124. //日落时间
  1125. var d = Sunsettime.substring(0,2) + ':' + Sunsettime.substring(2,4)
  1126. //经度
  1127. var multiIndex =that.data.multiIndex
  1128. if (longitude.length == 5) {
  1129. var e = longitude.substring(0,3) + "," + longitude.substring(3,5) + "'"
  1130. multiIndex[0] = parseInt(longitude.substring(0,3))
  1131. multiIndex[1] = parseInt(longitude.substring(3,5))
  1132. that.setData({
  1133. multiIndex:multiIndex
  1134. })
  1135. }
  1136. else if(longitude.length == 4){
  1137. var e = longitude.substring(0,2) + "," + longitude.substring(2,4) + "'"
  1138. multiIndex[0] = parseInt(longitude.substring(0,2))
  1139. multiIndex[1] = parseInt(longitude.substring(2,4))
  1140. that.setData({
  1141. multiIndex:multiIndex
  1142. })
  1143. }
  1144. else if(longitude.length == 3){
  1145. var e = longitude.substring(0,1) + "," + longitude.substring(1,3) + "'"
  1146. multiIndex[0] = parseInt(longitude.substring(0,1))
  1147. multiIndex[1] = parseInt(longitude.substring(1,3))
  1148. that.setData({
  1149. multiIndex:multiIndex
  1150. })
  1151. }
  1152. else if(longitude.length == 2){
  1153. var e = 0 + "," + longitude.substring(0,2) + "'"
  1154. multiIndex[0] = 0
  1155. multiIndex[1] = parseInt(longitude.substring(0,2))
  1156. that.setData({
  1157. multiIndex:multiIndex
  1158. })
  1159. }
  1160. else{
  1161. var e = 0 + "," + longitude.substring(0,1) + "'"
  1162. multiIndex[0] = 0
  1163. multiIndex[1] = parseInt(longitude.substring(0,1))
  1164. that.setData({
  1165. multiIndex:multiIndex
  1166. })
  1167. }
  1168. //纬度
  1169. var multiIndex2 = that.data.multiIndex2
  1170. if (latitude.length == 5) {
  1171. var f = latitude.substring(0,3) + "," + latitude.substring(3,5) + "'"
  1172. multiIndex2[0] = parseInt(latitude.substring(0,3))
  1173. multiIndex2[1] = parseInt(latitude.substring(3,5))
  1174. that.setData({
  1175. multiIndex2:multiIndex2
  1176. })
  1177. }
  1178. else if(latitude.length == 4){
  1179. var f = latitude.substring(0,2) + "," + latitude.substring(2,4) + "'"
  1180. multiIndex2[0] = parseInt(latitude.substring(0,2))
  1181. multiIndex2[1] = parseInt(latitude.substring(2,4))
  1182. that.setData({
  1183. multiIndex2:multiIndex2
  1184. })
  1185. }
  1186. else if(latitude.length == 3){
  1187. var f = latitude.substring(0,1) + "," + latitude.substring(1,3) + "'"
  1188. multiIndex2[0] = parseInt(latitude.substring(0,1))
  1189. multiIndex2[1] = parseInt(latitude.substring(1,3))
  1190. that.setData({
  1191. multiIndex2:multiIndex2
  1192. })
  1193. }
  1194. else if(latitude.length == 2){
  1195. var f = 0 + "," + latitude.substring(0,2) + "'"
  1196. multiIndex2[0] = 0
  1197. multiIndex2[1] = parseInt(latitude.substring(0,2))
  1198. that.setData({
  1199. multiIndex2:multiIndex2
  1200. })
  1201. }
  1202. else{
  1203. var f = 0 + "," + latitude.substring(0,1) + "'"
  1204. multiIndex2[0] = 0
  1205. multiIndex2[1] = parseInt(latitude.substring(0,1))
  1206. that.setData({
  1207. multiIndex2:multiIndex2
  1208. })
  1209. }
  1210. //光照度
  1211. var g = Illuminance
  1212. //消防报警
  1213. if(Firealarm == "0"){
  1214. that.setData({
  1215. Firealarmtext:"正常",
  1216. Firealarmcolor:"#5cfe03"
  1217. })
  1218. }
  1219. else{
  1220. that.setData({
  1221. Firealarmtext:"异常",
  1222. Firealarmcolor:"red"
  1223. })
  1224. }
  1225. that.setData({
  1226. times:a,
  1227. dates:b,
  1228. Sunrisetime:c,
  1229. Sunsettime:d,
  1230. longitude:e,
  1231. latitude:f,
  1232. Illuminance:g,
  1233. loops:loops,
  1234. })
  1235. }
  1236. })
  1237. wx.request({
  1238.   url: "https://api.heclouds.com/cmds?device_id=" + deviceId,
  1239.   method: 'POST',
  1240.   header: {
  1241.     "api-key": deviceKey
  1242.   },
  1243.   data: {
  1244.     "Reflash": 4 //刷新设备当前时间,并且将设备当前时间上传
  1245.   },
  1246.   success(res) {
  1247. setTimeout(function(){
  1248. wx.request({
  1249. url: 'https://api.heclouds.com/devices/' + that.data.deviceId + '/datastreams',
  1250. method:'GET',
  1251. header:{"api-key": that.data.deviceKey},
  1252. success:ever =>{
  1253. let {data} = ever.data
  1254. for(let i = 0; i < data.length; i++){
  1255. //时间
  1256. if (data[i].id == "SysTime") {
  1257. var times = data[i].current_value
  1258. }
  1259. //日期
  1260. else if(data[i].id == "SysDate"){
  1261. var dates = data[i].current_value
  1262. }
  1263. }
  1264. if(times.length == 5){
  1265. var a = "0" + times.substring(0,1) + ':' + times.substring(1,3) + ':' + times.substring(3,5)
  1266. }
  1267. else if(times.length == 6){
  1268. var a =times.substring(0,2) + ':' + times.substring(2,4) + ':' + times.substring(4,6)
  1269. }
  1270. else{
  1271. a = times
  1272. }
  1273. //日期
  1274. var b ='20' + dates.substring(0,2) + '-' + dates.substring(2,4) + '-' + dates.substring(4,6)
  1275. that.setData({
  1276. times:a,
  1277. dates:b
  1278. })
  1279. }
  1280. })
  1281. },2000)
  1282.   },
  1283. })
  1284. }
  1285. else{
  1286. that.setData({status:'离线',deviceName:[]})
  1287. }
  1288. }
  1289. }
  1290. },
  1291. /**
  1292. * 生命周期函数--监听页面隐藏
  1293. */
  1294. onHide() {
  1295. },
  1296. /**
  1297. * 生命周期函数--监听页面卸载
  1298. */
  1299. onUnload() {
  1300. },
  1301. /**
  1302. * 页面相关事件处理函数--监听用户下拉动作
  1303. */
  1304. onPullDownRefresh() {
  1305. },
  1306. /**
  1307. * 页面上拉触底事件的处理函数
  1308. */
  1309. onReachBottom() {
  1310. },
  1311. /**
  1312. * 用户点击右上角分享
  1313. */
  1314. onShareAppMessage() {
  1315. }
  1316. })