deviceWake.js 16 KB

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