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.

260 regels
8.2 KiB

  1. using DTO;
  2. using HKCardOUT.Helper;
  3. using HKCardOUT.Logic.Model;
  4. using HKCardOUT.Logic.Service;
  5. using HKCardOUT.Views;
  6. using ImTools;
  7. using NStandard;
  8. using S7.Net.Types;
  9. using SqlSugar.DistributedSystem.Snowflake;
  10. using Stylet;
  11. using StyletIoC;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.Linq;
  18. using System.Text.RegularExpressions;
  19. using System.Threading;
  20. using System.Windows;
  21. using System.Windows.Documents;
  22. using System.Windows.Forms;
  23. using UHFHelper;
  24. using XExten.Advance.CacheFramework;
  25. using XExten.Advance.CacheFramework.RunTimeCache;
  26. using XExten.Advance.LinqFramework;
  27. namespace HKCardOUT.ViewModels
  28. {
  29. public class RootViewModel : Conductor<IScreen>
  30. {
  31. public delegate void ReadCardFunc(DKoutput? input);
  32. public event ReadCardFunc ReadFunc;
  33. private readonly object locker = new object();
  34. private IContainer Container;
  35. private Thread thread1;
  36. readonly System.Timers.Timer timer1 = new();
  37. public RootViewModel(IContainer Container)
  38. {
  39. this.Container = Container;
  40. this.Activated += RootViewModel_Activated;
  41. this.Closed += RootViewModel_Closed;
  42. MainThread();
  43. }
  44. /// <summary>
  45. /// 关闭页面
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. private void RootViewModel_Closed(object sender, CloseEventArgs e)
  50. {
  51. if (timer1!=null)
  52. {
  53. timer1.Stop();
  54. timer1.Dispose();
  55. }
  56. UHF_RS485_Helper.GetInstance().Close();
  57. }
  58. private void RootViewModel_Activated(object sender, ActivationEventArgs e)
  59. {
  60. ReadFunc -= ReadCard;
  61. ReadFunc += ReadCard;
  62. // 打开串口
  63. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  64. {
  65. PortName = DataBus.COM,
  66. BaudRate = 57600,
  67. DataBits = 8
  68. });
  69. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  70. {
  71. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  72. }
  73. thread1 = new(new ThreadStart(GetData))
  74. {
  75. IsBackground = true
  76. };
  77. thread1.Start();
  78. }
  79. /// <summary>
  80. /// 读取信息
  81. /// </summary>
  82. public void GetData()
  83. {
  84. //开启循环监听数据
  85. timer1.Interval = 500;
  86. timer1.Elapsed += (o, a) =>
  87. {
  88. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  89. {
  90. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  91. timer1.Stop();
  92. return;
  93. }
  94. else
  95. {
  96. DataBus.StoreInfo.Devices.ForEach(item =>
  97. {
  98. //var stopwatch = Stopwatch.StartNew();
  99. //stopwatch.Restart();
  100. var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
  101. if (res != null)
  102. {
  103. if (!res.ResData.IsMatch(new Regex("0{20}")))
  104. ReadFunc?.Invoke(res);
  105. }
  106. //Debug.WriteLine("打印时间:"+ item.Address.AsInt() + "_"+stopwatch.ElapsedMilliseconds);
  107. });
  108. }
  109. };
  110. timer1.Start();
  111. }
  112. protected override void OnViewLoaded()
  113. {
  114. //广告初始化
  115. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  116. {
  117. Ad = x.Remaek,
  118. IsActive = false,
  119. Device = t.Name,
  120. Stalls = x.Name
  121. }).ToList();
  122. if (Init != null)
  123. {
  124. Ad = new ObservableCollection<AdDTO>(Init);
  125. AdOpen();
  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. Thread.Sleep(3000);
  159. }
  160. catch (Exception ex)
  161. {
  162. HandyControl.Controls.Growl.InfoGlobal(ex.Message);
  163. }
  164. }), "循环状态监测线程", false);
  165. }
  166. private void AdOpen()
  167. {
  168. try
  169. {
  170. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  171. for (int i = 0; i < SC; i++)
  172. {
  173. var win = new AdWindow(Ad[i].Ad);
  174. var rectangle = System.Windows.Forms.Screen.AllScreens[i].WorkingArea;
  175. win.Height = rectangle.Height;
  176. win.Width = rectangle.Width;
  177. win.Top = 0;
  178. win.Left = rectangle.Left;
  179. win.Owner = this.Main;
  180. win.Show();
  181. }
  182. }
  183. catch
  184. {
  185. var win = new AdWindow("暂无广告");
  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. #endregion
  196. #region 命令
  197. /// <summary>
  198. /// 广告位置
  199. /// </summary>
  200. /// <param name="input"></param>
  201. public void UpdateAction(AdDTO input)
  202. {
  203. input.IsActive = !input.IsActive;
  204. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  205. if (SC >= 1)
  206. {
  207. if (input.IsActive)
  208. {
  209. if (!DataBus.AdStatus.ContainsKey(input.Device))
  210. {
  211. var win = new Views.AdWindow(input.Ad);
  212. win.Left = System.Windows.Forms.Screen.AllScreens[1].WorkingArea.Left;
  213. DataBus.AdStatus.Add(input.Device, win);
  214. win.Show();
  215. }
  216. }
  217. else
  218. {
  219. if (DataBus.AdStatus.ContainsKey(input.Device))
  220. {
  221. DataBus.AdStatus[input.Device].Close();
  222. DataBus.AdStatus.Remove(input.Device);
  223. }
  224. }
  225. }
  226. }
  227. public void ReadCard(DKoutput input)
  228. {
  229. lock (locker)
  230. {
  231. Container.Get<HKCore>().DeviceSale(new SaleLog
  232. {
  233. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  234. Location = input.Address.AsInt().ToString()
  235. });
  236. Result = new ObservableCollection<SaleLog>(this.Container.Get<HKCore>().PullDaySaleLog());
  237. }
  238. }
  239. #endregion
  240. }
  241. }