private void HandleMessage(MessageState state, CincronMessage msg, int repeatCount) { var queueChange = msg as CincronMessage.QueuePositionChange; //machine cycle start. For now it is pallet rotating into machine if (queueChange != null && queueChange.CurrentLocation.Location == PalletLocationEnum.Machine && queueChange.NewQueuePosition == "10010") { _log.RecordMachineStart( mats: FindMaterial(queueChange.Pallet), pallet: queueChange.Pallet, statName: "MC", statNum: queueChange.CurrentLocation.Num, program: "", timeUTC: queueChange.TimeUTC, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); } //machine cycle end. StepNo changing to 5 signals cycle end. var stepChange = msg as CincronMessage.PartNewStep; if (stepChange != null && stepChange.StepNo == 5) { var machineCycleStart = FindMachineStart(_log.CurrentPalletLog(stepChange.Pallet)); if (machineCycleStart != null) { _log.RecordMachineEnd( mats: machineCycleStart.Material.Select(JobLogDB.EventLogMaterial.FromLogMat), pallet: stepChange.Pallet, statName: "MC", statNum: machineCycleStart.LocationNum, program: "", timeUTC: stepChange.TimeUTC, result: "", elapsed: stepChange.TimeUTC.Subtract(machineCycleStart.EndTimeUTC), active: TimeSpan.Zero, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); } } //program end. FindMachineStart correctly returns null if we have already recorded //cycle end. var progEnd = msg as CincronMessage.ProgramFinished; if (progEnd != null) { var machineCycleStart = FindMachineStart(_log.CurrentPalletLog(progEnd.Pallet)); if (machineCycleStart != null) { _log.RecordMachineEnd( mats: machineCycleStart.Material.Select(JobLogDB.EventLogMaterial.FromLogMat), pallet: progEnd.Pallet, statName: "MC", statNum: machineCycleStart.LocationNum, program: "", timeUTC: progEnd.TimeUTC, result: "", elapsed: progEnd.TimeUTC.Subtract(machineCycleStart.EndTimeUTC), active: TimeSpan.Zero, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); } } //part completed message. Store in memory since typically there is an Unload Start event //which happens right afterwords. var comp = msg as CincronMessage.PartCompleted; if (comp != null) { for (int i = 0; i < repeatCount; i++) { if (!state.PartCompletedMessagesBySetup.ContainsKey(comp.Setup)) { state.PartCompletedMessagesBySetup[comp.Setup] = new List <CincronMessage.PartCompleted>(); } state.PartCompletedMessagesBySetup[comp.Setup].Add(comp); } } //move to unload. Store in memory, typically there is an UnloadStart event soon if (queueChange != null && queueChange.CurrentLocation.Location == PalletLocationEnum.LoadUnload && queueChange.NewQueuePosition == "10010") { _log.RecordGeneralMessage( mat: null, pallet: queueChange.Pallet, program: "PalletMoveToLoad", result: queueChange.CurrentLocation.Num.ToString(), timeUTC: queueChange.TimeUTC, foreignId: ForeignId(msg), originalMessage: msg.LogMessage); } //unload start. Use the completed parts and last unload station from the state. var unloadStart = msg as CincronMessage.PartUnloadStart; if (unloadStart != null) { var oldEvts = _log.CurrentPalletLog(unloadStart.Pallet); var lul = FindLastLoadStation(oldEvts); _log.RecordUnloadStart( mats: CreateUnloadMaterial(state, unloadStart.Pallet, oldEvts), pallet: unloadStart.Pallet, lulNum: lul, timeUTC: unloadStart.TimeUTC, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); state.PartCompletedMessagesBySetup.Clear(); } var loadStart = msg as CincronMessage.PartLoadStart; if (loadStart != null) { var oldEvts = _log.CurrentPalletLog(loadStart.Pallet); var lul = FindLastLoadStation(oldEvts); _log.RecordLoadStart( mats: CreateLoadMaterial(loadStart), pallet: loadStart.Pallet, lulNum: lul, timeUTC: loadStart.TimeUTC, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); } //end of load and unload on step change to 2 if (stepChange != null && stepChange.StepNo == 2) { //create end unload, then pallet cycle, then end load. var oldEvts = _log.CurrentPalletLog(stepChange.Pallet); var loadStartCycle = FindLoadStart(oldEvts); var unloadStartCycle = FindUnloadStart(oldEvts); if (unloadStartCycle != null) { _log.RecordUnloadEnd( mats: unloadStartCycle.Material.Select(JobLogDB.EventLogMaterial.FromLogMat), pallet: stepChange.Pallet, lulNum: unloadStartCycle.LocationNum, timeUTC: stepChange.TimeUTC, elapsed: stepChange.TimeUTC.Subtract(unloadStartCycle.EndTimeUTC), active: TimeSpan.Zero, foreignId: ForeignId(msg), originalMessage: msg.LogMessage ); } var mats = new Dictionary <string, IEnumerable <JobLogDB.EventLogMaterial> >(); if (loadStartCycle != null) { _log.AddPendingLoad( pal: stepChange.Pallet, key: stepChange.Pallet, load: loadStartCycle.LocationNum, elapsed: stepChange.TimeUTC.Subtract(loadStartCycle.EndTimeUTC), active: TimeSpan.Zero, foreignID: ForeignId(msg) ); mats[stepChange.Pallet] = loadStartCycle.Material.Select(JobLogDB.EventLogMaterial.FromLogMat); } _log.CompletePalletCycle(stepChange.Pallet, stepChange.TimeUTC, ForeignId(msg), mats, generateSerials: false); } }