// 启动每日定时亮灯过程 public static NormalResult StartPerdayTask() { // 初始化任务调度器 if (_initialized == false) { JobManager.Initialize(); _initialized = true; } TurnBackLampOff(); // 每日亮灯时段 string time_range = WpfClientInfo.Config.Get("tasks", "lamp", null); if (string.IsNullOrEmpty(time_range)) { return(new NormalResult()); } try { var times = ParseTimeRange(time_range); var start = times[0]; var end = times[1]; // 专门检查是否在亮灯时间范围内 DateTime now = DateTime.Now; DateTime current_start = GetTodayTime(start); DateTime current_end = GetTodayTime(end); if (now >= current_start && now <= current_end) { TurnBackLampOn(); } // 安排任务 // 开灯 JobManager.AddJob( () => { TurnBackLampOn(); }, s => s.ToRunEvery(1).Days().At(start.Hour, start.Minute) ); // 关灯 JobManager.AddJob( () => { TurnBackLampOff(); }, s => s.ToRunEvery(1).Days().At(end.Hour, end.Minute) ); /* * // Schedule a simple task to run at a specific time * Schedule(() => { }).ToRunEvery(1).Days().At(21, 15); */ return(new NormalResult()); } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"解析每日亮灯时间范围字符串时发现错误: {ExceptionUtil.GetDebugText(ex)}"); return(new NormalResult { Value = -1, ErrorInfo = $"解析每日亮灯时间范围字符串时发现错误: { ex.Message}" }); } }
public void LoadPhoto(string photo_path) { if (string.IsNullOrEmpty(photo_path)) { App.Invoke(new Action(() => { this.SetPhoto((Stream)null); })); return; } // TODO: 照片可以缓存到本地。每次只需要获取 timestamp 即可。如果 timestamp 和缓存的不一致再重新获取一次 Stream stream = null; try { // 先尝试从缓存获取 /* * string cacheDir = Path.Combine(WpfClientInfo.UserDir, "photo"); * PathUtil.CreateDirIfNeed(cacheDir); * string fileName = Path.Combine(cacheDir, GetPath(photo_path)); */ string fileName = GetCachePhotoPath(photo_path); if (File.Exists(fileName)) { try { stream = new MemoryStream(File.ReadAllBytes(fileName)); // stream = File.OpenRead(fileName); } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"从读者照片本地缓存文件 {fileName} 读取内容时出现异常:{ExceptionUtil.GetDebugText(ex)}"); stream = null; } } if (stream == null) { // 断网状态 if (ShelfData.LibraryNetworkCondition != "OK") { fileName = Path.Combine(WpfClientInfo.DataDir, "photo_not_available.jpg"); try { stream = new MemoryStream(File.ReadAllBytes(fileName)); } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"从 photo_not_available.jpg 文件 {fileName} 读取内容时出现异常:{ExceptionUtil.GetDebugText(ex)}"); stream = null; } } else { // 联网状态 stream = new MemoryStream(); var channel = App.CurrentApp.GetChannel(); TimeSpan old_timeout = channel.Timeout; channel.Timeout = TimeSpan.FromSeconds(30); try { long lRet = channel.GetRes( null, photo_path, stream, "data,content", // strGetStyle, null, // byte [] input_timestamp, out string strMetaData, out byte[] baOutputTimeStamp, out string strOutputPath, out string strError); if (lRet == -1) { // SetGlobalError("patron", $"获取读者照片时出错: {strError}"); // return; throw new Exception($"获取读者照片(path='{photo_path}')时出错: {strError}"); } // 顺便存储到本地 { bool suceed = false; stream.Seek(0, SeekOrigin.Begin); try { using (var file = File.OpenWrite(fileName)) { StreamUtil.DumpStream(stream, file); } suceed = true; } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"读者照片写入本地缓存文件 {fileName} 时出现异常:{ExceptionUtil.GetDebugText(ex)}"); } finally { if (suceed == false) { File.Delete(fileName); } } } stream.Seek(0, SeekOrigin.Begin); } finally { channel.Timeout = old_timeout; App.CurrentApp.ReturnChannel(channel); } } } App.Invoke(new Action(() => { this.SetPhoto(stream); })); stream = null; } finally { if (stream != null) { stream.Dispose(); } } }
// 启动一般监控任务 public static void StartMonitorTask() { if (_monitorTask != null) { return; } CancellationToken token = _cancel.Token; // bool download_complete = false; token.Register(() => { _eventMonitor.Set(); }); _monitorTask = Task.Factory.StartNew(async() => { WpfClientInfo.WriteInfoLog("书柜监控专用线程开始"); try { while (token.IsCancellationRequested == false) { // await Task.Delay(TimeSpan.FromSeconds(10)); _eventMonitor.WaitOne(_monitorIdleLength); token.ThrowIfCancellationRequested(); // *** // 关闭天线射频 if (_tagAdded) { _ = Task.Run(async() => { try { await SelectAntennaAsync(); } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"关闭天线射频 SelectAntennaAsync() 时出现异常: {ExceptionUtil.GetDebugText(ex)}"); } }); _tagAdded = false; } if (DateTime.Now - _lastDetectTime > _detectPeriod) { DetectLibraryNetwork(); _lastDetectTime = DateTime.Now; } // 提醒关门 WarningCloseDoor(); // 下载或同步读者信息 string startDate = LoadStartDate(); if (/*download_complete == false || */ string.IsNullOrEmpty(startDate)) { // 如果 Config 中没有记载断点位置,说明以前从来没有首次同步过。需要进行一次首次同步 if (string.IsNullOrEmpty(startDate)) { // SaveStartDate(""); var repl_result = await PatronReplication.DownloadAllPatronRecordAsync(token); if (repl_result.Value == -1) { // TODO: 判断通讯出错的错误码。如果是通讯出错,则稍后需要重试下载 } else { SaveStartDate(repl_result.StartDate); } // 立刻允许接着做一次零星同步 ActivateMonitor(); } // download_complete = true; } else { // 进行零星同步 if (DateTime.Now - _lastReplicateTime > _replicatePeriod) { // string startDate = LoadStartDate(); // testing // startDate = "20200507:0-"; if (string.IsNullOrEmpty(startDate) == false) { string endDate = DateTimeUtil.DateTimeToString8(DateTime.Now); // parameters: // strLastDate 处理中断或者结束时返回最后处理过的日期 // last_index 处理或中断返回时最后处理过的位置。以后继续处理的时候可以从这个偏移开始 // return: // -1 出错 // 0 中断 // 1 完成 ReplicationResult repl_result = await PatronReplication.DoReplication( startDate, endDate, LogType.OperLog, token); if (repl_result.Value == -1) { WpfClientInfo.WriteErrorLog($"同步出错: {repl_result.ErrorInfo}"); } else if (repl_result.Value == 1) { string lastDate = repl_result.LastDate + ":" + repl_result.LastIndex + "-"; // 注意 - 符号不能少。少了意思就会变成每次只获取一条日志记录了 SaveStartDate(lastDate); } _lastReplicateTime = DateTime.Now; } } } } _monitorTask = null; } catch (OperationCanceledException) { } catch (Exception ex) { WpfClientInfo.WriteErrorLog($"书柜监控专用线程出现异常: {ExceptionUtil.GetDebugText(ex)}"); App.SetError("shelf_monitor", $"书柜监控专用线程出现异常: {ex.Message}"); } finally { WpfClientInfo.WriteInfoLog("书柜监控专用线程结束"); } }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); }