自动化立体仓库 - WMS系统
cl
2 天以前 18c51d40be82435289ba3be6bd5f8e15fdf786f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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<String, Object> 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));
        }
    }
}