public Instance Copy() { lock (_lock) { var result = new Instance(); result.Timestamp = Timestamp; result.Sequence = Sequence; result.AgentInstanceId = AgentInstanceId; foreach (var val in Values) { if (val != null) { result.Values.Add(val.Copy()); } } return result; } }
public Return Process(Instance instance) { bool conditionsMet = false; // Set Result to Default Value (in case nothing changes) Return result = Default; foreach (Value value in Values) { conditionsMet = false; foreach (var trigger in value.Triggers) { var instanceValue = instance.Values.Find(x => FindTrigger(x, trigger)); if (instanceValue != null) { conditionsMet = trigger.Process(instanceValue); } // if any triggers are not met then break if (!conditionsMet) break; } if (conditionsMet || value.Triggers.Count == 0) { foreach (MultiTrigger multiTrigger in value.MultiTriggers) { foreach (Trigger trigger in multiTrigger.Triggers) { var instanceValue = instance.Values.Find(x => FindTrigger(x, trigger)); if (instanceValue != null) { conditionsMet = trigger.Process(instanceValue); } // if any trigger is met then break (only one has to be met) if (conditionsMet) break; } // if none of the triggers were met then break (at least one has to be met) if (!conditionsMet) break; } } if (conditionsMet) { result = value.Result; // Break from loop since it shouldn't be able to meet any more Value's conditions break; } } // Get CaptureItems foreach (var captureItem in CaptureItems) { captureItem.PreviousValue = captureItem.Value; var instanceValue = instance.Values.ToList().Find(x => Tools.GetValue(x, "Id") == Tools.GetValue(captureItem, "Link")); if (instanceValue != null) { if (instanceValue.Value != captureItem.Value) { captureItem.Value = instanceValue.Value; } captureItem.Sequence = instanceValue.ChangedSequence; } else captureItem.Value = ""; result.CaptureItems = CaptureItems.ToList(); } return result; }
private static string FormatDataItemType(Instance.DataItemValue value) { if (!string.IsNullOrEmpty(value.Type)) { return value.Type.ToLower(); } return null; }
private static bool FindTrigger(Instance.DataItemValue value, Trigger trigger) { if (trigger.Link != null) { if (trigger.LinkType == TriggerLinkType.Type) { return FormatDataItemType(value) == trigger.Link.ToLower(); } else { return value.Id == trigger.Link; } } return false; }
public bool Process(Instance.DataItemValue instanceValue) { if (Modifier == TriggerModifier.NOT) { if (Tools.GetValue(instanceValue, "Value") != Tools.GetValue(this, "Value")) return true; } else if (Modifier == TriggerModifier.GREATER_THAN) { double trigger_val = double.MinValue; double val = double.MinValue; if (double.TryParse(instanceValue.Value, out val) && double.TryParse(Value, out trigger_val)) { if (val > trigger_val) return true; } } else if (Modifier == TriggerModifier.LESS_THAN) { double trigger_val = double.MinValue; double val = double.MinValue; if (double.TryParse(instanceValue.Value, out val) && double.TryParse(Value, out trigger_val)) { if (val < trigger_val) return true; } } else if (Modifier == TriggerModifier.CONTAINS) { if (Regex.IsMatch(instanceValue.Value, "^(?=.*" + Value + ").+$", RegexOptions.IgnoreCase)) return true; } else if (Modifier == TriggerModifier.CONTAINS_MATCH_CASE) { if (Regex.IsMatch(instanceValue.Value, "^(?=.*" + Value + ").+$")) return true; } else if (Modifier == TriggerModifier.CONTAINS_WHOLE_WORD) { if (Regex.IsMatch(instanceValue.Value, Value + "\\b", RegexOptions.IgnoreCase)) return true; } else if (Modifier == TriggerModifier.CONTAINS_WHOLE_WORD_MATCH_CASE) { if (Regex.IsMatch(instanceValue.Value, Value + "\\b")) return true; } else { if (Tools.GetValue(instanceValue, "Value") == Tools.GetValue(this, "Value")) return true; } return false; }
private List<CycleData> Process(Instance instanceData) { var result = new List<CycleData>(); var cc = Configuration.Get(configuration); var gc = GeneratedEvents.Configuration.Get(configuration); if (cc != null && cc.CycleEventName != null && gc != null) { // Find the CycleEventName in the Generated Events Configuration var cycleEvent = gc.Events.Find(x => x.Name.ToLower() == cc.CycleEventName.ToLower()); if (cycleEvent != null) { // Search for cycle name link in InstanceData var instanceValue = instanceData.Values.Find(x => x.Id == cc.CycleNameLink); if (instanceValue != null) { var cycleOverrides = new List<CycleOverride>(); // Get CycleOverride values from InstanceData foreach (var ovr in cc.OverrideLinks) { var cycleOverride = GetOverrideFromInstanceData(ovr, instanceData); if (cycleOverride != null) cycleOverrides.Add(cycleOverride); } // Process Cycle Event using instanceData var eventReturn = cycleEvent.Process(instanceData); if (eventReturn != null) { // Get the name of the cycle string cycleName = instanceValue.Value; // Get the name of the cycleEvent (cycleEvent.Value) string cycleEventValue = eventReturn.Value; // Set cycle to stored cycleData object CycleData cycle = storedCycle; if (cycle != null) { // Update Stop Time (running total so that items such as OEE (which is based on cycles) can be updated) cycle.Stop = instanceData.Timestamp; if (cycle.Name != cycleName || cycle.Event != cycleEventValue || !CompareOverrideLists(cycle.CycleOverrides, cycleOverrides)) { cycle.Completed = true; // Add a copy of the current cycle to the list result.Add(cycle.Copy()); // Check if new cycle has been started if (cycle.Name != cycleName || cycleEventValue == cc.StoppedEventValue) { // Set cycle to new instance cycle = new CycleData(); cycle.CycleId = Guid.NewGuid().ToString(); cycle.InstanceId = Guid.NewGuid().ToString(); cycle.Name = cycleName; cycle.Event = cycleEventValue; cycle.Completed = false; // Set Production Type var productionType = cc.ProductionTypes.Find(x => x.EventValue == cycleEventValue); if (productionType != null) cycle.ProductionType = productionType.ProductionType; else cycle.ProductionType = CycleProductionType.UNCATEGORIZED; cycle.Start = instanceData.Timestamp; cycle.Stop = instanceData.Timestamp; cycle.CycleOverrides = cycleOverrides.ToList(); } else { // Set cycle to new Event cycle.InstanceId = Guid.NewGuid().ToString(); cycle.Event = cycleEventValue; cycle.Completed = false; // Set Production Type var productionType = cc.ProductionTypes.Find(x => x.EventValue == cycleEventValue); if (productionType != null) cycle.ProductionType = productionType.ProductionType; else cycle.ProductionType = CycleProductionType.UNCATEGORIZED; cycle.Start = instanceData.Timestamp; cycle.Stop = instanceData.Timestamp; cycle.CycleOverrides = cycleOverrides.ToList(); } } result.Add(cycle); } else { // Set cycle to new instance cycle = new CycleData(); cycle.CycleId = Guid.NewGuid().ToString(); cycle.InstanceId = Guid.NewGuid().ToString(); cycle.Name = cycleName; cycle.Event = cycleEventValue; // Set Production Type var productionType = cc.ProductionTypes.Find(x => x.EventValue == cycleEventValue); if (productionType != null) cycle.ProductionType = productionType.ProductionType; else cycle.ProductionType = CycleProductionType.UNCATEGORIZED; cycle.Start = instanceData.Timestamp; cycle.Stop = instanceData.Timestamp; cycle.CycleOverrides = cycleOverrides.ToList(); result.Add(cycle); } storedCycle = cycle.Copy(); } } } } return result; }
private static CycleOverride GetOverrideFromInstanceData(string overrideId, Instance instanceData) { CycleOverride result = null; var val = instanceData.Values.Find(x => x.Id == overrideId); if (val != null) { double d = -1; if (double.TryParse(val.Value, out d)) { result = new CycleOverride(); result.Id = overrideId; result.Value = d; } } return result; }
private static GeneratedEvent ProcessEvent(Event e, Instance instance) { // Process Event using InstanceData Return eventReturn = e.Process(instance); if (e.PreviousValue == null) e.PreviousValue = e.Default.Copy(); if (e.CurrentValue != null) { e.PreviousValue = e.CurrentValue.Copy(); e.PreviousValue.Id = e.CurrentValue.Id; } if (eventReturn != e.CurrentValue) { e.CurrentValue = eventReturn.Copy(); e.CurrentValue.ChangedTimeStamp = instance.Timestamp; e.CurrentValue.ChangedSequence = instance.Sequence; } e.CurrentValue.TimeStamp = instance.Timestamp; e.CurrentValue.Sequence = instance.Sequence; // Set Duration if (e.CurrentValue.TimeStamp > DateTime.MinValue && e.PreviousValue.TimeStamp > DateTime.MinValue) { eventReturn.Duration = (e.CurrentValue.TimeStamp - e.PreviousValue.TimeStamp).TotalSeconds; } else { eventReturn.Duration = 0; } // Create new GeneratedEvent object var result = new GeneratedEvent(); result.EventName = e.Name; result.PreviousValue = new ValueData(e.PreviousValue); result.CurrentValue = new ValueData(e.CurrentValue); result.CaptureItems.AddRange(e.CurrentValue.CaptureItems); return result; }
private static void ProcessGenerated(Snapshot snapshot, GeneratedEvents.Configuration gec, Instance currentInstanceData, ReturnData currentData) { var e = gec.Events.Find(x => x.Name.ToLower() == snapshot.Link.ToLower()); if (e != null) { Return returnValue = e.Process(currentInstanceData); if (snapshot.Value != returnValue.Value) { if (returnValue != null) { var value = e.Values.Find(x => x.Result.NumVal == returnValue.NumVal); if (value != null) { snapshot.PreviousTimestamp = GetTimestampFromCurrent(value, currentData); } } snapshot.PreviousValue = snapshot.Value; snapshot.Value = returnValue.Value; } snapshot.Timestamp = returnValue.TimeStamp; } }