using DyeingComputer.UserClass;
using LiveChartsCore.Geo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTK.Graphics.ES11;
using ScottPlot.Palettes;
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.Forms;
using System.Windows.Markup;
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 SunlightCentralizedControlManagement_SCCM_.ViewModel.ManualDyelotModel;
using static System.Net.WebRequestMethods;
using static System.Windows.Forms.AxHost;
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 BDC_SQL.BDCSqlHelper bDCSqlHelper = new BDC_SQL.BDCSqlHelper();
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 DataTable Dyelot_CALL = new DataTable();
public static DataTable Whole_dat = new DataTable();
public static UserControls.info[] inf = new UserControls.info[999]; //定义总览信息卡
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"));
}
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];
Dyelot_CALL = SQLiteHelpers.ExecuteDataSet("select * from Dyelot", null).Tables[0].Clone();
SQLiteHelpers.Close();
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"));
USERCapacity(App.USER_Purview);//App.USER_Purview);
UPort();//启动串口
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 SerialPort port1 = new SerialPort();//创建串口
public static SerialPort port2 = new SerialPort();//创建串口
public static SerialPort port3 = new SerialPort();//创建串口
void UPort()
{
try
{
int Ba = 9600;
try { Ba = Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD1")); }
catch (Exception) { Ba = 9600; }
port1.PortName = Configini.IniReadvalue("SYS", "COMP1");
port1.BaudRate = Ba;//配置
port1.DataBits = 8;
port1.Parity = Parity.Even;
port1.StopBits = StopBits.One;
port1.ReadTimeout = 500;
port1.WriteTimeout = 500;
port1.Open();//打开串口
UPort1 = true;
}
catch (Exception e)
{
UPort1 = false;
LogGing.LogGingDATA(e.Message);
}
try
{
int Ba2 = 9600;
try { Ba2 = Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD1")); }
catch (Exception) { Ba2 = 9600; }
port2.PortName = Configini.IniReadvalue("SYS", "COMP2");
port2.BaudRate = Ba2;//配置
port2.DataBits = 8;
port2.Parity = Parity.Even;
port2.StopBits = StopBits.One;
port2.ReadTimeout = 500;
port2.WriteTimeout = 500;
port2.Open();//打开串口
UPort2 = true;
}
catch (Exception e)
{
UPort2 = false;
LogGing.LogGingDATA(e.Message);
}
try
{
int Ba3 = 9600;
try { Ba3 = Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD1")); }
catch (Exception) { Ba3 = 9600; }
port3.PortName = Configini.IniReadvalue("SYS", "COMP3");
port3.BaudRate = Ba3;//配置
port3.DataBits = 8;
port3.Parity = Parity.Even;
port3.StopBits = StopBits.One;
port3.ReadTimeout = 500;
port3.WriteTimeout = 500;
port3.Open();//打开串口
UPort3 = true;
}
catch (Exception e)
{
UPort3 = false;
LogGing.LogGingDATA(e.Message);
}
}//ck
private async void Port_link()
{
await Task.Run(() =>
{
try
{
if (stringQueueSerial.Count > 0) //信息发送队列
{
QueueSerial t = stringQueueSerial.Dequeue();
if (t.ID == "PORT1")
{
if (UPort1)
{
port1.Write(t.DAT);
}
}
else if (t.ID == "PORT2")
{
if (UPort2)
{
port2.Write(t.DAT);
}
}
else if (t.ID == "PORT3")
{
if (UPort3)
{
port3.Write(t.DAT);
}
}
}
}
catch (Exception ex)
{
}
finally
{
Port_link();
}
});
}//发送
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);
}
}
void Tick_Main_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{
/*下传工单信息*/
SQLiteHelper WorkOrderSQL = new SQLiteHelper(DBAddress); //数据库连接路径
WorkOrderSQL.Open(); //打开数据库
DataTable WorkOrderstepdata = WorkOrderSQL.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++)
{
if (Selet_Machines(Machines, "Type", "ID='" + k + "'").ToString() == "838")
{
string WorkOrderdata_m = WorkOrderstepdata.Select()[k].Field