|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- using BPA.Helper;
- using DTO;
- using HKCardOUT.Helper;
- using HKCardOUT.Logic.Model;
- using HKCardOUT.Logic.Service;
- using HKCardOUT.Views;
- using HKLog;
- using ImTools;
- using Stylet;
- using StyletIoC;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.IO;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using UHFHelper;
- using XExten.Advance.InternalFramework.Securities.Common;
- using XExten.Advance.LinqFramework;
- using XExten.Advance.StaticFramework;
-
- namespace HKCardOUT.ViewModels
- {
- public class RootViewModel : Conductor<IScreen>
- {
- public delegate void ReadCardFunc(DKoutput? input);
- public event ReadCardFunc ReadFunc;
- private IContainer Container;
- public RootViewModel(IContainer Container)
- {
- this.Container = Container;
- this.Activated += RootViewModel_Activated;
- this.Closed += RootViewModel_Closed;
- var serv = Container.Get<HKCore>();
- serv.GetAllCardStatus();
- ProcessCard();
- InsertData();
- }
-
- /// <summary>
- /// 关闭页面
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void RootViewModel_Closed(object sender, CloseEventArgs e)
- {
- ThreadManage.GetInstance().Dispose();
- UHF_RS485_Helper.GetInstance().Close();
- }
- private void RootViewModel_Activated(object sender, ActivationEventArgs e)
- {
- ReadFunc -= ReadCard;
- ReadFunc += ReadCard;
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- while (!SerialPort.GetPortNames().Contains(DataBus.COM))
- {
- Thread.Sleep(1000);
- }
-
- UHF_RS485_Helper.GetInstance().DisConnect = new Action(() =>
- {
- ThreadManage.GetInstance().StopTask("串口监听");
- });
-
- UHF_RS485_Helper.GetInstance().OpenOk = new Action(() =>
- {
- Object async = new object();
- DataBus.StoreInfo.Devices.ForEach(item =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- try
- {
- while (true)
- {
- if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
- {
- if (DataBus.StoreInfo != null)
- {
- DKoutput res = null;
- res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
- if (res != null)
- {
- HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 卡号地址:{res.Address}----------卡号数据:{res.ResData}");
- if (!res.ResData.IsNullOrEmpty())
- {
- if (res.ResData.Length == 20)
- ReadFunc?.Invoke(res);
- }
- Thread.Sleep(2000);
- }
- }
- }
- Thread.Sleep(50);
- }
- }
- catch (Exception ex)
- {
- HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 异常信息:{ex.Message}");
- Monitor.Exit(async);
- }
- }), $"监听{item.Address}", true);
- });
- });
-
- // 打开串口
- UHF_RS485_Helper.GetInstance().Open(new SerialParam
- {
- PortName = DataBus.COM,
- BaudRate = 57600,
- DataBits = 8
- });
- if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
- {
- HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
- }
- }), "打开串口");
-
- }
- protected override void OnViewLoaded()
- {
- //广告初始化
- Task.Factory.StartNew(new Action(() =>
- {
- var Init = DataBus.StoreInfo?.Devices.Join(DataBus.StoreInfo?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
- {
- Ad = x.Remaek,
- Device = t.Name,
- Stalls = x.Name
- }).ToList();
- if (Init != null)
- {
- Ad = new ObservableCollection<AdDTO>(Init);
- var route = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt"));
- SyncStatic.WriteFile(Encoding.UTF8.GetBytes(SyncStatic.Compress(Init.ToJson(), SecurityType.Base64)), route);
- AdOpen();
- }
- else
- {
- var jsons = SyncStatic.Decompress(SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AD.txt")), SecurityType.Base64);
- Ad = new ObservableCollection<AdDTO>(jsons.ToModel<List<AdDTO>>());
- AdOpen();
- }
- }));
-
- }
-
- #region 属性
- public RootView Main { get; set; }
- ObservableCollection<SaleLog> _Result;
- public ObservableCollection<SaleLog> Result
- {
- get => _Result;
- set => SetAndNotify(ref _Result, value);
- }
- ObservableCollection<AdDTO> _Ad;
- public ObservableCollection<AdDTO> Ad
- {
- get => _Ad;
- set => SetAndNotify(ref _Ad, value);
- }
- #endregion
-
- #region 屏幕检查
- private void AdOpen()
- {
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- var win = new AdWindow(Ad[0].Ad);
- win.DataContext = new AdWindowViewModel();
- var rectangle = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
- win.Height = rectangle.Height;
- win.Width = rectangle.Width;
- win.Top = 0;
- win.Left = rectangle.Left;
- win.Owner = this.Main;
- win.Show();
- }));
- }
-
- #endregion
-
- #region 命令
- public void ReadCard(DKoutput input)
- {
- var mo = new SaleLogDto
- {
- CardNo = input.ResData.Substring(1, input.ResData.Length - 1),
- Location = input.Address.AsInt().ToString(),
- Date = DateTime.Now
- };
- HKLogImport.WriteInfo("读卡成功:" + mo.ToJson());
- if (Stacks.Count > 0)
- {
- var check = Stacks.Where(t => t.CardNo == mo.CardNo).Where(t => t.Location == mo.Location).FirstOrDefault();
- if (check == null)
- Stacks.Enqueue(mo);
- }
- else
- Stacks.Enqueue(mo);
- }
-
- private void ProcessCard()
- {
- ThreadManage.GetInstance().StartLong(() =>
- {
-
- if (0 < Stacks.Count && Stacks.TryDequeue(out SaleLogDto Info))
- {
- if (Info != null)
- {
- if (!HKControl.Main.GetInstance.GetIsSwipe(Info.Location.AsInt()))
- {
- Stacks.Enqueue(Info);
- HKLogImport.WriteInfo($"{Info.Location} 窗口不允许出餐");
- }
- else
- {
- var Core = Container.Get<HKCore>();
- HKLogImport.WriteInfo("队列读取成功:" + Info.ToJson());
- if (Info.Date >= DataBus.Times.AMStartTime && Info.Date < DataBus.Times.AMEndTime)
- {
- Core.DeviceSaleAM(new SaleLog
- {
- CardNo = Info.CardNo,
- Location = Info.Location,
- IsSync = false
- });
- }
- if (Info.Date >= DataBus.Times.PMStartTime && Info.Date < DataBus.Times.PMEndTime)
- {
- Core.DeviceSalePM(new SaleLog
- {
- CardNo = Info.CardNo,
- Location = Info.Location,
- IsSync = false
- });
-
- }
- if (Info.Date >= DataBus.Times.ATStartTime && Info.Date < DataBus.Times.ATEndTime)
- {
- Core.DeviceSaleAT(new SaleLog
- {
- CardNo = Info.CardNo,
- Location = Info.Location,
- IsSync = false
- });
-
- }
- }
- }
- }
- Thread.Sleep(50);
- }, "processCard");
- }
-
- private void InsertData()
- {
- ThreadManage.GetInstance().StartLong(() =>
- {
- Container.Get<HKCore>().WriteData();
- Thread.Sleep(1000);
- }, "写入数据", true);
- }
-
- private static ConcurrentQueue<SaleLogDto> Stacks = new ConcurrentQueue<SaleLogDto>();
- #endregion
-
- }
- }
|