using HMI.Base;
|
using HMI.Core.Entity;
|
using HMI.Core.Util;
|
using HMI.UI.Util;
|
using HMI.Util;
|
using HMI.Util.Alarm;
|
using Microsoft.Win32;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.Data;
|
using System.IO;
|
using System.Windows;
|
using System.Windows.Controls;
|
|
namespace HMI.AlarmInfoManage
|
{
|
/// <summary>
|
/// UserControl1.xaml 的交互逻辑
|
/// </summary>
|
public partial class AlarmInfoManage : PluginControl
|
{
|
private string alarmTextPath;
|
|
|
private string dbPath;
|
|
private SqliteHelper sqliteHelper;
|
|
//private string opcIP = "127.0.0.1";
|
//private int opcPort = 6543;
|
//private int localPort = 18888;
|
//private SCADAApi lTSCADAApi = new OPCClient();
|
|
private ObservableCollection<AlarmText> datasource = new ObservableCollection<AlarmText>();
|
|
public ObservableCollection<AlarmText> Datasource
|
{
|
get { return datasource; }
|
set { Set(ref datasource, value); }
|
}
|
|
/// <summary>
|
/// 报警文本列表
|
/// </summary>
|
private ObservableCollection<AlarmText> alarmTexts = new ObservableCollection<AlarmText>();
|
public ObservableCollection<AlarmText> AlarmTexts
|
{
|
get { return alarmTexts; }
|
set { Set(ref alarmTexts, value); }
|
}
|
|
private double size = 30;
|
|
private int currentPage = 0;
|
public int CurrentPage
|
{
|
get => currentPage;
|
set
|
{
|
Set(ref currentPage, value);
|
RefreshData();
|
}
|
}
|
|
private int maxPage = 0;
|
public int MaxPage
|
{
|
get => maxPage;
|
set
|
{
|
Set(ref maxPage, value);
|
}
|
}
|
|
public AlarmInfoManage()
|
{
|
DataContext = this;
|
InitializeComponent();
|
}
|
|
public override void SetBaseFolder(string folder)
|
{
|
base.SetBaseFolder(folder);
|
dbPath = Path.Combine(BaseFolder, "alarm.db");
|
alarmTextPath = Path.Combine(BaseFolder, "AlarmText.data");
|
}
|
|
public override void InitFinish()
|
{
|
if (!File.Exists(dbPath))
|
{
|
SqliteHelper.CreateDatabase(dbPath);
|
}
|
sqliteHelper = new SqliteHelper(dbPath);
|
sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS Alarm (
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
source TEXT NOT NULL,
|
alarmcode TEXT,
|
alarminfo TEXT,
|
time DATETIME);");
|
|
LoadAlarmText();
|
|
AlarmUtil.OnAlarm += AlarmUtil_OnAlarm;
|
//if (!lTSCADAApi.Connect2(opcIP, opcPort, localPort, out string errMsg))
|
//{
|
//}
|
//else
|
//{
|
// this.MonitorTimer.Start();
|
//}
|
}
|
|
private void AlarmUtil_OnAlarm(object sender, List<AlarmText> e)
|
{
|
foreach (AlarmText alarm in e)
|
{
|
var sql = string.Empty;
|
if (alarm.IsPLCAlarm)
|
{
|
var currentInfo = alarm.AlarmInfo;
|
sql = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');",
|
alarm.Source, alarm.AlarmCode, currentInfo, alarm.Time);
|
}
|
else
|
{
|
sql = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');",
|
alarm.Source, alarm.AlarmCode, alarm.AlarmInfo, alarm.Time);
|
}
|
sqliteHelper.ExecuteSql(sql);
|
}
|
}
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
switch (btn.Name)
|
{
|
case "settingBtn":
|
OpenSettingWindow();
|
break;
|
case "refreshBtn":
|
LoadAlarmText();
|
RefreshPage(CurrentPage);
|
break;
|
case "importBtn":
|
ImportText();
|
break;
|
default:
|
break;
|
}
|
}
|
|
private void OpenSettingWindow()
|
{
|
AlarmSettingList listModal = new AlarmSettingList(log)
|
{
|
Owner = Window.GetWindow(this)
|
};
|
listModal.ShowDialog();
|
}
|
|
private void RefreshPage(int? page = null)
|
{
|
var sql = "SELECT COUNT (*) as num FROM Alarm;";
|
var dt = sqliteHelper.GetDataTable(sql);
|
if (dt.Rows.Count <= 0)
|
{
|
CurrentPage = 1;
|
MaxPage = 1;
|
return;
|
}
|
int count = int.Parse(dt.Rows[0]["num"].ToString());
|
MaxPage = (int)Math.Ceiling(count / size);
|
|
if (page != null)
|
{
|
CurrentPage = page.Value;
|
}
|
else if (CurrentPage > MaxPage)
|
{
|
CurrentPage = MaxPage;
|
}
|
}
|
|
private void RefreshData()
|
{
|
var list = new ObservableCollection<AlarmText>();
|
var sql = $"SELECT source, alarmcode, alarminfo, time FROM Alarm ORDER BY time DESC LIMIT ({CurrentPage - 1} * {size}), {size};";
|
var dt = sqliteHelper.GetDataTable(sql);
|
if (dt.Rows.Count > 0)
|
{
|
foreach (DataRow dr in dt.Rows)
|
{
|
var at = new AlarmText
|
{
|
Source = dr["source"].ToString(),
|
AlarmCode = dr["alarmcode"].ToString(),
|
AlarmInfo = dr["alarminfo"].ToString(),
|
Time = dr["time"].ToString(),
|
};
|
list.Add(at);
|
}
|
}
|
Datasource = list;
|
}
|
|
private void ImportText()
|
{
|
try
|
{
|
OpenFileDialog openDialog = new OpenFileDialog();
|
openDialog.Filter = "Files (*.xlsm)|*.xlsm";
|
if (openDialog.ShowDialog() == true)
|
{
|
this.ImportExcelAlarmText(openDialog.FileName);
|
}
|
}
|
catch (Exception ex)
|
{
|
log.Error("AlarmText Import Failed.", ex);
|
ToastHelper.Error(Window.GetWindow(this), $"{this.lang("App.ImportFailed")}: {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 导入报警代码文本
|
/// </summary>
|
/// <param name="excelFile">电子设计表格文件</param>
|
//private void ImportExcelAlarmText(string excelFile)
|
//{
|
// int index = 0;
|
|
// try
|
// {
|
// if (excelFile.IndexOf(".xlsm") < 0)
|
// {
|
// this.Warning(this.lang("Alm.OnlySupportXlsm"));
|
// return;
|
// }
|
|
// this.alarmTexts.Clear();
|
// XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
|
// XSSFSheet sheet = (XSSFSheet)workbook.GetSheet("sheet1");
|
// XSSFRow row = null;
|
|
// for (int i = 2; i < (sheet.LastRowNum + 1); i++)
|
// {
|
// index = i;
|
// row = (XSSFRow)sheet.GetRow(i);
|
// if (row != null)
|
// {
|
// AlarmText alarm = new AlarmText();
|
// alarm.AlarmCode = row.Cells[0].StringCellValue;
|
// alarm.OpNo = row.Cells[1].StringCellValue;
|
// alarm.ModuleNo = row.Cells[2].StringCellValue;
|
// alarm.ModuleName_CN = row.Cells[3].StringCellValue;
|
// alarm.ModuleName_EN = row.Cells[4].StringCellValue;
|
// alarm.MAlarmCode = row.Cells[5].StringCellValue;
|
// alarm.MAlarmText_CN = row.Cells[6].StringCellValue;
|
// alarm.MAlarmText_EN = row.Cells[7].StringCellValue;
|
// this.alarmTexts.Add(alarm);
|
// }
|
// }
|
|
// if (sheet.LastRowNum > 0)
|
// {
|
// XmlHelper.SerializeToXml(textSettingPath, this.alarmTexts);
|
// }
|
|
// this.Success(this.lang("App.ImportSuccess"));
|
// }
|
// catch (Exception ex)
|
// {
|
// log.Error($"Row {index} AlarmText Import Failed.", ex);
|
// this.Error($"{this.lang("Alm.RowImportFailed", index)}: {ex.Message}");
|
// }
|
//}
|
|
/// <summary>
|
/// 导入报警代码文本
|
/// </summary>
|
/// <param name="excelFile">电子设计表格文件</param>
|
private void ImportExcelAlarmText(string excelFile)
|
{
|
int index = 0;
|
|
try
|
{
|
if (excelFile.IndexOf(".xlsm") < 0)
|
{
|
this.Warning(this.lang("Alm.OnlySupportXlsm"));
|
return;
|
}
|
|
this.alarmTexts = new ObservableCollection<AlarmText>
|
(ExcelHelper.ReadFromExcel<AlarmText>(excelFile));
|
|
if (alarmTexts.Count > 0)
|
{
|
XmlHelper.SerializeToXml(alarmTextPath, this.alarmTexts);
|
}
|
LoadAlarmText();
|
this.Success(this.lang("App.ImportSuccess"));
|
}
|
catch (Exception ex)
|
{
|
log.Error($"Row {index} AlarmText Import Failed.", ex);
|
this.Error($"{this.lang("Alm.RowImportFailed", index)}: {ex.Message}");
|
}
|
}
|
|
private void LoadAlarmText()
|
{
|
try
|
{
|
AlarmTexts = XmlHelper.DeserializeFromXml<ObservableCollection<AlarmText>>(alarmTextPath);
|
}
|
catch (Exception ex)
|
{
|
log.Error("LoadAlarmText Error.", ex);
|
AlarmTexts = new ObservableCollection<AlarmText>();
|
}
|
}
|
|
|
|
private void UserControl_Loaded(object sender, RoutedEventArgs e)
|
{
|
//if (!string.IsNullOrEmpty(systemSetting.PlcIp))
|
//{
|
// if (this.plc.isConnection)
|
// {
|
// this.plc.Simens_S7_ConnectPLC(systemSetting.PlcIp, 102, 0, 1, string.Empty);
|
// }
|
//}
|
RefreshPage(1);
|
Loaded -= UserControl_Loaded;
|
}
|
|
//private void MonitorTimer_Elapsed_bak(object sender, ElapsedEventArgs e)
|
//{
|
// try
|
// {
|
// this.MonitorTimer.Enabled = false;
|
// isHaveAlarm = false;
|
// foreach (var item in signAlarmSetting)
|
// {
|
// //isHaveAlarm = isHaveAlarm ||
|
// // this.plc.Simens_S7_ReadBool_DB(item.DBAddress, item.StartAddress, item.Length);
|
// }
|
|
// ////有报警
|
// if (isHaveAlarm)
|
// {
|
// this.isNewAlarm = false;
|
// this.currentAlarmCachePlc.Clear();
|
// RefreshDBSetting();
|
// for (int i = 0; i < ((int)alarmCodeBytes.Length / 4); i++)
|
// {
|
// int code = S7.GetDIntAt(alarmCodeBytes, 4 * i);
|
// ////有报警
|
// if (code != 0)
|
// {
|
// string acode = string.Format("{0:d10}", code);
|
// alarm = AlarmTexts.FirstOrDefault(item => item.AlarmCode == acode);
|
// if (alarm != null)
|
// {
|
// alarm.Source = "PLC";
|
// alarm.Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
// this.currentAlarmCachePlc.Add(alarm);
|
// ////有新报警
|
// if (this.preAlarmCachePlc.Find(alarm => alarm.AlarmCode == acode) == null)
|
// {
|
// this.isNewAlarm = true;
|
// this.SendLogMsg(alarm.ModuleName_CN + alarm.MAlarmText_CN);
|
// }
|
// }
|
// }
|
// }
|
|
// ////有新报警和报警减少时刷新报警表
|
// if (this.isNewAlarm || (this.currentAlarmCachePlc.Count != this.preAlarmCachePlc.Count))
|
// {
|
// this.currentAlarmCachePlcToMain = XmlHelper.DeepClone(this.currentAlarmCachePlc);
|
// this.RefershAlarm();
|
// }
|
|
// this.preAlarmCachePlc.Clear();
|
// this.preAlarmCachePlc = XmlHelper.DeepClone(this.currentAlarmCachePlc);
|
// }
|
// else
|
// {
|
// if (this.preAlarmCachePlc.Count != 0 || this.currentAlarmCachePlcToMain.Count != 0 || this.currentAlarmCachePlc.Count != 0)
|
// {
|
// this.preAlarmCachePlc.Clear();
|
// this.currentAlarmCachePlcToMain.Clear();
|
// this.currentAlarmCachePlc.Clear();
|
// this.RefershAlarm();
|
// }
|
// }
|
// }
|
// catch (Exception ex)
|
// {
|
// log.Error("sdadsa", ex);
|
// }
|
// finally
|
// {
|
// this.MonitorTimer.Enabled = true;
|
// }
|
//}
|
}
|
}
|