deviceWake.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // pages/deviceWake/deviceWake.js
  2. const app = getApp();
  3. const {
  4. formatNumber
  5. } = require('./../../utils/util.js');
  6. const {
  7. deviceWakedetail
  8. } = require('./../../utils/api.js');
  9. import lexin_devices from '../../utils/lexin/devices';
  10. import lexin_util from '../../utils/lexin/util.js';
  11. let _this = null
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. nvabarData: {
  18. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  19. title: '定时设置', //导航栏 中间的标题
  20. },
  21. switch1Checked: false,
  22. alarm_id_0_time: "00:00:00",
  23. switch2Checked: false,
  24. deviceMac: null,
  25. devicerStatus: false, // false 已断开连接
  26. alarm_id_0: {}, // 休眠
  27. alarm_id_1: {}, // 唤醒
  28. timeindex: 1,
  29. timeArray: [{
  30. id: 600,
  31. name: '10分钟'
  32. },
  33. {
  34. id: 1800,
  35. name: '30分钟'
  36. },
  37. {
  38. id: 3600,
  39. name: '60分钟'
  40. },
  41. {
  42. id: 5400,
  43. name: '90分钟'
  44. }
  45. ],
  46. clientType: "",
  47. time0: '00:00:00',
  48. time: '07:00:00',
  49. repeat: "",
  50. repeat0: "",
  51. week: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  52. week_actives: [],
  53. isOne: true,
  54. wakeName: "",
  55. toastData: {
  56. titlePicUrl: "./../../../img/gt.png",
  57. titlePicUrlWidth: 140,
  58. title: ["温馨提醒"],
  59. info: ["当前设备电量低"],
  60. isShowTwoBut: true,
  61. twoButText: "好的",
  62. footTop: 10,
  63. },
  64. twoButCallback: () => {},
  65. toast_visible: false,
  66. // 后续新加需求
  67. deviceOther: null,
  68. updataDeviceInfo: false,
  69. repeatIndex: 0,
  70. },
  71. /**
  72. * 生命周期函数--监听页面加载
  73. */
  74. onLoad(options) {
  75. _this = this;
  76. var clientType = options.clientType;
  77. var deviceId = options.deviceId;
  78. var deviceMac = lexin_util.getDeviceMacId(deviceId);
  79. // var deviceMac = `AIrSMArT_${deviceId.split(lexin_devices.leXin)[1]}`;
  80. deviceWakedetail({
  81. clientType: clientType,
  82. deviceMac: deviceMac,
  83. }).then((res) => {
  84. if (res && res.length > 0) {
  85. this.setData({
  86. wakeName: res[0].audioName || null
  87. });
  88. }
  89. })
  90. this.data.clientType = options.clientType
  91. this.data.deviceMac = lexin_util.getDeviceMacId(options.deviceId);
  92. // this.data.deviceMac = `AIrSMArT_${options.deviceId.split(lexin_devices.leXin)[1]}`;
  93. app.PubMsg({
  94. type: "get_dev_info",
  95. DstDeviceName: this.data.deviceMac
  96. });
  97. // 赋值按钮回调
  98. this.setData({
  99. twoButCallback: () => {
  100. _this.setData({
  101. toast_visible: false
  102. })
  103. }
  104. })
  105. },
  106. onShow(options) {
  107. app.PubMsg({
  108. type: "get_dev_info",
  109. DstDeviceName: _this.data.deviceMac
  110. });
  111. },
  112. mqttCallback(type, option) {
  113. let payloads = null;
  114. if (option) {
  115. payloads = JSON.parse(option.payload);
  116. };
  117. console.log(payloads)
  118. switch (type) {
  119. case "message_onoffline":
  120. if (payloads.state !== "online") {
  121. wx.showToast({
  122. title: '设备已断开连接',
  123. icon: 'none',
  124. duration: 2000
  125. });
  126. _this.setData({
  127. devicerStatus: false,
  128. })
  129. } else {
  130. _this.setData({
  131. devicerStatus: true,
  132. })
  133. }
  134. break;
  135. case "message":
  136. // 设备信息
  137. if (payloads.type === "get_dev_info") {
  138. _this.setData({
  139. deviceOther: payloads.other,
  140. updataDeviceInfo: true,
  141. });
  142. if (payloads.other.alarm) {
  143. payloads.other.alarm.map((v) => {
  144. if (v.alarm_id === "0") {
  145. v.action = "update";
  146. // 休眠
  147. _this.setData({
  148. alarm_id_0: v
  149. });
  150. // 定时时间
  151. if (v.on_off_timestamp !== "") {
  152. _this.setData({
  153. alarm_id_0_time: v.on_off_timestamp
  154. })
  155. }
  156. // 是否启用
  157. if (v.enable === "1") {
  158. _this.setData({
  159. switch1Checked: true
  160. })
  161. } else {
  162. _this.setData({
  163. switch1Checked: false
  164. })
  165. }
  166. // 重复
  167. if (v.week_actives) {
  168. _this.setData({
  169. repeat0: _this.repeatText(v.week_actives),
  170. })
  171. }
  172. } else if (v.alarm_id === "1") {
  173. // 唤醒
  174. _this.setData({
  175. alarm_id_1: v
  176. });
  177. // 定时时间
  178. if (v.on_off_timestamp !== "") {
  179. _this.setData({
  180. time: v.on_off_timestamp
  181. })
  182. }
  183. // 是否启用
  184. if (v.enable === "1") {
  185. _this.setData({
  186. switch2Checked: true
  187. })
  188. } else {
  189. _this.setData({
  190. switch2Checked: false
  191. })
  192. }
  193. // 重复
  194. if (v.week_actives) {
  195. _this.setData({
  196. repeat: _this.repeatText(v.week_actives),
  197. })
  198. }
  199. }
  200. })
  201. }
  202. } else if (payloads.type === "alert_set") {
  203. if (payloads.code === 200) {
  204. app.PubMsg({
  205. type: "get_dev_info",
  206. DstDeviceName: _this.data.deviceMac
  207. });
  208. } else {
  209. wx.showModal({
  210. title: '保存失败',
  211. content: '请保持设备网络通畅并检查是否已连接电源?',
  212. success(res) {
  213. if (res.confirm) {
  214. console.log('用户点击确定')
  215. } else if (res.cancel) {
  216. console.log('用户点击取消')
  217. }
  218. }
  219. })
  220. }
  221. }
  222. default:
  223. }
  224. },
  225. // 休眠-选择时间
  226. switch1Change(e) {
  227. let other = this.data.alarm_id_0;
  228. if (e.detail.value) {
  229. _this.setData({
  230. switch1Checked: e.detail.value
  231. });
  232. const thisTime = Math.round(new Date() / 1000);
  233. other = this.setOther(other, 1);
  234. other.on_off_timestamp = _this.data.alarm_id_0_time;
  235. // 有重复
  236. if (other.week_actives && other.week_actives.filter((v) => v === 1).length > 0) {
  237. // other.week_actives = [0,1,1,1,1,1,0],
  238. other.weekly_repeat = "1";
  239. } else {
  240. other.weekly_repeat = "0";
  241. };
  242. // 如果跨天了
  243. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  244. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  245. other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  246. };
  247. app.PubMsg({
  248. type: "alert_set",
  249. DstDeviceName: _this.data.deviceMac,
  250. other
  251. });
  252. } else {
  253. _this.setData({
  254. switch1Checked: e.detail.value
  255. });
  256. other = this.setOther(other, 0);
  257. app.PubMsg({
  258. type: "alert_set",
  259. DstDeviceName: _this.data.deviceMac,
  260. other
  261. });
  262. }
  263. },
  264. switch2Change(e) {
  265. _this.data.updataDeviceInfo = false;
  266. let other = _this.data.alarm_id_1;
  267. if (e.detail.value) {
  268. if (_this.data.deviceOther && _this.data.deviceOther.Power < 20) {
  269. _this.setData({
  270. toast_visible: true,
  271. });
  272. };
  273. // app.PubMsg({
  274. // type: "get_dev_info",
  275. // DstDeviceName: _this.data.deviceMac
  276. // });
  277. if (!other) {
  278. wx.showToast({
  279. title: '设备已断开连接',
  280. icon: "none"
  281. })
  282. return
  283. }
  284. other.on_off_timestamp = _this.data.time;
  285. other = this.setOther(other, 1);
  286. let week_actives = other.week_actives ?? []
  287. if (week_actives.filter((v) => v === 1).length > 0) {
  288. // other.week_actives = [0,1,1,1,1,1,0],
  289. other.weekly_repeat = "1";
  290. } else {
  291. other.weekly_repeat = "0";
  292. };
  293. app.PubMsg({
  294. type: "alert_set",
  295. DstDeviceName: _this.data.deviceMac,
  296. other
  297. });
  298. // 不要循环发送,也不要改alarm_id_0.enable
  299. // 循环查询
  300. // const Interval = setInterval(() => {
  301. // if (_this.data.updataDeviceInfo) {
  302. // clearInterval(Interval);
  303. // other.on_off_timestamp = _this.data.time;
  304. // other = this.setOther(other, 1);
  305. // if (other.week_actives.filter((v) => v === 1).length > 0) {
  306. // // other.week_actives = [0,1,1,1,1,1,0],
  307. // other.weekly_repeat = "1";
  308. // } else {
  309. // other.weekly_repeat = "0";
  310. // };
  311. // app.PubMsg({
  312. // type: "alert_set",
  313. // DstDeviceName: _this.data.deviceMac,
  314. // other
  315. // });
  316. // 需打开休眠
  317. // if (_this.data.alarm_id_0.enable === "0") {
  318. // const thisTime = Math.round(new Date() / 1000);
  319. // const alarm_id_0 = this.setOther(_this.data.alarm_id_0, 1);
  320. // alarm_id_0.on_off_timestamp = _this.data.alarm_id_0_time;
  321. // // 有重复
  322. // if (alarm_id_0.week_actives.filter((v) => v === 1).length > 0) {
  323. // alarm_id_0.weekly_repeat = "1";
  324. // } else {
  325. // alarm_id_0.weekly_repeat = "0";
  326. // };;
  327. // // 如果跨天了
  328. // if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  329. // const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  330. // alarm_id_0.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  331. // };
  332. // app.PubMsg({
  333. // type: "alert_set",
  334. // DstDeviceName: _this.data.deviceMac,
  335. // other: alarm_id_0
  336. // });
  337. // }
  338. // }
  339. // }, 500);
  340. } else {
  341. other = this.setOther(other, 0);
  342. app.PubMsg({
  343. type: "alert_set",
  344. DstDeviceName: _this.data.deviceMac,
  345. other
  346. });
  347. }
  348. },
  349. // bindPickerChange(e) {
  350. // let other = this.data.alarm_id_0;
  351. // const thisTime = Math.round(new Date() / 1000);
  352. // other = this.setOther(other, 1);
  353. // other.on_off_timestamp = this.formatTime(thisTime + this.data.timeArray[e.detail.value].id, "h:m:s");
  354. // // 如果跨天了
  355. // if(parseInt(this.formatTime(thisTime + this.data.timeArray[e.detail.value].id, "D")) > parseInt(this.formatTime(thisTime, "D"))) {
  356. // const date = new Date(_this.formatTime(thisTime, "Y-M-D") + " 00:00:00");
  357. // other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  358. // };
  359. // app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
  360. // },
  361. bindTimeChange0: function (e) {
  362. this.setData({
  363. alarm_id_0_time: e.detail.value + ":00"
  364. });
  365. let other = this.data.alarm_id_0;
  366. other.on_off_timestamp = e.detail.value + ":00";
  367. other = this.setOther(other, 1);
  368. // 有重复
  369. if (other.week_actives.filter((v) => v === 1).length > 0) {
  370. // other.week_actives = [0,1,1,1,1,1,0],
  371. other.weekly_repeat = "1";
  372. } else {
  373. other.weekly_repeat = "0";
  374. };
  375. // 如果跨天了
  376. const thisTime = Math.round(new Date() / 1000);
  377. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  378. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  379. other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  380. };
  381. app.PubMsg({
  382. type: "alert_set",
  383. DstDeviceName: _this.data.deviceMac,
  384. other
  385. });
  386. },
  387. bindTimeChange: function (e) {
  388. this.setData({
  389. time: e.detail.value + ":00"
  390. });
  391. let other = this.data.alarm_id_1;
  392. other.on_off_timestamp = e.detail.value + ":00";
  393. other = this.setOther(other, 1);
  394. app.PubMsg({
  395. type: "alert_set",
  396. DstDeviceName: _this.data.deviceMac,
  397. other
  398. });
  399. },
  400. setRepeat(arr) {
  401. let other = this.data.repeatIndex === "0" ? _this.data.alarm_id_0 : _this.data.alarm_id_1;
  402. other = this.setOther(other, 1);
  403. other.week_actives = arr;
  404. other.weekly_repeat = arr.filter((v) => v === 1).length > 0 ? "1" : "0";
  405. // 倒计时
  406. if (this.data.repeatIndex === "0") {
  407. // 如果跨天了
  408. const thisTime = Math.round(new Date() / 1000);
  409. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  410. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  411. other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  412. };
  413. }
  414. app.PubMsg({
  415. type: "alert_set",
  416. DstDeviceName: _this.data.deviceMac,
  417. other
  418. });
  419. },
  420. goWakeList() {
  421. wx.navigateTo({
  422. url: './../wakeList/wakeList?clientType=' + this.data.clientType + "&deviceMac=" + this.data.deviceMac,
  423. })
  424. },
  425. gorepeat(e) {
  426. this.data.repeatIndex = e.currentTarget.dataset.index;
  427. this.data.week_actives = e.currentTarget.dataset.index === "0" ? _this.data.alarm_id_0.week_actives : _this.data.alarm_id_1.week_actives;
  428. wx.navigateTo({
  429. url: './../repeat/repeat?week_actives=' + this.data.week_actives
  430. })
  431. },
  432. setdeviceWakedetail() {
  433. deviceWakedetail({
  434. clientType: this.data.clientType,
  435. deviceMac: this.data.deviceMac,
  436. }).then((res) => {
  437. this.setData({
  438. wakeName: res[0].audioName
  439. })
  440. })
  441. },
  442. /**
  443. * 时间戳转化为年 月 日 时 分 秒
  444. * number: 传入时间戳
  445. * format:返回格式,支持自定义,但参数必须与formateArr里保持一致
  446. */
  447. formatTime(number, format) {
  448. var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  449. var returnArr = [];
  450. var date = new Date(number * 1000);
  451. returnArr.push(date.getFullYear());
  452. returnArr.push(formatNumber(date.getMonth() + 1));
  453. returnArr.push(formatNumber(date.getDate()));
  454. returnArr.push(formatNumber(date.getHours()));
  455. returnArr.push(formatNumber(date.getMinutes()));
  456. returnArr.push(formatNumber(date.getSeconds()));
  457. for (var i in returnArr) {
  458. format = format.replace(formateArr[i], returnArr[i]);
  459. }
  460. return format;
  461. },
  462. // type: 0 关闭 1 开启
  463. setOther(other, type) {
  464. if (type === 0) {
  465. other.enable = "0";
  466. other.alarm_name = "close";
  467. other.operation = "off";
  468. } else if (type === 1) {
  469. other.enable = "1";
  470. other.alarm_name = "open";
  471. other.operation = "on";
  472. }
  473. const thisTime = Math.round(new Date() / 1000);
  474. other.current_timestamp = this.formatTime(thisTime, "Y-M-D h:m:s");
  475. other.action = "update",
  476. other.song_uri = "-1";
  477. other.version = 3;
  478. other.is_debug = app.globalData.is_debug
  479. return other
  480. },
  481. // 判断当天时间前后
  482. compareDate(t1, t2) {
  483. var date = new Date();
  484. var a = t1.split(":");
  485. var b = t2.split(":");
  486. return date.setHours(a[0], a[1]) > date.setHours(b[0], b[1]);
  487. },
  488. repeatText(arr) {
  489. let text = "";
  490. if (arr.toString() === [0, 1, 1, 1, 1, 1, 0].toString()) {
  491. text = "工作日";
  492. } else if (arr.toString() === [1, 0, 0, 0, 0, 0, 1].toString()) {
  493. text = "周末";
  494. } else {
  495. arr.map((v, index) => {
  496. if (v === 1) {
  497. if (text === "") {
  498. text = text + _this.data.week[index]
  499. } else {
  500. text = text + "、" + _this.data.week[index];
  501. }
  502. }
  503. });
  504. if (text === "") {
  505. text = "不重复";
  506. };
  507. }
  508. return text.length < 20 ? text : "每日";
  509. }
  510. })