From 2d3df9a07771c32e70df85bb4928cdf2f29cab1f Mon Sep 17 00:00:00 2001 From: nanguaguag <2022181640245@stu.scu.edu.cn> Date: Wed, 24 Jul 2024 15:41:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BB=91=E5=AE=9A=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=80=81=E4=BA=BA=E8=B7=8C?= =?UTF-8?q?=E5=80=92=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/MQTT.md | 38 ++++- docs/RainmakerAPI.md | 135 ------------------ ...14\351\235\242\350\256\276\350\256\241.md" | 42 ++++++ src/pages/aboutPage/about.sass | 12 +- src/pages/aboutPage/about.vue | 56 +++++++- src/pages/bindPage/bind.vue | 15 +- src/pages/chatPage/chat.sass | 3 + src/pages/chatPage/chat.vue | 2 +- src/pages/homePage/home.vue | 3 +- src/pages/para2Page/para2.vue | 6 - src/utils/drugData.ts | 12 +- src/utils/global_data.ts | 87 +++++------ src/utils/mqtt_req.ts | 31 ++-- 13 files changed, 221 insertions(+), 221 deletions(-) delete mode 100644 docs/RainmakerAPI.md diff --git a/docs/MQTT.md b/docs/MQTT.md index e37f611..85f0f63 100644 --- a/docs/MQTT.md +++ b/docs/MQTT.md @@ -1,7 +1,41 @@ # MQTT API -给药逻辑: +## 绑定药盒和可穿戴设备 +### 陀螺仪端和药盒端: +1. 想一个随机字符串作为id(例如:a1b2c3d4) +2. 把ClientId写死进代码里 + - 药盒的clientId:box_id(box_a1b2c3d4) + - 陀螺仪的clientId:gyroscope_id(gyroscope_a1b2c3d4) +3. 根据id生成二维码([二维码生成在线工具](https://cli.im/)) +4. 把二维码打印下来贴在药盒上 -## +### 小程序端 + +1. 扫码获取id(a1b2c3d4) +2. 算出药盒的clientId:box_id(box_a1b2c3d4) +3. 算出陀螺仪的clientId:gyroscope_id(gyroscope_a1b2c3d4) + +## 监听老人跌倒信息 + +### 陀螺仪端: + +若监测到老人跌倒,则发布到主题 `drug/id/slip` + +```json +{ + // 写死在代码里的id + "id": "a1b2c3d4", + "code": 400, + "message": "监测到患者摔倒", + // 时间戳(精度:秒) + "timestamp": 1721804282 +} +``` + +### 小程序端 + +绑定好设备后,订阅主题 `drug/id/slip` + +若获取到code=400的消息,则弹窗提醒,消息就是message里的内容。 diff --git a/docs/RainmakerAPI.md b/docs/RainmakerAPI.md deleted file mode 100644 index d53c956..0000000 --- a/docs/RainmakerAPI.md +++ /dev/null @@ -1,135 +0,0 @@ -RainMaker 支持两种类型的 API:未经身份验证和已通过身份验证。 - -- 对于未经身份验证的 API,无需在 HTTP 标头中提供任何身份验证令牌。 - -当用户成功登录时,他会收到响应中的access_token。对于经过身份验证的 API,此access_token需要作为身份验证令牌在“授权”HTTP 标头中传递。 - -## 1. 创建用户 - - - -参数: - -```json -{ - "user_name": "", // 邮箱或带有国家、地区代码的手机号 - "password": "" -} -``` - -请求后相应邮箱或手机号会收到验证码verification_code,接着同一个请求地址,用验证码确认用户 - -参数: - -``` -{ - "user_name": "username@domain.com or <+Mobile Number with country code>", - "verification_code": "verification_code" -} -``` -注册成功 - -## 2. 登录 - - - -参数: -```json -{ - "user_name": "username@domain.com or +Mobile Number with country code", - "password": "password" -} -``` - -登录成功返回: - -```json -{ - "status": "success", - "description": "" -} -``` - -其中accesstoken为身份验证标识,在之后的请求中需在请求头中传递 - -## 3. 修改用户数据 - - - -### 3.1. 添加数据 - -参数: - -```json -{ - "profile": { - "value": { - "age": 20, // 设为null即为删除 - "gender": "男", -... - } - } -} -``` - -### 3.2. 修改已有值 - -参数: - -```json -{ - "membership_info": { - "value": { - "premium": false - } - }, - "profile": { - "value": { - "age": 19 - } - } -} -``` - -### 3.3. 其他小功能类似 - -## 4. 获取用户数据 - - - -无参数 - -返回: - -## 5. 获取用户详细信息 - - - - -custom_data为true表示获取包括自定义数据,返回值: - -## 6. 修改用户姓名或手机号 - - - -参数: - -```json -{ - "name": "" -} -``` -或 -```json -{ - "phone_number": "+ Mobile Number with country code" -} -``` - -修改手机号会收到验证码,确认手机号: - -```json -{ - "verification_code": "verification_code" -} -``` diff --git "a/docs/\347\225\214\351\235\242\350\256\276\350\256\241.md" "b/docs/\347\225\214\351\235\242\350\256\276\350\256\241.md" index e69de29..6bf94be 100644 --- "a/docs/\347\225\214\351\235\242\350\256\276\350\256\241.md" +++ "b/docs/\347\225\214\351\235\242\350\256\276\350\256\241.md" @@ -0,0 +1,42 @@ +我现在大概能理清药物逻辑了: + +每个药物有下面几个参数: + +- id: string, // 唯一标识 +- drugName: string, // 药品名称 +- dosage: string, // 给药剂量 +- form: string, // 药物剂型 +- dose: string, // 给药方式 +- interval: string, // 给药间隔 +- time: string, // 初次给药 +- startDate: string, // 开始日期 +- stopDate: string, // 结束日期 + +drug.id用「创建该药物时的timestamp」代替,理论上是不会重复的。 + +创建药物时填入这几个参数,程序会自动计算出用药方案,并推算出之后**开始日期~结束日期**之间的每一次给药。(但是将来的每一次用药都是根据所有用药方案实时推算出来,不存入任何全局变量中) + +小程序打开时会计算出「现在之前所有漏掉的服药」。计算方式是从「每种药的用药记录」中“最新的那项”开始,根据该药物的给药间隔,算出到现在为止漏掉的服药。然后就每次漏掉的服药询问——是否已经服药or是否跳过这次,并将这次服药加入「用药记录」中。**注意不管是否跳过服药都会加入用药记录,跳过则record.skip=true**。用药记录永远记录的是「已经结束的」服药计划,是个数组,其中的每个元素有下面这些参数: + +- public id: string, // 唯一标识 +- public drugName: string, // 药品名称 +- public dosage: string, // 给药剂量 +- public form: string, // 药物剂型 +- public dose: string, // 给药方式 +- public date: string, // 服药日期 +- public time: string, // 服药时间 +- public skip: Boolean, // 是否跳过 (默认为false) + +其中用药记录的id就是该药物的id,用于通过用药记录定位到这个药物。总结一下: + +- 如果患者按时服药,即小程序端启动时虽然计算出了很多漏掉的服药记录,但是马上接收到了药盒端传过来的服药记录,则消除所有漏掉的服药记录。 +- 如果患者没有按时服药,则同理,监护人就能在小程序端看到患者的服药情况,提醒患者服药。 +- 如果监护人在用药计划中更改某个药物的间隔、开始时间、结束时间等,会立刻生效,但是「已经结束的服药记录」完全不变,只会改变将来的那些服药计划。比如将来的某次服药时间 = 该药物最新的服药记录的时间 + 更改后的服药间隔。 +- 如果监护人在用药计划中删除某个药物,可能会因为药物记录的id找不到而报错,这个也是待解决。 +- 如果患者同一个药物有多次用药记录缺失,则理论上会弹出所有缺失的记录,让监护人选择是否服药。监护人此时不能因为看到这些缺失的记录就给患者喂药,不然一次性计量超标会出人命的,这个如何解决还没想好。 + + +旺:每次到点提醒时,患者点击触摸屏上的按钮“打开药箱”,打开箱子取药,这个事件表示已经服药,自动记录到用药记录里;提醒超过一定时间还没打开药箱那就默认没吃药,也记录进去。每次服药时不需要询问是否已经用药。 +尧:也不冲突,在「一定时间」内患者没有服用时,监护人打开小程序就能看到这个提醒,正好可以提醒患者服药。每种药物最多出现一个提醒,使用体验也会好一些。 + + diff --git a/src/pages/aboutPage/about.sass b/src/pages/aboutPage/about.sass index a135692..a9da3e2 100644 --- a/src/pages/aboutPage/about.sass +++ b/src/pages/aboutPage/about.sass @@ -1,4 +1,10 @@ .root - display: flex - flex-direction: column - align-items: center + .content + margin: 30px + .text + display: inline-block + .link + color: blue + text-decoration: underline + cursor: pointer + display: inline-block diff --git a/src/pages/aboutPage/about.vue b/src/pages/aboutPage/about.vue index eb29b03..60904b9 100644 --- a/src/pages/aboutPage/about.vue +++ b/src/pages/aboutPage/about.vue @@ -1,7 +1,57 @@ diff --git a/src/pages/bindPage/bind.vue b/src/pages/bindPage/bind.vue index 3ef0637..2910dab 100644 --- a/src/pages/bindPage/bind.vue +++ b/src/pages/bindPage/bind.vue @@ -1,8 +1,8 @@ @@ -17,6 +17,7 @@ import "./bind.sass"; import Taro from "@tarojs/taro"; import { setGlobalData, getGlobalData } from "../../utils/global_data"; import { ref, reactive } from "vue"; +import { connect } from "../../utils/mqtt_req"; // // 允许从相机和相册扫码 // Taro.scanCode({ @@ -58,6 +59,16 @@ function onScanFunc() { // 扫码成功以后跳到签到成功页面、释放加载按钮 state.loading = false; Taro.hideLoading(); + setGlobalData("drug_id", data.result); + connect(); // 连接 mqtt + Taro.showToast({ + title: "成功绑定到药箱", + icon: "success", + duration: 2000, + }); + setTimeout(() => { + Taro.navigateBack(); + }, 1000); } }, fail: (err) => { diff --git a/src/pages/chatPage/chat.sass b/src/pages/chatPage/chat.sass index e69ff72..f048159 100644 --- a/src/pages/chatPage/chat.sass +++ b/src/pages/chatPage/chat.sass @@ -103,3 +103,6 @@ right: -6.25rpx border-top-right-radius: 2.5rpx border-bottom-left-radius: 0 + +.at-input__input + text-align: left diff --git a/src/pages/chatPage/chat.vue b/src/pages/chatPage/chat.vue index 3847e39..6cb06de 100644 --- a/src/pages/chatPage/chat.vue +++ b/src/pages/chatPage/chat.vue @@ -137,7 +137,7 @@ function getRespFromAI(mesg) { }, data: { // apikey: process.env.OPENAI_API_KEY, - model: "gpt-3.5-turbo", + model: "gpt-4o-mini", messages: mesg, }, success: (res) => { diff --git a/src/pages/homePage/home.vue b/src/pages/homePage/home.vue index 6eff807..ad12c40 100644 --- a/src/pages/homePage/home.vue +++ b/src/pages/homePage/home.vue @@ -233,7 +233,7 @@ let drugs = ref(getGlobalData("drugs")); let drugRecord = ref(getGlobalData("drugRecord")); // State参数 let state = reactive({ - timesUp: false, + timesUp: true, showSecondaryFabs: false, untokenDrugs: [], nextDrug: undefined, @@ -276,6 +276,7 @@ function onTimeUp(params) { function toggleFloatBtn() { state.showSecondaryFabs = !state.showSecondaryFabs; console.log("connecting..."); + console.log(drugRecord.value); connect(); setTimeout(() => { subscribe("hello"); diff --git a/src/pages/para2Page/para2.vue b/src/pages/para2Page/para2.vue index aa422ca..747d358 100644 --- a/src/pages/para2Page/para2.vue +++ b/src/pages/para2Page/para2.vue @@ -33,12 +33,6 @@ let paraName = params["name"]; // 获取健康指标数据 let paraData = getGlobalData("healthIndicators")[paraName]; -// 更新页面数据 -useDidShow((e) => { - console.log("fetch data again"); - // TODO -}); - function addRecord() { console.log("addRecord"); Taro.navigateTo({ diff --git a/src/utils/drugData.ts b/src/utils/drugData.ts index 2cb93a0..73a6b7f 100644 --- a/src/utils/drugData.ts +++ b/src/utils/drugData.ts @@ -142,9 +142,8 @@ export function getUntokenDrugs(drugs: DrugData[], drugRecord: DrugRecord[]): [ if (startstamp <= nowTime && nowTime <= stopstamp) { // 最新服药记录 let latestStamp = startstamp; // 如果是第一次服药,则最新服药记录为开始服药时间 - let latestRecord = classifiedDrugRecord[drug.id].slice(-1)[0]; - if (latestRecord !== undefined) { - // 说明该药物有服药记录=不是第一次服药 + if (classifiedDrugRecord[drug.id]) { // 说明该药物不是第一次服药 + let latestRecord = classifiedDrugRecord[drug.id].slice(-1)[0]; latestStamp = toTimeStamp(latestRecord.date, latestRecord.time); } // 找到下一个服药时间 @@ -210,10 +209,9 @@ export function getFutureDrugs( if (startstamp <= begStamp && begStamp <= stopstamp) { // 最新服药记录的时间(timestamp) let latestStamp = startstamp; // 如果是第一次服药,则最新服药记录为开始服药时间 - let latestRecord = classifiedDrugRecord[drug.id].slice(-1)[0]; - if (latestRecord !== undefined) { - // 说明该药物有服药记录=不是第一次服药 - latestStamp = DrugRecord.getTime(latestRecord); + if (classifiedDrugRecord[drug.id]) { // 说明该药物不是第一次服药 + let latestRecord = classifiedDrugRecord[drug.id].slice(-1)[0]; + latestStamp = toTimeStamp(latestRecord.date, latestRecord.time); } // 找到大于begStamp的下一个服药时间 let N = Math.ceil((begStamp - latestStamp) / interval); diff --git a/src/utils/global_data.ts b/src/utils/global_data.ts index 61c06b7..c395b46 100644 --- a/src/utils/global_data.ts +++ b/src/utils/global_data.ts @@ -2,6 +2,7 @@ import PatientInfo from "./patientInfo" import { DrugData, DrugRecord } from "./drugData"; let globalData = { + box_id: "", MQTTurl: "i7148e41.ala.us-east-1.emqxsl.com", // 底部Tab栏 tabList: [ @@ -29,60 +30,44 @@ let globalData = { ], // 用药记录 drugRecord: [ - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-11', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-11', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-12', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-12', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-13', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-13', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-14', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-14', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-15', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-15', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-17', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-17', '20:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-18', '08:00:00', false), - new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-18', '20:00:00', false), - // new DrugRecord('1720656000', '阿司匹林', '1', '片', '口服', '2024-07-19', '08:00:00', false), - new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-17', '09:00:00', false), - new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-17', '19:00:00', false), - new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-18', '05:00:00', false), - new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-18', '15:00:00', false), - new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-19', '00:00:00', false), - // new DrugRecord('1720656001', '山莨菪碱', '1', '片', '口服', '2024-07-19', '10:00:00', false), - new DrugRecord('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '2024-07-18', '11:00:00', false), - new DrugRecord('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '2024-07-18', '17:00:00', false), - new DrugRecord('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '2024-07-18', '23:00:00', false), - new DrugRecord('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '2024-07-19', '05:00:00', false), - // new DrugRecord('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '2024-07-19', '11:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-17', '14:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-17', '17:30:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-17', '21:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '00:30:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '04:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '07:30:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '11:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '14:30:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '18:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '21:30:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '01:00:00', false), - new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '04:30:00', false), - // new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '08:00:00', false), - // new DrugRecord('1720656003', '阿托品', '2', '片', '口服', '2024-07-18', '11:30:00', false), - new DrugRecord('1720656004', '胰岛素', '1', '片', '静脉注射', '2024-07-14', '16:00:00', false), - new DrugRecord('1720656004', '胰岛素', '1', '片', '静脉注射', '2024-07-15', '16:00:00', false), - new DrugRecord('1720656004', '胰岛素', '1', '片', '静脉注射', '2024-07-16', '16:00:00', false), - new DrugRecord('1720656004', '胰岛素', '1', '片', '静脉注射', '2024-07-17', '16:00:00', false), - new DrugRecord('1720656004', '胰岛素', '1', '片', '静脉注射', '2024-07-18', '16:00:00', false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-19", "00:00:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-19", "16:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-19", "20:30:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-20", "07:00:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-20", "08:00:00", false), + new DrugRecord("1720656004", "胰岛素", "1", "滴", "静脉注射", "2024-07-20", "08:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-20", "17:30:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-20", "20:00:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-21", "00:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-21", "04:00:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-21", "08:00:00", false), + new DrugRecord("1720656004", "胰岛素", "1", "滴", "静脉注射", "2024-07-21", "08:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-21", "14:30:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-21", "16:00:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-21", "20:00:00", false), + new DrugRecord("1720656003", "阿托品", "2", "片", "口服", "2024-07-21", "21:30:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-22", "01:00:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-22", "08:00:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-22", "08:00:00", false), + new DrugRecord("1720656004", "胰岛素", "1", "滴", "静脉注射", "2024-07-22", "08:00:00", false), + new DrugRecord("1720656003", "阿托品", "2", "片", "口服", "2024-07-22", "11:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-22", "11:30:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-22", "20:00:00", false), + new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-22", "22:00:00", false), + new DrugRecord("1720656002", "毛果芸香碱", "1", "滴", "滴眼", "2024-07-23", "00:00:00", false), + new DrugRecord("1720656003", "阿托品", "2", "片", "口服", "2024-07-23", "00:30:00", false), + new DrugRecord("1720656000", "阿司匹林", "1", "片", "口服", "2024-07-23", "08:00:00", false), + // new DrugRecord("1720656004", "胰岛素", "1", "滴", "静脉注射", "2024-07-23", "08:00:00", false), + // new DrugRecord("1720656001", "山莨菪碱", "2", "片", "口服", "2024-07-23", "08:30:00", false), ], // 药物数据 drugs: [ - // 阿司匹林 每隔12小时1片,从 2024-10-01 08:00:00 起,至 2024-10-10 结束 - new DrugData('1720656000', '阿司匹林', '1', '片', '口服', '12:00:00', '08:00:00', '2024-07-11', '2024-10-10'), - new DrugData('1720656001', '山莨菪碱', '2', '片', '口服', '10:00:00', '10:00:00', '2024-07-11', '2024-10-10'), - new DrugData('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '06:00:00', '08:00:00', '2024-07-11', '2024-10-10'), - new DrugData('1720656003', '阿托品', '2', '片', '口服', '03:30:00', '08:00:00', '2024-07-11', '2024-10-10'), - new DrugData('1720656004', '胰岛素', '1', '滴', '静脉注射', '24:00:00', '08:00:00', '2024-07-11', '2024-10-10'), + // 阿司匹林 每隔12小时1片,从 2024-07-11 08:00:00 起,至 2024-10-10 结束 + new DrugData('1720656000', '阿司匹林', '1', '片', '口服', '12:00:00', '08:00:00', '2024-07-20', '2024-10-10'), + new DrugData('1720656001', '山莨菪碱', '2', '片', '口服', '10:30:00', '10:00:00', '2024-07-19', '2024-10-10'), + new DrugData('1720656002', '毛果芸香碱', '1', '滴', '滴眼', '16:00:00', '08:00:00', '2024-07-18', '2024-10-10'), + new DrugData('1720656003', '阿托品', '2', '片', '口服', '13:30:00', '08:00:00', '2024-07-21', '2024-10-10'), + new DrugData('1720656004', '胰岛素', '1', '滴', '静脉注射', '24:00:00', '08:00:00', '2024-07-19', '2024-10-10'), ], // 健康指标 healthIndicators: { diff --git a/src/utils/mqtt_req.ts b/src/utils/mqtt_req.ts index e5217af..c8e099b 100644 --- a/src/utils/mqtt_req.ts +++ b/src/utils/mqtt_req.ts @@ -5,6 +5,7 @@ import Taro from "@tarojs/taro"; let host = getGlobalData('MQTTurl'); let client: any = null; // 初始化MQTT client + const mqttOptions = { keepalive: 30, protocolVersion: 4, // MQTT V3.1.1 @@ -12,7 +13,7 @@ const mqttOptions = { clientId: 'wechat_' + genRandStr(6), username: 'nanguagua', // password: atob("RU1RWG5nZzEyMw=="), // 我靠,这个atob会导致真机预览和真机调试失败白屏!!! - password: 'EQMXngg123', + password: 'EMQXngg123', clean: true, // Clean session }; @@ -24,22 +25,32 @@ export function connect() { console.log("连接中..."); client = mqtt.connect(`wxs://${host}:8084/mqtt`, mqttOptions); client.on("connect", () => { + // 连接成功后,订阅主题 Taro.showToast({ title: "连接成功", }); console.log("链接成功"); - client.on("message", (topic: string, payload: string) => { + // 订阅老人跌倒事件 + let drug_id = getGlobalData('drug_id'); + client.subscribe(`drug/${drug_id}/slip`); + console.log(`成功订阅主题:drug/${drug_id}/slip`); + client.on("message", (topic: string, payload: string|any) => { payload = payload.toString(); try { payload = JSON.parse(payload); } catch (error) { console.log("payload is not JSON"); + return; } - // Taro.showModal({ - // content: `收到消息 - Topic: ${topic},Payload: ${payload}`, - // showCancel: false, - // }); console.log("recieve message:", payload, "from topic:", topic); + if (topic === `drug/${drug_id}/slip`) { + if (payload.code === 400) { + Taro.showModal({ + content: payload.message, + showCancel: false, + }); + } + } }); client.on("error", (error: any) => { @@ -76,10 +87,10 @@ export function subscribe(subTopic: string) { export function unsubscribe(subTopic: string) { if (client) { client.unsubscribe(subTopic) - Taro.showModal({ - content: `成功取消订阅主题:${subTopic}`, - showCancel: false, - }); + // Taro.showModal({ + // content: `成功取消订阅主题:${subTopic}`, + // showCancel: false, + // }); return; } console.log("Client is null");