25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

237 satır
8.5 KiB

  1. using DTO;
  2. using HKCardOUT.Helper;
  3. using HKCardOUT.Logic.Model;
  4. using HKCardOUT.Logic.Service;
  5. using HKCardOUT.Views;
  6. using NStandard;
  7. using Stylet;
  8. using StyletIoC;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.IO.Ports;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading;
  18. using System.Windows.Documents;
  19. using UHFHelper;
  20. using XExten.Advance.InternalFramework.Securities.Common;
  21. using XExten.Advance.LinqFramework;
  22. using XExten.Advance.StaticFramework;
  23. using System.Diagnostics;
  24. using System.Threading.Tasks;
  25. namespace HKCardOUT.ViewModels
  26. {
  27. public class RootViewModel : Conductor<IScreen>
  28. {
  29. public delegate void ReadCardFunc(DKoutput? input);
  30. public event ReadCardFunc ReadFunc;
  31. private readonly object locker = new object();
  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. }
  39. /// <summary>
  40. /// 关闭页面
  41. /// </summary>
  42. /// <param name="sender"></param>
  43. /// <param name="e"></param>
  44. private void RootViewModel_Closed(object sender, CloseEventArgs e)
  45. {
  46. ThreadManage.GetInstance().Dispose();
  47. UHF_RS485_Helper.GetInstance().Close();
  48. }
  49. private void RootViewModel_Activated(object sender, ActivationEventArgs e)
  50. {
  51. ReadFunc -= ReadCard;
  52. ReadFunc += ReadCard;
  53. ThreadManage.GetInstance().Start(new Action(() =>
  54. {
  55. while (!SerialPort.GetPortNames().Contains(DataBus.COM))
  56. {
  57. Thread.Sleep(1000);
  58. }
  59. UHF_RS485_Helper.GetInstance().DisConnect = new Action(() =>
  60. {
  61. ThreadManage.GetInstance().StopTask("串口监听");
  62. });
  63. UHF_RS485_Helper.GetInstance().OpenOk = new Action(() =>
  64. {
  65. ThreadManage.GetInstance().StartLong(new Action(() =>
  66. {
  67. if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
  68. {
  69. if (DataBus.StoreInfo != null)
  70. {
  71. DataBus.StoreInfo.Devices.ForEach(item =>
  72. {
  73. var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
  74. if (res != null)
  75. {
  76. HKLog.HKLogImport.WriteInfo($"卡号地址:{res.Address}----------卡号数据:{res.ResData}");
  77. if (!res.ResData.IsMatch(new Regex("0{20}")))
  78. ReadFunc?.Invoke(res);
  79. if (res.ResData.Contains("888") || res.ResData.Contains("999") || res.ResData.Contains("666"))
  80. ReadFunc?.Invoke(res);
  81. }
  82. });
  83. }
  84. else
  85. HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络,并重启程序!");
  86. }
  87. Thread.Sleep(50);
  88. }), "串口监听");
  89. });
  90. // 打开串口
  91. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  92. {
  93. PortName = DataBus.COM,
  94. BaudRate = 57600,
  95. DataBits = 8
  96. });
  97. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  98. {
  99. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  100. }
  101. }), "打开串口");
  102. }
  103. protected override void OnViewLoaded()
  104. {
  105. //广告初始化
  106. Task.Factory.StartNew(new Action(() =>
  107. {
  108. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  109. {
  110. Ad = x.Remaek,
  111. Device = t.Name,
  112. Stalls = x.Name
  113. }).ToList();
  114. if (Init != null)
  115. {
  116. Ad = new ObservableCollection<AdDTO>(Init);
  117. var route = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt"));
  118. SyncStatic.WriteFile(Encoding.UTF8.GetBytes(SyncStatic.Compress(Init.ToJson(), SecurityType.Base64)), route);
  119. AdOpen();
  120. }
  121. else
  122. {
  123. var jsons = SyncStatic.Decompress(SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt")), SecurityType.Base64);
  124. Ad = new ObservableCollection<AdDTO>(jsons.ToModel<List<AdDTO>>());
  125. AdOpen();
  126. }
  127. }));
  128. }
  129. #region 属性
  130. public RootView Main { get; set; }
  131. ObservableCollection<SaleLog> _Result;
  132. public ObservableCollection<SaleLog> Result
  133. {
  134. get => _Result;
  135. set => SetAndNotify(ref _Result, value);
  136. }
  137. ObservableCollection<AdDTO> _Ad;
  138. public ObservableCollection<AdDTO> Ad
  139. {
  140. get => _Ad;
  141. set => SetAndNotify(ref _Ad, value);
  142. }
  143. #endregion
  144. #region 网络检查
  145. private void AdOpen()
  146. {
  147. App.Current.Dispatcher.Invoke(new Action(() =>
  148. {
  149. try
  150. {
  151. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  152. for (int i = 0; i < SC; i++)
  153. {
  154. var win = new AdWindow(Ad[i].Ad);
  155. var rectangle = System.Windows.Forms.Screen.AllScreens[i].WorkingArea;
  156. win.Height = rectangle.Height;
  157. win.Width = rectangle.Width;
  158. win.Top = 0;
  159. win.Left = rectangle.Left;
  160. win.Owner = this.Main;
  161. win.Show();
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. Debug.WriteLine(ex.ToString());
  167. var win = new AdWindow("暂无广告");
  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. }
  178. #endregion
  179. #region 命令
  180. public void ReadCard(DKoutput input)
  181. {
  182. lock (locker)
  183. {
  184. var Core = Container.Get<HKCore>();
  185. if (DateTime.Now >= DataBus.Times.AMStartTime&& DateTime.Now < DataBus.Times.AMEndTime)
  186. {
  187. Core.DeviceSaleAM(new SaleLog
  188. {
  189. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  190. Location = input.Address.AsInt().ToString(),
  191. IsSync = false
  192. });
  193. }
  194. if (DateTime.Now >= DataBus.Times.PMStartTime && DateTime.Now < DataBus.Times.PMEndTime)
  195. {
  196. Core.DeviceSalePM(new SaleLog
  197. {
  198. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  199. Location = input.Address.AsInt().ToString(),
  200. IsSync = false
  201. });
  202. }
  203. if (DateTime.Now >= DataBus.Times.ATStartTime && DateTime.Now < DataBus.Times.ATEndTime)
  204. {
  205. Core.DeviceSaleAT(new SaleLog
  206. {
  207. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  208. Location = input.Address.AsInt().ToString(),
  209. IsSync = false
  210. });
  211. }
  212. }
  213. }
  214. #endregion
  215. }
  216. }