fengxiang
2018-05-10 c976fedb4d58a23aff930188d4031adac54a3037
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.moral.controller;
 
import com.alibaba.fastjson.JSON;
import com.moral.common.bean.ResultBean;
import com.moral.entity.Alarm;
import com.moral.entity.Device;
import com.moral.entity.Sensor;
import com.moral.entity.alarm.AlarmConfig;
import com.moral.entity.alarm.AlarmConfigValue;
import com.moral.entity.charts.TimeUnits;
import com.moral.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.*;
 
@RestController
@RequestMapping("alarm")
public class AlarmController {
    private  static Logger logger = Logger.getLogger(AlarmController.class);
    @Resource
    DeviceService deviceService;
    @Resource
    SensorService sensorService;
    @Resource
    HistoryService historyService;
    @Resource
    AlarmConfigService alarmConfigService;
    @Resource
    AlarmService alarmService;
    @RequestMapping(value = "/count-by-times", method = RequestMethod.GET)
    public ResultBean<List<Map>> countByTimes(Date start, Date end,@RequestParam(value = "timeUnits")Optional<TimeUnits> timeUnits){
      return  new ResultBean<>(alarmService.countByTimes(start,end,timeUnits.isPresent()?timeUnits.get():null));
    }
    /**
     *
     * @param alarm
     * @return
     */
    @RequestMapping(value = "/alarm-show", method = RequestMethod.GET)
    public ModelAndView alarmShow(Alarm alarm){
        ModelAndView model = new ModelAndView();
        if (alarm.getMac()!=null&&alarm.getState()!=null
            &&alarm.getJson()!=null&&alarm.getTime()!=null){
            model.setViewName("/alarm/device-data");
            Device device = deviceService.getDeviceByMac(alarm.getMac());
            if(!CollectionUtils.isEmpty(device.getOrganizationIds())){
                List<Sensor> sensorList  = sensorService.queryByVersionId(device.getDeviceVersionId()).getData();
                String data = historyService.queryValueByMacAndTime(alarm.getMac(),alarm.getTime());
                AlarmConfigValue alarmConfigValue
                         = alarmConfigService
                        .queryValueByOrganizationId(device.getOrganizationIds().get(0))
                        .get().getValue();
                if(device!=null
                    &&!CollectionUtils.isEmpty(sensorList)
                    &&!StringUtils.isEmpty(data)
                    &&alarmConfigValue!=null
                    &&alarmConfigValue.getAlarmLevels()!=null){
                    Map<String,Object> params = new HashMap<>();
                    model.addObject("alarm",JSON.toJSON(alarm));
                    model.addObject("device",JSON.toJSON(device));
                    model.addObject("sensors",JSON.toJSON(sensorList));
                    model.addObject("data",JSON.toJSON(data));
                    model.addObject("alarmLevels",JSON.toJSON(alarmConfigValue.getAlarmLevels()));
                }
                else{
                    StringBuilder message = new StringBuilder();
                    message.append("some params is null,alarm:");
                    message.append(JSON.toJSON(alarm));
                    message.append(",device:");
                    message.append(JSON.toJSON(device));
                    message.append(",sensorList:");
                    message.append(JSON.toJSON(sensorList));
                    message.append(",data:");
                    message.append(JSON.toJSON(data));
                    logger.warn(message.toString());
                    model.setViewName("403");
                }
            }else{
                StringBuilder message = new StringBuilder();
                message.append("some params is null,alarm:");
                message.append(JSON.toJSON(alarm));
                message.append(",device:");
                message.append(JSON.toJSON(device));
                logger.warn(message.toString());
                model.setViewName("403");
            }
 
        }else{
            StringBuilder message = new StringBuilder();
            message.append("some params is null,alarm:");
            message.append(JSON.toJSON(alarm));
            model.setViewName("403");
        }
        return  model;
    }
}