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 snapshotForAdmin() { Snapshot s = snapshot; if (s == null) { refreshCache(); s = snapshot; } Map root = new LinkedHashMap<>(); root.put("enabled", s != null && s.enabled); Map 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; } }