457 lines
20 KiB
C#
457 lines
20 KiB
C#
using CommunityToolkit.Mvvm;
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using SunlightAggregationManager.UserClass;
|
||
using SunlightAggregationManager.View;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Net;
|
||
using System.Reflection.PortableExecutable;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Xml.Linq;
|
||
|
||
namespace SunlightAggregationManager.ViewModel
|
||
{
|
||
public partial class MainWindowViewModel : ObservableObject
|
||
{
|
||
//调用配置文件
|
||
private static UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Configini.ini");
|
||
public static string SQLIP=""; //读配置文件
|
||
public static string SQLNAME="";
|
||
public static string SQMOD= "";
|
||
public static string SQLUSER = "";
|
||
public static string SQLPASWORD = "";
|
||
public static string MachineName = "SERVER";
|
||
public static string MachineVer = "1.0.0";
|
||
public static string MachineIP = "";
|
||
public static int MachineIPP1 = 7789;
|
||
public static int MachineIPP2 = 7790;
|
||
public static int MachineHTTPP = 7791;
|
||
public static string Enterprise = "";
|
||
public static string TCP_E = "0";
|
||
public static string HTTP_E = "0";
|
||
public static string TLS_E = "0";
|
||
public static string SERVERIP = "";
|
||
public static string SERVERNAME = "";
|
||
public static string SERVERUSER = "";
|
||
public static string SERRVERPASWORD = "";
|
||
public static int ChemicalNumber, DyeMeasurementNumber, DyeDissolveNumber, PowderNumber;
|
||
|
||
private static Timer? _timer;
|
||
public static DataTable ChemicalStatistics = new DataTable();
|
||
public static DataTable DBC_Machines = new DataTable();
|
||
public static DataBase dataBase = new DataBase();
|
||
public static UserClass.SqliteHelper SQLiteHelpers = null!; //定义数据库
|
||
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SunlightAggregationManager.db"; //数据库路径
|
||
public static DataTable ActionLog = new DataTable();
|
||
[ObservableProperty]
|
||
public static DataTable _machines = new DataTable(); //设备表缓存
|
||
[ObservableProperty]
|
||
public static DataTable _userData = new DataTable();
|
||
[ObservableProperty]
|
||
public static DataTable _service = new DataTable();
|
||
|
||
public MainWindowViewModel()
|
||
{
|
||
SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
||
SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
||
SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3");
|
||
SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
||
SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
||
MachineName = Configini.IniReadvalue("SYS", "Name");
|
||
Enterprise = Configini.IniReadvalue("SYS", "Enterprise");
|
||
TCP_E = Configini.IniReadvalue("NETWORK", "TCP");
|
||
HTTP_E = Configini.IniReadvalue("NETWORK", "HTTP");
|
||
TLS_E = Configini.IniReadvalue("NETWORK", "TLS");
|
||
SERVERIP = Configini.IniReadvalue("SERVER", "SERVERIP");
|
||
SERVERNAME = Configini.IniReadvalue("SERVER", "SERVERNAME");
|
||
SERVERUSER = Configini.IniReadvalue("SERVER", "SERVERUSER");
|
||
SERRVERPASWORD = Configini.IniReadvalue("SERVER", "SERRVERPASWORD");
|
||
|
||
//本地数据库(sqlite)
|
||
try
|
||
{
|
||
SQLiteHelpers = new UserClass.SqliteHelper(DBAddress); //数据库连接路径
|
||
SQLiteHelpers.Open(); //打开数据库
|
||
UserData = SQLiteHelpers.ExecuteDataSet("select * from USER order by UserID asc", null)?.Tables[0] ?? new DataTable();
|
||
Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines order by ID asc", null)?.Tables[0] ?? new DataTable();
|
||
ActionLog = (SQLiteHelpers.ExecuteDataSet("select * from ActionLog", null)?.Tables[0] ?? new DataTable()).Clone();
|
||
// SQLiteHelpers.Close();
|
||
DataRow[] dataRows = UserData.Select("State<90");
|
||
foreach (DataRow row in dataRows)
|
||
{
|
||
row["State"] = 0;
|
||
row["LinkID"] = 0;
|
||
}
|
||
|
||
Machines.RowChanged += Machines_Updata;
|
||
Machines.RowDeleting += Machines_Updata;
|
||
UserData.RowChanged += UserData_Updata;//注册userdata表更新事件
|
||
UserData.RowDeleting += UserData_Updata;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
|
||
//服务信息表
|
||
//DataTable Service = new DataTable();
|
||
Service.BeginInit();
|
||
Service.Columns.Add("ID", typeof(int));
|
||
Service.Columns.Add("Status", typeof(string));
|
||
Service.Columns.Add("Service", typeof(string));
|
||
|
||
//运行数据库
|
||
Service.Rows.Add(0, "RUN", "SQL_DataBase_Link");
|
||
dataBase.Config(SQLIP, SQLNAME, SQMOD, SQLUSER, SQLPASWORD);
|
||
|
||
if (TCP_E == "1")//启动tcp(内网直连)
|
||
{
|
||
try
|
||
{
|
||
MachineIPP1 = int.Parse(Configini.IniReadvalue("NETWORK", "TCP_PORT1"));
|
||
}
|
||
catch (Exception) { MachineIPP1 = 7789; }
|
||
try
|
||
{
|
||
MachineIPP2 = int.Parse(Configini.IniReadvalue("NETWORK", "TCP_PORT2"));
|
||
}
|
||
catch (Exception) { MachineIPP2 = 7790; }
|
||
|
||
Service.Rows.Add(1, "RUN", "TCP");
|
||
AsyncTcpServer.Configuration(MachineIPP1, MachineIPP2);
|
||
}
|
||
else
|
||
{
|
||
Service.Rows.Add(1, "STOP", "TCP");
|
||
}
|
||
if (HTTP_E == "1")//启动tcp(内网直连)
|
||
{
|
||
try
|
||
{
|
||
MachineHTTPP = int.Parse(Configini.IniReadvalue("NETWORK", "HTTP_PORT"));
|
||
}
|
||
catch (Exception) { MachineHTTPP = 7791; }
|
||
|
||
Service.Rows.Add(2, "RUN", "HTTP");
|
||
AsyncHttpServer.Configuration(MachineHTTPP);
|
||
}
|
||
else
|
||
{
|
||
Service.Rows.Add(2, "STOP", "HTTP");
|
||
}
|
||
|
||
//获取当前IP
|
||
MachineIP = GetLocalIP.GetLocalIPv4Address().ToString();
|
||
Console.WriteLine(MachineIP);
|
||
//连接sunlight服务器
|
||
Service.Rows.Add(3, "RUN", "Sunlight_Server_Link");
|
||
AsyncTcpClient.Configuration(SERVERIP + ":7795", SERVERNAME, SERVERUSER, SERRVERPASWORD);
|
||
|
||
Service.EndInit();
|
||
|
||
|
||
_timer = new Timer(TimeTask, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30));
|
||
}
|
||
|
||
//Machines操作数据库
|
||
private void Machines_Updata(object sender, DataRowChangeEventArgs e)
|
||
{
|
||
Dictionary<string, object> row_entity = new Dictionary<string, object>();
|
||
Dictionary<string, object> userlog = new Dictionary<string, object>();
|
||
|
||
if (e.Action == DataRowAction.Change)
|
||
{
|
||
//修改的行回传
|
||
foreach (DataColumn col in Machines.Columns)
|
||
{
|
||
row_entity.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
|
||
if (ActionLog.Columns.Contains(col.ColumnName))//添加到行为记录表
|
||
{
|
||
userlog.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
}
|
||
}
|
||
SQLiteHelpers.Update("Machines", row_entity, "ID='" + e.Row.Field<object>("ID") + "'", null);
|
||
userlog.Add("Action", "Command");
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
userlog.Add("Note", "Machine = " + e.Row.Field<object>("Machine"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
|
||
}
|
||
if (e.Action == DataRowAction.Add)
|
||
{
|
||
//添加的行回传
|
||
foreach (DataColumn col in Machines.Columns)
|
||
{
|
||
row_entity.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
}
|
||
SQLiteHelpers.InsertData("Machines", row_entity);
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
userlog.Add("Action", "New Account");
|
||
userlog.Add("Note", "Machine = " + e.Row.Field<object>("Machine"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
}
|
||
if (e.Action == DataRowAction.Delete)
|
||
{
|
||
//删除的行回传
|
||
SQLiteHelpers.Delete("Machines", "ID='" + e.Row.Field<object>("ID") + "'", null);
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
userlog.Add("Action", "Delete Account");
|
||
userlog.Add("Note", "Machine = " + e.Row.Field<object>("Machine"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
}
|
||
}
|
||
//UserData操作数据库
|
||
private void UserData_Updata(object sender, DataRowChangeEventArgs e)
|
||
{
|
||
Dictionary<string, object> row_entity = new Dictionary<string, object>();
|
||
Dictionary<string, object> userlog = new Dictionary<string, object>();
|
||
|
||
if (e.Action == DataRowAction.Change)
|
||
{
|
||
//修改的行回传
|
||
foreach (DataColumn col in UserData.Columns)
|
||
{
|
||
row_entity.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
|
||
if (ActionLog.Columns.Contains(col.ColumnName))//添加到行为记录表
|
||
{
|
||
userlog.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
}
|
||
}
|
||
SQLiteHelpers.Update("USER", row_entity,"User='"+ e.Row.Field<object>("User")+"'", null);
|
||
|
||
userlog.Add("Action", "Command");
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
|
||
}
|
||
if (e.Action == DataRowAction.Add)
|
||
{
|
||
//添加的行回传
|
||
foreach (DataColumn col in UserData.Columns)
|
||
{
|
||
row_entity.Add(col.ColumnName, e.Row.Field<object>(col.ColumnName) ?? DBNull.Value);//传入字段和值(值允许为空)
|
||
}
|
||
SQLiteHelpers.InsertData("USER", row_entity);
|
||
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
userlog.Add("Action", "New Account");
|
||
userlog.Add("Name", MachineName);
|
||
userlog.Add("Terminal", "Terminal");
|
||
userlog.Add("Note", "User = "+ e.Row.Field<object>("User"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
}
|
||
if (e.Action == DataRowAction.Delete)
|
||
{
|
||
//删除的行回传
|
||
SQLiteHelpers.Delete("USER", "User='" + e.Row.Field<object>("User") + "'", null);
|
||
|
||
userlog.Add("Time", DateTime.Now.ToString("yyyy-MM-dd dd:HH:mm:ss:fff"));
|
||
userlog.Add("Action", "Delete Account");
|
||
userlog.Add("Name", MachineName);
|
||
userlog.Add("Terminal", "Terminal");
|
||
userlog.Add("Note", "User = " + e.Row.Field<object>("User"));
|
||
SQLiteHelpers.InsertData("ActionLog", userlog);
|
||
}
|
||
}
|
||
|
||
//数据表操作
|
||
public static void Insert_Memory(DataTable DB, Dictionary<string, object> Value)//插入数据
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
DataRow drEmployee = DB.NewRow();
|
||
drEmployee.BeginEdit();
|
||
|
||
foreach (KeyValuePair<string, object> kvp in Value)
|
||
{
|
||
drEmployee[kvp.Key] = kvp.Value;
|
||
}
|
||
drEmployee.EndEdit();
|
||
DB.Rows.Add(drEmployee);
|
||
drEmployee.AcceptChanges();
|
||
// drEmployee.ClearErrors();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("Insert_Row:" + ex.ToString());
|
||
}
|
||
}
|
||
public static void Delete_Memory(DataTable DB, string? key)//删除数据
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
DataRow drEmployee = DB.Select(key).First();
|
||
drEmployee.Delete();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("Delete_Row:" + ex.ToString());
|
||
}
|
||
}
|
||
public static DataRow? Selet_Row(DataTable DB, string? key)//查询
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
DataRow drEmployee = DB.Select(key).First();
|
||
return drEmployee;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("Selet_Row:" + ex.ToString());
|
||
return null;
|
||
}
|
||
}
|
||
public static object? Selet_Memory(DataTable DB, string name, string? key)//查询
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
DataRow[] dataRows = DB.Select(key);
|
||
if (dataRows.Count() > 0)
|
||
{
|
||
DataRow drEmployee = dataRows.First();
|
||
object? index = drEmployee.Field<object>(name);
|
||
return index;
|
||
}
|
||
else { return null; }
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("Selet_Memory:" + ex.ToString());
|
||
return null;
|
||
}
|
||
}
|
||
public static void Updata_Memory(DataTable DB, string key, string? Value, string name)//更新数据
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
var dr = DB.Select(key);
|
||
if (dr.Length > 0)
|
||
{
|
||
DataRow drEmployee = dr.First();
|
||
drEmployee.BeginEdit();
|
||
drEmployee[name] = Value;
|
||
drEmployee.EndEdit();
|
||
drEmployee.AcceptChanges();
|
||
// drEmployee.ClearErrors();
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("SDTD:" + ex.ToString());
|
||
}
|
||
}
|
||
public static void Updata_Memory(DataTable DB, string key, Dictionary<string, object> Value)//更新数据
|
||
{
|
||
try
|
||
{
|
||
lock (DB)
|
||
{
|
||
var dr = DB.Select(key);
|
||
if (dr.Length > 0)
|
||
{
|
||
DataRow drEmployee = dr.First();
|
||
drEmployee.BeginEdit();
|
||
|
||
foreach (KeyValuePair<string, object> kvp in Value)
|
||
{
|
||
drEmployee[kvp.Key] = kvp.Value;
|
||
}
|
||
drEmployee.EndEdit();
|
||
drEmployee.AcceptChanges();
|
||
// drEmployee.ClearErrors();
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("SDTD:" + ex.ToString());
|
||
}
|
||
}
|
||
|
||
private static int EXTime = 120;
|
||
private static async void TimeTask(object? state)
|
||
{
|
||
if (EXTime == 120)//1小时
|
||
{
|
||
EXTask();//统计配送消息
|
||
EXTime = 0;
|
||
}
|
||
|
||
|
||
DBC_Machines = await dataBase.ExecuteQueryAsync("SELECT *FROM [BatchDyeingCentral].[dbo].[Machines]");
|
||
|
||
EXTime++;
|
||
|
||
}
|
||
private static async void EXTask()
|
||
{
|
||
try
|
||
{
|
||
string DyelotsBulkedRecipe_sql = "SELECT [ProductCode],[ProductName],SUM(DispenseGrams)" +
|
||
"as TotalDispenseGrams FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] " +
|
||
"where DispenseEndTime > '" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND DispenseEndTime < '" +
|
||
DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "' GROUP BY [ProductCode],[ProductName] " +
|
||
"ORDER BY TotalDispenseGrams DESC ";
|
||
string Dye_sql =
|
||
"SELECT [Dyelot],[ReDye],[StepNumber] FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] " +
|
||
"where ((DispenseEndTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND DispenseEndTime < '" +
|
||
DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "') OR (DispenseTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") +
|
||
"' AND DispenseTime < '" + DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "')) " +
|
||
"AND ProductType = 1 GROUP BY [Dyelot],[ReDye],[StepNumber];";
|
||
string Chemical_sql =
|
||
"SELECT [Dyelot],[ReDye],[StepNumber] FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] " +
|
||
"where ((DispenseEndTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND DispenseEndTime < '" +
|
||
DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "') OR (DispenseTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") +
|
||
"' AND DispenseTime < '" + DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "')) " +
|
||
"AND ProductType = 2 GROUP BY [Dyelot],[ReDye],[StepNumber];";
|
||
string Powder_sql =
|
||
"SELECT [Dyelot],[ReDye],[StepNumber] FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] " +
|
||
"where ((DispenseEndTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND DispenseEndTime < '" +
|
||
DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "') OR (DispenseTime >= '" + DateTime.Now.ToString("yyyy-MM-dd") +
|
||
"' AND DispenseTime < '" + DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "')) " +
|
||
"AND ProductType = 3 GROUP BY [Dyelot],[ReDye],[StepNumber];";
|
||
string DyeDissolve =
|
||
"SELECT *FROM [BatchDyeingCentral].[dbo].[DyeSolutionHistory] " + "where DispenseEndTime >= '" +
|
||
DateTime.Now.ToString("yyyy-MM-dd") + "' AND DispenseEndTime < '" + DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "'";
|
||
|
||
ChemicalStatistics = await dataBase.ExecuteQueryAsync(DyelotsBulkedRecipe_sql);
|
||
DyeMeasurementNumber = await dataBase.ExecuteQueryAsyncCount(Dye_sql);
|
||
ChemicalNumber = await dataBase.ExecuteQueryAsyncCount(Chemical_sql);
|
||
PowderNumber = await dataBase.ExecuteQueryAsyncCount(Powder_sql);
|
||
DyeDissolveNumber = await dataBase.ExecuteQueryAsyncCount(DyeDissolve);
|
||
}
|
||
catch (Exception ex)
|
||
{//错误处理
|
||
LogGing.ERRDATA(ex);
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
finally {
|
||
Console.WriteLine("当日染料称量["+ DyeMeasurementNumber+ "]染料输送["+ DyeDissolveNumber + "]" +
|
||
"助剂输送["+ ChemicalNumber + "]粉体输送["+ PowderNumber + "]");
|
||
}
|
||
}
|
||
}
|
||
} |