fengxiang
2018-05-11 fd13d232df7292d93172d1200235a77f286f64ed
设备  redis 同步完善
1 files added
7 files modified
103 ■■■■■ changed files
src/main/java/com/moral/controller/MobileController.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ProfessionController.java 22 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ScreenController.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/entity/Device.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/moral/entity/adapter/EquDeviceAdapter.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/DeviceServiceImpl.java 31 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java 30 ●●●●● patch | view | raw | blame | history
src/main/resources/system/alarmLevels.json 12 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/MobileController.java
@@ -89,7 +89,7 @@
     */
    @RequestMapping("reportDevice")
    public AppData<String> installDevice(EquDeviceAdapter equDeviceAdapter) {
        deviceService.saveOrUpdateDevice(equDeviceAdapter.toDevice());
        deviceService.addOrModify(equDeviceAdapter.toDevice());
        return new AppData<String>("");
    }
src/main/java/com/moral/controller/ProfessionController.java
New file
@@ -0,0 +1,22 @@
package com.moral.controller;
import com.moral.common.bean.ResultBean;
import com.moral.entity.Profession;
import com.moral.service.ProfessionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("profession")
public class ProfessionController {
      @Resource
      ProfessionService professionService;
      @GetMapping(value = "getall")
     public ResultBean<List<Profession>> getALL(){
          return  new ResultBean<>(professionService.getProfessiontList());
      }
}
src/main/java/com/moral/controller/ScreenController.java
@@ -177,6 +177,7 @@
                 alarmLevelMap.entrySet().stream().forEach(entry -> {
                     //当 increment都为0时,替换为默认值
                     AlarmSensorLevel alarmSensorLevel = entry.getValue();
                     if(alarmSensorLevel.getIncrement()!=null){
                     boolean isAllZero = alarmSensorLevel.getIncrement().stream().allMatch(item ->  item == 0);
                     if(isAllZero){
                         AlarmSensorLevel defaultAlarmSensorLevel = finalDefaultSensorLevelMap==null?null:finalDefaultSensorLevelMap.get(entry.getKey());
@@ -187,6 +188,7 @@
                             alarmSensorLevel.setIncrement(tempList);
                         }
                     }
                     }
                 });
                 //map to list
                 Map<String, AlarmSensorLevel> finalAlarmLevelMap = alarmLevelMap;
src/main/java/com/moral/entity/Device.java
@@ -76,7 +76,7 @@
     */
    private Integer deviceVersionId;
    private String isDelete;
    private String professionId;
    private Integer professionId;
    /**
     * Sets the uid.
     *
src/main/java/com/moral/entity/adapter/EquDeviceAdapter.java
@@ -47,7 +47,7 @@
        device.setMonitorPointId(monitorpoint);
    }
    public void setProfessionId(String professionId) {
    public void setProfessionId(Integer professionId) {
        device.setProfessionId(professionId);
    }
    public Device toDevice(){
src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -117,6 +117,13 @@
        List<Device> devices = deviceMapper.select(device);
        return devices;
    }
    /**
     *
     * @param mac
     * @param fromCache
     * @return 包含组织数组
     */
    @Override
    public  Device getDeviceByMac (String mac ,boolean fromCache){
        if(fromCache){
@@ -271,20 +278,20 @@
     */
    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());// 组织数组
//             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);
                 device.setOrganizationIds(orgIds);
             }
             String key = "device_"+device.getMac();
             redisUtils.set(key,simpleDevice);
             redisUtils.set(key,device);
         }
    }
    @Override
@@ -340,7 +347,7 @@
    public void addOrModify(Device device){
        try{
            //mac 转小写
            if(StringUtils.isBlank(device.getMac())){
            if(!StringUtils.isBlank(device.getMac())){
                device.setMac(device.getMac().toLowerCase());
            }
            if(device.getId()==null){
@@ -349,9 +356,9 @@
                deviceMapper.insertSelective(device);
            }else{
                deviceMapper.updateByPrimaryKeySelective(device);
            }
                //刷新redis里设备信息
                refreshDeviceInRedis(device);
            }
        }
        catch (Exception ex){
            throw  ex;
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -95,9 +95,17 @@
                monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE);
                monitorPointMapper.insertSelective(monitorPoint);
            }else{
                MonitorPoint queryMonitorPoint = new MonitorPoint();
                queryMonitorPoint.setId(monitorPoint.getId());
                queryMonitorPoint.setOrganizationId(monitorPoint.getOrganizationId());
                // num = 1,说明未改变,此查询在更新之前
                Integer num =  monitorPointMapper.selectCount(queryMonitorPoint);
                boolean needRefreshCach = (num!=1);
                monitorPointMapper.updateByPrimaryKeySelective(monitorPoint);
                if(needRefreshCach){
                // 刷新当前监控点下设备 在redis里设备信息
                refreshDevicesInRedis(monitorPoint.getId());
                }
            }
        }
        catch (Exception ex){
@@ -108,24 +116,24 @@
      刷新当前监控点下设备 在redis里设备信息
     */
    private void  refreshDevicesInRedis(int monitorPointId){
        Device device = new Device();
        device.setMonitorPointId(monitorPointId);
        List<Device> deviceList = deviceMapper.select(device);
        Device queryDevice = new Device();
        queryDevice.setMonitorPointId(monitorPointId);
        List<Device> deviceList = deviceMapper.select(queryDevice);
        if (!CollectionUtils.isEmpty(deviceList)){
            List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(monitorPointId);
            if (!CollectionUtils.isEmpty(orgIds)){
                deviceList.stream().forEach(dev ->{
                    if(!StringUtils.isNullOrEmpty(dev.getMac())){
                        String key = "device_"+dev.getMac();
                        // 简化的设备信息 用以缓存redis
                        Device simpleDevice = new Device();
                        simpleDevice.setId(dev.getId());
                        simpleDevice.setDeviceVersion(dev.getDeviceVersion());
                        simpleDevice.setMac(dev.getMac());
                        simpleDevice.setMonitorPointId(dev.getMonitorPointId());
//                        // 简化的设备信息 用以缓存redis
//                        Device simpleDevice = new Device();
//                        simpleDevice.setId(dev.getId());
//                        simpleDevice.setDeviceVersion(dev.getDeviceVersion());
//                        simpleDevice.setMac(dev.getMac());
//                        simpleDevice.setMonitorPointId(dev.getMonitorPointId());
                        // 设置新组织关系,防止读写分离时数据库同步延迟
                        simpleDevice.setOrganizationIds(orgIds);
                        redisUtils.set(key,simpleDevice);
                        dev.setOrganizationIds(orgIds);
                        redisUtils.set(key,dev);
                    }
                });
            }
src/main/resources/system/alarmLevels.json
@@ -50,9 +50,9 @@
    "e5": {
      "enable": 0,
      "increment": [
        0.01,
        0.02,
        0.05
        0.1,
        0.5,
        1
      ],
      "degressEnable": 0,
      "degression": [0,0,0]
@@ -90,9 +90,9 @@
    "e9": {
      "enable": 0,
      "increment": [
        0.01,
        0.02,
        0.03
        100,
        500,
        1000
      ],
      "degressEnable": 0,
      "degression": [0,0,0]