You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 rivejä
11 KiB

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