using Aucma.Scada.Business; using HighWayIot.Config; using HighWayIot.Log4net; using HighWayIot.Plc; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Aucma.Scada.UI.Page.InStoreInfo { /// /// NullMaterial.xaml 的交互逻辑 /// public partial class NullMaterialWindow : Window { private Dictionary _plcDictionary = new Dictionary(); private AppConfig appConfig = AppConfig.Instance; private PlcConfig plcConfig = PlcConfig.Instance; private LogHelper logHelper = LogHelper.Instance; private PlcPool _pool = PlcPool.Instance; private InStoreBusiness inStoreBusiness = InStoreBusiness.Instance; private Timer _timer; /// /// 箱壳空板入库处理 /// /// /// public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip); public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; public NullMaterialWindow() { InitializeComponent(); _plcDictionary = _pool.GetAll(); // 初始化定时器 _timer = new Timer(1000); // 1000 毫秒 = 1 秒 _timer.Elapsed += TimerElapsed; // 绑定定时器事件 } private void TimerElapsed(object sender, ElapsedEventArgs e) { int nullAmount = ReadNullAmount(); App.Current.Dispatcher.BeginInvoke((Action)(() => { NullAmount.Text = nullAmount.ToString(); })); } /// /// 读取plc空板数量 /// /// /// public int ReadNullAmount() { int result = 0; try { IPlc _plc = _plcDictionary[appConfig.shellStoreCode]; if (_plc != null) { result = _plc.readInt32ByAddress(plcConfig.null_shell_amount); return result; } } catch (Exception ex) { logHelper.Error("打开放空板按钮:" + ex.Message.ToString()); } return result; } private void Button_Click(object sender, RoutedEventArgs e) { _timer.Start(); // 启动定时器 if (OpenNullKey()) { MessageBox.Show("成功打开"); } else { MessageBox.Show("打开空板开关失败!"); } } private void Button_Click_1(object sender, RoutedEventArgs e) { _timer.Stop(); if (OpenNullKey()) { MessageBox.Show("成功关闭"); } else { MessageBox.Show("关闭空板开关失败!"); } } /// /// 打开放空板按钮 /// /// /// public bool OpenNullKey() { bool result = false; try { IPlc _plc = _plcDictionary[appConfig.shellStoreCode]; if (_plc != null) { result = _plc.writeBoolByAddress(plcConfig.null_shell_open, true); } } catch (Exception ex) { logHelper.Error("打开放空板按钮:" + ex.Message.ToString()); } return result; } /// /// 关闭放空板按钮 /// /// /// public bool CloseNullKey() { bool result = false; try { IPlc _plc = _plcDictionary[appConfig.shellStoreCode]; if (_plc != null) { result = _plc.writeBoolByAddress(plcConfig.null_shell_open, false); } } catch (Exception ex) { logHelper.Error("关闭放空板按钮:" + ex.Message.ToString()); } return result; } protected override void OnClosed(EventArgs e) { base.OnClosed(e); // 在窗口关闭时停止定时器 _timer.Stop(); _timer.Dispose(); } private void Button_Click_2(object sender, RoutedEventArgs e) { window.Close(); } private void Button_Click_3(object sender, RoutedEventArgs e) { int amount ; try { amount = int.Parse(NullAmount.Text); }catch (Exception ex) { amount=0; } if (amount <= 0) { MessageBox.Show("数量要大于0"); NullAmount.Text = ""; return; } MessageBoxResult result = MessageBox.Show("确认创建"+ amount+"个空板入库任务吗,请仔细核对空板数量", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // 创建空板入库任务 for(int i = 0; i < amount; i++) { inStoreBusiness.InStore("1111111111111111111111", appConfig.shellHikRobotIp); } } } } }