/// <summary> /// 开启一个更新Server的任务 /// 刷新两次 /// </summary> /// <param name="deviceId">The dn.</param> /// <param name="sn">The alarm sn.</param> private void CreateUpdateServerTask(string deviceId, int sn) { var logPre = $"Server [Sn:{sn}] [{deviceId}]"; try { logger.NotifyProcess.Info($"{logPre} New Update Server Task."); #region DeviceId已存在 var exsit = this.UpdateServerTasks.FirstOrDefault(x => x.DeviceId == deviceId); if (exsit != null) { //如果首次刷新已经执行过,此处再执行一次 if (exsit.FirstRefreshTime < DateTime.Now) { exsit.FirstRefreshTime = DateTime.Now.AddSeconds(10); StartFirstUpdateServer(exsit, true); } logger.NotifyProcess.Debug($"{logPre} [preSn:{ exsit.Sn}] Delay LastRefreshTime:{DateTime.Now.AddSeconds(60):HH:mm:ss}."); exsit.Sn = sn; exsit.LastRefreshTime = DateTime.Now.AddSeconds(60);//延长最后一次刷新时间 return; } #endregion var task = new UpdateTask <Server>(deviceId, sn); logger.NotifyProcess.Debug($"{logPre} Will StartUpdateTask [First:{task.FirstRefreshTime:HH:mm:ss}].[Last Nearby :{task.LastRefreshTime:HH:mm:ss}]"); StartFirstUpdateServer(task, false); StartLastUpdateServer(task); this.UpdateServerTasks.Add(task); } catch (Exception e) { logger.NotifyProcess.Error(e, $"{logPre} StartUpdateTask Error."); } }
/// <summary> /// Starts the first update server. /// </summary> /// <param name="task">The task.</param> /// <param name="isResart">The is resart.</param> private void StartFirstUpdateServer(UpdateTask <Server> task, bool isResart) { var logPre = $"Server [Sn:{task.Sn}] [{task.DeviceId}] [isResart:{isResart}] FirstRefreshTask"; Task.Run(async() => { try { Thread.Sleep(TimeSpan.FromSeconds(10)); logger.NotifyProcess.Debug($"{logPre} Start [{task.FirstRefreshTime:HH:mm:ss}]"); var server = await QueryServerDetailsById(task.DeviceId); task.DeviceFirst = server; logger.NotifyProcess.Debug($"{logPre} Query Server Finish:[{JsonConvert.SerializeObject(server)}]."); await ServerConnector.Instance.Sync(server); logger.NotifyProcess.Debug($"{logPre} Sync Server Success."); } catch (Exception e) { logger.NotifyProcess.Error(e, $"{logPre} Error."); } }); }
private void StartFirstUpdateEnclosure(UpdateTask <Enclosure> task, bool isResart) { var logPre = $"Enclosure [Sn:{task.Sn}] [{task.DeviceId}] [isResart:{isResart}] FirstRefreshTask"; Task.Run(async() => { try { Thread.Sleep(TimeSpan.FromSeconds(10)); logger.NotifyProcess.Debug($"{logPre} Start [{task.FirstRefreshTime:HH:mm:ss}]"); var enclosure = await QueryEnclosureDetailsById(task.DeviceId); task.DeviceFirst = enclosure; logger.NotifyProcess.Debug($"{logPre} Query Enclosure Finish:[{JsonConvert.SerializeObject(enclosure)}]."); await EnclosureConnector.Instance.Sync(enclosure); logger.NotifyProcess.Debug($"{logPre} Sync Enclosure Success."); } catch (Exception e) { logger.NotifyProcess.Error(e, $"{logPre} Error."); } }); }
/// <summary> /// Starts the last update enclosure. /// </summary> /// <param name="task">The task.</param> private void StartLastUpdateEnclosure(UpdateTask <Enclosure> task) { var logPre = $"Enclosure [Sn:{task.Sn}] [{task.DeviceId}] LastRefreshTask:"; Task.Run(async() => { try { while (true) { Thread.Sleep(TimeSpan.FromSeconds(60)); if (DateTime.Now > task.LastRefreshTime) { logger.NotifyProcess.Debug($"{logPre} Start [{task.LastRefreshTime:HH:mm:ss}]"); var enclosure = await QueryEnclosureDetailsById(task.DeviceId); var isChange = task.CheckIsChange(enclosure); logger.NotifyProcess.Debug($"{logPre} Query Enclosure Finish:[IsChange:{isChange}][{JsonConvert.SerializeObject(enclosure)}]."); if (isChange) { logger.NotifyProcess.Debug($"{logPre} Query Enclosure Finish:[{JsonConvert.SerializeObject(enclosure)}]."); await EnclosureConnector.Instance.Sync(enclosure); logger.NotifyProcess.Debug($"{logPre} Sync Enclosure Success."); } this.UpdateEnclosureTasks.Remove(task); break; } Thread.Sleep(TimeSpan.FromSeconds(15)); } } catch (Exception e) { logger.NotifyProcess.Error(e, $"{logPre} Error."); } }); }