| | |
| | | using CommunityToolkit.Mvvm.ComponentModel; |
| | | using ConnectionUtil; |
| | | using HMI.Base; |
| | | using HMI.Core.Setting; |
| | | using HMI.Core.Util; |
| | | using HMI.UI.Util; |
| | | using HMI.Util; |
| | | using HslCommunication.Profinet.Siemens; |
| | | using log4net; |
| | | using Microsoft.Win32; |
| | | using PLCHelper; |
| | | using PLCHelper.Entity; |
| | | using Plugin.CurrentSensorControl.Entity; |
| | | using System.Collections.ObjectModel; |
| | | using System.IO; |
| | |
| | | |
| | | private System.Timers.Timer timer; |
| | | private System.Timers.Timer opcTimer; |
| | | private SCADAApi api; |
| | | public static ILog Logger { get; private set; } |
| | | |
| | | private volatile bool needRead = false; |
| | | |
| | | private SiemensS7Net api; |
| | | |
| | | private bool plcMute = false; |
| | | public bool PlcMute |
| | | { |
| | | get => plcMute; |
| | | set |
| | | { |
| | | try |
| | | { |
| | | string name = $"PLC_Mute"; |
| | | if (S7Context.WriteData(api, name, value)) |
| | | { |
| | | plcMute = value; |
| | | OnPropertyChanged(nameof(PlcMute)); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | public ControlPage() |
| | | { |
| | |
| | | opcTimer.AutoReset = false; |
| | | opcTimer.Interval = 1000; |
| | | |
| | | S7Context.Init(SystemSetting.Instance.PlcIp, SystemSetting.Instance.PlcPort); |
| | | |
| | | DataContext = this; |
| | | InitializeComponent(); |
| | | } |
| | |
| | | private void OpcTimer_Elapsed(object sender, ElapsedEventArgs e) |
| | | { |
| | | opcTimer.Enabled = false; |
| | | |
| | | try |
| | | { |
| | | if (S7Context.GetData(api, "PLC_Mute", out object val)) |
| | | { |
| | | plcMute = (bool)val; |
| | | OnPropertyChanged("PlcMute"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | } |
| | | |
| | | try |
| | | { |
| | | var time = DateTime.Now; |
| | | ovenCollection.Refresh(); |
| | | S7Context.ResetFailCount(); |
| | | log.Debug($"Time Cast: {(DateTime.Now - time).TotalMilliseconds}ms"); |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | private void Timer_Elapsed(object sender, ElapsedEventArgs e) |
| | | { |
| | | timer.Enabled = false; |
| | | |
| | | try |
| | | { |
| | | foreach (var ctl in controls) |
| | |
| | | public override void InitFinish() |
| | | { |
| | | Logger = log; |
| | | try |
| | | { |
| | | LoadBurnInSetting(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | log.Error("LoadBurnInSetting Error.", ex); |
| | | } |
| | | } |
| | | |
| | | public override void Active() |
| | | { |
| | | if (!isLoaded) return; |
| | | StartReadCurrent(); |
| | | } |
| | | |
| | |
| | | public override void DoClose(object sender, EventArgs e) |
| | | { |
| | | StopReadCurrent(); |
| | | S7Context.Release(); |
| | | base.DoClose(sender, e); |
| | | } |
| | | |
| | |
| | | |
| | | private void ClearSensorControl() |
| | | { |
| | | foreach (var ctl in diWrap.Children) |
| | | { |
| | | if (ctl is SingleSensor ss) |
| | | { |
| | | ss.Close(); |
| | | } |
| | | } |
| | | diWrap.Children.Clear(); |
| | | controls.Clear(); |
| | | //foreach (var ctl in diWrap.Children) |
| | | //{ |
| | | // if (ctl is SingleSensor ss) |
| | | // { |
| | | // ss.Close(); |
| | | // } |
| | | //} |
| | | //diWrap.Children.Clear(); |
| | | //controls.Clear(); |
| | | } |
| | | |
| | | private void RefreshSensorControl(bool start) |
| | | { |
| | | StopReadCurrent(); |
| | | ClearSensorControl(); |
| | | foreach (var data in Datasource) |
| | | { |
| | | SingleSensor ss = new SingleSensor(data.Name, data.Ip, data.Port, data.Address, data.Channel) |
| | | { |
| | | Width = 240 |
| | | }; |
| | | diWrap.Children.Add(ss); |
| | | controls.Add(ss); |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | ss.Connect(); |
| | | }); |
| | | } |
| | | if (start) StartReadCurrent(); |
| | | //StopReadCurrent(); |
| | | //ClearSensorControl(); |
| | | //foreach (var data in Datasource) |
| | | //{ |
| | | // SingleSensor ss = new SingleSensor(data.Name, data.Ip, data.Port, data.Address, data.Channel) |
| | | // { |
| | | // Width = 240 |
| | | // }; |
| | | // diWrap.Children.Add(ss); |
| | | // controls.Add(ss); |
| | | // Task.Factory.StartNew(() => |
| | | // { |
| | | // ss.Connect(); |
| | | // }); |
| | | //} |
| | | //if (start) StartReadCurrent(); |
| | | } |
| | | |
| | | private void ConnectAllSensor() |
| | | { |
| | | foreach (var ctl in diWrap.Children) |
| | | { |
| | | if (ctl is SingleSensor ss) |
| | | { |
| | | ss.Connect(); |
| | | } |
| | | } |
| | | //foreach (var ctl in diWrap.Children) |
| | | //{ |
| | | // if (ctl is SingleSensor ss) |
| | | // { |
| | | // ss.Connect(); |
| | | // } |
| | | //} |
| | | } |
| | | |
| | | private void DisconnectAllSensor() |
| | | { |
| | | foreach (var ctl in diWrap.Children) |
| | | { |
| | | if (ctl is SingleSensor ss) |
| | | { |
| | | ss.Close(); |
| | | } |
| | | } |
| | | //foreach (var ctl in diWrap.Children) |
| | | //{ |
| | | // if (ctl is SingleSensor ss) |
| | | // { |
| | | // ss.Close(); |
| | | // } |
| | | //} |
| | | } |
| | | |
| | | private bool isLoaded = false; |
| | | private void SimpleMaskControl_Loaded(object sender, System.Windows.RoutedEventArgs e) |
| | | { |
| | | LoadSensorData(); |
| | | RefreshSensorControl(needRead); |
| | | LoadBurnInSetting(); |
| | | Loaded -= SimpleMaskControl_Loaded; |
| | | ShowMask(() => |
| | | { |
| | | api = S7Context.Get($"1"); |
| | | Dispatcher.Invoke(() => |
| | | { |
| | | LoadSensorData(); |
| | | RefreshSensorControl(needRead); |
| | | //LoadBurnInSetting(); |
| | | StartReadCurrent(); |
| | | isLoaded = true; |
| | | Loaded -= SimpleMaskControl_Loaded; |
| | | }); |
| | | }, (ex) => |
| | | { |
| | | ToastHelper.Error("Loaded Failed."); |
| | | log.Error("Load Error.", ex); |
| | | }, null, "请稍后"); |
| | | } |
| | | |
| | | private void OpenDataModal(SensorData data) |
| | |
| | | |
| | | private void DoDelete(SensorData data) |
| | | { |
| | | if (MsgBoxHelper2.WarnChoose("删除?") == MessageBoxResult.OK) |
| | | if (MsgBoxHelper2.WarnChoose("确定要删除吗?") == MessageBoxResult.OK) |
| | | { |
| | | var index = Datasource.IndexOf(data); |
| | | if (index >= 0) |
| | |
| | | |
| | | private void LoadBurnInSetting() |
| | | { |
| | | S7Context.dataCache.Clear(); |
| | | ObservableCollection<PLCTagData> datas; |
| | | try |
| | | { |
| | | string path = Path.Combine(BaseFolder, "BurnInSettingData.data"); |
| | | if (File.Exists(path)) |
| | | { |
| | | datas = XmlHelper.DeserializeFromXml<ObservableCollection<PLCTagData>>(path); |
| | | } |
| | | else |
| | | { |
| | | datas = new ObservableCollection<PLCTagData>(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | datas = new ObservableCollection<PLCTagData>(); |
| | | } |
| | | BurnInSettingData = datas; |
| | | LoadBurnInSettingCache(); |
| | | S7Context.LoadTagData(); |
| | | BurnInSettingData = S7Context.Datasource; |
| | | } |
| | | |
| | | private void LoadBurnInSettingCache() |
| | | { |
| | | foreach (var data in BurnInSettingData) |
| | | { |
| | | S7Context.dataCache[data.Name] = data; |
| | | } |
| | | S7Context.LoadTagData(); |
| | | } |
| | | |
| | | private void OpenBurnSettingModal() |