No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

277 líneas
10 KiB

  1. using BPA.Helper;
  2. using DTO;
  3. using HKCardOUT.Helper;
  4. using HKCardOUT.Logic.Model;
  5. using HKCardOUT.Logic.Service;
  6. using HKCardOUT.Views;
  7. using HKLog;
  8. using ImTools;
  9. using Stylet;
  10. using StyletIoC;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Diagnostics;
  16. using System.IO;
  17. using System.IO.Ports;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using UHFHelper;
  23. using XExten.Advance.InternalFramework.Securities.Common;
  24. using XExten.Advance.LinqFramework;
  25. using XExten.Advance.StaticFramework;
  26. namespace HKCardOUT.ViewModels
  27. {
  28. public class RootViewModel : Conductor<IScreen>
  29. {
  30. public delegate void ReadCardFunc(DKoutput? input);
  31. public event ReadCardFunc ReadFunc;
  32. private IContainer Container;
  33. public RootViewModel(IContainer Container)
  34. {
  35. this.Container = Container;
  36. this.Activated += RootViewModel_Activated;
  37. this.Closed += RootViewModel_Closed;
  38. var serv = Container.Get<HKCore>();
  39. serv.GetAllCardStatus();
  40. ProcessCard();
  41. InsertData();
  42. }
  43. /// <summary>
  44. /// 关闭页面
  45. /// </summary>
  46. /// <param name="sender"></param>
  47. /// <param name="e"></param>
  48. private void RootViewModel_Closed(object sender, CloseEventArgs e)
  49. {
  50. ThreadManage.GetInstance().Dispose();
  51. UHF_RS485_Helper.GetInstance().Close();
  52. }
  53. private void RootViewModel_Activated(object sender, ActivationEventArgs e)
  54. {
  55. ReadFunc -= ReadCard;
  56. ReadFunc += ReadCard;
  57. ThreadManage.GetInstance().Start(new Action(() =>
  58. {
  59. while (!SerialPort.GetPortNames().Contains(DataBus.COM))
  60. {
  61. Thread.Sleep(1000);
  62. }
  63. UHF_RS485_Helper.GetInstance().DisConnect = new Action(() =>
  64. {
  65. ThreadManage.GetInstance().StopTask("串口监听");
  66. });
  67. UHF_RS485_Helper.GetInstance().OpenOk = new Action(() =>
  68. {
  69. Object async = new object();
  70. DataBus.StoreInfo.Devices.ForEach(item =>
  71. {
  72. ThreadManage.GetInstance().Start(new Action(() =>
  73. {
  74. try
  75. {
  76. while (true)
  77. {
  78. if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
  79. {
  80. if (DataBus.StoreInfo != null)
  81. {
  82. DKoutput res = null;
  83. res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
  84. if (res != null)
  85. {
  86. HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 卡号地址:{res.Address}----------卡号数据:{res.ResData}");
  87. if (!res.ResData.IsNullOrEmpty())
  88. {
  89. if (res.ResData.Length == 20)
  90. ReadFunc?.Invoke(res);
  91. }
  92. Thread.Sleep(2000);
  93. }
  94. }
  95. }
  96. Thread.Sleep(50);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 异常信息:{ex.Message}");
  102. Monitor.Exit(async);
  103. }
  104. }), $"监听{item.Address}", true);
  105. });
  106. });
  107. // 打开串口
  108. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  109. {
  110. PortName = DataBus.COM,
  111. BaudRate = 57600,
  112. DataBits = 8
  113. });
  114. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  115. {
  116. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  117. }
  118. }), "打开串口");
  119. }
  120. protected override void OnViewLoaded()
  121. {
  122. //广告初始化
  123. Task.Factory.StartNew(new Action(() =>
  124. {
  125. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  126. {
  127. Ad = x.Remaek,
  128. Device = t.Name,
  129. Stalls = x.Name
  130. }).ToList();
  131. if (Init != null)
  132. {
  133. Ad = new ObservableCollection<AdDTO>(Init);
  134. var route = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt"));
  135. SyncStatic.WriteFile(Encoding.UTF8.GetBytes(SyncStatic.Compress(Init.ToJson(), SecurityType.Base64)), route);
  136. AdOpen();
  137. }
  138. else
  139. {
  140. var jsons = SyncStatic.Decompress(SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt")), SecurityType.Base64);
  141. Ad = new ObservableCollection<AdDTO>(jsons.ToModel<List<AdDTO>>());
  142. AdOpen();
  143. }
  144. }));
  145. }
  146. #region 属性
  147. public RootView Main { get; set; }
  148. ObservableCollection<SaleLog> _Result;
  149. public ObservableCollection<SaleLog> Result
  150. {
  151. get => _Result;
  152. set => SetAndNotify(ref _Result, value);
  153. }
  154. ObservableCollection<AdDTO> _Ad;
  155. public ObservableCollection<AdDTO> Ad
  156. {
  157. get => _Ad;
  158. set => SetAndNotify(ref _Ad, value);
  159. }
  160. #endregion
  161. #region 屏幕检查
  162. private void AdOpen()
  163. {
  164. App.Current.Dispatcher.Invoke(new Action(() =>
  165. {
  166. var win = new AdWindow(Ad[0].Ad);
  167. win.DataContext = new AdWindowViewModel();
  168. var rectangle = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
  169. win.Height = rectangle.Height;
  170. win.Width = rectangle.Width;
  171. win.Top = 0;
  172. win.Left = rectangle.Left;
  173. win.Owner = this.Main;
  174. win.Show();
  175. }));
  176. }
  177. #endregion
  178. #region 命令
  179. public void ReadCard(DKoutput input)
  180. {
  181. var mo = new SaleLogDto
  182. {
  183. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  184. Location = input.Address.AsInt().ToString(),
  185. Date = DateTime.Now
  186. };
  187. HKLogImport.WriteInfo("读卡成功:" + mo.ToJson());
  188. if (Stacks.Count > 0)
  189. {
  190. var check = Stacks.Where(t => t.CardNo == mo.CardNo).Where(t => t.Location == mo.Location).FirstOrDefault();
  191. if (check == null)
  192. Stacks.Enqueue(mo);
  193. }
  194. else
  195. Stacks.Enqueue(mo);
  196. }
  197. private void ProcessCard()
  198. {
  199. ThreadManage.GetInstance().StartLong(() =>
  200. {
  201. if (0 < Stacks.Count && Stacks.TryDequeue(out SaleLogDto Info))
  202. {
  203. if (Info != null)
  204. {
  205. if (!HKControl.Main.GetInstance.GetIsSwipe(Info.Location.AsInt()))
  206. {
  207. Stacks.Enqueue(Info);
  208. HKLogImport.WriteInfo($"{Info.Location} 窗口不允许出餐");
  209. }
  210. else
  211. {
  212. var Core = Container.Get<HKCore>();
  213. HKLogImport.WriteInfo("队列读取成功:" + Info.ToJson());
  214. if (Info.Date >= DataBus.Times.AMStartTime && Info.Date < DataBus.Times.AMEndTime)
  215. {
  216. Core.DeviceSaleAM(new SaleLog
  217. {
  218. CardNo = Info.CardNo,
  219. Location = Info.Location,
  220. IsSync = false
  221. });
  222. }
  223. if (Info.Date >= DataBus.Times.PMStartTime && Info.Date < DataBus.Times.PMEndTime)
  224. {
  225. Core.DeviceSalePM(new SaleLog
  226. {
  227. CardNo = Info.CardNo,
  228. Location = Info.Location,
  229. IsSync = false
  230. });
  231. }
  232. if (Info.Date >= DataBus.Times.ATStartTime && Info.Date < DataBus.Times.ATEndTime)
  233. {
  234. Core.DeviceSaleAT(new SaleLog
  235. {
  236. CardNo = Info.CardNo,
  237. Location = Info.Location,
  238. IsSync = false
  239. });
  240. }
  241. }
  242. }
  243. }
  244. Thread.Sleep(50);
  245. }, "processCard");
  246. }
  247. private void InsertData()
  248. {
  249. ThreadManage.GetInstance().StartLong(() =>
  250. {
  251. Container.Get<HKCore>().WriteData();
  252. Thread.Sleep(1000);
  253. }, "写入数据", true);
  254. }
  255. private static ConcurrentQueue<SaleLogDto> Stacks = new ConcurrentQueue<SaleLogDto>();
  256. #endregion
  257. }
  258. }