Add project files.
This commit is contained in:
475
Database.cs
Normal file
475
Database.cs
Normal file
@@ -0,0 +1,475 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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, Description 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));
|
||||
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, Description) VALUES (@SIID, @Enable, @Description)";
|
||||
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
|
||||
insertCmd.Parameters.AddWithValue("@Enable", item.Enable);
|
||||
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 FSMData: " + e.Message);
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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 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), reader.GetFieldValue<UInt16>(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 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));
|
||||
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
|
||||
{
|
||||
public String SIID { get; set; }
|
||||
public Boolean Enable { get; set; }
|
||||
public String Description { get; set; }
|
||||
public String Value { get; set; }
|
||||
public String LastUpdate { get; set; }
|
||||
public FSMData(String siid, Boolean enable, String description)
|
||||
{
|
||||
this.SIID = siid;
|
||||
this.Enable = enable;
|
||||
this.Description = description;
|
||||
this.Value = "";
|
||||
this.LastUpdate = "";
|
||||
}
|
||||
public FSMData(String siid)
|
||||
{
|
||||
this.SIID = siid;
|
||||
this.Enable = true;
|
||||
this.Description = "";
|
||||
this.Value = "";
|
||||
this.LastUpdate = "";
|
||||
}
|
||||
public FSMData(String siid, Boolean enable)
|
||||
{
|
||||
this.SIID = siid;
|
||||
this.Enable = enable;
|
||||
this.Description = "";
|
||||
this.LastUpdate = "";
|
||||
this.Value = "";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ModbusData
|
||||
{
|
||||
public String SIID { get; set; }
|
||||
public UInt16 Register { get; set; }
|
||||
public String Description { get; set; }
|
||||
|
||||
public String Value { get; set; }
|
||||
public String LastUpdate { get; set; }
|
||||
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 = "";
|
||||
}
|
||||
}
|
||||
|
||||
class VXData
|
||||
{
|
||||
public String SIID { get; set; }
|
||||
public Byte FrameID { get; set; }
|
||||
public Byte CIN { get; set; }
|
||||
public String Description { get; set; }
|
||||
public String Value { get; set; }
|
||||
public String LastUpdate { get; set; }
|
||||
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 = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user