using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.AutoTask; using log4net; using Repository.Station; using Service.Plc.Client; using Service.Plc.Msg; namespace Service.MyTask; [Scope] public class PlcHeartTask: ITask { private static readonly ILog Log = LogManager.GetLogger(typeof(PlcHeartTask)); private volatile bool _stop; public string Name() { return "PlcHeartTask"; } public int Interval() { return 1000 * 1; } private static bool heart = false; public BinInfoRepository BinInfoRepository { get; set; } public void Handle() { Log.Info("Plc start write heart"); if (PlcMgr.PlcClient != null) { Log.Info("Plc write connect and satrt set value"); heart = !heart; HostToPlc writeHostToPlc = new HostToPlc(); writeHostToPlc.ProtocolVersion1.Value = 2; writeHostToPlc.ProtocolVersion2.Value = 0; writeHostToPlc.ProtocolVersion3.Value = 1; writeHostToPlc.CommunicationDiagnosis.Value = (ushort)(heart ? 1 : 0); //writeHostToPlc.Years.Value = (ushort)DateTime.Now.Year; //writeHostToPlc.Month.Value = (ushort)DateTime.Now.Month; //writeHostToPlc.Day.Value = (ushort)DateTime.Now.Day; //writeHostToPlc.Hour.Value = (ushort)DateTime.Now.Hour; //writeHostToPlc.Points.Value = (ushort)DateTime.Now.Minute; //writeHostToPlc.Seconds.Value = (ushort)DateTime.Now.Second; var lstBinInfo = BinInfoRepository.Query(); writeHostToPlc.ChargingStatus02.Value = (short)(lstBinInfo[1].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus03.Value = (short)(lstBinInfo[2].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus04.Value = (short)(lstBinInfo[3].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus05.Value = (short)(lstBinInfo[4].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus06.Value = (short)(lstBinInfo[5].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus07.Value = (short)(lstBinInfo[6].ChargeStatus == 1 ? 1010 : 1000); writeHostToPlc.ChargingStatus08.Value = (short)(lstBinInfo[7].ChargeStatus == 1 ? 1010 : 1000); //TODO 后面8个仓写9~16还是11~18 bool write01 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion1); bool write02 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion2); bool write03 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion3); bool write0 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.CommunicationDiagnosis); //bool write1 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Years); //bool write2 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Month); //bool write3 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Day); //bool write4 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Hour); //bool write5 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Points); //bool write6 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Seconds); bool writeCharge2 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus02); bool writeCharge3 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus03); bool writeCharge4 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus04); bool writeCharge5 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus05); bool writeCharge6 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus06); bool writeCharge7 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus07); bool writeCharge8 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus08); Log.Info("Plc write finish"); } else { Log.Info("Plc write not connect"); } } public bool Stoped() { return _stop; } public void Stop() { _stop = true; } public void ResetStop() { _stop = false; } }