/// <summary> /// Compares the specified current value. /// </summary> /// <param name="currentValue">The current value.</param> /// <param name="compareValue">The compare value.</param> /// <param name="compareType">Type of the compare.</param> /// <param name="operation">The operation.</param> /// <returns></returns> private bool Compare(object currentValue, object compareValue, WatchFieldType compareType, CompareOperation operation) { switch (compareType) { case WatchFieldType.DateTime: try { var d1 = Convert.ToDateTime(currentValue); var d2 = Convert.ToDateTime(compareValue); if (operation == CompareOperation.More) { return(d1 >= d2); } else if (operation == CompareOperation.Less) { return(d1 <= d2); } else { return(d1.Equals(d2)); } } catch (Exception ex) { log.Debug("Error during Datetime compare.", ex); } break; case WatchFieldType.Double: try { var d1 = Convert.ToDouble(currentValue); var d2 = Convert.ToDouble(compareValue); if (operation == CompareOperation.More) { return(d1 >= d2); } else if (operation == CompareOperation.Less) { return(d1 <= d2); } else { return(d1.Equals(d2)); } } catch (Exception ex) { log.Debug("Error during Double compare.", ex); } break; case WatchFieldType.Integer: try { var i1 = Convert.ToInt32(currentValue); var i2 = Convert.ToInt32(compareValue); if (operation == CompareOperation.More) { return(i1 >= i2); } else if (operation == CompareOperation.Less) { return(i1 <= i2); } else { return(i1.Equals(i2)); } } catch (Exception ex) { log.Debug("Error during Integer compare.", ex); } break; case WatchFieldType.Boolean: return(currentValue.Equals(compareValue)); case WatchFieldType.String: return(currentValue.Equals(compareValue)); default: break; } return(false); }
/// <summary> /// Executes the notification check. /// </summary> /// <param name="currentValue">The current value.</param> private void ExecuteNotificationCheck(object currentValue) { bool isNotify = false; Notification notifObj = null; //Check if notification is configures with watch parameter if (activWatchRule.WatchParameter != null) { var ruleType = activWatchRule.WatchField.Type; if (ruleType == WatchFieldType.DateTime || ruleType == WatchFieldType.Integer || ruleType == WatchFieldType.Double) { WatchFieldType compareType = WatchFieldType.Integer; #region Max //check if currentValue is max if (activWatchRule.WatchParameter.Max != null) { object compareValue; //shorter var max = activWatchRule.WatchParameter.Max; //check if a max watchfield value is defined otherwise use single value if (max.WatchField != null) { try { compareValue = ReadValue(max.WatchField); } catch (InvalidOperationException ioex) { log.Debug(String.Format("Error during the logging process of a Watch Value. Details: {0}", ioex.Message), ioex); return; } compareType = max.WatchField.Type; } else if (max.SingleValue != null) { compareValue = max.SingleValue.Value; compareType = max.SingleValue.Type; } else { return; } // currentValue >= compareValue -> NOTIFY isNotify = Compare(currentValue, compareValue, compareType, CompareOperation.More); if (isNotify) { notifObj = new Notification { LastValue = currentValue.ToString(), Message = String.Format("The watched field {0}.{1} reached the defined maximum {2} at {3}", activWatchRule.WatchField.WatchProperty, activWatchRule.WatchField.WatchObject, compareValue, DateTime.Now), Type = "MaxReached", WatchField = activWatchRule.WatchField, WatchRuleName = activWatchRule.Name }; } } #endregion #region Min //check if currentValue is min if (activWatchRule.WatchParameter.Min != null) { object compareValue; var min = activWatchRule.WatchParameter.Min; if (min.WatchField != null) { try { compareValue = ReadValue(min.WatchField); } catch (InvalidOperationException ioex) { log.Debug(String.Format("Error during the logging process of a Watch Value. Details: {0}", ioex.Message), ioex); return; } compareType = min.WatchField.Type; } else if (min.SingleValue != null) { compareValue = min.SingleValue.Value; compareType = min.SingleValue.Type; } else { return; } // currentValue <= compareValue -> NOTIFIY isNotify = Compare(currentValue, compareValue, compareType, CompareOperation.Less); if (isNotify) { notifObj = new Notification { LastValue = currentValue.ToString(), Message = String.Format("The watched field {0}.{1} reached the defined minimum {2} at {3}", activWatchRule.WatchField.WatchProperty, activWatchRule.WatchField.WatchObject, compareValue, DateTime.Now), Type = "MinReached", WatchField = activWatchRule.WatchField, WatchRuleName = activWatchRule.Name }; } } #endregion } else { log.Debug("The Max and Min Compare function can only be used with numeric compareable datatypes."); } #region Value //check if currentValue is value if (activWatchRule.WatchParameter.Value != null) { WatchFieldType compareType = WatchFieldType.String; object compareValue; var value = activWatchRule.WatchParameter.Value; //check if a watchfield value is defined otherwise use single value if (value.WatchField != null) { try { compareValue = ReadValue(value.WatchField); } catch (InvalidOperationException ioex) { log.Debug(String.Format("Error during the logging process of a Watch Value. Details: {0}", ioex.Message), ioex); return; } compareType = value.WatchField.Type; } else if (value.SingleValue != null) { compareValue = value.SingleValue.Value; compareType = value.SingleValue.Type; } else { return; } // currentValue == compareValue -> NOTIFY isNotify = Compare(currentValue, compareValue, compareType, CompareOperation.Equal); if (isNotify) { notifObj = new Notification { LastValue = currentValue.ToString(), Message = String.Format("The watched field {0}.{1} reached the defined value {2} at {3}", activWatchRule.WatchField.WatchProperty, activWatchRule.WatchField.WatchObject, compareValue, DateTime.Now), Type = "ValueReached", WatchField = activWatchRule.WatchField, WatchRuleName = activWatchRule.Name }; } } #endregion if (isNotify && lastNotification.AddMinutes(5) <= DateTime.Now) { WatchNotificationOccuredHandler(notifObj); lastNotification = DateTime.Now; } } else { log.DebugFormat("For the rule {0} the notification is activated but no WatchParamaters are configured.", activWatchRule.Name); } }