PLCNotifier.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. namespace MinicsConsole.Connector;
  2. public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier, ITcpConnectNority, IDisposable
  3. {
  4. private readonly Fins_Tcp _fins = new();
  5. public string Name { get; set; } = "PLC Fins Tcp";
  6. public bool StartService()
  7. {
  8. if (address.PLCAddress is null)
  9. return false;
  10. if (string.IsNullOrEmpty(address.PLCAddress.IPAddress))
  11. return false;
  12. this._fins.Initialize(this);
  13. this._fins.Connect(address.PLCAddress.IPAddress, address.PLCAddress.Port, PlcHeartBeatCallBack);
  14. return true;
  15. }
  16. public void Dispose()
  17. {
  18. this._fins?.Dispose();
  19. }
  20. private bool PlcHeartBeatCallBack()
  21. {
  22. if (address.PLCAddress is null)
  23. return true;
  24. if (string.IsNullOrEmpty(address.PLCAddress.HeartBeatAddress))
  25. return true;
  26. try
  27. {
  28. return _fins.SetData<bool>(address.PLCAddress.HeartBeatAddress, true);
  29. }
  30. catch
  31. {
  32. return false;
  33. }
  34. }
  35. void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  36. {
  37. if (this._fins is null)
  38. return;
  39. if (!this._PlcConnected)
  40. return;
  41. if (!address.PLCChannelsAddresses.TryGetSubValue(mini8, channel, out ChannelAddressPLC? channelAddress) || channelAddress is null)
  42. return;
  43. if (string.IsNullOrEmpty(channelAddress.PV))
  44. return;
  45. if (!this._fins.SetData(channelAddress.PV, channelData.PV))
  46. log.Warning($"PLCNotifier ChannelInfoNotify Send Mini8 - {mini8} channel {channel} PV {channelData.PV} Failed");
  47. }
  48. private bool _PlcConnected = false;
  49. void ITcpConnectNority.Connect(string ip, int port)
  50. {
  51. this._PlcConnected = true;
  52. log.Info($"PLC Connected {ip}:{port}");
  53. }
  54. void ITcpConnectNority.DisConnect(string ip, int port)
  55. {
  56. this._PlcConnected = false;
  57. log.Warning($"PLC Disconnected {ip}:{port}");
  58. }
  59. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  60. {
  61. }
  62. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  63. {
  64. }
  65. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  66. {
  67. }
  68. }