Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

263 řádky
9.4 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. MainThread();
  37. this.Activated += RootViewModel_Activated;
  38. this.Closed += RootViewModel_Closed;
  39. }
  40. /// <summary>
  41. /// 关闭页面
  42. /// </summary>
  43. /// <param name="sender"></param>
  44. /// <param name="e"></param>
  45. private void RootViewModel_Closed(object sender, CloseEventArgs e)
  46. {
  47. ThreadManage.GetInstance().Dispose();
  48. UHF_RS485_Helper.GetInstance().Close();
  49. }
  50. private void RootViewModel_Activated(object sender, ActivationEventArgs e)
  51. {
  52. ReadFunc -= ReadCard;
  53. ReadFunc += ReadCard;
  54. ThreadManage.GetInstance().Start(new Action(() =>
  55. {
  56. while (!SerialPort.GetPortNames().Contains(DataBus.COM))
  57. {
  58. Thread.Sleep(1000);
  59. }
  60. UHF_RS485_Helper.GetInstance().DisConnect = new Action(() =>
  61. {
  62. ThreadManage.GetInstance().StopTask("串口监听");
  63. });
  64. UHF_RS485_Helper.GetInstance().OpenOk = new Action(() =>
  65. {
  66. ThreadManage.GetInstance().StartLong(new Action(() =>
  67. {
  68. if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
  69. {
  70. if (DataBus.StoreInfo != null)
  71. {
  72. DataBus.StoreInfo.Devices.ForEach(item =>
  73. {
  74. var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
  75. if (res != null)
  76. {
  77. Debug.WriteLine($"卡号地址:{res.Address}----------卡号数据:{res.ResData}");
  78. if (!res.ResData.IsMatch(new Regex("0{20}")))
  79. ReadFunc?.Invoke(res);
  80. }
  81. });
  82. }
  83. else
  84. HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络,并重启程序!");
  85. }
  86. Thread.Sleep(10);
  87. }), "串口监听");
  88. });
  89. // 打开串口
  90. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  91. {
  92. PortName = DataBus.COM,
  93. BaudRate = 57600,
  94. DataBits = 8
  95. });
  96. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  97. {
  98. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  99. }
  100. }), "打开串口");
  101. }
  102. protected override void OnViewLoaded()
  103. {
  104. //广告初始化
  105. Task.Factory.StartNew(new Action(() =>
  106. {
  107. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  108. {
  109. Ad = x.Remaek,
  110. Device = t.Name,
  111. Stalls = x.Name
  112. }).ToList();
  113. if (Init != null)
  114. {
  115. Ad = new ObservableCollection<AdDTO>(Init);
  116. var route = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt"));
  117. SyncStatic.WriteFile(Encoding.UTF8.GetBytes(SyncStatic.Compress(Init.ToJson(), SecurityType.Base64)), route);
  118. AdOpen();
  119. }
  120. else
  121. {
  122. var jsons = SyncStatic.Decompress(SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt")), SecurityType.Base64);
  123. Ad = new ObservableCollection<AdDTO>(jsons.ToModel<List<AdDTO>>());
  124. AdOpen();
  125. }
  126. }));
  127. }
  128. #region 属性
  129. public RootView Main { get; set; }
  130. ObservableCollection<SaleLog> _Result;
  131. public ObservableCollection<SaleLog> Result
  132. {
  133. get => _Result;
  134. set => SetAndNotify(ref _Result, value);
  135. }
  136. ObservableCollection<AdDTO> _Ad;
  137. public ObservableCollection<AdDTO> Ad
  138. {
  139. get => _Ad;
  140. set => SetAndNotify(ref _Ad, value);
  141. }
  142. #endregion
  143. #region 网络检查
  144. private void MainThread()
  145. {
  146. ThreadManage.GetInstance().StartLong(new Action(() =>
  147. {
  148. try
  149. {
  150. //1.检测网络上下线
  151. bool network = HKHelpers.GetInstance().GetNetworkState();
  152. if (network != DataBus.NetWordState)
  153. {
  154. if (network) HandyControl.Controls.Growl.InfoGlobal("网络连接成功");
  155. else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
  156. DataBus.NetWordState = network;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. HandyControl.Controls.Growl.InfoGlobal(ex.Message);
  162. }
  163. Thread.Sleep(3000);
  164. }), "循环状态监测线程", false);
  165. }
  166. private void AdOpen()
  167. {
  168. App.Current.Dispatcher.Invoke(new Action(() =>
  169. {
  170. try
  171. {
  172. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  173. for (int i = 0; i < SC; i++)
  174. {
  175. var win = new AdWindow(Ad[i].Ad);
  176. var rectangle = System.Windows.Forms.Screen.AllScreens[i].WorkingArea;
  177. win.Height = rectangle.Height;
  178. win.Width = rectangle.Width;
  179. win.Top = 0;
  180. win.Left = rectangle.Left;
  181. win.Owner = this.Main;
  182. win.Show();
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. Debug.WriteLine(ex.ToString());
  188. var win = new AdWindow("暂无广告");
  189. var rectangle = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
  190. win.Height = rectangle.Height;
  191. win.Width = rectangle.Width;
  192. win.Top = 0;
  193. win.Left = rectangle.Left;
  194. win.Owner = this.Main;
  195. win.Show();
  196. }
  197. }));
  198. }
  199. #endregion
  200. #region 命令
  201. public void ReadCard(DKoutput input)
  202. {
  203. lock (locker)
  204. {
  205. var Core = Container.Get<HKCore>();
  206. var AM = DataBus.AM.Split(",").Select(t => t.AsInt()).ToList();
  207. var PM = DataBus.PM.Split(",").Select(t => t.AsInt()).ToList();
  208. var AT = DataBus.AT.Split(",").Select(t => t.AsInt()).ToList();
  209. if (DateTime.Now.Hour >= AM[0] && DateTime.Now.Hour < AM[1])
  210. {
  211. Core.DeviceSaleAM(new SaleLog
  212. {
  213. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  214. Location = input.Address.AsInt().ToString(),
  215. IsSync = false
  216. });
  217. }
  218. if (DateTime.Now.Hour >= PM[0] && DateTime.Now.Hour < PM[1])
  219. {
  220. Core.DeviceSalePM(new SaleLog
  221. {
  222. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  223. Location = input.Address.AsInt().ToString(),
  224. IsSync = false
  225. });
  226. }
  227. if (DateTime.Now.Hour >= AT[0] && DateTime.Now.Hour < AT[1])
  228. {
  229. Core.DeviceSaleAT(new SaleLog
  230. {
  231. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  232. Location = input.Address.AsInt().ToString(),
  233. IsSync = false
  234. });
  235. }
  236. }
  237. }
  238. #endregion
  239. }
  240. }