deviceWake.js 15 KB

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