工业级运维app手机api
沈斌
2017-11-15 a93eae62e20f3c8c94166021581161379e8c1947
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
package com.moral.monitor.listener.quartz;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.moral.monitor.dao.JobDao;
import com.moral.monitor.entity.Equipment;
import com.moral.monitor.util.RedisUtil;
 
public class StopStateCheck {
 
    @Resource
    JobDao jobDao;
 
    @Resource
    RedisTemplate<String, String> redisTemplate;
 
    private Logger logger = LoggerFactory.getLogger(StopStateCheck.class);
 
    private int waits = 10000;
 
    public void check() {
 
        List<Equipment> equipmentList = jobDao.getAllEquipment();
        for (int i = 0; i < equipmentList.size(); i++) {
            Equipment equipment = equipmentList.get(i);
            String mac = equipment.getMac();
            String static_equ_json = RedisUtil.get(redisTemplate, "static_equ_" + mac);
            //设备资料为null,说明未收到该设备的数据,直接离线
            if(StringUtils.isEmpty(static_equ_json)) {
                //这里为空,直接离线
                   jobDao.updateStateByMac(mac, 4);
                   return;
            }
            Map<String,String> seJSON = JSON.parseObject(static_equ_json,new TypeReference<Map<String,String>>() {});
            if(!seJSON.isEmpty()) {
                String orgId = seJSON.get("orgId");
                String areaCode = seJSON.get("areaCode");
                String monitorpointId = seJSON.get("monitorpointId");
 
                String key = "state_" + orgId + "_" + areaCode + "_" + monitorpointId + "_" + mac;
                String state_json = RedisUtil.get(redisTemplate, key);
 
                System.out.println("++++++++++++++++++++");
                System.out.println(key + " => " + state_json);
                logger.error(key + " => " + state_json);
 
                //设备实时state为null,说明未收到该设备的数据,直接离线
                if(StringUtils.isEmpty(state_json)) {
                       //这里为空,直接离线
                       jobDao.updateStateByMac(mac, 4);
                       return;
                }
                Map<String,String> stateJSON = JSON.parseObject(state_json,new TypeReference<Map<String,String>>() {});
                if(!stateJSON.isEmpty()) {
                    Long time = Long.parseLong(stateJSON.get("time"));
                    if(time + waits < new Date().getTime()) {
                        jobDao.updateStateByMac(mac, 4);
                    }
                } else {
                    jobDao.updateStateByMac(mac, 4);
                }
            } else {
                jobDao.updateStateByMac(mac, 4);
            }
        }
 
//        List<Equipment> equipmentList = jobDao.getAllEquipment();
//        for (int i = 0; i < equipmentList.size(); i++) {
//            Equipment equipment = equipmentList.get(i);
//            String mac = equipment.getMac();
//            Logger logger = jobDao.getLoggerByMac(mac);
//            if(logger != null) {
//                Date time = logger.getTime();
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTime(time);
//                calendar.add(Calendar.MINUTE, 10);
//                Date date = new Date();
//                if(calendar.getTime().getTime() < date.getTime()) {
//                    jobDao.updateStateByMac(mac, 4);
//                }
//            } else {
//                jobDao.updateStateByMac(mac, 4);
//            }
//        }
    }
}