You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
9.0 KiB
231 lines
9.0 KiB
using CommunityToolkit.Mvvm;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using SunlightAggregationManager.UserClass;
|
|
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.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 TCP_E = "0";
|
|
public static string HTTP_E = "0";
|
|
public static string TLS_E = "0";
|
|
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();
|
|
|
|
|
|
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");
|
|
|
|
TCP_E = Configini.IniReadvalue("NETWORK", "TCP");
|
|
HTTP_E = Configini.IniReadvalue("NETWORK", "HTTP");
|
|
TLS_E = Configini.IniReadvalue("NETWORK", "TLS");
|
|
|
|
//本地数据库(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();
|
|
|
|
if (UserData.Columns.Contains("UserID"))
|
|
{
|
|
UserData.Columns.Remove("UserID");
|
|
}
|
|
DataRow[] dataRows = UserData.Select("State<90");
|
|
foreach (DataRow row in dataRows)
|
|
{
|
|
row["State"] = 0;
|
|
row["LinkID"] = 0;
|
|
}
|
|
|
|
|
|
UserData.RowChanged += UserData_Updata;//注册userdata表更新事件
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
}
|
|
|
|
//运行数据库
|
|
DataBase dataBase = new DataBase();
|
|
dataBase.Config(SQLIP, SQLNAME, SQMOD, SQLUSER, SQLPASWORD);
|
|
|
|
if (TCP_E == "1")//启动tcp(内网直连)
|
|
{
|
|
int P1, P2;
|
|
try
|
|
{
|
|
P1 =int.Parse( Configini.IniReadvalue("NETWORK", "TCP_PORT1"));
|
|
}
|
|
catch (Exception) { P1 = 7789; }
|
|
try
|
|
{
|
|
P2 = int.Parse(Configini.IniReadvalue("NETWORK", "TCP_PORT2"));
|
|
}
|
|
catch (Exception) { P2 = 7790; }
|
|
|
|
using var _ = AsyncTcpServer.TcpMain(P1,P2);
|
|
}
|
|
}
|
|
|
|
|
|
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 object? Selet_Memory(DataTable DB, string name, string? key)//查询
|
|
{
|
|
try
|
|
{
|
|
lock (DB)
|
|
{
|
|
DataRow drEmployee = DB.Select(key).First();
|
|
object? index = drEmployee.Field<object>(name);
|
|
return index;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("SDTD:" + 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());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|