Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

177 lignes
5.8 KiB

  1. using DTO;
  2. using HKCardOUT.Helper;
  3. using HKCardOUT.Logic.Model;
  4. using HKCardOUT.Logic.Service;
  5. using NStandard;
  6. using SqlSugar.DistributedSystem.Snowflake;
  7. using Stylet;
  8. using StyletIoC;
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. using System.Threading;
  14. using System.Windows.Documents;
  15. using UHFHelper;
  16. using XExten.Advance.CacheFramework;
  17. using XExten.Advance.CacheFramework.RunTimeCache;
  18. using XExten.Advance.LinqFramework;
  19. namespace HKCardOUT.ViewModels
  20. {
  21. public class RootViewModel : Conductor<IScreen>
  22. {
  23. public delegate void ReadCardFunc(DKoutput? input);
  24. public event ReadCardFunc ReadFunc;
  25. private readonly object locker = new object();
  26. private IContainer Container;
  27. public RootViewModel(IContainer Container)
  28. {
  29. this.Container = Container;
  30. MainThread();
  31. }
  32. protected override void OnViewLoaded()
  33. {
  34. System.Timers.Timer timer = new System.Timers.Timer(500);
  35. timer.Enabled = true;
  36. timer.Elapsed += (s, e) =>
  37. {
  38. Result = new ObservableCollection<SaleLog>(this.Container.Get<HKCore>().PullDaySaleLog());
  39. };
  40. timer.Start();
  41. //广告初始化
  42. var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  43. {
  44. Ad = x.Remaek,
  45. IsActive = false,
  46. Device = t.Name,
  47. Stalls = x.Name
  48. }).ToList();
  49. if (Init != null)
  50. Ad = new ObservableCollection<AdDTO>(Init);
  51. }
  52. #region 属性
  53. ObservableCollection<SaleLog> _Result;
  54. public ObservableCollection<SaleLog> Result
  55. {
  56. get => _Result;
  57. set => SetAndNotify(ref _Result, value);
  58. }
  59. ObservableCollection<AdDTO> _Ad;
  60. public ObservableCollection<AdDTO> Ad
  61. {
  62. get => _Ad;
  63. set => SetAndNotify(ref _Ad, value);
  64. }
  65. #endregion
  66. #region 方法
  67. private void MainThread()
  68. {
  69. ReadFunc -= ReadCard;
  70. ReadFunc += ReadCard;
  71. ThreadManage.GetInstance().StartLong(new Action(() =>
  72. {
  73. try
  74. {
  75. //1.检测网络上下线
  76. bool network = HKHelpers.GetInstance().GetNetworkState();
  77. if (network != DataBus.NetWordState)
  78. {
  79. if (network) HandyControl.Controls.Growl.InfoGlobal("网络连接成功");
  80. else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
  81. DataBus.NetWordState = network;
  82. }
  83. Thread.Sleep(3000);
  84. }
  85. catch (Exception ex)
  86. {
  87. HandyControl.Controls.Growl.InfoGlobal(ex.Message);
  88. }
  89. }), "循环状态监测线程", false);
  90. try
  91. {
  92. // 初始化 串口
  93. UHF_RS485_Helper.GetInstance().Open(new SerialParam
  94. {
  95. PortName = DataBus.COM,
  96. BaudRate = 57600,
  97. DataBits = 8
  98. });
  99. }
  100. catch (Exception)
  101. {
  102. HandyControl.Controls.Growl.WarningGlobal("未接入相应设备");
  103. }
  104. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  105. HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
  106. DataBus.StoreInfo.Devices.ForEach(item =>
  107. {
  108. System.Timers.Timer timer = new System.Timers.Timer(500);
  109. timer.Enabled = true;
  110. timer.Elapsed += (sender, eve) =>
  111. {
  112. if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
  113. return;
  114. else
  115. {
  116. var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt()).Result;
  117. if (res != null)
  118. {
  119. if (!res.ResData.IsMatch(new Regex("0{20}")))
  120. ReadFunc?.Invoke(res);
  121. }
  122. }
  123. };
  124. timer.Start();
  125. });
  126. }
  127. #endregion
  128. #region 命令
  129. /// <summary>
  130. /// 广告位置
  131. /// </summary>
  132. /// <param name="input"></param>
  133. public void UpdateAction(AdDTO input)
  134. {
  135. input.IsActive = !input.IsActive;
  136. var SC = System.Windows.Forms.Screen.AllScreens.Count();
  137. if (SC >= 1)
  138. {
  139. if (input.IsActive)
  140. {
  141. if (!DataBus.AdStatus.ContainsKey(input.Device))
  142. {
  143. var win = new Views.AdWindow(input.Ad);
  144. DataBus.AdStatus.Add(input.Device, win);
  145. win.Show();
  146. }
  147. }
  148. else
  149. {
  150. if (DataBus.AdStatus.ContainsKey(input.Device))
  151. {
  152. DataBus.AdStatus[input.Device].Close();
  153. DataBus.AdStatus.Remove(input.Device);
  154. }
  155. }
  156. }
  157. }
  158. public void ReadCard(DKoutput input)
  159. {
  160. lock (locker)
  161. {
  162. Container.Get<HKCore>().DeviceSale(new SaleLog
  163. {
  164. CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
  165. Location = input.Address.AsInt().ToString()
  166. });
  167. }
  168. }
  169. #endregion
  170. }
  171. }