package com.doumee.service.business.impl;
|
|
import com.doumee.biz.system.SystemDictDataBiz;
|
import com.doumee.core.device.model.ElectronicConstant;
|
import com.doumee.core.utils.Constants;
|
import com.doumee.dao.system.model.SystemDictData;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.PostConstruct;
|
|
/**
|
* 从数据字典 ELECTRICAL_PARAM 加载智能电表第三方平台连接参数。
|
*/
|
@Slf4j
|
@Service
|
public class ElectronicConfigService {
|
|
@Autowired
|
private SystemDictDataBiz systemDictDataBiz;
|
|
@PostConstruct
|
public void init() {
|
refreshFromDict();
|
}
|
|
/** 重新从数据字典加载配置(修改字典后重启服务生效,或主动调用刷新) */
|
public void refreshFromDict() {
|
ElectronicConstant.auth_code = read(Constants.ELECTRICAL_AUTH_CODE, ElectronicConstant.DEFAULT_AUTH_CODE);
|
ElectronicConstant.nonce = read(Constants.ELECTRICAL_NONCE, ElectronicConstant.DEFAULT_NONCE);
|
ElectronicConstant.notify_url = read(Constants.ELECTRICAL_NOTIFY_URL, ElectronicConstant.DEFAULT_NOTIFY_URL);
|
ElectronicConstant.api_url = read(Constants.ELECTRICAL_API_URL, ElectronicConstant.DEFAULT_API_URL);
|
ElectronicConstant.api2_url = read(Constants.ELECTRICAL_API2_URL, ElectronicConstant.DEFAULT_API2_URL);
|
log.info("electrical config loaded from dict {}, api_url={}, api2_url={}, notify_url={}",
|
Constants.ELECTRICAL_PARAM, ElectronicConstant.api_url, ElectronicConstant.api2_url, ElectronicConstant.notify_url);
|
}
|
|
private String read(String label, String defaultValue) {
|
try {
|
SystemDictData data = systemDictDataBiz.queryByCode(Constants.ELECTRICAL_PARAM, label);
|
if (data != null && StringUtils.isNotBlank(data.getCode())) {
|
return data.getCode().trim();
|
}
|
log.warn("electrical config [{}] empty, use default", label);
|
} catch (Exception e) {
|
log.warn("electrical config [{}] load failed, use default", label, e);
|
}
|
return defaultValue;
|
}
|
}
|