我正在处理警报触发器应用程序,如果警报开/关,我必须插入新记录。 详细说明:
当有闹铃状态进入时,它应检查今天是否有任何报警?如果是,那么今天是否记录了同样的警报? 如果是,则插入新记录。 如果没有则忽略已经记录的并且没有为同一个警报记录。 或者,如果今天没有录制的闹钟,则插入新的Active = true。
但是在同一天录音开启和关闭之后发生了什么,如果再次报警在同一天继续插入新记录,因为在检查现有记录时都会记录开/关。
如果第二次报警进来,我有点无能为力。
代码: D B: 报警表列:
AlarmPosition: it could be 4,23,22
Message: On/Off
IsActive: During insert record inserted as True once the trap sent it changed to false.
TrapCapDate: During insert this date updated.
TrapSentDate: once the trap sent it updated with sent datetime and change the isactive to false.
C#:
public static void InsertSNMPOSAlarm(int alarmId, int alarmPosition, string message, int activationCode, bool isActive)
{
try
{
if (message == THMessage.AlarmOn)
{
//check if on already recorded with today date
if (CheckTodayAlarm(alarmId, alarmPosition, DateTime.Today,THMessage.AlarmOn))
{
//check if off recorded with today date.
if (CheckTodayAlarm(alarmId, alarmPosition, DateTime.Today,THMessage.AlarmOff))
{
//insert new record with today
using (ESEntities context = new ESEntities ())
{
context.AddToSNMPOffshoreAlarms(new SNMPOffshoreAlarm
{
AlarmID = alarmId,
AlarmPosition = alarmPosition,
Message = message,
UpdatedOn = DateTime.Now,
IsActive = isActive,
ActivationCode = activationCode,
TrapCaptureDate = DateTime.Today
});
context.SaveChanges();
}
}
}
else
{
using (ESEntities context = new ESEntities ())
{
context.AddToSNMPOffshoreAlarms(new SNMPOffshoreAlarm
{
AlarmID = alarmId,
AlarmPosition = alarmPosition,
Message = message,
UpdatedOn = DateTime.Now,
IsActive = isActive,
ActivationCode = activationCode,
TrapCaptureDate = DateTime.Today
});
context.SaveChanges();
}
}
}
else if (message == THMessage.AlarmOff)
{
if (CheckTodayAlarm(alarmId, alarmPosition, DateTime.Today,THMessage.AlarmOn))
{
if (!CheckTodayAlarm(alarmId, alarmPosition, DateTime.Today, THMessage.AlarmOff))
{
//insert new record with today
using (ESEntities context = new ESEntities ())
{
context.AddToSNMPOffshoreAlarms(new SNMPOffshoreAlarm
{
AlarmID = alarmId,
AlarmPosition = alarmPosition,
Message = message,
UpdatedOn = DateTime.Now,
IsActive = isActive,
ActivationCode = activationCode,
TrapCaptureDate = DateTime.Today
});
context.SaveChanges();
}
}
}
}
}
catch (System.Exception ex)
{
LogError(ex, "SNMPDB", "InsertSNMPOffShoreAlarm");
}
}
private static bool CheckTodayAlarm(int alarmid, int alarmPosition, DateTime trapCaptureDate,string alarmMsg)
{
bool exists = false;
try
{
using (ESEntities context = new ESEntities ())
{
var query = context.SNMPOffshoreAlarms.Where(p => p.AlarmID == alarmid &&
p.AlarmPosition == alarmPosition && p.TrapCaptureDate == trapCaptureDate
&& p.Message == alarmMsg && p.IsActive == true).FirstOrDefault();
if (query != null)
exists = true;
}
}
catch (System.Exception ex)
{
LogError(ex, "SNMPDB", "CheckTodayAlarm");
}
return exists;
}