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.

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