diff --git a/src/manage-web-notuse/MonitorController.java b/src/manage-web-notuse/MonitorController.java deleted file mode 100644 index 9f82f30..0000000 --- a/src/manage-web-notuse/MonitorController.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.campus.water.web.controller; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping("/api/admin/monitor") -public class MonitorController { - @Autowired - private RealTimeService realTimeService; - @Autowired - private HistoryDataService historyDataService; - - @GetMapping("/realTime/{deviceId}") - public Object getRealTimeData(@PathVariable String deviceId) { - return realTimeService.getRealTimeData(deviceId); - } - - @GetMapping("/realTime/all") - public List getAllRealTimeData() { - return realTimeService.getAllRealTimeData(); - } - - @GetMapping("/history/{deviceId}") - public List getHistoryData( - @PathVariable String deviceId, - @RequestParam String startTime, - @RequestParam String endTime - ) { - return historyDataService.getHistoryData(deviceId, startTime, endTime); - } -} \ No newline at end of file diff --git a/src/manage-web-notuse/RealTimeService.java b/src/manage-web-notuse/RealTimeService.java deleted file mode 100644 index 5ee933e..0000000 --- a/src/manage-web-notuse/RealTimeService.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.campus.water.web; - -import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -@Service -public class RealTimeService { - - private ConcurrentHashMap realTimeData = new ConcurrentHashMap<>(); - private List alerts = new ArrayList<>(); - - public void updateRealTimeData(Object sensorData) { - System.out.println("更新实时数据"); - } - - public void addAlert(Object alertData) { - alerts.add(alertData); - System.out.println("添加告警"); - } - - public Object getRealTimeData(String deviceId) { - return realTimeData.get(deviceId); - } - - public List getAllRealTimeData() { - return new ArrayList<>(realTimeData.values()); - } -} \ No newline at end of file diff --git a/src/manage-web-notuse/WebMqttConfig.java b/src/manage-web-notuse/WebMqttConfig.java deleted file mode 100644 index 4f4670c..0000000 --- a/src/manage-web-notuse/WebMqttConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.campus.water.web.config; - -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class WebMqttConfig { - // 修改为本地MQTT - private String mqttBroker = "tcp://localhost:1883"; - private String clientId = "campus-water-admin-web"; - - @Bean - public MqttClient mqttClient() throws Exception { - MqttClient client = new MqttClient(mqttBroker, clientId, new MemoryPersistence()); - MqttConnectOptions options = new MqttConnectOptions(); - options.setCleanSession(false); - options.setKeepAliveInterval(60); - client.connect(options); - System.out.println("管理Web已连接MQTT服务器:" + mqttBroker); - return client; - } - - @Bean - public String[] subscribeTopics() { - return new String[]{ - "forward/web/device/#", - "forward/web/alert/#" - }; - } - - @Bean - public int[] qosLevels() { - return new int[]{0, 1}; - } -} \ No newline at end of file diff --git a/src/manage-web-notuse/WebMqttSubscriber.java b/src/manage-web-notuse/WebMqttSubscriber.java deleted file mode 100644 index 604501f..0000000 --- a/src/manage-web-notuse/WebMqttSubscriber.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.campus.water.web.mqtt; - -import com.alibaba.fastjson2.JSONObject; -import org.eclipse.paho.client.mqttv3.IMqttMessageListener; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; - -@Component -public class WebMqttSubscriber { - @Autowired - private MqttClient mqttClient; - @Autowired - private String[] subscribeTopics; - @Autowired - private int[] qosLevels; - @Autowired - private RealTimeService realTimeService; - - @PostConstruct - public void startSubscribe() throws Exception { - for (int i = 0; i < subscribeTopics.length; i++) { - String topic = subscribeTopics[i]; - int qos = qosLevels[i]; - mqttClient.subscribe(topic, qos, messageListener()); - System.out.println("管理Web已订阅主题:" + topic); - } - } - - private IMqttMessageListener messageListener() { - return (topic, message) -> { - String payload = new String(message.getPayload()); - System.out.println("管理Web接收数据:主题=" + topic + ",内容=" + payload); - - // 直接使用JSONObject处理数据 - JSONObject data = JSONObject.parseObject(payload); - - if (topic.startsWith("forward/web/device/")) { - realTimeService.updateRealTimeData(data); - } else if (topic.startsWith("forward/web/alert/")) { - realTimeService.addAlert(data); - } - }; - } -} \ No newline at end of file diff --git a/src/mqtt-server-notuse/DataForwarder.java b/src/mqtt-server-notuse/DataForwarder.java deleted file mode 100644 index 99a2123..0000000 --- a/src/mqtt-server-notuse/DataForwarder.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.campus.water.mqtt.core; - -import com.alibaba.fastjson2.JSONObject; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.Map; - -@Component -public class DataForwarder { - @Autowired - private MqttClient mqttClient; - - public void forwardToWeb(Map sensorData) throws Exception { - String deviceId = (String) sensorData.get("deviceId"); - String topic = "forward/web/device/" + deviceId; - String payload = JSONObject.toJSONString(sensorData); - publish(topic, payload, 0); - System.out.println("转发到管理Web: " + deviceId); - } - - public void forwardToWeb(Map alertData, boolean isAlert) throws Exception { - String deviceId = (String) alertData.get("deviceId"); - String topic = "forward/web/alert/" + deviceId; - String payload = JSONObject.toJSONString(alertData); - publish(topic, payload, 1); - System.out.println("转发告警到管理Web: " + deviceId); - } - - public void forwardToStudentApp(Map sensorData) throws Exception { - Integer deviceType = (Integer) sensorData.get("deviceType"); - if (deviceType != 1) return; - - String deviceId = (String) sensorData.get("deviceId"); - String terminalId = getRelatedTerminalId(deviceId); - String topic = "forward/student/terminal/" + terminalId; - - Double tdsValue = (Double) sensorData.get("tdsValue"); - String payload = JSONObject.toJSONString(new JSONObject() {{ - put("terminalId", terminalId); - put("tdsValue", tdsValue); - put("waterQuality", getWaterQuality(tdsValue)); - put("timestamp", sensorData.get("timestamp")); - }}); - publish(topic, payload, 0); - System.out.println("转发到学生APP: " + terminalId); - } - - public void forwardToRepairApp(Map sensorData) throws Exception { - String areaId = (String) sensorData.get("areaId"); - String deviceId = (String) sensorData.get("deviceId"); - String topic = "forward/repair/area/" + areaId + "/device/" + deviceId; - String payload = JSONObject.toJSONString(sensorData); - publish(topic, payload, 0); - System.out.println("转发到维修APP: " + deviceId); - } - - public void forwardToRepairApp(Map alertData, boolean isAlert) throws Exception { - String areaId = (String) alertData.get("areaId"); - String deviceId = (String) alertData.get("deviceId"); - String topic = "forward/repair/area/" + areaId + "/alert/" + deviceId; - String payload = JSONObject.toJSONString(alertData); - publish(topic, payload, 1); - System.out.println("转发告警到维修APP: " + deviceId); - } - - private void publish(String topic, String payload, int qos) throws Exception { - MqttMessage message = new MqttMessage(payload.getBytes()); - message.setQos(qos); - mqttClient.publish(topic, message); - } - - private String getRelatedTerminalId(String deviceId) { - return deviceId.equals("WM001") ? "TERM001" : "TERM002"; - } - - private String getWaterQuality(Double tds) { - if (tds == null) return "未知"; - if (tds < 50) return "纯净水(无矿物质)"; - else if (tds < 300) return "优质矿化水"; - else if (tds < 600) return "合格矿化水"; - else return "水质超标(不建议饮用)"; - } -} \ No newline at end of file diff --git a/src/mqtt-server-notuse/MqttConfig.java b/src/mqtt-server-notuse/MqttConfig.java deleted file mode 100644 index 6d0c562..0000000 --- a/src/mqtt-server-notuse/MqttConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.campus.water.mqtt.config; - -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class MqttConfig { - // 修改为本地MQTT broker - private String brokerUrl = "tcp://localhost:1883"; - private String clientId = "campus-water-mqtt-server"; - private int keepAlive = 60; - - @Bean - public MqttClient mqttClient() throws Exception { - MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence()); - - MqttConnectOptions options = new MqttConnectOptions(); - options.setCleanSession(false); - options.setKeepAliveInterval(keepAlive); - options.setAutomaticReconnect(true); - - client.connect(options); - System.out.println("MQTT服务器已连接Broker:" + brokerUrl); - return client; - } - - @Bean - public String[] subscribeTopics() { - return new String[]{ - "sensor/watermaker/#", - "sensor/watersupply/#", - "alert/#" - }; - } - - @Bean - public int[] qosLevels() { - return new int[]{0, 0, 1}; - } -} \ No newline at end of file diff --git a/src/mqtt-server-notuse/MqttSubscriber.java b/src/mqtt-server-notuse/MqttSubscriber.java deleted file mode 100644 index 3f83755..0000000 --- a/src/mqtt-server-notuse/MqttSubscriber.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.campus.water.mqtt.core; - -import com.alibaba.fastjson2.JSONObject; -import org.eclipse.paho.client.mqttv3.IMqttMessageListener; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.Map; - -@Component -public class MqttSubscriber { - @Autowired - private MqttClient mqttClient; - @Autowired - private String[] subscribeTopics; - @Autowired - private int[] qosLevels; - @Autowired - private DataForwarder dataForwarder; - - @PostConstruct - public void startSubscribe() throws Exception { - for (int i = 0; i < subscribeTopics.length; i++) { - String topic = subscribeTopics[i]; - int qos = qosLevels[i]; - mqttClient.subscribe(topic, qos, messageListener()); - System.out.println("MQTT服务器已订阅主题:" + topic + "(QoS=" + qos + ")"); - } - } - - private IMqttMessageListener messageListener() { - return (topic, message) -> { - String payload = new String(message.getPayload()); - System.out.println("MQTT服务器接收数据:主题=" + topic + ",内容=" + payload); - - try { - if (topic.startsWith("sensor/watermaker/")) { - // 解析制水机数据 - Map sensorData = parseWaterMakerData(topic, payload); - dataForwarder.forwardToWeb(sensorData); - dataForwarder.forwardToStudentApp(sensorData); - - } else if (topic.startsWith("sensor/watersupply/")) { - // 解析供水机数据 - Map sensorData = parseWaterSupplyData(topic, payload); - dataForwarder.forwardToWeb(sensorData); - dataForwarder.forwardToRepairApp(sensorData); - - } else if (topic.startsWith("alert/")) { - // 解析告警数据 - Map alertData = parseAlertMessage(topic, payload); - dataForwarder.forwardToWeb(alertData); - dataForwarder.forwardToRepairApp(alertData); - } - } catch (Exception e) { - System.err.println("处理MQTT消息异常: " + e.getMessage()); - e.printStackTrace(); - } - }; - } - - private Map parseWaterMakerData(String topic, String payload) { - Map data = new HashMap<>(); - parseCommonData(topic, payload, data); - data.put("deviceType", 1); // 制水机 - - JSONObject json = JSONObject.parseObject(payload); - data.put("tdsValue", json.getDouble("tds")); - data.put("waterFlow", json.getDouble("flow")); - data.put("waterPressure", json.getDouble("pressure")); - data.put("filterLife", json.getInteger("filter_life")); - data.put("leakage", json.getBoolean("leakage")); - - data.put("status", determineStatus(data)); - return data; - } - - private Map parseWaterSupplyData(String topic, String payload) { - Map data = new HashMap<>(); - parseCommonData(topic, payload, data); - data.put("deviceType", 2); // 供水机 - - JSONObject json = JSONObject.parseObject(payload); - data.put("waterFlow", json.getDouble("flow")); - data.put("waterPressure", json.getDouble("pressure")); - data.put("waterLevel", json.getDouble("water_level")); - - data.put("status", determineStatus(data)); - return data; - } - - private Map parseAlertMessage(String topic, String payload) { - Map alert = new HashMap<>(); - - String[] parts = topic.split("/"); - alert.put("deviceId", parts.length >= 2 ? parts[1] : "unknown"); - - JSONObject json = JSONObject.parseObject(payload); - alert.put("alertType", json.getString("alert_type")); - alert.put("alertLevel", json.getString("alert_level")); - alert.put("alertMessage", json.getString("alert_message")); - alert.put("areaId", json.getString("area_id")); - alert.put("deviceType", json.getInteger("device_type")); - alert.put("timestamp", LocalDateTime.now().toString()); - - return alert; - } - - private void parseCommonData(String topic, String payload, Map data) { - String[] parts = topic.split("/"); - data.put("deviceId", parts.length >= 3 ? parts[2] : "unknown"); - - JSONObject json = JSONObject.parseObject(payload); - data.put("areaId", json.getString("area_id")); - data.put("temperature", json.getDouble("temperature")); - data.put("humidity", json.getDouble("humidity")); - data.put("timestamp", LocalDateTime.now().toString()); - } - - private String determineStatus(Map data) { - Integer deviceType = (Integer) data.get("deviceType"); - - if (deviceType == 1) { // 制水机 - Double tdsValue = (Double) data.get("tdsValue"); - Integer filterLife = (Integer) data.get("filterLife"); - Boolean leakage = (Boolean) data.get("leakage"); - - if (tdsValue != null && tdsValue > 600) return "error"; - if (filterLife != null && filterLife < 10) return "error"; - if (leakage != null && leakage) return "error"; - if (tdsValue != null && tdsValue > 300) return "warning"; - } else if (deviceType == 2) { // 供水机 - Double waterLevel = (Double) data.get("waterLevel"); - if (waterLevel != null && waterLevel < 10) return "error"; - if (waterLevel != null && waterLevel < 20) return "warning"; - } - return "normal"; - } -} \ No newline at end of file diff --git a/src/repair-app-notuse/ RepairMqttSubscriber.java b/src/repair-app-notuse/ RepairMqttSubscriber.java deleted file mode 100644 index fae45b3..0000000 --- a/src/repair-app-notuse/ RepairMqttSubscriber.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.campus.water.repair.mqtt; - -import com.alibaba.fastjson2.JSONObject; -import com.campus.water.repair.config.RepairMqttConfig; -import com.campus.water.repair.entity.AreaDeviceVO; -import com.campus.water.repair.entity.RepairAlertVO; -import com.campus.water.repair.service.AreaDeviceService; -import com.campus.water.repair.service.WorkOrderService; -import org.eclipse.paho.client.mqttv3.IMqttMessageListener; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * 维修APP端MQTT订阅器(仅接收本辖区设备数据/告警,适配“抢单/巡检”需求) - */ -@Component -public class RepairMqttSubscriber { - @Autowired - private RepairMqttConfig mqttConfig; - @Autowired - private AreaDeviceService areaDeviceService; - @Autowired - private WorkOrderService workOrderService; - - // 维修员登录后订阅本辖区数据(先查询维修员所属片区) - public void subscribeAreaData(String repairmanId, String areaId) throws Exception { - MqttClient client = mqttConfig.mqttClient(repairmanId); - - // 1. 订阅本辖区设备数据(供水机水位/制水机状态) - String deviceTopic = mqttConfig.getDeviceSubscribeTopic(areaId); - client.subscribe(deviceTopic, 0, deviceMessageListener(areaId)); - - // 2. 订阅本辖区告警报文(触发抢单) - String alertTopic = mqttConfig.getAlertSubscribeTopic(areaId); - client.subscribe(alertTopic, 1, alertMessageListener(areaId)); - - System.out.println("维修APP(ID:" + repairmanId + ")已订阅片区" + areaId + "数据"); - } - - // 设备数据监听器(缓存辖区设备状态,供巡检使用) - private IMqttMessageListener deviceMessageListener(String areaId) { - return (topic, message) -> { - String payload = new String(message.getPayload()); - AreaDeviceVO deviceVO = JSONObject.parseObject(payload, AreaDeviceVO.class); - areaDeviceService.updateAreaDeviceStatus(areaId, deviceVO); - System.out.println("维修APP接收片区" + areaId + "设备数据:" + payload); - }; - } - - // 告警监听器(新告警触发工单,推送抢单提醒) - private IMqttMessageListener alertMessageListener(String areaId) { - return (topic, message) -> { - String payload = new String(message.getPayload()); - RepairAlertVO alertVO = JSONObject.parseObject(payload, RepairAlertVO.class); - // 触发工单生成(存入MySQL)并推送抢单提醒 - workOrderService.createWorkOrderFromAlert(alertVO); - System.out.println("维修APP接收片区" + areaId + "告警:" + payload + "(已生成工单)"); - }; - } -} \ No newline at end of file diff --git a/src/repair-app-notuse/RepairMqttConfig.java b/src/repair-app-notuse/RepairMqttConfig.java deleted file mode 100644 index fb67d32..0000000 --- a/src/repair-app-notuse/RepairMqttConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.campus.water.repair.config; - -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * 维修APP端MQTT配置(仅订阅本辖区设备数据/告警,适配维修员权限需求) - */ -@Configuration -public class RepairMqttConfig { - private String mqttBroker = "tcp://mqtt-server.campus.com:1883"; - private String clientIdPrefix = "campus-water-repair-"; // 客户端ID前缀(拼接维修员ID) - - // 动态生成客户端ID(按维修员ID区分) - @Bean - public String clientId(String repairmanId) { - return clientIdPrefix + repairmanId; - } - - @Bean - public MqttClient mqttClient(String repairmanId) throws Exception { - String clientId = clientId(repairmanId); - MqttClient client = new MqttClient(mqttBroker, clientId, new MemoryPersistence()); - MqttConnectOptions options = new MqttConnectOptions(); - options.setCleanSession(false); // 重连后恢复订阅,避免错过工单 - options.setKeepAliveInterval(60); - client.connect(options); - System.out.println("维修APP(ID:" + repairmanId + ")已连接MQTT服务器"); - return client; - } - - // 订阅主题(仅本辖区设备数据+告警,适配“维修员仅查看本辖区”需求) - public String getDeviceSubscribeTopic(String areaId) { - return "forward/repair/area/" + areaId + "/device/#"; - } - - public String getAlertSubscribeTopic(String areaId) { - return "forward/repair/area/" + areaId + "/alert/#"; - } -} \ No newline at end of file diff --git a/src/repair-app-notuse/WorkOrderController.java b/src/repair-app-notuse/WorkOrderController.java deleted file mode 100644 index 69d31d6..0000000 --- a/src/repair-app-notuse/WorkOrderController.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.campus.water.repair.controller; - -import com.campus.water.repair.entity.WorkOrderVO; -import com.campus.water.repair.service.WorkOrderService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -/** - * 工单接口(供维修APP前端调用,适配“抢单/处理工单、跟踪状态”需求) - */ -@RestController -@RequestMapping("/api/repair/workOrder") -public class WorkOrderController { - @Autowired - private WorkOrderService workOrderService; - - // 1. 获取本辖区待抢单工单(需求“抢单”功能) - @GetMapping("/pending/{repairmanId}") - public List getPendingWorkOrder(@PathVariable String repairmanId) { - String areaId = workOrderService.getRepairmanArea(repairmanId); // 获取维修员所属片区 - return workOrderService.getPendingWorkOrderByArea(areaId); - } - - // 2. 抢单(需求核心功能,需校验辖区权限) - @PostMapping("/grab") - public String grabWorkOrder( - @RequestParam String repairmanId, - @RequestParam String orderId - ) { - return workOrderService.grabWorkOrder(repairmanId, orderId) ? - "抢单成功,请及时处理" : "工单已被抢,或您无权限抢此工单"; - } - - // 3. 提交工单处理结果(需求“处理工单并提交”功能) - @PostMapping("/complete") - public String completeWorkOrder( - @RequestParam String orderId, - @RequestParam String repairmanId, - @RequestParam String dealNote, // 处理备注(如“已更换滤芯”) - @RequestParam String imgUrl // 现场照片URL(可选) - ) { - workOrderService.completeWorkOrder(orderId, repairmanId, dealNote, imgUrl); - return "工单处理完成,已提交审核"; - } - - // 4. 查看个人处理中的工单(需求“跟踪工单状态”功能) - @GetMapping("/processing/{repairmanId}") - public List getProcessingWorkOrder(@PathVariable String repairmanId) { - return workOrderService.getProcessingWorkOrderByRepairman(repairmanId); - } -} \ No newline at end of file diff --git a/src/student-app-notuse/ StudentMqttConfig.java b/src/student-app-notuse/ StudentMqttConfig.java deleted file mode 100644 index 72d5b88..0000000 --- a/src/student-app-notuse/ StudentMqttConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.campus.water.student.config; - -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * 学生APP端MQTT配置(仅订阅终端机水质数据,适配学生权限需求) - */ -@Configuration -public class StudentMqttConfig { - private String mqttBroker = "tcp://mqtt-server.campus.com:1883"; - private String clientIdPrefix = "campus-water-student-"; // 客户端ID前缀(拼接学生ID) - - // 动态生成客户端ID(按学生ID区分,避免冲突) - @Bean - public String clientId(String studentId) { - return clientIdPrefix + studentId; - } - - @Bean - public MqttClient mqttClient(String studentId) throws Exception { - String clientId = clientId(studentId); - MqttClient client = new MqttClient(mqttBroker, clientId, new MemoryPersistence()); - MqttConnectOptions options = new MqttConnectOptions(); - options.setCleanSession(true); // 学生APP登录后重新订阅,无需保留会话 - options.setKeepAliveInterval(120); // 移动端心跳间隔 longer,适配电池续航 - client.connect(options); - System.out.println("学生APP(ID:" + studentId + ")已连接MQTT服务器"); - return client; - } - - // 订阅主题(仅终端机水质数据,适配学生“扫码查水质”需求) - public String getSubscribeTopic(String terminalId) { - return "forward/student/terminal/" + terminalId; - } -} \ No newline at end of file diff --git a/src/student-app-notuse/DrinkController.java b/src/student-app-notuse/DrinkController.java deleted file mode 100644 index 64708e8..0000000 --- a/src/student-app-notuse/DrinkController.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.campus.water.student.controller; - -import com.campus.water.student.entity.DrinkRecordVO; -import com.campus.water.student.service.DrinkRecordService; -import com.campus.water.student.service.WaterQualityService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -/** - * 饮水接口(供学生APP前端调用,适配“扫码用水、查看饮水量”需求) - */ -@RestController -@RequestMapping("/api/student/drink") -public class DrinkController { - @Autowired - private DrinkRecordService drinkRecordService; - @Autowired - private WaterQualityService waterQualityService; - - // 1. 扫码用水(生成饮水记录,需求核心功能) - @PostMapping("/scan") - public String scanDrink( - @RequestParam String studentId, - @RequestParam String terminalId, - @RequestParam double volume // 饮水量(终端机流量传感器获取或按时间估算) - ) { - // 获取当前终端机水质(用于记录) - String waterQuality = waterQualityService.getWaterQuality(terminalId); - // 生成饮水记录(存入MySQL) - drinkRecordService.createDrinkRecord(studentId, terminalId, volume, waterQuality); - return "扫码用水成功,饮水量:" + volume + "L,水质:" + waterQuality; - } - - // 2. 查询今日饮水量(需求“查看每日饮水量”功能) - @GetMapping("/today/{studentId}") - public DrinkRecordVO getTodayDrink(@PathVariable String studentId) { - return drinkRecordService.getTodayDrinkRecord(studentId); - } - - // 3. 查询历史饮水量(按日/周/月筛选) - @GetMapping("/history/{studentId}") - public List getHistoryDrink( - @PathVariable String studentId, - @RequestParam String type, // type: day/week/month - @RequestParam String date // 日期(如2024-05-20) - ) { - return drinkRecordService.getHistoryDrinkRecord(studentId, type, date); - } -} \ No newline at end of file diff --git a/src/student-app-notuse/StudentMqttSubscriber.java b/src/student-app-notuse/StudentMqttSubscriber.java deleted file mode 100644 index 943a6c3..0000000 --- a/src/student-app-notuse/StudentMqttSubscriber.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.campus.water.student.mqtt; - -import com.alibaba.fastjson2.JSONObject; -import com.campus.water.student.config.StudentMqttConfig; -import com.campus.water.student.entity.WaterQualityVO; -import com.campus.water.student.service.WaterQualityService; -import org.eclipse.paho.client.mqttv3.IMqttMessageListener; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * 学生APP端MQTT订阅器(仅接收终端机水质数据,适配“扫码查水质”需求) - */ -@Component -public class StudentMqttSubscriber { - @Autowired - private StudentMqttConfig mqttConfig; - @Autowired - private WaterQualityService waterQualityService; - - // 学生扫码后订阅对应终端机的水质数据 - public void subscribeTerminalWaterQuality(String studentId, String terminalId) throws Exception { - // 1. 创建MQTT客户端(按学生ID区分) - MqttClient client = mqttConfig.mqttClient(studentId); - - // 2. 订阅该终端机的水质主题 - String topic = mqttConfig.getSubscribeTopic(terminalId); - client.subscribe(topic, 0, messageListener(terminalId)); - System.out.println("学生APP(ID:" + studentId + ")已订阅终端机" + terminalId + "水质数据"); - } - - // 消息监听器(缓存水质数据,供APP前端展示) - private IMqttMessageListener messageListener(String terminalId) { - return (topic, message) -> { - String payload = new String(message.getPayload()); - System.out.println("学生APP接收终端机" + terminalId + "水质数据:" + payload); - - // 解析水质数据并缓存 - WaterQualityVO qualityVO = JSONObject.parseObject(payload, WaterQualityVO.class); - waterQualityService.updateWaterQuality(terminalId, qualityVO); - }; - } -} \ No newline at end of file