753 lines
25 KiB
C#
753 lines
25 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data.SQLite;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FAtoPA
|
|
{
|
|
class Database
|
|
{
|
|
String connectionString = "Data Source=database.db";
|
|
|
|
/// <summary>
|
|
/// Create Database Object
|
|
/// </summary>
|
|
public Database()
|
|
{
|
|
|
|
CreateFSMTable();
|
|
CreateModbusTable();
|
|
CreateVXTable();
|
|
}
|
|
|
|
private bool CreateFSMTable()
|
|
{
|
|
//Debug.WriteLine("About to execute CreateFSMTable");
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
var createtablecmd = connection.CreateCommand();
|
|
createtablecmd.CommandText = "CREATE TABLE IF NOT EXISTS FsmData (SIID TEXT PRIMARY KEY, Enable INTEGER, Label TEXT, Type TEXT)";
|
|
createtablecmd.ExecuteNonQuery();
|
|
Debug.WriteLine("CreateFSMTable success");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("Error CreateFSMTable, Exception : " + ex.Message);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public bool ClearFSMTable() {
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
var deleteCmd = connection.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM FsmData";
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting FSMData: " + e.Message);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public List<FSMData> GetFSMDatas()
|
|
{
|
|
List<FSMData> fsmDatas = new List<FSMData>();
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
var selectCmd = connection.CreateCommand();
|
|
selectCmd.CommandText = "SELECT * FROM FsmData";
|
|
using (var reader = selectCmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
|
|
FSMData fsmData = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
|
|
fsmDatas.Add(fsmData);
|
|
}
|
|
}
|
|
}
|
|
return fsmDatas;
|
|
}
|
|
|
|
public bool AddFSMData(params FSMData[] data)
|
|
{
|
|
using(var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
foreach (var item in data)
|
|
{
|
|
var insertCmd = connection.CreateCommand();
|
|
insertCmd.CommandText = "INSERT INTO FsmData (SIID, Enable, Label, Type) VALUES (@SIID, @Enable, @Label, @Type)";
|
|
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
|
|
insertCmd.Parameters.AddWithValue("@Enable", item.Enable);
|
|
insertCmd.Parameters.AddWithValue("@Label", item.Label);
|
|
insertCmd.Parameters.AddWithValue("@Type", item.Type);
|
|
try
|
|
{
|
|
int result = insertCmd.ExecuteNonQuery();
|
|
if (result <= 0)
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error inserting FSMData: " + e.Message);
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public FSMData FSMDataHaveSIID(String SIID)
|
|
{
|
|
if (SIID!=null && SIID.Length > 0)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var findCmd = conn.CreateCommand();
|
|
findCmd.CommandText = "SELECT FROM FsmData WHERE SIID = @SIID";
|
|
findCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
using (var reader = findCmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
FSMData result = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public bool RemoveFSMDatabySIID(String SIID)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var deleteCmd = conn.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM FsmData WHERE SIID = @SIID";
|
|
deleteCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting FSMData: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool CreateModbusTable()
|
|
{
|
|
//Debug.WriteLine("About to execute CreateModbusTable");
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
var modbuscmd = connection.CreateCommand();
|
|
modbuscmd.CommandText = "CREATE TABLE IF NOT EXISTS ModbusData (SIID TEXT PRIMARY KEY, Register INTEGER, Description TEXT)";
|
|
modbuscmd.ExecuteNonQuery();
|
|
Debug.WriteLine("CreateModbusTable success");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("Error CreateModbusTable, Exception : " + ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool ClearModbusTable()
|
|
{
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
var deleteCmd = connection.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM ModbusData";
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting ModbusData: " + e.Message);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Boolean AddModbusData(params ModbusData[] data)
|
|
{
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
foreach (var item in data)
|
|
{
|
|
var insertCmd = connection.CreateCommand();
|
|
insertCmd.CommandText = "INSERT INTO ModbusData (SIID, Register, Description) VALUES (@SIID, @Register, @Description)";
|
|
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
|
|
insertCmd.Parameters.AddWithValue("@Register", item.Register);
|
|
insertCmd.Parameters.AddWithValue("@Description", item.Description);
|
|
try
|
|
{
|
|
int result = insertCmd.ExecuteNonQuery();
|
|
if (result <= 0)
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error inserting ModbusData: " + e.Message);
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public ModbusData ModbusDataHaveSIID(String SIID)
|
|
{
|
|
if (SIID != null && SIID.Length > 0)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var findCmd = conn.CreateCommand();
|
|
findCmd.CommandText = "SELECT FROM ModbusData WHERE SIID = @SIID";
|
|
findCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
using (var reader = findCmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
ModbusData result = new ModbusData(reader.GetString(0),(ushort) reader.GetInt16(1), reader.GetString(2));
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<ModbusData> GetModbusDatas()
|
|
{
|
|
List<ModbusData> modbusDatas = new List<ModbusData>();
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
var selectCmd = connection.CreateCommand();
|
|
selectCmd.CommandText = "SELECT * FROM ModbusData";
|
|
using (var reader = selectCmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
ModbusData modbusdata = new ModbusData(reader.GetString(0), (ushort) reader.GetInt16(1), reader.GetString(2));
|
|
modbusDatas.Add(modbusdata);
|
|
}
|
|
}
|
|
}
|
|
return modbusDatas;
|
|
}
|
|
|
|
public bool RemoveModbusDatabySIID(String SIID)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var deleteCmd = conn.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM ModbusData WHERE SIID = @SIID";
|
|
deleteCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting ModbusData: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool CreateVXTable()
|
|
{
|
|
//Debug.WriteLine("About to execute CreateVXTable");
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
var vxlancmd = connection.CreateCommand();
|
|
vxlancmd.CommandText = "CREATE TABLE IF NOT EXISTS VxTable (SIID TEXT PRIMARY KEY, FrameID INTEGER, CIN INTEGER, Description TEXT)";
|
|
vxlancmd.ExecuteNonQuery();
|
|
Debug.WriteLine("CreateVXTable success");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("Error CreateVXTable, Exception : " + ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool ClearVXTable()
|
|
{
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
var deleteCmd = connection.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM VxTable";
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting VXData: " + e.Message);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public VXData VXDataHaveSIID(String SIID)
|
|
{
|
|
if (SIID != null && SIID.Length > 0)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var findCmd = conn.CreateCommand();
|
|
findCmd.CommandText = "SELECT FROM VxTable WHERE SIID = @SIID";
|
|
findCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
using (var reader = findCmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
VXData result = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<VXData> GetVXDatas()
|
|
{
|
|
List<VXData> vxDatas = new List<VXData>();
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
var selectCmd = connection.CreateCommand();
|
|
selectCmd.CommandText = "SELECT * FROM VxTable";
|
|
using (var reader = selectCmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
VXData vxdata = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
|
|
vxDatas.Add(vxdata);
|
|
}
|
|
}
|
|
}
|
|
return vxDatas;
|
|
}
|
|
|
|
public bool RemoveVXDatabySIID(String SIID)
|
|
{
|
|
using (var conn = new SQLiteConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
var deleteCmd = conn.CreateCommand();
|
|
deleteCmd.CommandText = "DELETE FROM VxTable WHERE SIID = @SIID";
|
|
deleteCmd.Parameters.AddWithValue("@SIID", SIID);
|
|
try
|
|
{
|
|
int result = deleteCmd.ExecuteNonQuery();
|
|
return (result > 0);
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error deleting VXData: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool AddVXData(params VXData[] data) {
|
|
using(var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
foreach (var item in data)
|
|
{
|
|
var insertCmd = connection.CreateCommand();
|
|
insertCmd.CommandText = "INSERT INTO VxTable (SIID, FrameID, CIN, Description) VALUES (@SIID, @FrameID, @CIN, @Description)";
|
|
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
|
|
insertCmd.Parameters.AddWithValue("@FrameID", item.FrameID);
|
|
insertCmd.Parameters.AddWithValue("@CIN", item.CIN);
|
|
insertCmd.Parameters.AddWithValue("@Description", item.Description);
|
|
try
|
|
{
|
|
int result = insertCmd.ExecuteNonQuery();
|
|
if (result <= 0)
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
catch (SQLiteException e)
|
|
{
|
|
Debug.WriteLine("Error inserting VXData: " + e.Message);
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class FSMData : INotifyPropertyChanged
|
|
{
|
|
private String _siid;
|
|
public String SIID
|
|
{
|
|
get => _siid;
|
|
set
|
|
{
|
|
if (_siid != value)
|
|
{
|
|
_siid = value;
|
|
OnPropertyChanged(nameof(SIID));
|
|
}
|
|
}
|
|
}
|
|
private Boolean _enable;
|
|
public Boolean Enable
|
|
{
|
|
get => _enable;
|
|
set
|
|
{
|
|
if (_enable != value)
|
|
{
|
|
_enable = value;
|
|
OnPropertyChanged(nameof(Enable));
|
|
}
|
|
}
|
|
}
|
|
private String _label;
|
|
public String Label {
|
|
get => _label;
|
|
set {
|
|
if (_label != value)
|
|
{
|
|
_label = value;
|
|
OnPropertyChanged(nameof(Label));
|
|
}
|
|
}
|
|
}
|
|
private String _type;
|
|
public String Type
|
|
{
|
|
get => _type;
|
|
set
|
|
{
|
|
if (_type != value)
|
|
{
|
|
_type = value;
|
|
OnPropertyChanged(nameof(Type));
|
|
}
|
|
}
|
|
}
|
|
private String _value;
|
|
public String Value {
|
|
get => _value;
|
|
set
|
|
{
|
|
if (_value != value)
|
|
{
|
|
_value = value;
|
|
OnPropertyChanged(nameof(Value));
|
|
}
|
|
}
|
|
}
|
|
private String _lastupdate;
|
|
public String LastUpdate {
|
|
get => _lastupdate;
|
|
set {
|
|
if (_lastupdate != value)
|
|
{
|
|
_lastupdate = value;
|
|
OnPropertyChanged(nameof(LastUpdate));
|
|
}
|
|
}
|
|
}
|
|
public FSMData(String siid, Boolean enable, String label, String type)
|
|
{
|
|
this.SIID = siid;
|
|
this.Enable = enable;
|
|
this.Label = label;
|
|
this.Type = type;
|
|
this.Value = "";
|
|
this.LastUpdate = "";
|
|
}
|
|
|
|
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
}
|
|
|
|
class ModbusData : INotifyPropertyChanged
|
|
{
|
|
private String _siid;
|
|
public String SIID
|
|
{
|
|
get => _siid;
|
|
set
|
|
{
|
|
if (_siid != value)
|
|
{
|
|
_siid = value;
|
|
OnPropertyChanged(nameof(SIID));
|
|
}
|
|
}
|
|
}
|
|
private UInt16 _register;
|
|
public UInt16 Register
|
|
{
|
|
get => _register;
|
|
set
|
|
{
|
|
if (_register != value)
|
|
{
|
|
_register = value;
|
|
OnPropertyChanged(nameof(Register));
|
|
}
|
|
}
|
|
}
|
|
private String _description;
|
|
public String Description
|
|
{
|
|
get => _description;
|
|
set
|
|
{
|
|
if (_description != value)
|
|
{
|
|
_description = value;
|
|
OnPropertyChanged(nameof(Description));
|
|
}
|
|
}
|
|
}
|
|
private String _value;
|
|
public String Value
|
|
{
|
|
get => _value;
|
|
set
|
|
{
|
|
if (_value != value)
|
|
{
|
|
_value = value;
|
|
OnPropertyChanged(nameof(Value));
|
|
}
|
|
}
|
|
}
|
|
private String _lastupdate;
|
|
public String LastUpdate
|
|
{
|
|
get => _lastupdate;
|
|
set
|
|
{
|
|
if (_lastupdate != value)
|
|
{
|
|
_lastupdate = value;
|
|
OnPropertyChanged(nameof(LastUpdate));
|
|
}
|
|
}
|
|
}
|
|
public ModbusData(String siid, UInt16 register, String description)
|
|
{
|
|
this.SIID = siid;
|
|
this.Register = register;
|
|
this.Description = description;
|
|
this.Value = "";
|
|
this.LastUpdate = "";
|
|
}
|
|
public ModbusData(String siid, UInt16 register)
|
|
{
|
|
this.SIID = siid;
|
|
this.Register = register;
|
|
this.Description = siid + " To " + register;
|
|
this.Value = "";
|
|
this.LastUpdate = "";
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
|
|
class VXData : INotifyPropertyChanged
|
|
{
|
|
private String _siid;
|
|
public String SIID
|
|
{
|
|
get => _siid;
|
|
set
|
|
{
|
|
if (_siid != value)
|
|
{
|
|
_siid = value;
|
|
OnPropertyChanged(nameof(SIID));
|
|
}
|
|
}
|
|
}
|
|
private Byte _frameid;
|
|
public Byte FrameID
|
|
{
|
|
get => _frameid;
|
|
set
|
|
{
|
|
if (_frameid != value)
|
|
{
|
|
_frameid = value;
|
|
OnPropertyChanged(nameof(FrameID));
|
|
}
|
|
}
|
|
}
|
|
private Byte _cin;
|
|
public Byte CIN
|
|
{
|
|
get => _cin;
|
|
set
|
|
{
|
|
if (_cin != value)
|
|
{
|
|
_cin = value;
|
|
OnPropertyChanged(nameof(CIN));
|
|
}
|
|
}
|
|
}
|
|
private String _description;
|
|
public String Description
|
|
{
|
|
get => _description;
|
|
set
|
|
{
|
|
if (_description != value)
|
|
{
|
|
_description = value;
|
|
OnPropertyChanged(nameof(Description));
|
|
}
|
|
}
|
|
}
|
|
private String _value;
|
|
public String Value
|
|
{
|
|
get => _value;
|
|
set
|
|
{
|
|
if (_value != value)
|
|
{
|
|
_value = value;
|
|
OnPropertyChanged(nameof(Value));
|
|
}
|
|
}
|
|
}
|
|
private String _lastupdate;
|
|
public String LastUpdate
|
|
{
|
|
get => _lastupdate;
|
|
set
|
|
{
|
|
if (_lastupdate != value)
|
|
{
|
|
_lastupdate = value;
|
|
OnPropertyChanged(nameof(LastUpdate));
|
|
}
|
|
}
|
|
}
|
|
public VXData(String siid, Byte frameid, Byte cin, String Description)
|
|
{
|
|
this.SIID = siid;
|
|
this.FrameID = frameid;
|
|
this.CIN = cin;
|
|
this.Description = Description;
|
|
this.Value = "";
|
|
this.LastUpdate = "";
|
|
}
|
|
public VXData(String siid, Byte frameid, Byte cin)
|
|
{
|
|
this.SIID = siid;
|
|
this.FrameID = frameid;
|
|
this.CIN = cin;
|
|
this.Description = siid + " To " + frameid + "." + cin;
|
|
this.Value = "";
|
|
this.LastUpdate = "";
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
}
|