工业级运维app手机api
fengxiang
2017-10-25 dbc28ad9f10a7e4ae3d1c837f721b168343e961e
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
package com.moral.monitor.service;
 
import com.moral.monitor.dao.WeChatDao;
import com.moral.monitor.entity.AlarmSensor;
import com.moral.monitor.entity.Equipment;
import com.moral.monitor.listener.message.WeChat;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * Created by a on 2017/5/26.
 */
@Service
public class WeChatService {
    @Resource
    WeChatDao weChatDao;
 
    public Equipment weiXindetail(String mac) {
        Equipment equipment = weChatDao.selectFromequipment(mac);
        return equipment;
    }
 
 
    public Map wxMap(String mac, long time){
        Date d = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(d);
 
        String receiveMsg = weChatDao.selectFromloggerbymacandtime(mac,date);
 
        Map<String, String> receiveMsgMap = toMap(receiveMsg);
 
        String rmac = receiveMsgMap.remove("mac");
        String rver = receiveMsgMap.remove("ver");
        String rtime = receiveMsgMap.remove("time");
 
 
        List<AlarmSensor> sensors = weChatDao.findAllsensorBymac(mac);
 
        Map<String,String> sensor_descvalue=new TreeMap<String, String>();
        for (AlarmSensor sensor : sensors) {
            if (!(receiveMsgMap.containsKey(sensor.getMac_key()))) {
                continue;
            }
 
            String mac_key = sensor.getMac_key();
            double value = Double.parseDouble(receiveMsgMap.get(sensor.getMac_key()));
            String displayvalue = digit(value, sensor.getDigit());
            String units=sensor.getUnits();
            sensor_descvalue.put(sensor.getSensor(),displayvalue+units);
        }
        return sensor_descvalue;
    }
 
 
    public String digit(double num, int digit) {
        BigDecimal y = new BigDecimal(num);
        double b = y.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
        double c = b - (int) b;
        if (c == 0) {
            return String.valueOf((int) b);
        } else {
            return String.valueOf(b);
        }
    }
 
    public Map<String, String> toMap(Object object) {
        Map<String, String> data = new LinkedHashMap<String, String>();
        JSONObject jsonObject = JSONObject.fromObject(object);
        Iterator ite = jsonObject.keys();
        while (ite.hasNext()) {
            String key = ite.next().toString();
            String value = jsonObject.get(key).toString();
            data.put(key, value);
        }
        return data;
    }
 
 
 
}