deviceWake.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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_util from '../../../utils/lexin/util.js';
  9. import routePath from '../../../utils/routePath.js';
  10. import routeUtil from '../../../utils/routeUtil.js';
  11. Page({
  12. data: {
  13. nvabarData: {
  14. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  15. title: '定时设置', //导航栏 中间的标题
  16. },
  17. switch1Checked: false,
  18. alarm_id_0_time: "00:00:00",
  19. switch2Checked: false,
  20. deviceMac: null,
  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 deviceId = options.deviceId;
  70. var clientType = options.clientType;
  71. var deviceMac = lexin_util.getDeviceMacId(deviceId);
  72. that.data.deviceId = deviceId;
  73. that.data.deviceMac = deviceMac;
  74. that.data.clientType = clientType;
  75. deviceWakedetail({
  76. clientType: clientType,
  77. deviceMac: deviceMac,
  78. }).then((res) => {
  79. if (res && res.length > 0) {
  80. that.setData({
  81. wakeName: res[0].audioName || null
  82. });
  83. }
  84. })
  85. app.PubMsg({
  86. type: "get_dev_info",
  87. DstDeviceName: deviceMac
  88. });
  89. // 赋值按钮回调
  90. that.setData({
  91. twoButCallback: () => {
  92. that.setData({
  93. toast_visible: false
  94. })
  95. }
  96. })
  97. },
  98. onShow(options) {
  99. var that = this;
  100. var deviceMac = that.data.deviceMac;
  101. app.PubMsg({
  102. type: "get_dev_info",
  103. DstDeviceName: deviceMac,
  104. });
  105. },
  106. mqttCallback(type, option) {
  107. var that = this;
  108. let payloads = null;
  109. if (option) {
  110. payloads = JSON.parse(option.payload);
  111. };
  112. switch (type) {
  113. case "message_onoffline":
  114. if (payloads.state !== "online") {
  115. // option.payload: {"uuid":"AIrSMArT_7cdfa1fcbb24","state":"offline","userid":"1"}
  116. var deviceMacId = payloads.uuid;
  117. if (deviceMacId == that.data.deviceMac) {
  118. wx.showToast({
  119. title: '设备已断开连接',
  120. icon: 'none',
  121. duration: 2000
  122. });
  123. }
  124. }
  125. break;
  126. case "message":
  127. // 设备信息
  128. if (payloads.type === "get_dev_info") {
  129. that.setData({
  130. deviceOther: payloads.other,
  131. updataDeviceInfo: true,
  132. });
  133. if (payloads.other.alarm) {
  134. payloads.other.alarm.map((v) => {
  135. if (v.alarm_id === "0") {
  136. v.action = "update";
  137. // 休眠
  138. that.setData({
  139. alarm_id_0: v
  140. });
  141. // 定时时间
  142. if (v.on_off_timestamp !== "") {
  143. that.setData({
  144. alarm_id_0_time: v.on_off_timestamp
  145. })
  146. }
  147. // 是否启用
  148. if (v.enable === "1") {
  149. that.setData({
  150. switch1Checked: true
  151. })
  152. } else {
  153. that.setData({
  154. switch1Checked: false
  155. })
  156. }
  157. // 重复
  158. if (v.week_actives) {
  159. that.setData({
  160. repeat0: that.repeatText(v.week_actives),
  161. })
  162. }
  163. } else if (v.alarm_id === "1") {
  164. // 唤醒
  165. that.setData({
  166. alarm_id_1: v
  167. });
  168. // 定时时间
  169. if (v.on_off_timestamp !== "") {
  170. that.setData({
  171. time: v.on_off_timestamp
  172. })
  173. }
  174. // 是否启用
  175. if (v.enable === "1") {
  176. that.setData({
  177. switch2Checked: true
  178. })
  179. } else {
  180. that.setData({
  181. switch2Checked: false
  182. })
  183. }
  184. // 重复
  185. if (v.week_actives) {
  186. that.setData({
  187. repeat: that.repeatText(v.week_actives),
  188. })
  189. }
  190. }
  191. })
  192. }
  193. } else if (payloads.type === "alert_set") {
  194. if (payloads.code === 200) {
  195. app.PubMsg({
  196. type: "get_dev_info",
  197. DstDeviceName: that.data.deviceMac
  198. });
  199. } else {
  200. wx.showModal({
  201. title: '保存失败',
  202. content: '请保持设备网络通畅并检查是否已连接电源?',
  203. success(res) {
  204. if (res.confirm) {
  205. console.log('用户点击确定')
  206. } else if (res.cancel) {
  207. console.log('用户点击取消')
  208. }
  209. }
  210. })
  211. }
  212. }
  213. default:
  214. }
  215. },
  216. // 休眠-选择时间
  217. switch1Change(e) {
  218. var that = this;
  219. let other = that.data.alarm_id_0;
  220. if (e.detail.value) {
  221. that.setData({
  222. switch1Checked: e.detail.value
  223. });
  224. const thisTime = Math.round(new Date() / 1000);
  225. other = that.setOther(other, 1);
  226. other.on_off_timestamp = that.data.alarm_id_0_time;
  227. // 有重复
  228. if (other.week_actives && other.week_actives.filter((v) => v === 1).length > 0) {
  229. // other.week_actives = [0,1,1,1,1,1,0],
  230. other.weekly_repeat = "1";
  231. } else {
  232. other.weekly_repeat = "0";
  233. };
  234. // 如果跨天了
  235. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  236. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  237. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  238. };
  239. app.PubMsg({
  240. type: "alert_set",
  241. DstDeviceName: that.data.deviceMac,
  242. other
  243. });
  244. } else {
  245. that.setData({
  246. switch1Checked: e.detail.value
  247. });
  248. other = that.setOther(other, 0);
  249. app.PubMsg({
  250. type: "alert_set",
  251. DstDeviceName: that.data.deviceMac,
  252. other
  253. });
  254. }
  255. },
  256. switch2Change(e) {
  257. var that = this;
  258. that.data.updataDeviceInfo = false;
  259. let other = that.data.alarm_id_1;
  260. if (e.detail.value) {
  261. if (that.data.deviceOther && that.data.deviceOther.Power < 20) {
  262. that.setData({
  263. toast_visible: true,
  264. });
  265. };
  266. // app.PubMsg({
  267. // type: "get_dev_info",
  268. // DstDeviceName: that.data.deviceMac
  269. // });
  270. if (!other) {
  271. wx.showToast({
  272. title: '设备已断开连接',
  273. icon: "none"
  274. })
  275. return
  276. }
  277. other.on_off_timestamp = that.data.time;
  278. other = that.setOther(other, 1);
  279. let week_actives = other.week_actives ?? []
  280. if (week_actives.filter((v) => v === 1).length > 0) {
  281. // other.week_actives = [0,1,1,1,1,1,0],
  282. other.weekly_repeat = "1";
  283. } else {
  284. other.weekly_repeat = "0";
  285. };
  286. app.PubMsg({
  287. type: "alert_set",
  288. DstDeviceName: that.data.deviceMac,
  289. other
  290. });
  291. // 不要循环发送,也不要改alarm_id_0.enable
  292. // 循环查询
  293. // const Interval = setInterval(() => {
  294. // if (that.data.updataDeviceInfo) {
  295. // clearInterval(Interval);
  296. // other.on_off_timestamp = that.data.time;
  297. // other = that.setOther(other, 1);
  298. // if (other.week_actives.filter((v) => v === 1).length > 0) {
  299. // // other.week_actives = [0,1,1,1,1,1,0],
  300. // other.weekly_repeat = "1";
  301. // } else {
  302. // other.weekly_repeat = "0";
  303. // };
  304. // app.PubMsg({
  305. // type: "alert_set",
  306. // DstDeviceName: that.data.deviceMac,
  307. // other
  308. // });
  309. // 需打开休眠
  310. // if (that.data.alarm_id_0.enable === "0") {
  311. // const thisTime = Math.round(new Date() / 1000);
  312. // const alarm_id_0 = that.setOther(that.data.alarm_id_0, 1);
  313. // alarm_id_0.on_off_timestamp = that.data.alarm_id_0_time;
  314. // // 有重复
  315. // if (alarm_id_0.week_actives.filter((v) => v === 1).length > 0) {
  316. // alarm_id_0.weekly_repeat = "1";
  317. // } else {
  318. // alarm_id_0.weekly_repeat = "0";
  319. // };;
  320. // // 如果跨天了
  321. // if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  322. // const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  323. // alarm_id_0.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  324. // };
  325. // app.PubMsg({
  326. // type: "alert_set",
  327. // DstDeviceName: that.data.deviceMac,
  328. // other: alarm_id_0
  329. // });
  330. // }
  331. // }
  332. // }, 500);
  333. } else {
  334. other = that.setOther(other, 0);
  335. app.PubMsg({
  336. type: "alert_set",
  337. DstDeviceName: that.data.deviceMac,
  338. other
  339. });
  340. }
  341. },
  342. // bindPickerChange(e) {
  343. // var that = this;
  344. // let other = that.data.alarm_id_0;
  345. // const thisTime = Math.round(new Date() / 1000);
  346. // other = that.setOther(other, 1);
  347. // other.on_off_timestamp = that.formatTime(thisTime + that.data.timeArray[e.detail.value].id, "h:m:s");
  348. // // 如果跨天了
  349. // if(parseInt(that.formatTime(thisTime + that.data.timeArray[e.detail.value].id, "D")) > parseInt(that.formatTime(thisTime, "D"))) {
  350. // const date = new Date(that.formatTime(thisTime, "Y-M-D") + " 00:00:00");
  351. // other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  352. // };
  353. // app.PubMsg({type: "alert_set", DstDeviceName: that.data.deviceMac, other});
  354. // },
  355. bindTimeChange0: function (e) {
  356. var that = this;
  357. that.setData({
  358. alarm_id_0_time: e.detail.value + ":00"
  359. });
  360. let other = that.data.alarm_id_0;
  361. other.on_off_timestamp = e.detail.value + ":00";
  362. other = that.setOther(other, 1);
  363. // 有重复
  364. if (other.week_actives.filter((v) => v === 1).length > 0) {
  365. // other.week_actives = [0,1,1,1,1,1,0],
  366. other.weekly_repeat = "1";
  367. } else {
  368. other.weekly_repeat = "0";
  369. };
  370. // 如果跨天了
  371. const thisTime = Math.round(new Date() / 1000);
  372. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  373. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  374. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  375. };
  376. app.PubMsg({
  377. type: "alert_set",
  378. DstDeviceName: that.data.deviceMac,
  379. other
  380. });
  381. },
  382. bindTimeChange: function (e) {
  383. var that = this;
  384. that.setData({
  385. time: e.detail.value + ":00"
  386. });
  387. let other = that.data.alarm_id_1;
  388. other.on_off_timestamp = e.detail.value + ":00";
  389. other = that.setOther(other, 1);
  390. app.PubMsg({
  391. type: "alert_set",
  392. DstDeviceName: that.data.deviceMac,
  393. other
  394. });
  395. },
  396. setRepeat(arr) {
  397. var that = this;
  398. let other = that.data.repeatIndex === "0" ? that.data.alarm_id_0 : that.data.alarm_id_1;
  399. other = that.setOther(other, 1);
  400. other.week_actives = arr;
  401. other.weekly_repeat = arr.filter((v) => v === 1).length > 0 ? "1" : "0";
  402. // 倒计时
  403. if (that.data.repeatIndex === "0") {
  404. // 如果跨天了
  405. const thisTime = Math.round(new Date() / 1000);
  406. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  407. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  408. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  409. };
  410. }
  411. app.PubMsg({
  412. type: "alert_set",
  413. DstDeviceName: that.data.deviceMac,
  414. other
  415. });
  416. },
  417. goWakeList() {
  418. var that = this;
  419. var deviceId = that.data.deviceId;
  420. var clientType = that.data.clientType;
  421. ///唤醒的设置特殊,智能这样用
  422. var deviceMac = lexin_util.getMacByDeviceId(deviceId);
  423. wx.navigateTo({
  424. url: './../wakeList/wakeList?clientType=' + clientType + "&deviceMac=" + deviceMac,
  425. })
  426. },
  427. gorepeat(e) {
  428. var that = this;
  429. that.data.repeatIndex = e.currentTarget.dataset.index;
  430. that.data.week_actives = e.currentTarget.dataset.index === "0" ? that.data.alarm_id_0.week_actives : that.data.alarm_id_1.week_actives;
  431. var param = '?week_actives=' + that.data.week_actives;
  432. routeUtil.jumpParam(routePath.repeat, param);
  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. })