deviceWake.js 16 KB

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