Exemple #1
0
        /// <summary>
        /// 创建人:张辽阔
        /// 创建时间:2016-12-08
        /// 创建记录:接收触发源的请求和给云通讯发送数据到达通知
        /// </summary>
        /// <param name="parameter">接收触发源的请求和给云通讯发送数据到达通知的参数</param>
        public void ReceiveTriggerAndRequestCloudCommunication(ReceiveTriggerAndRequestCloudCommunicationParameter parameter)
        {
            Task.Run(() =>
            {
                try
                {
                    if (ValidateData <ReceiveTriggerAndRequestCloudCommunicationParameter>(parameter) && parameter.PlatformId > 0)
                    {
                        lock (PlatformCacheData.MuiltPlatformCacheData)
                        {
                            PlatformDataStatus platformDataStatus;
                            //如果已存在该平台的信息
                            if (PlatformCacheData.MuiltPlatformCacheData.TryGetValue(parameter.PlatformId, out platformDataStatus))
                            {
                                platformDataStatus.DataCount += 1;
                            }
                            //如果未存在该平台的信息
                            else
                            {
                                platformDataStatus = new PlatformDataStatus();
                                PlatformCacheData.MuiltPlatformCacheData.Add(parameter.PlatformId, platformDataStatus);

                                //给云通讯发送通知
                                RestClient client = new RestClient(requestURL);
                                ReceiveCloudProxyNotifyParameter receiveCloudProxyNotifyPara = new ReceiveCloudProxyNotifyParameter();
                                receiveCloudProxyNotifyPara.PlatformId = parameter.PlatformId;
                                string result = client.Post(receiveCloudProxyNotifyPara.ToClientString(), requestMethod);
                                //定义委托启动定时器
                                Action startTimer = () =>
                                {
                                    platformDataStatus.PlatformTimer.Elapsed += (s, e) => PlatformTimer_Elapsed(s, e, parameter.PlatformId);
                                    platformDataStatus.PlatformTimer.Interval = 60000;
                                    platformDataStatus.PlatformTimer.Start();
                                };

                                //如果该路径请求失败,则启动定时器
                                if (result.Equals(requestURL + "/" + requestMethod))
                                {
                                    startTimer();
                                }
                                else
                                {
                                    BaseResponse <ReceiveCloudProxyNotifyResult> resultObj = Json.JsonDeserialize <BaseResponse <ReceiveCloudProxyNotifyResult> >(result);
                                    //如果该路径请求失败,则启动定时器
                                    if (!(resultObj != null && resultObj.IsSuccessful && resultObj.Result.PlatformId == parameter.PlatformId))
                                    {
                                        startTimer();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    LogHelper.WriteLog(e);
                }
            });
        }
Exemple #2
0
        /// <summary>
        /// 创建人:张辽阔
        /// 创建时间:2016-12-12
        /// 创建记录:云代理定时器方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlatformTimer_Elapsed(object sender, ElapsedEventArgs e, int platformId)
        {
            Action stopTimer = () =>
            {
                lock (PlatformCacheData.MuiltPlatformCacheData)
                {
                    PlatformDataStatus tempDataStatus;
                    if (PlatformCacheData.MuiltPlatformCacheData.TryGetValue(platformId, out tempDataStatus))
                    {
                        tempDataStatus.PlatformTimer.Stop();
                        tempDataStatus.PlatformTimer.Close();
                        tempDataStatus.PlatformTimer.Dispose();
                        PlatformCacheData.MuiltPlatformCacheData.Remove(platformId);
                    }
                }
            };

            try
            {
                //给云通讯发送通知
                RestClient client = new RestClient(requestURL);
                ReceiveTriggerAndRequestCloudCommunicationParameter parameter = new ReceiveTriggerAndRequestCloudCommunicationParameter
                {
                    PlatformId = platformId
                };
                string result = client.Post(parameter.ToClientString(), requestMethod, 30000);
                //如果该路径请求成功,则停止定时器
                if (!result.Equals(requestURL + "/" + requestMethod))
                {
                    BaseResponse <ReceiveCloudProxyNotifyResult> resultObj = Json.JsonDeserialize <BaseResponse <ReceiveCloudProxyNotifyResult> >(result);
                    if (resultObj != null && resultObj.IsSuccessful && resultObj.Result.PlatformId == platformId)
                    {
                        stopTimer();
                    }
                }
            }
            catch (Exception ex)
            {
                stopTimer();
                LogHelper.WriteLog(ex);
            }
        }