沈斌
2018-04-26 b98924dab9f433d152944f189e0a28edb09ed7e7
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);
   }
   /**
@@ -202,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);
@@ -246,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){