Esempio n. 1
0
        private static ResponseMessage DoSendMessage(RequestMessage message)
        {
            if (message == null || !message.IsValid) return new ResponseMessage(MessageSendingResult.RequestMessageNullOrNotComplete, "Request messaage is null or not complete.");
            if (!ConfigSettings.Enabled) return new ResponseMessage(MessageSendingResult.SendingDisabled, "Cats sending is disabled.");

            try
            {
                var obj = new
                {
                    src_id = message.Source.Trim(),
                    dev_id = message.Device.Trim(),
                    item_id = message.Title.Trim(),
                    level = message.Level.ToString().ToLower(),
                    message = message.Message.Trim()
                };

                var resp = SendRequest(DynamicJson.Serialize(obj));
                var respObj = DynamicJson.Parse(resp);
                if (Convert.ToBoolean(respObj.success))
                    return new ResponseMessage(MessageSendingResult.Succeed, resp);
                else
                    return new ResponseMessage(MessageSendingResult.ProcessFailed, resp);
            }
            catch (Exception ex)
            {
                return new ResponseMessage(MessageSendingResult.Exception, ex.ToString());
            }
        }
Esempio n. 2
0
        public void SendMessage_Test()
        {

            RequestMessage message = new RequestMessage()
            {

                Device = "test",
                Level = MessageLevel.High,
                Message = "test",
                Source = "unitest",
                Title = "test"
            };


            ResponseMessage resmessage = CatsAgent.SendMessage(message);

            var t1 = resmessage.Message;
            var t2 = resmessage.Result;

            Assert.IsTrue(t1.Length > 0);
            Assert.AreEqual(t2, MessageSendingResult.Succeed);


            System.Threading.ManualResetEvent hand = new System.Threading.ManualResetEvent(false);

            CatsAgent.SendMessageAsync(message, new
                 Action<ResponseMessage>((ar) => { hand.Set(); }));

            hand.WaitOne();
        }
Esempio n. 3
0
 /// <summary>
 /// 发送报警消息(异步方式)
 /// </summary>
 /// <param name="message">请求消息</param>
 /// <param name="callback">回调方法,将在异步请求完成后执行,null表示无需回调</param>
 public static void SendMessageAsync(RequestMessage message, Action<ResponseMessage> callback)
 {
     Task.Factory.StartNew(() =>
         {
             var resp = DoSendMessage(message);
             if (callback != null)
             {
                 try
                 {
                     callback(resp);
                 }
                 catch
                 { }
             }
         });
 }
Esempio n. 4
0
 /// <summary>
 /// 发送报警消息(同步方式)
 /// </summary>
 /// <param name="message">请求消息</param>
 /// <returns></returns>
 public static ResponseMessage SendMessage(RequestMessage message)
 {
     return DoSendMessage(message);
 }