選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RootViewModel.cs 8.3 KiB

2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. Debug.WriteLine($"卡号地址:{res.Address}----------卡号数据:{res.ResData}");
  77. if (!res.ResData.IsMatch(new Regex("0{20}")))
  78. ReadFunc?.Invoke(res);
  79. }
  80. });
  81. }
  82. else
  83. HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络,并重启程序!");
  84. }
  85. Thread.Sleep(50);
  86. }), "串口监听");
  87. });
  88. // 打开串口
  89. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  90. {
  91. PortName = DataBus.COM,
  92. BaudRate = 57600,
  93. DataBits = 8
  94. });
  95. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  96. {
  97. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  98. }
  99. }), "打开串口");
  100. }
  101. protected override void OnViewLoaded()
  102. {
  103. //广告初始化
  104. Task.Factory.StartNew(new Action(() =>
  105. {
  106. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  107. {
  108. Ad = x.Remaek,
  109. Device = t.Name,
  110. Stalls = x.Name
  111. }).ToList();
  112. if (Init != null)
  113. {
  114. Ad = new ObservableCollection<AdDTO>(Init);
  115. var route = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt"));
  116. SyncStatic.WriteFile(Encoding.UTF8.GetBytes(SyncStatic.Compress(Init.ToJson(), SecurityType.Base64)), route);
  117. AdOpen();
  118. }
  119. else
  120. {
  121. var jsons = SyncStatic.Decompress(SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt")), SecurityType.Base64);
  122. Ad = new ObservableCollection<AdDTO>(jsons.ToModel<List<AdDTO>>());
  123. AdOpen();
  124. }
  125. }));
  126. }
  127. #region 属性
  128. public RootView Main { get; set; }
  129. ObservableCollection<SaleLog> _Result;
  130. public ObservableCollection<SaleLog> Result
  131. {
  132. get => _Result;
  133. set => SetAndNotify(ref _Result, value);
  134. }
  135. ObservableCollection<AdDTO> _Ad;
  136. public ObservableCollection<AdDTO> Ad
  137. {
  138. get => _Ad;
  139. set => SetAndNotify(ref _Ad, value);
  140. }
  141. #endregion
  142. #region 网络检查
  143. private void AdOpen()
  144. {
  145. App.Current.Dispatcher.Invoke(new Action(() =>
  146. {
  147. try
  148. {
  149. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  150. for (int i = 0; i < SC; i++)
  151. {
  152. var win = new AdWindow(Ad[i].Ad);
  153. var rectangle = System.Windows.Forms.Screen.AllScreens[i].WorkingArea;
  154. win.Height = rectangle.Height;
  155. win.Width = rectangle.Width;
  156. win.Top = 0;
  157. win.Left = rectangle.Left;
  158. win.Owner = this.Main;
  159. win.Show();
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. Debug.WriteLine(ex.ToString());
  165. var win = new AdWindow("暂无广告");
  166. var rectangle = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
  167. win.Height = rectangle.Height;
  168. win.Width = rectangle.Width;
  169. win.Top = 0;
  170. win.Left = rectangle.Left;
  171. win.Owner = this.Main;
  172. win.Show();
  173. }
  174. }));
  175. }
  176. #endregion
  177. #region 命令
  178. public void ReadCard(DKoutput input)
  179. {
  180. lock (locker)
  181. {
  182. var Core = Container.Get<HKCore>();
  183. if (DateTime.Now >= DataBus.Times.AMStartTime&& DateTime.Now < DataBus.Times.AMEndTime)
  184. {
  185. Core.DeviceSaleAM(new SaleLog
  186. {
  187. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  188. Location = input.Address.AsInt().ToString(),
  189. IsSync = false
  190. });
  191. }
  192. if (DateTime.Now >= DataBus.Times.PMStartTime && DateTime.Now < DataBus.Times.PMEndTime)
  193. {
  194. Core.DeviceSalePM(new SaleLog
  195. {
  196. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  197. Location = input.Address.AsInt().ToString(),
  198. IsSync = false
  199. });
  200. }
  201. if (DateTime.Now >= DataBus.Times.ATStartTime && DateTime.Now < DataBus.Times.ATEndTime)
  202. {
  203. Core.DeviceSaleAT(new SaleLog
  204. {
  205. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  206. Location = input.Address.AsInt().ToString(),
  207. IsSync = false
  208. });
  209. }
  210. }
  211. }
  212. #endregion
  213. }
  214. }