deviceWake.js 16 KB

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