工业级运维app手机api
沈斌
2018-02-26 0db07f7dc57ebbde26962138884d5ec86fc74b26
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.moral.monitor.listener.message;
 
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import static com.moral.monitor.listener.quartz.GetAccessToken.access_token;
 
/**
 * Created by a on 2017/3/13.
 */
public class WeChat implements Runnable {
    private Logger logger = LoggerFactory.getLogger(WeChat.class);
    private int level;
    private String zu;
    private String wx;
    private Long time;
    private String mac;
    public void run() {
        long ssatime = new Date().getTime();
        String result = sendWechatmsgToUser(level, zu, wx, mac, time);
        long esatime = new Date().getTime();
        logger.warn("  微信报警耗时 " + (esatime - ssatime) + "==========" + Thread.currentThread().getName() + "=========");
        if (!"success".equals(result)) {
            logger.warn("微信发送失败" + result);
        }
    }
 
    public static JSONObject packJsonmsg(Integer level, String zu, String time) {
        String lev = null;
        String col = null;
        if (level == 3) {
            lev = "三级";
            col = "#fd416e";
        } else if (level == 2) {
            lev = "二级";
            col = "#fe926f";
        } else if (level == 1) {
            lev = "一级";
            col = "#fdf24a";
        }
        JSONObject json = new JSONObject();
        // 标题
        JSONObject first = new JSONObject();
        first.put("value", "七星瓢虫坏境监控智能报警");
        first.put("color", "#000000");
        json.put("first", first);
        // 报警来源
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "江苏省昆山市摩瑞尔电器有限公司");
        keyword1.put("color", "#000000");
        json.put("keyword1", keyword1);
        // 报警内容
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", "环境数(工业级)监测到异常数据");
        keyword2.put("color", "#000000");
        json.put("keyword2", keyword2);
        // 报警组别
        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", zu + "指数超标");
        keyword3.put("color", "#000000");
        json.put("keyword3", keyword3);
        // 报警等级
        JSONObject keyword4 = new JSONObject();
        keyword4.put("value", lev);
        keyword4.put("color", col);
        json.put("keyword4", keyword4);
        // 报警时间
        JSONObject keyword5 = new JSONObject();
        keyword5.put("value", time);
        keyword5.put("color", "#000000");
        json.put("keyword5", keyword5);
        // 提示语
        JSONObject remark = new JSONObject();
        remark.put("value", "情况紧急,请马上处理(点击查看详情)");
        remark.put("color", "#000000");
        json.put("remark", remark);
        return json;
    }
 
    //微信: 等级,传感中文名,联系人,报警信息
    public static String sendWechatmsgToUser(int level, String zu, String wx, String mac, Long time) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
        //处理时间,准备mac 地址,做为微信公从号推送内容的参数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = sdf.format(new Date());
        //获取 要发送的 json 对象 .报警组,报警数据,日期
        JSONObject data = packJsonmsg(level, zu, times);
        // 微信详情页  时间,mac, 等级
        String clickurl = "http://101.37.22.173:8080/task/detail.do?time=" + time + "&mac=" + mac + "&level=" + level;
        String topcolor = "#FF0000";
        String templat_id = "j8E0d-GgKljSCpuTlCoYdWS6kquaSE1AnvmklE5MoO0";
        JSONObject json = new JSONObject();
        try {
            json.put("touser", wx);
            json.put("template_id", templat_id);
            json.put("url", clickurl);
            json.put("topcolor", topcolor);
            json.put("data", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String result = httpsRequest(url, "POST", json.toString());
        try {
            JSONObject resultJson = new JSONObject(result);
            String errmsg = (String) resultJson.get("errmsg");
            if (!"ok".equals(errmsg)) {  //如果为errmsg为ok,则代表发送成功,公众号推送信息给用户了。
                return errmsg;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "success";
    }
 
 
    public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
        try {
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            System.out.println("连接超时:{}");
        } catch (Exception e) {
            System.out.println("https请求异常:{}");
        }
        return null;
    }
 
    public WeChat(int level, String zu, String wx, String mac, long time) {
        this.level = level;
        this.zu = zu;
        this.wx = wx;
        this.mac = mac;
        this.time = time;
    }
}