scene.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. const {postGroup,getGroup,putGroup,delGroup} = require('../../api/group')
  2. const {postDevice,getDevice,putDevice,delDevice} = require('../../api/device')
  3. const {getBasic,getData,postData} = require('../../api/intell')
  4. // pages/scene/scene.js
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. remove:true,//删除工程
  11. Modify:true,//修改工程名称
  12. array:[],//分组
  13. brray:[],
  14. keyid:[],
  15. inputVal:[],
  16. getName:[],//发送往服务器的工程名称参数
  17. subscript:0,
  18. Id:[],
  19. key:[],
  20. developline:[],
  21. status:0,
  22. Status:"",
  23. phone:'',
  24. displaypicture:"https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/ling.png",
  25. display:[]
  26. },
  27. //刷新列表
  28. Refreshlist:function(){
  29. wx.showToast({
  30. title:'刷新中',
  31. icon:'loading',
  32. duration: 2000
  33. })
  34. var that = this
  35. getGroup().then(res => {
  36. if(res.data.code == 0){
  37. var data = res.data.data
  38. if(data != null){
  39. for(let i = 0;i <data.length;i++){
  40. if(data[i].devices != null){
  41. for(let j = 0;j < data[i].devices.length;j++){
  42. if(data[i].devices[j].module == 0){
  43. let id = data[i].devices[j].deviceId
  44. let key = data[i].devices[j].deviceKey
  45. getBasic(id,key).then(ever =>{
  46. if(ever.data.errno == 0){
  47. if(ever.data.data.online == true){
  48. data[i].devices[j].status = '在线'
  49. }
  50. else{
  51. data[i].devices[j].status = '离线'
  52. }
  53. wx.setStorageSync('group',data)
  54. let tgroups = wx.getStorageSync('group')
  55. for(let i = 0;i < tgroups.length;i++){
  56. if(tgroups[i].groupName.length > 10){
  57. tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
  58. }
  59. }
  60. that.setData({array:tgroups})
  61. }
  62. else{
  63. wx.showModal({
  64. title:'错误',
  65. content:ever.data.error,
  66. showCancel:false
  67. })
  68. }
  69. })
  70. }
  71. else{
  72. let status = data[i].devices[j].status
  73. switch(status){
  74. case 0:
  75. data[i].devices[j].status = '离线'
  76. break;
  77. case 1:
  78. data[i].devices[j].status = '在线'
  79. break
  80. }
  81. wx.setStorageSync('group',data)
  82. let tgroups = wx.getStorageSync('group')
  83. for(let i = 0;i < tgroups.length;i++){
  84. if(tgroups[i].groupName.length > 10){
  85. tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
  86. }
  87. }
  88. that.setData({array:tgroups})
  89. }
  90. }
  91. }
  92. else{
  93. wx.setStorageSync('group',data)
  94. let group = wx.getStorageSync('group')
  95. for(let i = 0;i < group.length;i++){
  96. if(group[i].groupName.length > 10){
  97. group[i].groupName = group[i].groupName.slice(0,9) + '....'
  98. }
  99. }
  100. that.setData({array:group})
  101. }
  102. }
  103. }
  104. }
  105. else{
  106. wx.showModal({
  107. title:'错误',
  108. content:res.data.msg,
  109. showCancel:false
  110. })
  111. }
  112. })
  113. },
  114. //添加分组
  115. apption:function(){
  116. var that = this
  117. wx.showModal({
  118. title:'添加工程',
  119. editable:true,
  120. placeholderText:'请输入名称',
  121. success:function(ever){
  122. if(ever.confirm){
  123. if (ever.content == '') {
  124. wx.showModal({
  125. title:'提示',
  126. content:'请输入工程名',
  127. showCancel:false
  128. })
  129. }
  130. else if(ever.content.length > 15){
  131. wx.showModal({
  132. title:'提示',
  133. content:'分组名称不能大于15个字符',
  134. showCancel:false
  135. })
  136. }
  137. else{
  138. postGroup({groupName:ever.content}).then((res)=>{
  139. if(res.data.code == 0){
  140. if(wx.getStorageSync('group') == ''){
  141. wx.setStorageSync('group', [])
  142. var a = wx.getStorageSync('group')
  143. a.push({id:res.data.data.id,groupName:ever.content,devices:[]})
  144. wx.setStorageSync('group', a)
  145. }
  146. else{
  147. var b = wx.getStorageSync('group')
  148. b.push({id:res.data.data.id,groupName:ever.content,devices:[]})
  149. wx.setStorageSync('group', b)
  150. }
  151. var group = wx.getStorageSync('group')
  152. for(let i = 0;i < group.length;i++){
  153. if(group[i].groupName.length > 10){
  154. group[i].groupName = group[i].groupName.slice(0,9) + '....'
  155. }
  156. }
  157. that.setData({array:group})
  158. }
  159. else{
  160. wx.showModal({
  161. title:'错误',
  162. content:res.data.msg,
  163. showCancel:false
  164. })
  165. }
  166. })
  167. }
  168. }
  169. }
  170. })
  171. },
  172. //删除分组
  173. removeapption:function(e){
  174. var that = this
  175. wx.showModal({
  176. title:'提示',
  177. content:'确定删除该工程',
  178. success:function(res){
  179. if (res.confirm) {
  180. var index = e.currentTarget.dataset.indexdel//获取对应下标
  181. var Idindex = that.data.array[index].id
  182. var groupName = that.data.array[index].groupName
  183. delGroup(Idindex).then((res)=>{
  184. if(res.data.code == 0){
  185. if(groupName == wx.getStorageSync('groupName')){
  186. wx.setStorageSync('groupName', '')
  187. wx.setStorageSync('deviceName', '')
  188. }
  189. var deleteid = wx.getStorageSync('group')
  190. deleteid.splice(index,1)
  191. wx.setStorageSync('group', deleteid)
  192. var Group = wx.getStorageSync('group')
  193. for(let i = 0;i < Group.length;i++){
  194. if(Group[i].groupName.length > 10){
  195. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  196. }
  197. }
  198. that.setData({array: Group})
  199. }
  200. else{
  201. wx.showModal({
  202. title:'提示',
  203. content:res.data.msg,
  204. showCancel:false
  205. })
  206. }
  207. })
  208. }
  209. }
  210. })
  211. },
  212. //修改分组名称
  213. ShowChanges:function(e){
  214. var that = this
  215. wx.showModal({
  216. title:'修改工程名称',
  217. editable:true,
  218. placeholderText:'请输入名称',
  219. success:res =>{
  220. if (res.confirm) {
  221. var index = e.currentTarget.dataset.indexdel//获取对应下标
  222. var Idindex = that.data.array[index].id//获取对应下标的id值
  223. var groupName = that.data.array[index].name
  224. if (res.content == '') {
  225. wx.showModal({
  226. title:'提示',
  227. content:'请输入工程名',
  228. showCancel:false
  229. })
  230. }
  231. else if(res.content.length > 15){
  232. wx.showModal({
  233. title:'提示',
  234. content:'分组名称不能大于15个字符',
  235. showCancel:false
  236. })
  237. }
  238. else{
  239. putGroup({id:Idindex,groupName:res.content}).then(ever=>{
  240. if (ever.data.code == 0) {
  241. if(groupName == wx.getStorageSync('groupName')){
  242. wx.setStorageSync('groupName',res.content)
  243. }
  244. let group = wx.getStorageSync('group')
  245. group[index].groupName = res.content
  246. wx.setStorageSync('group',group)
  247. var Group = wx.getStorageSync('group')
  248. for(let i = 0;i < Group.length;i++){
  249. if(Group[i].groupName.length > 10){
  250. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  251. }
  252. }
  253. that.setData({array:Group})
  254. }
  255. else{
  256. wx.showModal({
  257. title:'提示',
  258. content:ever.data.msg,
  259. showCancel:false
  260. })
  261. }
  262. })
  263. }
  264. }
  265. }
  266. })
  267. },
  268. //扫码(添加设备)
  269. scanCodeEvent: function(e){
  270. var that = this
  271. var index = e.currentTarget.dataset.indexdel//获取对应下标
  272. var Idindex = that.data.array[index].id//分组Id
  273. Idindex = parseInt(Idindex)
  274. var group = wx.getStorageSync('group')
  275. //获取设备
  276. wx.scanCode({
  277. success(res){
  278. if(res.result.substring(0,1) == '{'){
  279. let parameter = res.result.match(/\d+(\.\d+)?/g)
  280. let ID = parameter[0]
  281. let SN = parameter[1]
  282. //添加设备
  283. postDevice({groupId:Idindex,deviceSn:SN,deviceId:ID,module:1,deviceName:SN}).then(add =>{
  284. if(add.data.code == 0){
  285. //查询设备
  286. getDevice(add.data.data.id,Idindex).then(get =>{
  287. var Get = get.data.data
  288. switch(Get[0].status){
  289. case 0:
  290. Get[0].status = '离线'
  291. break
  292. case 1:
  293. Get[0].status = '在线'
  294. break
  295. }
  296. if(group[index].devices != null){
  297. group[index].devices.push(Get[0])
  298. }
  299. else{
  300. group[index].devices = []
  301. group[index].devices.push(Get[0])
  302. }
  303. wx.setStorageSync('group', group)
  304. var Group = wx.getStorageSync('group')
  305. for(let i = 0;i < Group.length;i++){
  306. if(Group[i].groupName.length > 10){
  307. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  308. }
  309. }
  310. that.setData({array: Group})
  311. })
  312. }
  313. else{
  314. wx.showModal({
  315. title:'提示',
  316. content:add.data.msg,
  317. showCancel:false
  318. })
  319. }
  320. })
  321. }
  322. else{
  323. var arr = res.result.split(' ');
  324. var a = arr[0]//设备id
  325. var b = arr[1]//设备key
  326. //获取该设备的在线状态和编码
  327. getBasic(a,b).then(ever=>{
  328. var online = ever.data.data.online//设备在线状态
  329. switch (online) {
  330. case true:
  331. that.setData({status:1})
  332. that.setData({Status:"在线"})
  333. break;
  334. default:
  335. that.setData({status:0})
  336. that.setData({Status:"离线"})
  337. break;
  338. }
  339. var title = ever.data.data.title//设备编码
  340. getData(a,b).then(gu =>{
  341. let trys = gu.data.data
  342. for(let w = 0;w < trys.length;w++){
  343. if(trys[w].id == "RelayCount"){
  344. var num = trys[w].current_value
  345. num = parseInt(num)
  346. }
  347. }
  348. var numName = ''
  349. for(let p = 0;p < num;p++){
  350. numName = numName + '回路' + (p + 1) + ','
  351. }
  352. numName = numName.slice(0,numName.length - 1)
  353. //添加设备
  354. postDevice({groupId:Idindex,deviceSn:title,deviceName:title,deviceId:a,deviceKey:b,status:that.data.status,circuitNum:num,circuits:numName,module:0})
  355. .then(su => {
  356. if (su.data.code == 0){
  357. //将设备的信息储存至本地
  358. if(group[index].devices != null){
  359. group[index].devices.push({
  360. id:su.data.data.id,
  361. deviceSn:title,
  362. deviceName:title,
  363. status:that.data.Status,
  364. deviceId:a,
  365. deviceKey:b,
  366. circuitNum:num,
  367. circuits:numName,
  368. module:0
  369. })
  370. }
  371. else{
  372. group[index].devices = []
  373. group[index].devices.push({
  374. id:su.data.data.id,
  375. deviceSn:title,
  376. deviceName:title,
  377. status:that.data.Status,
  378. deviceId:a,
  379. deviceKey:b,
  380. circuitNum:num,
  381. circuits:numName,
  382. module:0
  383. })
  384. }
  385. wx.setStorageSync('group', group)
  386. var Group = wx.getStorageSync('group')
  387. for(let i = 0;i < Group.length;i++){
  388. if(Group[i].groupName.length > 10){
  389. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  390. }
  391. }
  392. that.setData({array: Group})
  393. }
  394. else{
  395. wx.showModal({
  396. title:'提示',
  397. content:su.data.msg,
  398. showCancel:false
  399. })
  400. }
  401. })
  402. })
  403. })
  404. }
  405. }
  406. })
  407. },
  408. //修改设备名称
  409. modifyequipmentname:function(e){
  410. var that = this
  411. var key = e.currentTarget.dataset.indexdel
  412. var group = wx.getStorageSync('group')
  413. var deviceId = group[key[0]].devices[key[1]].id
  414. var groupName = group[key[0]].groupName//获取当前的分组名称
  415. var groupId = group[key[0]].id
  416. var deviceName = group[key[0]].devices[key[1]].deviceName//获取当前的设备名称
  417. wx.showModal({
  418. title:'修改设备名称',
  419. editable:true,
  420. placeholderText:'请输入名称',
  421. success:res =>{
  422. if (res.confirm) {
  423. if (res.content == '') {
  424. wx.showModal({
  425. title:'提示',
  426. showCancel:false,
  427. content:'请输入名称'
  428. })
  429. }
  430. else if(res.content.length > 10){
  431. wx.showModal({
  432. title:'提示',
  433. showCancel:false,
  434. content:'设备名称不能大于10个字符'
  435. })
  436. }
  437. else{
  438. putDevice({deviceName:res.content,id:deviceId,groupId:groupId}).then(ever=>{
  439. if(ever.data.code == 0){
  440. if(groupName == wx.getStorageSync('groupName') && deviceName == wx.getStorageSync('deviceName')){
  441. wx.setStorageSync('deviceName',res.content)
  442. }
  443. group[key[0]].devices[key[1]].deviceName = res.content
  444. wx.setStorageSync('group', group)
  445. var Group = wx.getStorageSync('group')
  446. for(let i = 0;i < Group.length;i++){
  447. if(Group[i].groupName.length > 10){
  448. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  449. }
  450. }
  451. that.setData({array: Group})
  452. }
  453. else{
  454. wx.showModal({
  455. title:'错误',
  456. content:ever.data.msg,
  457. showCancel:false
  458. })
  459. }
  460. })
  461. }
  462. }
  463. }
  464. })
  465. },
  466. //删除设备
  467. Deletedevice:function(e){
  468. var that = this
  469. wx.showModal({
  470. title:'删除设备',
  471. content:'确定删除该设备吗?',
  472. success:function(res){
  473. if(res.confirm){
  474. let key = e.currentTarget.dataset.indexdel//获得分组和设备的下标值([分组下标,设备下标])
  475. let group = wx.getStorageSync('group')
  476. let subscript = group[key[0]].devices[key[1]].id//设备对应的id
  477. var groupName = group[key[0]].groupName
  478. var deviceName = group[key[0]].devices[key[1]].deviceName
  479. delDevice(subscript).then(nrg=>{
  480. if (nrg.data.code == 0) {
  481. group[key[0]].devices.splice(key[1],1)
  482. wx.setStorageSync('group', group)
  483. var Group = wx.getStorageSync('group')
  484. for(let i = 0;i < Group.length;i++){
  485. if(Group[i].groupName.length > 10){
  486. Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
  487. }
  488. }
  489. that.setData({array: Group})
  490. if(groupName == wx.getStorageSync('groupName') && deviceName == wx.getStorageSync('deviceName')){
  491. wx.setStorageSync('deviceName','')
  492. }
  493. }
  494. else{
  495. wx.showModal({
  496. title:'提示',
  497. content:nrg.data.msg,
  498. showCancel:false
  499. })
  500. }
  501. })
  502. }
  503. }
  504. })
  505. },
  506. //设备的显示隐藏
  507. Develop:function(e){
  508. var that = this
  509. let array = that.data.array
  510. var pd = e.currentTarget.dataset.indexdel
  511. for(let item =0;item < array.length;item++){
  512. if (array[item].id == array[pd].id) {
  513. //判断当前对象中的insert是否为true(true为显示,其他为隐藏) insert是新增的一个值然后进行判断
  514. if (array[item].insert == "" || array[item].insert == undefined) {
  515. array[item].insert = "true"
  516. } else {
  517. array[item].insert = ""
  518. }
  519. }
  520. }
  521. that.setData({
  522. array:array
  523. })
  524. },
  525. /**
  526. * 生命周期函数--监听页面加载
  527. */
  528. onLoad(options) {
  529. wx.showToast({
  530. title:'获取数据中',
  531. icon:'loading',
  532. duration: 2000
  533. })
  534. var that = this
  535. getGroup().then(res => {
  536. if(res.data.code == 0){
  537. var data = res.data.data
  538. if(data != null){
  539. for(let i = 0;i <data.length;i++){
  540. if(data[i].devices != null){
  541. for(let j = 0;j < data[i].devices.length;j++){
  542. if(data[i].devices[j].module == 0){
  543. let id = data[i].devices[j].deviceId
  544. let key = data[i].devices[j].deviceKey
  545. getBasic(id,key).then(ever =>{
  546. if(ever.data.errno == 0){
  547. if(ever.data.data.online == true){
  548. data[i].devices[j].status = '在线'
  549. }
  550. else{
  551. data[i].devices[j].status = '离线'
  552. }
  553. wx.setStorageSync('group',data)
  554. let tgroups = wx.getStorageSync('group')
  555. for(let i = 0;i < tgroups.length;i++){
  556. if(tgroups[i].groupName.length > 10){
  557. tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
  558. }
  559. }
  560. that.setData({array:tgroups})
  561. }
  562. else{
  563. wx.showModal({
  564. title:'错误',
  565. content:ever.data.error,
  566. showCancel:false
  567. })
  568. }
  569. })
  570. }
  571. else{
  572. let status = data[i].devices[j].status
  573. switch(status){
  574. case 0:
  575. data[i].devices[j].status = '离线'
  576. break;
  577. case 1:
  578. data[i].devices[j].status = '在线'
  579. break
  580. }
  581. wx.setStorageSync('group',data)
  582. let tgroups = wx.getStorageSync('group')
  583. for(let i = 0;i < tgroups.length;i++){
  584. if(tgroups[i].groupName.length > 10){
  585. tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
  586. }
  587. }
  588. that.setData({array:tgroups})
  589. }
  590. }
  591. }
  592. else{
  593. wx.setStorageSync('group',data)
  594. let group = wx.getStorageSync('group')
  595. for(let i = 0;i < group.length;i++){
  596. if(group[i].groupName.length > 10){
  597. group[i].groupName = group[i].groupName.slice(0,9) + '....'
  598. }
  599. }
  600. that.setData({array:group})
  601. }
  602. }
  603. }
  604. }
  605. else{
  606. wx.showModal({
  607. title:'错误',
  608. content:res.data.msg,
  609. showCancel:false
  610. })
  611. }
  612. })
  613. },
  614. /**
  615. * 生命周期函数--监听页面初次渲染完成
  616. */
  617. onReady() {
  618. },
  619. /**
  620. * 生命周期函数--监听页面显示
  621. */
  622. onShow() {
  623. },
  624. /**
  625. * 生命周期函数--监听页面隐藏
  626. */
  627. onHide() {
  628. },
  629. /**
  630. * 生命周期函数--监听页面卸载
  631. */
  632. onUnload() {
  633. },
  634. /**
  635. * 页面相关事件处理函数--监听用户下拉动作
  636. */
  637. onPullDownRefresh() {
  638. },
  639. /**
  640. * 页面上拉触底事件的处理函数
  641. */
  642. onReachBottom() {
  643. },
  644. /**
  645. * 用户点击右上角分享
  646. */
  647. onShareAppMessage() {
  648. }
  649. })