deviceWake.js 14 KB

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