From 82007c3574640614f688479cc6c6e2ef5d729dbd Mon Sep 17 00:00:00 2001
From: fengxiang <110431245@qq.com>
Date: Thu, 26 Apr 2018 08:59:22 +0800
Subject: [PATCH] Date 时间不做 精确处理
---
src/main/java/com/moral/service/impl/DeviceServiceImpl.java | 122 ++++++++++++++++++++++++++++++++--------
1 files changed, 98 insertions(+), 24 deletions(-)
diff --git a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
index bf3ad03..a3c7ea1 100644
--- a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -12,6 +12,7 @@
import com.moral.common.bean.PageResult;
import com.moral.common.util.ExampleUtil;
import com.moral.common.util.RedisUtils;
+import com.moral.mapper.MonitorPointMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@@ -29,10 +30,25 @@
@Service
public class DeviceServiceImpl implements DeviceService {
-
+ //-----------------------redis key������-������---------------------------------
+ private static String AlARM = "alarm";//������������������key������
+ private static String ADJUST="adjust";//���������������key������
+ private static String DEVICE = "device";//������������������key������
+ private static String STATE = "state";//������������������key������
+ private static String DATA = "data";//������������������key������
+ //-----------------------redis key������-������---------------------------------
+ private String keysConnect(String... keys) {
+ StringBuilder key = new StringBuilder(keys[0]);
+ for(int i=1;i<keys.length;i++) {
+ key.append("_");
+ key.append(keys[i]);
+ }
+ return key.toString().toLowerCase();
+ }
@Resource
private DeviceMapper deviceMapper;
-
+ @Resource
+ private MonitorPointMapper monitorPointMapper;
@Resource
private AccountService accountService;
private Class ENTITY_CLASS = Device.class;
@@ -99,11 +115,39 @@
@Override
public Device getDeviceByMac(String mac) {
- ValidateUtil.notEmpty(mac, "param.is.null");
- Device device = new Device();
- device.setMac(mac);
- device = deviceMapper.selectOne(device);
+ Device device = getDeviceFromRedis(mac);
+ if(device==null) {
+ device = deviceMapper.selectWithOrgIdsByMac(mac);
+ if(device!=null){
+ setDeviceToRedis(mac,device);
+ }
+ }
return device;
+ }
+ /*
+ * ���������������������redis������key
+ */
+ private String getDeviceKey(String mac) {
+ return keysConnect(DEVICE,mac);
+ }
+ /*
+ * ������������������redis��������� ������
+ */
+ private void setDeviceToRedis(String mac,Device device){
+ String key = getDeviceKey(mac);
+ Device simpleDevice = new Device();
+ simpleDevice.setId(device.getId());// id
+ simpleDevice.setName(device.getName());// name
+ simpleDevice.setAddress(device.getAddress());// address
+ simpleDevice.setDeviceVersionId(device.getDeviceVersionId());// version
+ simpleDevice.setMac(device.getMac()); // mac
+ simpleDevice.setMonitorPointId(device.getMonitorPointId());// ���������id
+ simpleDevice.setOrganizationIds(device.getOrganizationIds());// ������������
+ redisUtils.set(key,simpleDevice);
+ }
+ private Device getDeviceFromRedis(String mac) {
+ String key = getDeviceKey(mac);
+ return redisUtils.get(key,Device.class);
}
/**
@@ -165,9 +209,13 @@
private void loadDeviceState(List<Device> list){
//���redis������������
list.stream().map( device -> {
- String mac = Optional.of(device.getMac()).get();
- String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase());
- device.setState(state);
+ String mac = device.getMac();
+ if(!StringUtils.isBlank(mac)){
+ String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase());
+ device.setState(state);
+ }else{
+ device.setState(Constants.DEVICE_STATE_OFFLINE);
+ }
return device;
}).count();
}
@@ -198,7 +246,27 @@
}
return device;
}
-
+ /*
+ ������ redis ���������������
+ */
+ private void refreshDeviceInRedis(Device device){
+ if(!StringUtils.isBlank(device.getMac())){
+ Device simpleDevice = new Device();
+ simpleDevice.setId(device.getId());// id
+ simpleDevice.setName(device.getName());// name
+ simpleDevice.setAddress(device.getAddress());// address
+ simpleDevice.setDeviceVersionId(device.getDeviceVersionId());// version
+ simpleDevice.setMac(device.getMac()); // mac
+ simpleDevice.setMonitorPointId(device.getMonitorPointId());// ���������id
+ simpleDevice.setOrganizationIds(device.getOrganizationIds());// ������������
+ if(device.getMonitorPointId()!=null){
+ List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(device.getMonitorPointId());
+ simpleDevice.setOrganizationIds(orgIds);
+ }
+ String key = "device_"+device.getMac();
+ redisUtils.set(key,simpleDevice);
+ }
+ }
@Override
public PageBean queryByPageBean(PageBean pageBean) {
Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
@@ -242,10 +310,13 @@
device.setMac(device.getMac().toLowerCase());
}
if(device.getId()==null){
+ device.setState(Constants.DEVICE_STATE_OFFLINE);
device.setIsDelete(Constants.IS_DELETE_FALSE);
deviceMapper.insertSelective(device);
}else{
deviceMapper.updateByPrimaryKeySelective(device);
+ //������redis���������������
+ refreshDeviceInRedis(device);
}
}
catch (Exception ex){
@@ -269,21 +340,24 @@
@Override
public List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData) {
List<Map<String,String>> list = macList.stream().map(mac->{
- mac = mac.toLowerCase();
- Device device = getDeviceWithOrgIdsByMac(mac);
Map<String,String> resultMap = new HashMap<>();
- Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
- if(!MapUtils.isEmpty(stateMap)){
- resultMap.putAll(stateMap);
- }else{
- resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
- resultMap.put("mac",mac);
- }
- if(BooleanUtils.isTrue(withData)){
- String dataKey = "data_"+mac;
- Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
- if(!MapUtils.isEmpty(dataMap)){
- resultMap.putAll(dataMap);
+ if(!StringUtils.isBlank(mac)){
+ mac = mac.toLowerCase();
+ Device device = getDeviceWithOrgIdsByMac(mac);
+ Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
+ if(!MapUtils.isEmpty(stateMap)){
+ resultMap.putAll(stateMap);
+ }else{
+ resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
+ resultMap.put("mac",mac);
+ }
+ //������data
+ if(BooleanUtils.isTrue(withData)){
+ String dataKey = "data_"+mac;
+ Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
+ if(!MapUtils.isEmpty(dataMap)){
+ resultMap.putAll(dataMap);
+ }
}
}
return resultMap;
--
Gitblit v1.8.0