using DyeingComputer.UserClass;
using LiveChartsCore.Geo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTK.Graphics.ES11;
using SunlightCentralizedControlManagement_SCCM_.Properties;
using SunlightCentralizedControlManagement_SCCM_.UserClass;
using SunlightCentralizedControlManagement_SCCM_.WindowsView;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Threading;
using System.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.Sockets;
using static MaterialDesignThemes.Wpf.Theme;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using static System.Net.WebRequestMethods;
namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
{
///
/// 变量传递至ui
///
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
if (propertyName != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
public class MainWindowViewModel : ViewModelBase
{
//调用配置文件
private static UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "SCCM.ini");
public static string SQLIP; //读配置文件
public static string SQLNAME;
public static string SQMOD;
public static string SQLUSER;
public static string SQLPASWORD;
public static string SN_ID;
public static string SN_KEY;
public static bool DyelotsDispenser;
public static SqlConnection conn_SC =new SqlConnection();//数据库
private SQLiteHelper SQLiteHelpers = null; //定义数据库
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径
public static DataTable Machines = new DataTable(); //设备表缓存
public static DataRow MachinesROW;
public static DataTable USER_data = new DataTable();
public static UserControls.info[] inf = new UserControls.info[999]; //定义总览信息卡
public static int ERR_c = 0;//错误计数器
public static bool[] USER_Capacity { set; get; } = new bool[99];
public MainWindowViewModel()
{
try
{
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");
SN_ID = Configini.IniReadvalue("SN", "SN2");
SN_KEY = Configini.IniReadvalue("SN", "SN1");
DyelotsDispenser = Convert.ToBoolean(Configini.IniReadvalue("SYS", "DyelotsDispenser"));
}
catch (Exception ex) { LogGing.ERRDATA(ex); }
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存
USER_data = SQLiteHelpers.ExecuteDataSet("select * from USER order by Name desc", null).Tables[0];
SQLiteHelpers.Close();
MachinesROW = Machines.NewRow();
USERCapacity(App.USER_Purview);//App.USER_Purview);
CountDown();
TcpClientNEW();
SQL_LINK();
}
public static void USERCapacity(string user) //权限
{
if (user != "ENGINEER")
{
string USER_Cap = USER_data.Select("Name='" + user + "'").First().Field("Capacity").ToString();
for (int i = 0; i < USER_Cap.Length; i++)
{
if (USER_Cap.Substring(i, 1) == "1")
{
USER_Capacity[i] = true;
}
else
{
USER_Capacity[i] = false;
}
}
}
else
{
for (int i = 0; i < 99; i++)
{
USER_Capacity[i] = true;
}
}
}
public static async void SQL_LINK()//连接dbc数据库
{
if (conn_SC.State == ConnectionState.Open) conn_SC.Close();
try
{
if (SQMOD == "Windows Authentication") //连接数据库测试
{
conn_SC = new SqlConnection("server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI");
}
else
{
conn_SC = new SqlConnection("server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASWORD);
}
await conn_SC.OpenAsync(); //连接数据库
//conn_SC.Close();
}
catch (Exception ex)
{
LogGing.LogGingDATA("[Database='" + ex + "']=SQLSERVER");
ERR_c++;
}
}
public void CountDown()
{
DispatcherTimer timer1s = new DispatcherTimer(DispatcherPriority.Normal);//初始化循环,每1秒调用一次Tick
timer1s.Interval = TimeSpan.FromMilliseconds(950);//秒
timer1s.Tick += Tick_Main_1S;
timer1s.Start();
DispatcherTimer dis50ms = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(10) //毫秒
};
dis50ms.Tick += new EventHandler(DisTimer_50MS);//每一秒执行的方法
dis50ms.Start();//计时开始
}//时间周期初始化
void DisTimer_50MS(object sender, EventArgs e)//Tick_Event周期执行事件50MS
{
if (stringQueue.Count > 0) //信息发送队列
{
QueueString t = stringQueue.Dequeue();
MachiensTcpClient[t.ID].SendAsync(t.DAT);
}
}
void Tick_Main_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{
DATA_view();
//下传工单信息
SQLiteHelper WorkOrderSQL = new SQLiteHelper(DBAddress); //数据库连接路径
WorkOrderSQL.Open(); //打开数据库
DataTable WorkOrderstepdata = WorkOrderSQL.ExecuteDataSet("select * from WorkOrder where State='111'", null).Tables[0]; //读取表写入缓存
for (int k = 0; k < WorkOrderstepdata.Rows.Count; k++)
{
string WorkOrderdata_m = WorkOrderstepdata.Select()[k].Field