deviceWake.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // 循环查询
  272. const Interval = setInterval(() => {
  273. if (_this.data.updataDeviceInfo) {
  274. clearInterval(Interval);
  275. other.on_off_timestamp = _this.data.time;
  276. other = this.setOther(other, 1);
  277. if (other.week_actives.filter((v) => v === 1).length > 0) {
  278. // other.week_actives = [0,1,1,1,1,1,0],
  279. other.weekly_repeat = "1";
  280. } else {
  281. other.weekly_repeat = "0";
  282. };
  283. app.PubMsg({
  284. type: "alert_set",
  285. DstDeviceName: _this.data.deviceMac,
  286. other
  287. });
  288. // 需打开休眠
  289. if (_this.data.alarm_id_0.enable === "0") {
  290. const thisTime = Math.round(new Date() / 1000);
  291. const alarm_id_0 = this.setOther(_this.data.alarm_id_0, 1);
  292. alarm_id_0.on_off_timestamp = _this.data.alarm_id_0_time;
  293. // 有重复
  294. if (alarm_id_0.week_actives.filter((v) => v === 1).length > 0) {
  295. alarm_id_0.weekly_repeat = "1";
  296. } else {
  297. alarm_id_0.weekly_repeat = "0";
  298. };;
  299. // 如果跨天了
  300. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  301. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  302. alarm_id_0.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  303. };
  304. app.PubMsg({
  305. type: "alert_set",
  306. DstDeviceName: _this.data.deviceMac,
  307. other: alarm_id_0
  308. });
  309. }
  310. }
  311. }, 500);
  312. } else {
  313. other = this.setOther(other, 0);
  314. app.PubMsg({
  315. type: "alert_set",
  316. DstDeviceName: _this.data.deviceMac,
  317. other
  318. });
  319. }
  320. },
  321. // bindPickerChange(e) {
  322. // let other = this.data.alarm_id_0;
  323. // const thisTime = Math.round(new Date() / 1000);
  324. // other = this.setOther(other, 1);
  325. // other.on_off_timestamp = this.formatTime(thisTime + this.data.timeArray[e.detail.value].id, "h:m:s");
  326. // // 如果跨天了
  327. // if(parseInt(this.formatTime(thisTime + this.data.timeArray[e.detail.value].id, "D")) > parseInt(this.formatTime(thisTime, "D"))) {
  328. // const date = new Date(_this.formatTime(thisTime, "Y-M-D") + " 00:00:00");
  329. // other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  330. // };
  331. // app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
  332. // },
  333. bindTimeChange0: function (e) {
  334. this.setData({
  335. alarm_id_0_time: e.detail.value + ":00"
  336. });
  337. let other = this.data.alarm_id_0;
  338. other.on_off_timestamp = e.detail.value + ":00";
  339. other = this.setOther(other, 1);
  340. // 有重复
  341. if (other.week_actives.filter((v) => v === 1).length > 0) {
  342. // other.week_actives = [0,1,1,1,1,1,0],
  343. other.weekly_repeat = "1";
  344. } else {
  345. other.weekly_repeat = "0";
  346. };
  347. // 如果跨天了
  348. const thisTime = Math.round(new Date() / 1000);
  349. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  350. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  351. other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  352. };
  353. app.PubMsg({
  354. type: "alert_set",
  355. DstDeviceName: _this.data.deviceMac,
  356. other
  357. });
  358. },
  359. bindTimeChange: function (e) {
  360. this.setData({
  361. time: e.detail.value + ":00"
  362. });
  363. let other = this.data.alarm_id_1;
  364. other.on_off_timestamp = e.detail.value + ":00";
  365. other = this.setOther(other, 1);
  366. app.PubMsg({
  367. type: "alert_set",
  368. DstDeviceName: _this.data.deviceMac,
  369. other
  370. });
  371. },
  372. setRepeat(arr) {
  373. let other = this.data.repeatIndex === "0" ? _this.data.alarm_id_0 : _this.data.alarm_id_1;
  374. other = this.setOther(other, 1);
  375. other.week_actives = arr;
  376. other.weekly_repeat = arr.filter((v) => v === 1).length > 0 ? "1" : "0";
  377. // 倒计时
  378. if (this.data.repeatIndex === "0") {
  379. // 如果跨天了
  380. const thisTime = Math.round(new Date() / 1000);
  381. if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
  382. const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  383. other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  384. };
  385. }
  386. app.PubMsg({
  387. type: "alert_set",
  388. DstDeviceName: _this.data.deviceMac,
  389. other
  390. });
  391. },
  392. goWakeList() {
  393. wx.navigateTo({
  394. url: './../wakeList/wakeList?clientType=' + this.data.clientType + "&deviceMac=" + this.data.deviceMac,
  395. })
  396. },
  397. gorepeat(e) {
  398. this.data.repeatIndex = e.currentTarget.dataset.index;
  399. this.data.week_actives = e.currentTarget.dataset.index === "0" ? _this.data.alarm_id_0.week_actives : _this.data.alarm_id_1.week_actives;
  400. wx.navigateTo({
  401. url: './../repeat/repeat?week_actives=' + this.data.week_actives
  402. })
  403. },
  404. setdeviceWakedetail() {
  405. deviceWakedetail({
  406. clientType: this.data.clientType,
  407. deviceMac: this.data.deviceMac,
  408. }).then((res) => {
  409. this.setData({
  410. wakeName: res[0].audioName
  411. })
  412. })
  413. },
  414. /**
  415. * 时间戳转化为年 月 日 时 分 秒
  416. * number: 传入时间戳
  417. * format:返回格式,支持自定义,但参数必须与formateArr里保持一致
  418. */
  419. formatTime(number, format) {
  420. var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  421. var returnArr = [];
  422. var date = new Date(number * 1000);
  423. returnArr.push(date.getFullYear());
  424. returnArr.push(formatNumber(date.getMonth() + 1));
  425. returnArr.push(formatNumber(date.getDate()));
  426. returnArr.push(formatNumber(date.getHours()));
  427. returnArr.push(formatNumber(date.getMinutes()));
  428. returnArr.push(formatNumber(date.getSeconds()));
  429. for (var i in returnArr) {
  430. format = format.replace(formateArr[i], returnArr[i]);
  431. }
  432. return format;
  433. },
  434. // type: 0 关闭 1 开启
  435. setOther(other, type) {
  436. if (type === 0) {
  437. other.enable = "0";
  438. other.alarm_name = "close";
  439. other.operation = "off";
  440. } else if (type === 1) {
  441. other.enable = "1";
  442. other.alarm_name = "open";
  443. other.operation = "on";
  444. }
  445. const thisTime = Math.round(new Date() / 1000);
  446. other.current_timestamp = this.formatTime(thisTime, "Y-M-D h:m:s");
  447. other.action = "update",
  448. other.song_uri = "-1";
  449. other.version = 3;
  450. other.is_debug = app.globalData.is_debug
  451. return other
  452. },
  453. // 判断当天时间前后
  454. compareDate(t1, t2) {
  455. var date = new Date();
  456. var a = t1.split(":");
  457. var b = t2.split(":");
  458. return date.setHours(a[0], a[1]) > date.setHours(b[0], b[1]);
  459. },
  460. repeatText(arr) {
  461. let text = "";
  462. if (arr.toString() === [0, 1, 1, 1, 1, 1, 0].toString()) {
  463. text = "工作日";
  464. } else if (arr.toString() === [1, 0, 0, 0, 0, 0, 1].toString()) {
  465. text = "周末";
  466. } else {
  467. arr.map((v, index) => {
  468. if (v === 1) {
  469. if (text === "") {
  470. text = text + _this.data.week[index]
  471. } else {
  472. text = text + "、" + _this.data.week[index];
  473. }
  474. }
  475. });
  476. if (text === "") {
  477. text = "不重复";
  478. };
  479. }
  480. return text.length < 20 ? text : "每日";
  481. }
  482. })