/// <summary> /// Adds the time types to the alarm model based on the historic alarm passed in. /// </summary> private void AddTimeTypes(HistoricalAlarm alarm, AlarmModel alarmModel) { alarmModel.TimeTypes = new List <TimeType>(); if (alarm.TimeType1 != null) { var tt = PemsEntities.TimeTypes.FirstOrDefault(x => x.TimeTypeId == alarm.TimeType1); if (tt != null) { alarmModel.TimeTypes.Add(new TimeType { Description = tt.TimeTypeDesc, Id = tt.TimeTypeId }); } } if (alarm.TimeType2 != null) { var tt = PemsEntities.TimeTypes.FirstOrDefault(x => x.TimeTypeId == alarm.TimeType2); if (tt != null) { alarmModel.TimeTypes.Add(new TimeType { Description = tt.TimeTypeDesc, Id = tt.TimeTypeId }); } } if (alarm.TimeType3 != null) { var tt = PemsEntities.TimeTypes.FirstOrDefault(x => x.TimeTypeId == alarm.TimeType3); if (tt != null) { alarmModel.TimeTypes.Add(new TimeType { Description = tt.TimeTypeDesc, Id = tt.TimeTypeId }); } } if (alarm.TimeType4 != null) { var tt = PemsEntities.TimeTypes.FirstOrDefault(x => x.TimeTypeId == alarm.TimeType4); if (tt != null) { alarmModel.TimeTypes.Add(new TimeType { Description = tt.TimeTypeDesc, Id = tt.TimeTypeId }); } } if (alarm.TimeType5 != null) { var tt = PemsEntities.TimeTypes.FirstOrDefault(x => x.TimeTypeId == alarm.TimeType5); if (tt != null) { alarmModel.TimeTypes.Add(new TimeType { Description = tt.TimeTypeDesc, Id = tt.TimeTypeId }); } } }
/// <summary> /// Clears an alarm. Creates a historic alarm, maintenance event, and removed the current active alarm from the system. /// </summary> public void ClearAlarm(AlarmModel model) { //get the original alarm item var alarm = PemsEntities.ActiveAlarms.FirstOrDefault( x => x.CustomerID == model.CustomerId && x.AreaID == model.AreaId && x.MeterId == model.AssetID && x.EventCode == model.AlarmCode && x.EventSource == model.EventSource && x.TimeOfOccurrance == model.TimeOccured); if (alarm != null) { //first, we have to check for a work order, and if it doesnt exist, create one so we can link the new historical alarm to the sf maint event. if (!alarm.WorkOrderId.HasValue) { var wOrder = new WorkOrder { AssignedBy = WebSecurity.CurrentUserId, AssignedTS = DateTime.Now, CustomerID = model.CustomerId, TechnicianID = model.TechnicianID }; PemsEntities.WorkOrders.Add(wOrder); PemsEntities.SaveChanges(); //now assign the alarm the newly created work order id alarm.WorkOrderId = wOrder.WorkOrderId; PemsEntities.SaveChanges(); } //now lets calculate the service designation SetTSDS(alarm.CustomerID); var masterId = GetTsdMasterId(model.TimeCleared, alarm.SLADue); //calculate the target service designnation here as well var tds = Tsds.FirstOrDefault(x => x.MasterId == masterId); //we will try to update an existing historical alarm if it already exists, otherwise we will insert a new one. //there is some old data that has duplicates in the historical and active, so this gracefully takes care of that, since all we want to do if the historical alarm exist is update the record with the active alarm information. var ExistinghistoricAlarm = PemsEntities.HistoricalAlarms.FirstOrDefault( x => x.CustomerID == alarm.CustomerID && x.AreaID == alarm.AreaID && x.MeterId == alarm.MeterId && x.EventCode == alarm.EventCode && x.EventSource == alarm.EventSource && x.TimeOfOccurrance == alarm.TimeOfOccurrance && x.EventState == 0); //if it was found, we have to delete the historical item and re-create one, since the eventUID is an identity column, we cant change it, but its not part of the primary key, so it might need to change. //So, we have to try to get the hsitorical alarm, delete it, then try to create a new one. if (ExistinghistoricAlarm != null) { PemsEntities.HistoricalAlarms.Remove(ExistinghistoricAlarm); PemsEntities.SaveChanges(); } //Now we need to create a new one //we have to insert a historical alarm var historicAlarm = new HistoricalAlarm { AlarmUID = alarm.AlarmUID, AreaID = alarm.AreaID, ClearedByUserId = model.ClosedBy, ClearingEventUID = 0, ClosureNote = model.ClosureNotes, CustomerID = alarm.CustomerID, EventCode = alarm.EventCode, EventSource = alarm.EventSource, EventState = 0, EventUID = (int)(alarm.EventUID ?? 0), GlobalMeterId = alarm.GlobalMeterID, MeterId = alarm.MeterId, Notes = alarm.Notes, SLADue = alarm.SLADue, TimeOfClearance = model.TimeCleared, TimeOfNotification = alarm.TimeOfNotification, TimeOfOccurrance = alarm.TimeOfOccurrance, TimeType1 = alarm.TimeType1, TimeType2 = alarm.TimeType2, TimeType3 = alarm.TimeType3, TimeType4 = alarm.TimeType4, TimeType5 = alarm.TimeType5, WorkOrderId = alarm.WorkOrderId }; //now determine the correct TSD Master if (tds != null) { historicAlarm.TargetServiceDesignation = tds.Id; } PemsEntities.HistoricalAlarms.Add(historicAlarm); PemsEntities.SaveChanges(); //we also need ot create a valid SFMeterMAintenanceEvent var maintenanceEvent = new SFMeterMaintenanceEvent { AreaId = alarm.AreaID, CustomerId = alarm.CustomerID, EventDateTime = model.ClosureNotification, GlobalMeterId = alarm.GlobalMeterID, MaintenanceCode = model.ResolutionCode, MeterId = alarm.MeterId, TechnicianId = model.TechnicianID, WorkOrderID = alarm.WorkOrderId }; PemsEntities.SFMeterMaintenanceEvents.Add(maintenanceEvent); PemsEntities.SaveChanges(); //now we must delete the alarm we jsut cleared PemsEntities.ActiveAlarms.Remove(alarm); PemsEntities.SaveChanges(); } }
/// <summary> /// Gets details of a historic alarm /// </summary> private AlarmModel GetHistoricalAlarmDetails(int customerID, HistoricalAlarm alarm) { var alarmModel = new AlarmModel(); if (alarm != null) { //Get the meter map for this alarm var metermap = PemsEntities.MeterMaps.FirstOrDefault(x => x.Areaid == alarm.AreaID && x.Customerid == alarm.CustomerID && x.MeterId == alarm.MeterId); var alarmEventCode = PemsEntities.EventCodes.FirstOrDefault(x => x.EventCode1 == alarm.EventCode && x.CustomerID == customerID); //Asset / Location Information alarmModel.Latitude = alarm.Meter == null ? 0 : alarm.Meter.Latitude ?? 0; alarmModel.Longitude = alarm.Meter == null ? 0 : alarm.Meter.Longitude ?? 0; alarmModel.MeterGroupId = alarm.Meter == null ? 0 : alarm.Meter.MeterGroup ?? 0; alarmModel.AssetClass = alarm.Meter == null ? AssetClass.Unknown : AssetFactory.GetAssetClass(alarm.Meter.MeterGroup); alarmModel.AssetType = alarm.Meter == null ? string.Empty : (new AssetFactory(ConnectionStringName)).GetAssetTypeDescription(alarm.Meter.MeterGroup, customerID); OperationalStatu opStatus = null; //name ans state are based on class if (alarmModel.AssetClass == AssetClass.Sensor) { alarmModel.AssetName = PemsEntities.Sensors.FirstOrDefault(x => x.SensorID == metermap.SensorID) == null ? "" : PemsEntities.Sensors.FirstOrDefault(x => x.SensorID == metermap.SensorID).SensorName; opStatus = PemsEntities.OperationalStatus.FirstOrDefault(x => x.OperationalStatusId == metermap.Sensor.OperationalStatus); alarmModel.OperationalStatus = opStatus == null ? "" : opStatus.OperationalStatusDesc ?? ""; } else if (alarmModel.AssetClass == AssetClass.Gateway) { alarmModel.AssetName = PemsEntities.Gateways.FirstOrDefault(x => x.GateWayID == metermap.GatewayID) == null ? "" : PemsEntities.Gateways.FirstOrDefault(x => x.GateWayID == metermap.GatewayID).Description; opStatus = PemsEntities.OperationalStatus.FirstOrDefault(x => x.OperationalStatusId == metermap.Gateway.OperationalStatus); alarmModel.OperationalStatus = opStatus == null ? "" : opStatus.OperationalStatusDesc ?? ""; } //everything else falls under meter for now else { alarmModel.AssetName = alarm.Meter == null ? "" : alarm.Meter.MeterName; opStatus = PemsEntities.OperationalStatus.FirstOrDefault(x => x.OperationalStatusId == metermap.Meter.OperationalStatusID); alarmModel.OperationalStatus = opStatus == null ? "" : opStatus.OperationalStatusDesc ?? ""; } alarmModel.AssetID = alarm.Meter.MeterId; alarmModel.Area = PemsEntities.Areas.FirstOrDefault(x => x.CustomerID == metermap.Customerid && x.AreaID == metermap.AreaId2) == null ? "" : PemsEntities.Areas.FirstOrDefault(x => x.AreaID == metermap.AreaId2 && x.CustomerID == metermap.Customerid).AreaName; //alarm.Meter.Area.AreaName; alarmModel.AreaId = metermap.AreaId2; // alarm.Meter.Area.AreaID; alarmModel.Zone = metermap.Zone == null ? string.Empty : PemsEntities.Zones.FirstOrDefault(x => x.ZoneId == metermap.ZoneId && metermap.Customerid == x.customerID) == null ? string.Empty : PemsEntities.Zones.FirstOrDefault(x => x.ZoneId == metermap.ZoneId && metermap.Customerid == x.customerID).ZoneName; alarmModel.Suburb = metermap == null ? string.Empty : metermap.CustomGroup11 == null ? string.Empty : metermap.CustomGroup11.DisplayName; alarmModel.Street = alarm.Meter.Location; alarmModel.BaysAffected = alarm.Meter.MaxBaysEnabled ?? 0; //Alarm / Meter information alarmModel.AlarmID = alarm.AlarmUID ?? 0; alarmModel.AlarmCode = alarmEventCode == null ? 0 : alarmEventCode.EventCode1; alarmModel.AlarmDesription = alarmEventCode == null ? string.Empty : alarmEventCode.EventDescAbbrev; alarmModel.AlarmDesriptionVerbose = alarmEventCode == null ? string.Empty : alarmEventCode.EventDescVerbose; alarmModel.ServiceTargetTime = alarm.SLADue ?? DateTime.MinValue; alarmModel.AlarmStatus = "Closed"; var alarmStatus = PemsEntities.AlarmStatus.FirstOrDefault(x => x.AlarmStatusId == 3); if (alarmStatus != null) { alarmModel.AlarmStatus = alarmStatus.AlarmStatusDesc; } alarmModel.TimeNotified = alarm.TimeOfNotification; alarmModel.TimeCleared = alarm.TimeOfClearance ?? DateTime.MinValue; //SLA Values //calculation for historical alarm time remaining: Service Target time - Time Cleared. NOTE, this is different for active alarms. using time cleared instead of current local time //set the defaults alarmModel.TimeRemainingTillTargetTime = 0; alarmModel.TimeRemainingTillTargetTimeDisplay = Constants.TimeFormats.timeFormatToDisplay_HHH_MM; if (alarm.SLADue.HasValue) { if (!String.IsNullOrEmpty(alarm.SLADue.Value.ToString())) { TimeSpan diffTime = (alarm.SLADue.Value - alarm.TimeOfClearance.Value); if (diffTime.TotalHours > 0) { alarmModel.TimeRemainingTillTargetTimeDisplay = FormatHelper.FormatTimeFromMinutes(Math.Round(diffTime.TotalMinutes, 0), (int)PEMSEnums.PEMSEnumsTimeFormats.timeFormat_HHH_MM); } } } else { alarmModel.TimeRemainingTillTargetTimeDisplay = FormatHelper.FormatTimeFromMinutes(0, (int)PEMSEnums.PEMSEnumsTimeFormats.timeFormat_HHH_MM); } //Target Service Designation - note, we are NOT regenerating this, just using what is assigned to the alarmModel.ServiceDesignation = "N/A"; alarmModel.ServiceDesignationId = -1; if (alarm.TargetServiceDesignation1 != null) { alarmModel.ServiceDesignation = alarm.TargetServiceDesignation1.TargetServiceDesignationDesc; alarmModel.ServiceDesignationId = alarm.TargetServiceDesignation1.TargetServiceDesignationId; } alarmModel.TimeOccured = alarm.TimeOfOccurrance; AddTimeTypes(alarm, alarmModel); alarmModel.TimeType1 = alarm.TimeType1 ?? 0; alarmModel.TimeType2 = alarm.TimeType2 ?? 0; alarmModel.TimeType3 = alarm.TimeType3 ?? 0; alarmModel.TimeType4 = alarm.TimeType4 ?? 0; alarmModel.TimeType5 = alarm.TimeType5 ?? 0; alarmModel.AlarmSource = PemsEntities.EventSources.FirstOrDefault(x => x.EventSourceCode == alarm.EventSource).EventSourceDesc; alarmModel.AlarmSeverity = alarmEventCode == null ? string.Empty : alarmEventCode.AlarmTier1 == null ? string.Empty : alarmEventCode.AlarmTier1.TierDesc; alarmModel.EntryNotes = alarm.Notes; alarmModel.CustomerId = customerID; //Closure Information var maintenanceEvent = PemsEntities.SFMeterMaintenanceEvents.FirstOrDefault( x => x.AreaId == alarm.Meter.AreaID && x.CustomerId == customerID && x.MeterId == alarm.MeterId && x.WorkOrderID == alarm.WorkOrderId); alarmModel.ResolutionCode = maintenanceEvent == null ? 1 : maintenanceEvent.MaintenanceCode; alarmModel.ResolutionCodeDesc = maintenanceEvent == null ? "" : PemsEntities.MaintenanceCodes.FirstOrDefault(x => x.MaintenanceCode1 == maintenanceEvent.MaintenanceCode) == null ? "" : PemsEntities.MaintenanceCodes.FirstOrDefault(x => x.MaintenanceCode1 == maintenanceEvent.MaintenanceCode).Description; //RJH: Set ClosureNotification = TimeCleared if no maintenance event. alarmModel.ClosureNotification = maintenanceEvent == null ? alarmModel.TimeCleared : maintenanceEvent.EventDateTime; alarmModel.TechnicianID = maintenanceEvent == null ? -1 : maintenanceEvent.TechnicianId; if (alarmModel.TechnicianID > 0) { alarmModel.TechnicianName = PemsEntities.TechnicianDetails.FirstOrDefault(x => x.TechnicianId == alarmModel.TechnicianID) == null ? "" : PemsEntities.TechnicianDetails.FirstOrDefault( x => x.TechnicianId == alarmModel.TechnicianID).Name; } alarmModel.ClosureNotes = alarm.ClosureNote; alarmModel.ClosedBy = alarm.ClearedByUserId ?? 0; if (alarm.ClearedByUserId != null) { var user = (new UserFactory()).GetUserById(alarm.ClearedByUserId.Value); if (user != null) { alarmModel.ClosedByName = user.Username; } } alarmModel.IsClosed = true; } alarmModel.ResolutionCodes = GetResolutionCodes(); alarmModel.TechnicianIDs = GetAlarmTechnicians(); return(alarmModel); }