using DyeingComputer.UserClass;
using LiveChartsCore.Geo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NModbus;
using OpenTK.Graphics.ES11;
using ScottPlot.Palettes;
using ScottPlot.TickGenerators.TimeUnits;
using SunlightCentralizedControlManagement_SCCM_.EX.Exchange;
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.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Data.SqlClient;
using System.Diagnostics.Eventing.Reader;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using System.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
using static MaterialDesignThemes.Wpf.Theme;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using static SunlightCentralizedControlManagement_SCCM_.View.MachinesView;
using static SunlightCentralizedControlManagement_SCCM_.ViewModel.ManualDyelotModel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
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();//数据库
public static SQLiteHelper SQLiteHelpers = null; //定义数据库
private BDC_SQL.BDCSqlHelper bDCSqlHelper = new BDC_SQL.BDCSqlHelper();
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径
public static SQLiteHelper SQLiteChartAdress = null; //定义数据库
private static readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db"; //数据库路径
public static DataTable Machines = new DataTable(); //设备表缓存
// public static DataRow MachinesROW;
public static DataTable USER_data = new DataTable();
public static DataTable Dyelot_CALL = new DataTable();
public static DataTable Whole_dat = new DataTable();
public static UserControls.info[] inf = new UserControls.info[200]; //定义总览信息卡
public static int ERR_c = 0;//错误计数器
public bool UPort1, UPort2, UPort3;
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"));
SQLiteChartAdress = new SQLiteHelper(ChartAdress); //数据库连接路径
SQLiteChartAdress.Open(); //打开数据库
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];
Dyelot_CALL = SQLiteHelpers.ExecuteDataSet("select * from Dyelot", null).Tables[0].Clone();
Whole_dat.Columns.Add("Machine", Type.GetType("System.String"));
Whole_dat.Columns.Add("DateTime", Type.GetType("System.DateTime"));
Whole_dat.Columns.Add("DAT", Type.GetType("System.Double"));
}
catch (Exception ex) { LogGing.ERRDATA(ex);
}
USERCapacity(App.USER_Purview);//App.USER_Purview);
ClientNEW();
TcpClientNEW();
PortClientNEW();
SQL_LINK();
CountDown();
}
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 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++;
}
if (SQMOD == "Windows Authentication") //连接数据库测试
{
bDCSqlHelper.Config(SQLIP, SQLNAME, SQLUSER, SQLPASWORD, true);
}
else
{
bDCSqlHelper.Config(SQLIP, SQLNAME, SQLUSER, SQLPASWORD, false);
}
}
public void CountDown()
{
DispatcherTimer dis1s = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1) //秒
};
dis1s.Tick += new EventHandler(Tick_Main_1S);//每执行的方法
dis1s.Start();//计时开始
DispatcherTimer dis50ms = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100) //毫秒
};
dis50ms.Tick += new EventHandler(DisTimer_50MS);//每执行的方法
dis50ms.Start();//计时开始
}//时间周期初始化
int Time5 = 0, Time30 = 0;
void DisTimer_50MS(object sender, EventArgs e)//Tick_Event周期执行事件50MS
{
int _id = -1;
if (stringQueue.Count > 0) //信息发送队列
{
QueueString t = stringQueue.Dequeue();
MachiensTcpClient[t.ID].SendAsync(t.DAT);
_id = t.ID;
}
if (stringQueueinf.Count > 0)
{
QueueString inf = stringQueueinf.Dequeue();
if (_id != inf.ID) MachiensTcpClient[inf.ID].SendAsync(inf.DAT);
}
/*if (stringQueueSerial.Count > 0) //ck信息发送队列
{
QueueSerial t = stringQueueSerial.Dequeue();
MachiensPortClient[t.ID].Send(t.DAT);
}*/
}
void Tick_Main_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{
/*下传工单信息*/
DataTable WorkOrderstepdata = SQLiteHelpers.ExecuteDataSet("select * from WorkOrder where (State='111' or State='112') AND EndTime >'" +
DateTime.Now.AddHours(-8).ToString("yyyy/MM/dd HH:mm:ss") + "'", null).Tables[0]; //读取表写入缓存
for (int k = 0; k < WorkOrderstepdata.Rows.Count; k++)
{
string WorkOrderdata_m = WorkOrderstepdata.Select()[k].Field