沈斌
2018-02-08 3120cdd01c8e6a2a1edefb6ff251c026f44dee5c
src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -14,6 +14,7 @@
import com.moral.common.util.RedisUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
@@ -164,17 +165,22 @@
   private void loadDeviceState(List<Device> list){
      //从redis里取状态
      list.stream().map( device -> {
         String state = getSateFromRedis(device.getMonitorPointId(),device.getMac());
         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();
   }
   private String getSateFromRedis(Integer mpId,String mac){
      Map<String,String> states  = getStateMapFromRedis(mpId,mac);
      Map<String,String> stateMap  = getStateMapFromRedis(mpId,mac);
      String state = null;
      if(states != null){
         state  = states.get("state");
      if(stateMap != null){
         state  = stateMap.get("state");
      }
      state = state == null ?Constants.DEVICE_STATE_OFFLINE:state;
      return  state;
@@ -235,6 +241,10 @@
   @Override
   public void addOrModify(Device device){
      try{
         //mac 转小写
         if(StringUtils.isBlank(device.getMac())){
            device.setMac(device.getMac().toLowerCase());
         }
         if(device.getId()==null){
            device.setIsDelete(Constants.IS_DELETE_FALSE);
            deviceMapper.insertSelective(device);
@@ -263,22 +273,25 @@
   @Override
   public List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData) {
      List<Map<String,String>> list = macList.stream().map(mac->{
         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);
         }
         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);
            }
         }else{
            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;
      }).collect(Collectors.toList());