private static void AddAnomalyToDatabase(SqlConnection sqlConnection, SqlTransaction sqlTransaction, AnomalyServiceResponse anomalyServiceResponseModel, string piServerName, DateTime anomalyDetectionStartTime, DateTime anomalyDetectionEndTime, AlertService alertService) { List <List <string> > filteredAnomalies = anomalyServiceResponseModel.Results.Output1.Value.Values.Where(x => Convert.ToDouble(x.ElementAt(8)) < ConfigurationSettings.AnomalyThreshold).ToList(); string anomalyoutputInsertQuery = "INSERT INTO AnomalyOutput(PowerScout, Temperature,Timestamp,Visibility,days,Breaker_details,kW_System,ScoredLabels,ScoredProbabilities, PiServername) VALUES (@PowerScout,@Temperature,@Timestamp,@Visibility,@days,@Breaker_details,@kW_System,@ScoredLabels,@ScoredProbabilities, @PiServerName)"; foreach (var rowData in filteredAnomalies) { using (SqlCommand cmd = new SqlCommand(anomalyoutputInsertQuery, sqlConnection, sqlTransaction)) { cmd.Parameters.AddWithValue("@PowerScout", rowData.ElementAt(0).ToString()); cmd.Parameters.AddWithValue("@Temperature", Convert.ToDouble(rowData.ElementAt(1))); cmd.Parameters.AddWithValue("@Timestamp", Convert.ToDateTime(rowData.ElementAt(2), CultureInfo.InvariantCulture).ToString(Constants.DATE_TIME_FORMAT)); cmd.Parameters.AddWithValue("@Visibility", Convert.ToDouble(rowData.ElementAt(3))); cmd.Parameters.AddWithValue("@days", rowData.ElementAt(4).ToString()); cmd.Parameters.AddWithValue("@Breaker_details", rowData.ElementAt(5).ToString()); cmd.Parameters.AddWithValue("@kW_System", Convert.ToDouble(rowData.ElementAt(6))); cmd.Parameters.AddWithValue("@ScoredLabels", Convert.ToDouble(rowData.ElementAt(7))); cmd.Parameters.AddWithValue("@ScoredProbabilities", Convert.ToDouble(rowData.ElementAt(8))); cmd.Parameters.AddWithValue("@PiServerName", piServerName); cmd.ExecuteNonQuery(); } } if (filteredAnomalies.Count > 0) { string anomalyAlertMessage = string.Format(Constants.ANOMALY_ALERT_MESSAGE, filteredAnomalies.Count, anomalyDetectionStartTime, anomalyDetectionEndTime); var alert = new Alert { AlertType = Constants.ALERT_TYPE_ANOMALY, Description = anomalyAlertMessage, TimeStamp = anomalyDetectionStartTime, PiServerName = piServerName }; alertService.AddNewAlert(sqlConnection, sqlTransaction, alert); } Console.WriteLine("AnomalyDetectionJob RowInserted : Created alert and anomaly for PiServer - {0}.", piServerName); }
private static void GenerateConsumptionAlertAndNotification(SqlConnection sqlConnection, SqlTransaction sqlTransaction, double consumption, PiServer piServer, bool isWeekEndConsumption = true) { var alertService = new AlertService(); var notificationService = new NotificationService(); double costTobeSaved = consumption * ConfigurationSettings.MeterKwhCost; string alertMessage = string.Format(Constants.RECOMMENDATION_ALERT_MESSAGE, costTobeSaved, isWeekEndConsumption ? "weekend" : "weekday"); var alert = new Alert { AlertType = Constants.ALERT_TYPE_RECOMMENDATION, Description = alertMessage, TimeStamp = piServer.PiServerCurrentDateTime, PiServerName = piServer.PiServerName }; alertService.AddNewAlert(sqlConnection, sqlTransaction, alert); notificationService.SendNotification(alertMessage, sqlConnection, sqlTransaction); Console.WriteLine("ConsumptionAlertJob RowInserted : Created alert and notification for PiServer - {0}.", piServer.PiServerName); }