123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- using IPLCConnect;
- using OilSourceModel;
- using OilSourceModel.Models;
- using SQLite;
- using System.Diagnostics.CodeAnalysis;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- namespace OilSourceService
- {
- public sealed class OilSourceService : IOilSourceService.IOilSourceService
- {
- public OilSourceService()
- {
- _timer = new System.Timers.Timer(5000);
- _timer.Elapsed += Timer_Elapsed;
- _timer.Enabled = false;
- _timer.AutoReset = true;
- _timer.Stop();
- }
- private object locker = new object();
- private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
- {
- try
- {
- Ping ping = new Ping();
- if (ping.Send(config.IP).Status == IPStatus.Success)
- {
- //_PLCConnect.Init(oilSourceConfig.IP, oilSourceConfig.Port);
- lock (locker)
- {
- PLCConnect?.Connect();
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// PLC断开连接后定时重连PLC计时器
- /// </summary>
- private System.Timers.Timer _timer = new System.Timers.Timer(5000);
- [AllowNull]
- public Action<bool> ConnectChanged { get; set; }
- /// <summary>
- /// 油源循环锁,当油源与PLC断开连接后自动阻塞轮询线程
- /// </summary>
- private ManualResetEvent OilLock = new ManualResetEvent(true);
- public OilSourceStatusModel OilSourceStatus { get; } = new OilSourceStatusModel();
- private OilSourceModel.Models.OilSourceConfigModel config = new OilSourceModel.Models.OilSourceConfigModel();
- private CancellationTokenSource _cts= new CancellationTokenSource();
- [AllowNull]
- public IPLCConnect.IPLCConnect PLCConnect { get; private set; }
- [AllowNull]
- public ICommunication.ICommunication Communication { get; private set; }
- [AllowNull]
- public SQLiteConnection DbConnection { get; private set; }
- public bool IsStart { get; private set; } = false;
- public void Init(ICommunication.ICommunication communication, SQLiteConnection dbConnection)
- {
- Communication = communication;
- DbConnection = dbConnection;
- ReadModel();
- Communication.GetEvent(Topic.CircuitPressure)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 2 && args.Data[0] is int index && args.Data[1] is float pressure)
- {
- if (!config.MainPumpAddress[index].IsPressureEnabled) return;
- PLCConnect?.Write(config.MainPumpAddress[index].Address, pressure);
- }
- });
- Communication.GetEvent(Topic.CircuitStart)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 2 && args.Data[0] is int index && args.Data[1] is bool state)
- {
- if (!config.MainPumpAddress[index].IsEnabled) return;
- if (state)
- {
- SetLevel(config.MainPumpAddress[index].StartAddress, true);
- }
- else
- {
- SetLevel(config.MainPumpAddress[index].StopAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.CircuitLoad)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 2 && args.Data[0] is int index && args.Data[1] is bool state)
- {
- if (!config.MainPumpAddress[index].IsLoadEnabled) return;
- if (state)
- {
- SetLevel(config.MainPumpAddress[index].LoadAddress, true);
- }
- else
- {
- SetLevel(config.MainPumpAddress[index].UnLoadAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.ForerunnerPressure)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is float pressure)
- {
- if (!config.ForerunnerAddress.IsPressureEnabled) return;
- PLCConnect?.Write(config.ForerunnerAddress.Address, pressure);
- }
- });
- Communication.GetEvent(Topic.ForerunnerStart)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.ForerunnerAddress.IsEnabled) return;
- if (state)
- {
- SetLevel(config.ForerunnerAddress.StartAddress, true);
- }
- else
- {
- SetLevel(config.ForerunnerAddress.StopAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.ForerunnerLoad)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.ForerunnerAddress.IsLoadEnabled) return;
- if (state)
- {
- SetLevel(config.ForerunnerAddress.LoadAddress, true);
- }
- else
- {
- SetLevel(config.ForerunnerAddress.UnLoadAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.AssistantPressure)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is float pressure)
- {
- if (!config.AssistantAddress.IsPressureEnabled) return;
- PLCConnect?.Write(config.AssistantAddress.Address, pressure);
- }
- });
- Communication.GetEvent(Topic.AssistantStart)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.AssistantAddress.IsEnabled) return;
- if (state)
- {
- SetLevel(config.AssistantAddress.StartAddress, true);
- }
- else
- {
- SetLevel(config.AssistantAddress.StopAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.AssistantLoad)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.AssistantAddress.IsLoadEnabled) return;
- if (state)
- {
- SetLevel(config.AssistantAddress.LoadAddress, true);
- }
- else
- {
- SetLevel(config.AssistantAddress.UnLoadAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.CirculatePressure)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is float pressure)
- {
- if (!config.CirculateAddress.IsPressureEnabled) return;
- PLCConnect?.Write(config.CirculateAddress.Address, pressure);
- }
- });
- Communication.GetEvent(Topic.CirculateStart)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.CirculateAddress.IsEnabled) return;
- if (state)
- {
- SetLevel(config.CirculateAddress.StartAddress, true);
- }
- else
- {
- SetLevel(config.CirculateAddress.StopAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.CirculateLoad)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (!config.CirculateAddress.IsLoadEnabled) return;
- if (state)
- {
- SetLevel(config.CirculateAddress.LoadAddress, true);
- }
- else
- {
- SetLevel(config.CirculateAddress.UnLoadAddress, true);
- }
- }
- });
- Communication.GetEvent(Topic.OilEmergencyStop)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 1 && args.Data[0] is bool state)
- {
- if (state)
- {
- SetLevel(config.EmergencyStopAddress, true);
- }
- else
- {
- SetLevel(config.EmergencyStopAddress, true);
- }
- }
- });
- }
- private async void SetLevel(string address, bool value)
- {
- if (config.LevelLogic == LevelLogic.Level)
- {
- PLCConnect?.Writebit(address, value);
- }
- else
- {
- PLCConnect?.Writebit(address, !value);
- await Task.Delay(config.LevelTime);
- PLCConnect?.Writebit(address, value);
- }
- }
- private void ReadModel()
- {
- DbConnection.CreateTable<OilSourceModel.Models.OilSourceConfigModel>();
- config = DbConnection.Table<OilSourceModel.Models.OilSourceConfigModel>()?.FirstOrDefault() ?? new OilSourceModel.Models.OilSourceConfigModel();
- DbConnection.CreateTable<OilSourcePumpAddressConfig>(tableName: nameof(config.MainPumpAddress));
- config.MainPumpAddress = DbConnection.Table<OilSourcePumpAddressConfig>(tableName: nameof(config.MainPumpAddress))?.Where(x=>!string.IsNullOrEmpty(x.Address) && x.IsEnabled)?.ToList() ?? new List<OilSourcePumpAddressConfig>();
- DbConnection.CreateTable<OilSourcePumpAddressConfig>(tableName: nameof(config.ForerunnerAddress));
- config.ForerunnerAddress = DbConnection.Table<OilSourcePumpAddressConfig>(tableName: nameof(config.ForerunnerAddress))?.FirstOrDefault() ?? new OilSourcePumpAddressConfig();
- DbConnection.CreateTable<OilSourcePumpAddressConfig>(tableName:nameof(config.CirculateAddress));
- config.CirculateAddress = DbConnection.Table<OilSourcePumpAddressConfig>(tableName: nameof(config.CirculateAddress))?.FirstOrDefault() ?? new OilSourcePumpAddressConfig();
- DbConnection.CreateTable<OilSourcePumpAddressConfig>(tableName: nameof(config.AssistantAddress));
- config.AssistantAddress = DbConnection.Table<OilSourcePumpAddressConfig>(tableName: nameof(config.AssistantAddress))?.FirstOrDefault() ?? new OilSourcePumpAddressConfig();
- DbConnection.CreateTable<OilSourceAnalogAddressConfig>(tableName: nameof(config.AnalogAddress));
- config.AnalogAddress = DbConnection.Table<OilSourceAnalogAddressConfig>(tableName: nameof(config.AnalogAddress))?.Where(x=>!string.IsNullOrEmpty(x.ValueAddress) && !string.IsNullOrEmpty(x.Name))?.ToList() ?? new List<OilSourceAnalogAddressConfig>();
- DbConnection.CreateTable<ErrorAddressConfig>(tableName: nameof(config.ErrorAddress));
- config.ErrorAddress = DbConnection.Table<ErrorAddressConfig>(tableName: nameof(config.ErrorAddress))?.Where(x=>!string.IsNullOrEmpty(x.Address) && !string.IsNullOrEmpty(x.Name))?.ToList() ?? new List<ErrorAddressConfig>();
- SaveModel();
- InitOilSourceStatus();
- if (!OilSourceStatus.IsEnabled) return;
- var plcs = IModel.Tools.PluginsLoader.Defalut.Load<IPLCConnect.IPLCConnect>(AppDomain.CurrentDomain.BaseDirectory);
- PLCConnect = plcs.FirstOrDefault(x => x.Protocol == config.Protocol);
- if (PLCConnect == null)
- {
- OilSourceStatus.IsEnabled = false;
- return;
- }
- try
- {
- PLCConnect.StatusChanged += PLCConnect_StatusChanged;
- PLCConnect.Init(config.IP, config.Port);
- OilLock.Set();
- Task.Run(async () =>
- {
- _cts = new CancellationTokenSource();
- while(!_cts.IsCancellationRequested)
- {
- OilLock.WaitOne();
- ReadOilSourceData();
- await Task.Delay(50);
- }
- });
- }
- catch
- {
- OilSourceStatus.IsEnabled = false;
- }
- }
- private void RepeatConnectPLC()
- {
- _timer.Enabled = true;
- _timer.Start();
- }
- private void SaveModel()
- {
- }
- private void InitOilSourceStatus()
- {
- if (config.AnalogAddress.Count > 0)
- {
- OilSourceStatus.OilSourceAnalogs.AddRange(config.AnalogAddress.Select(x=>new OilSourceAnalogModel()
- {
- Name = x.Name,
- Unit = x.Unit,
- }));
- }
- if(config.MainPumpAddress.Count>0)
- {
- OilSourceStatus.Circuit.AddRange(config.MainPumpAddress.Select(x => new CircuitModel()
- {
- Name = x.Name,
- IsEnable=x.IsEnabled,
- IsEnableLoad = x.IsLoadEnabled,
- IsEnablePressure = x.IsPressureEnabled,
- }));
- }
- OilSourceStatus.Circulate.Name = config.CirculateAddress.Name;
- OilSourceStatus.Circulate.IsEnableLoad = config.CirculateAddress.IsLoadEnabled;
- OilSourceStatus.Circulate.IsEnable = config.CirculateAddress.IsEnabled;
- OilSourceStatus.Circulate.IsEnablePressure = config.CirculateAddress.IsPressureEnabled;
- OilSourceStatus.Forerunner.Name = config.ForerunnerAddress.Name;
- OilSourceStatus.Forerunner.IsEnableLoad = config.ForerunnerAddress.IsLoadEnabled;
- OilSourceStatus.Forerunner.IsEnable = config.ForerunnerAddress.IsEnabled;
- OilSourceStatus.Forerunner.IsEnablePressure = config.ForerunnerAddress.IsPressureEnabled;
- OilSourceStatus.Assistant.Name = config.AssistantAddress.Name;
- OilSourceStatus.Assistant.IsEnableLoad = config.AssistantAddress.IsLoadEnabled;
- OilSourceStatus.Assistant.IsEnable = config.AssistantAddress.IsEnabled;
- OilSourceStatus.Assistant.IsEnablePressure = config.AssistantAddress.IsPressureEnabled;
- if(config.ErrorAddress.Count>0)
- {
- OilSourceStatus.OilErrors.AddRange(config.ErrorAddress.Select(x => new ErrorInfoModel()
- {
- Name = x.Name,
- }));
- }
- OilSourceStatus.IsEnabled = config.IsEnabled;
- }
- private void PLCConnect_StatusChanged(object? sender, bool e)
- {
- if (OilSourceStatus.IsConnect == e) return;
- ConnectChanged?.Invoke(e);
- OilSourceStatus.IsConnect = e;
- if(e)
- {
- OilLock.Reset();
- _timer.Stop();
- }
- else
- {
- OilLock.Set();
- RepeatConnectPLC();
- }
- }
- public void Start()
- {
- if (PLCConnect == null) return;
- if (PLCConnect.IsConnected) return;
- try
- {
- PLCConnect.Connect();
- }
- catch
- {
- }
- }
- public void ReadOilSourceData()
- {
- lock (locker)
- {
- if (!OilSourceStatus.IsEnabled || PLCConnect == null || !PLCConnect.IsConnected)
- {
- return;
- }
- for (int i = 0; i < OilSourceStatus.Circuit.Count; i++)
- {
- OilSourceStatus.Circuit[i].IsStart = PLCConnect.ReadBit(config.MainPumpAddress[i].IsStartAddress);
- //OilSourceStatus.Circuit[i].Pressure = _PLCConnect.Read<float>(oilSourceConfig.MainPumpAddress[i].Address);
- OilSourceStatus.Circuit[i].IsLoadPressure = PLCConnect.ReadBit(config.MainPumpAddress[i].IsLoadAddress);
- }
- OilSourceStatus.Circulate.IsLoadPressure = PLCConnect.ReadBit(config.CirculateAddress.IsLoadAddress);
- OilSourceStatus.Circulate.IsStart = PLCConnect.ReadBit(config.CirculateAddress.IsStartAddress);
- OilSourceStatus.Forerunner.IsStart = PLCConnect.ReadBit(config.ForerunnerAddress.IsStartAddress);
- OilSourceStatus.Forerunner.IsLoadPressure = PLCConnect.ReadBit(config.ForerunnerAddress.IsLoadAddress);
- for (int i = 0; i < OilSourceStatus.OilErrors.Count; i++)
- {
- OilSourceStatus.OilErrors[i].Status = PLCConnect.ReadBit(config.ErrorAddress[i].Address);
- }
- OilSourceStatus.IsRemote = PLCConnect.ReadBit(config.IsRemoteAddress);
- for (int i = 0; i < OilSourceStatus.OilSourceAnalogs.Count; i++)
- {
- OilSourceStatus.OilSourceAnalogs[i].Value = PLCConnect.Read<float>(config.AnalogAddress[i].ValueAddress);
- OilSourceStatus.OilSourceAnalogs[i].IsLowerError = PLCConnect.ReadBit(config.AnalogAddress[i].LowerErrorAddress);
- OilSourceStatus.OilSourceAnalogs[i].IsLowerWarn = PLCConnect.ReadBit(config.AnalogAddress[i].LowerWarnAddress);
- OilSourceStatus.OilSourceAnalogs[i].IsUpperError = PLCConnect.ReadBit(config.AnalogAddress[i].UpperErrorAddress);
- OilSourceStatus.OilSourceAnalogs[i].IsUpperWarn = PLCConnect.ReadBit(config.AnalogAddress[i].UpperWarnAddress);
- }
- Communication.GetEvent<OilSourceStatusModel>()?.Publish(this, OilSourceStatus);
- }
- }
- public void Stop()
- {
- OilLock.Reset();
- _cts?.Cancel();
- }
- }
- }
|