自动化立体仓库 - 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.zy.iot.service.impl;
 
import com.core.common.Cools;
import com.zy.iot.config.IotProperties;
import com.zy.iot.constant.IotSysConfigCodes;
import com.zy.iot.entity.IotTopicConfig;
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.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.util.LinkedHashMap;
import java.util.Map;
 
@Service
public class IotDbConfigServiceImpl implements IotDbConfigService {
 
    @Autowired
    private ConfigService configService;
    @Autowired
    private IotProperties iotProperties;
 
    private volatile Snapshot snapshot;
 
    @PostConstruct
    public void init() {
        refreshCache();
    }
 
    @Override
    public synchronized void refreshCache() {
        Snapshot s = new Snapshot();
        s.enabled = resolveEnabled();
        s.topics = resolveTopics();
        this.snapshot = s;
    }
 
    @Override
    public boolean isMqttEnabled() {
        Snapshot s = snapshot;
        return s != null && s.enabled;
    }
 
    @Override
    public IotTopicConfig getEffectiveTopics() {
        Snapshot s = snapshot;
        if (s == null || s.topics == null) {
            return copyTopics(iotProperties.getTopics());
        }
        return copyTopics(s.topics);
    }
 
    @Override
    public Map<String, Object> snapshotForAdmin() {
        Snapshot s = snapshot;
        if (s == null) {
            refreshCache();
            s = snapshot;
        }
        Map<String, Object> root = new LinkedHashMap<>();
        root.put("enabled", s != null && s.enabled);
        Map<String, String> topics = new LinkedHashMap<>();
        IotTopicConfig t = s != null && s.topics != null ? s.topics : iotProperties.getTopics();
        topics.put("egressStow", t.getEgressStow());
        topics.put("egressPick", t.getEgressPick());
        topics.put("egressFeedback", t.getEgressFeedback());
        topics.put("ingressStow", t.getIngressStow());
        topics.put("ingressPick", t.getIngressPick());
        topics.put("ingressFeedback", t.getIngressFeedback());
        root.put("topics", topics);
        return root;
    }
 
    private boolean resolveEnabled() {
        Config c = configService.selectConfigByCode(IotSysConfigCodes.MQTT_ENABLED);
        if (c != null && isConfigActive(c) && !Cools.isEmpty(c.getValue())) {
            String v = c.getValue().trim();
            if ("1".equals(v) || "true".equalsIgnoreCase(v) || "yes".equalsIgnoreCase(v)) {
                return true;
            }
            if ("0".equals(v) || "false".equalsIgnoreCase(v) || "no".equalsIgnoreCase(v)) {
                return false;
            }
        }
        return iotProperties.isEnabled();
    }
 
    private IotTopicConfig resolveTopics() {
        IotTopicConfig yml = iotProperties.getTopics();
        IotTopicConfig t = new IotTopicConfig();
        t.setEgressStow(orDb(IotSysConfigCodes.TOPIC_EGRESS_STOW, yml.getEgressStow()));
        t.setEgressPick(orDb(IotSysConfigCodes.TOPIC_EGRESS_PICK, yml.getEgressPick()));
        t.setEgressFeedback(orDb(IotSysConfigCodes.TOPIC_EGRESS_FEEDBACK, yml.getEgressFeedback()));
        t.setIngressStow(orDb(IotSysConfigCodes.TOPIC_INGRESS_STOW, yml.getIngressStow()));
        t.setIngressPick(orDb(IotSysConfigCodes.TOPIC_INGRESS_PICK, yml.getIngressPick()));
        t.setIngressFeedback(orDb(IotSysConfigCodes.TOPIC_INGRESS_FEEDBACK, yml.getIngressFeedback()));
        return t;
    }
 
    private String orDb(String code, String ymlDefault) {
        Config c = configService.selectConfigByCode(code);
        if (c != null && isConfigActive(c) && !Cools.isEmpty(c.getValue())) {
            return c.getValue().trim();
        }
        return ymlDefault;
    }
 
    private static boolean isConfigActive(Config c) {
        return c.getStatus() != null && c.getStatus() == 1;
    }
 
    private static IotTopicConfig copyTopics(IotTopicConfig src) {
        if (src == null) {
            return new IotTopicConfig();
        }
        IotTopicConfig c = new IotTopicConfig();
        c.setEgressStow(src.getEgressStow());
        c.setEgressPick(src.getEgressPick());
        c.setEgressFeedback(src.getEgressFeedback());
        c.setIngressStow(src.getIngressStow());
        c.setIngressPick(src.getIngressPick());
        c.setIngressFeedback(src.getIngressFeedback());
        return c;
    }
 
    private static final class Snapshot {
        private boolean enabled;
        private IotTopicConfig topics;
    }
}