using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.AutoTask; using log4net; using Service.PlcAfrica.Client; using Service.PlcAfrica.Msg; namespace Service.MyTask; [Scope] public class PlcHeartTask : ITask { private static readonly ILog Log = LogManager.GetLogger("PlcHeart"); private volatile bool _stop; public string Name() { return "PlcHeartTask"; } public int Interval() { return 1000 * 1; } private static bool heart = false; public void Handle() { Log.Info("Plc Start write heart"); if (PlcAfricaMgr.PlcAfricaClient != null && PlcAfricaMgr.PlcAfricaClient.Connected) { heart = !heart; PlcAfricaMsg writeHostToPlc = new PlcAfricaMsg(); writeHostToPlc.Heart.Value = (ushort)(heart ? 1 : 0); Log.Info("Plc Start write heart write begin"); bool write0 = PlcAfricaMgr.PlcAfricaClient.WriteValue(writeHostToPlc.Heart); Log.Info($"write return {write0}"); if (!write0) { Log.Info("Plc Start write heart write timeout"); } else { Log.Info("Plc Start write heart write success"); } } else Log.Info("Plc Start write heart fail"); } public bool Stoped() { return _stop; } public void Stop() { _stop = true; } public void ResetStop() { _stop = false; } }