DisplayBoard.cs
2.7 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using RestSharp;
namespace Model
{
public static class DisplayBoard
{
private static List<AlarmMsg> msgList = new();
public static void Add(string name, string key, string value, int type = 0)
{
msgList.Add(new AlarmMsg(name, key, value, type));
Common.log.Debug(string.Format("{0}添加监控面板,{1}", name, value));
}
public static string UpdateAlarmMsg()
{
if (msgList.Count == 0) return "";
string s = UpdateDeviceAlarmMsg();
msgList.Clear();
return s;
}
private static string UpdateDeviceAlarmMsg()
{
string msg = "OK";
try
{
Dictionary<string, string> paramMap = new Dictionary<string, string>();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(msgList);
paramMap.Add("deviceAlarmList", json);
string server = GetUrl(paramMap);
RestClient client = new RestClient(server) { Timeout = 2000 };
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json;charset=UTF-8");
IRestResponse response = client.Execute(request);
json = response.Content;
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic == null)
msg = "updateDeviceAlarmMsg 没有收到服务器反馈";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
private static string GetUrl(Dictionary<string, string> paramsMap)
{
string path = Common.BoardURL;
List<string> value = new List<string>();
foreach (string key in paramsMap.Keys)
{
string par = System.Web.HttpUtility.UrlEncode(paramsMap[key], Encoding.UTF8);
value.Add(key + "=" + par);
}
path += "?" + string.Join("&", value);
return path;
}
}
internal class AlarmMsg
{
public string name = "";
public string msgKey = "";
public string msgValue = "";
public int type;
public AlarmMsg(string name, string key, string value, int type = 0)
{
this.name = name;
msgKey = key;
msgValue = value;
this.type = type;
}
}
}