deviceWake.js 16 KB

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