package com.zy.iot.controller; import com.alibaba.fastjson.JSONObject; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.R; import com.zy.common.web.BaseController; import com.zy.integration.iot.client.IotMqttClient; import com.zy.iot.constant.IotSysConfigCodes; import com.zy.iot.service.IotDbConfigService; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.LinkedHashMap; import java.util.Map; /** * 后台:IoT MQTT 开关与 topic 存 sys_config,重连后订阅生效。 */ @RestController public class IotMqttAdminController extends BaseController { @Autowired private ConfigService configService; @Autowired private IotDbConfigService iotDbConfigService; @Autowired private IotMqttClient iotMqttClient; @RequestMapping(value = "/iotMqttAdmin/config/auth", method = RequestMethod.GET) @ManagerAuth(memo = "IoT MQTT 配置查询") public R getConfig() { Map data = new LinkedHashMap<>(iotDbConfigService.snapshotForAdmin()); data.put("mqttConnected", iotMqttClient.isConnected()); return R.ok(data); } @RequestMapping(value = "/iotMqttAdmin/config/save/auth", method = RequestMethod.POST) @ManagerAuth(memo = "IoT MQTT 配置保存") public R saveConfig(@RequestBody JSONObject body) { if (body == null) { return R.error("参数为空"); } if (body.containsKey("enabled")) { Boolean en = body.getBoolean("enabled"); upsert(IotSysConfigCodes.MQTT_ENABLED, "IoT MQTT 总开关", en == null ? "false" : String.valueOf(en)); } JSONObject topics = body.getJSONObject("topics"); if (topics != null) { putTopicIfPresent(topics, "egressStow", IotSysConfigCodes.TOPIC_EGRESS_STOW, "IoT topic egress stow"); putTopicIfPresent(topics, "egressPick", IotSysConfigCodes.TOPIC_EGRESS_PICK, "IoT topic egress pick"); putTopicIfPresent(topics, "egressFeedback", IotSysConfigCodes.TOPIC_EGRESS_FEEDBACK, "IoT topic egress feedback"); putTopicIfPresent(topics, "ingressStow", IotSysConfigCodes.TOPIC_INGRESS_STOW, "IoT topic ingress stow"); putTopicIfPresent(topics, "ingressPick", IotSysConfigCodes.TOPIC_INGRESS_PICK, "IoT topic ingress pick"); putTopicIfPresent(topics, "ingressFeedback", IotSysConfigCodes.TOPIC_INGRESS_FEEDBACK, "IoT topic ingress feedback"); } iotDbConfigService.refreshCache(); return R.ok(); } private void putTopicIfPresent(JSONObject topics, String key, String code, String name) { if (!topics.containsKey(key)) { return; } String v = topics.getString(key); if (v == null) { return; } upsert(code, name, v.trim()); } @RequestMapping(value = "/iotMqttAdmin/reconnect/auth", method = RequestMethod.POST) @ManagerAuth(memo = "IoT MQTT 重连") public R reconnect() { iotMqttClient.reconnectFromDbConfig(); return R.ok(); } private void upsert(String code, String name, String value) { if (Cools.isEmpty(code)) { return; } Config existing = configService.selectConfigByCode(code); if (existing != null) { existing.setValue(value); existing.setStatus((short) 1); configService.updateById(existing); } else { configService.insert(new Config(name, code, value, (short) 1, (short) 1)); } } }