DisplayBoard.cs 2.6 KB
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 List<AlarmMsg>();

        public static void Add(string name, string key, string value, int type = 0)
        {
            msgList.Add(new AlarmMsg(name, key, value, type));
        }

        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;
        }
    }

}