126 lines
3.2 KiB
C#
126 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MoviconHub.App.Models
|
|
{
|
|
/// <summary>
|
|
/// 报警模型
|
|
/// 用于保存报警信息
|
|
/// 的数据模型
|
|
/// </summary>
|
|
public class AlarmModel
|
|
{
|
|
private readonly IFreeSql _fsql;
|
|
private bool _isActive;
|
|
private DateTime _startTime;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="fsql">FreeSql实例</param>
|
|
public AlarmModel(IFreeSql fsql)
|
|
{
|
|
_fsql = fsql;
|
|
_isActive = false;
|
|
DeviceCode = string.Empty;
|
|
DeviceName = string.Empty;
|
|
AlarmMessage = string.Empty;
|
|
DeviceState = 0;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设备编号
|
|
/// </summary>
|
|
public string DeviceCode { get; set; }
|
|
|
|
/// <summary>
|
|
/// 设备名称
|
|
/// </summary>
|
|
public string DeviceName { get; set; }
|
|
|
|
/// <summary>
|
|
/// 报警信息
|
|
/// </summary>
|
|
public string AlarmMessage { get; set; }
|
|
|
|
/// <summary>
|
|
/// 设备状态码
|
|
/// </summary>
|
|
public int DeviceState { get; set; }
|
|
|
|
/// <summary>
|
|
/// Index
|
|
/// </summary>
|
|
public int Index { get; set; }
|
|
|
|
/// <summary>
|
|
/// 报警是否激活
|
|
/// </summary>
|
|
public bool IsActive
|
|
{
|
|
get => _isActive;
|
|
set
|
|
{
|
|
// 如果报警状态从激活变为不激活,保存报警记录
|
|
if (_isActive && !value)
|
|
{
|
|
SaveAlarmRecord();
|
|
}
|
|
// 如果报警状态从不激活变为激活,记录开始时间
|
|
else if (!_isActive && value)
|
|
{
|
|
_startTime = DateTime.Now;
|
|
}
|
|
|
|
_isActive = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新设备码数据
|
|
/// </summary>
|
|
public void UpdateDeviceInfo(string deviceName, string DeviceCode)
|
|
{
|
|
this.DeviceName = deviceName;
|
|
this.DeviceCode= DeviceCode;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 保存报警记录到数据库
|
|
/// </summary>
|
|
private void SaveAlarmRecord()
|
|
{
|
|
try
|
|
{
|
|
var endTime = DateTime.Now;
|
|
|
|
// 创建设备报警记录
|
|
var deviceAlarm = new DeviceAlarm
|
|
{
|
|
DeviceCode = this.DeviceCode,
|
|
DeviceName = this.DeviceName,
|
|
DeviceState = this.DeviceState,
|
|
AlarmMessage = this.AlarmMessage,
|
|
StartTime = _startTime,
|
|
EndTime = endTime
|
|
};
|
|
|
|
// 保存到数据库
|
|
_fsql.Insert(deviceAlarm).ExecuteAffrows();
|
|
|
|
Console.WriteLine($"报警记录已保存: {DeviceCode}, 开始时间: {_startTime}, 结束时间: {endTime}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"保存报警记录失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|