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.

183 lines
6.4 KiB

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTK.Graphics.ES11;
using SunlightCentralizedControlManagement_SCCM_.Properties;
using SunlightCentralizedControlManagement_SCCM_.UserClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using TouchSocket.Core;
using TouchSocket.Sockets;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
{
/// <summary>
/// 变量传递至ui
/// </summary>
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
{
public string sys_Time; //显示系统时间
public string Sys_Time //通知UI控件参数改变
{
get { return sys_Time; }
set { sys_Time = value; OnPropertyChanged("Sys_Time"); }
}
public void CountDown()
{
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
{
Interval = TimeSpan.FromSeconds(1)//秒
};
timer1s.Tick += Tick_Event_1S;
timer1s.Start();
DispatcherTimer timer5s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
{
Interval = TimeSpan.FromSeconds(5)//秒
};
timer5s.Tick += Tick_Event_5S;
timer5s.Start();
//设置定时器
DispatcherTimer disTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(500) //毫秒
};
disTimer.Tick += new EventHandler(DisTimer_500MS);//每一秒执行的方法
disTimer.Start();//计时开始
}//时间周期初始化
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
}
void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S
{
for (int i = 0; i < Machinesdata_Count; i++)
{
// if ( Selet_Machines(Machines,"State", i).ToString() =="801")
// MachiensTcpClient[i].SendAsync("SC800:SCCM["+ MachiensTcpClient[i].IP+";"+ MachiensTcpClient[i].Port+"]");
MachiensTcpClient[1].SendAsync("SC851BED2E58B6EBB4BD4");
}
// DataTable obj = JsonConvert.DeserializeObject<DataTable>(yt);//反序列化
}
void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS
{
}
private SQLiteHelper SQLiteHelpers = null; //定义数据库
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径
public static DataTable Machines = new DataTable(); //设备表缓存
public static View.info[] inf = new View.info[999]; //定义总览信息卡
public MainWindowViewModel()
{
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存
SQLiteHelpers.Close();
CountDown();
TcpClientNEW();
}
private readonly TcpClient[] MachiensTcpClient = new TcpClient[999];
public int Machinesdata_Count;
public void TcpClientNEW()
{
// bool Machine_ ;
DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''", "id asc");//获取连接有效的组
Machinesdata_Count = machinesdata.Count();
for (int i = 0; i < Machinesdata_Count; i++)
{
MachiensTcpClient[i] = new TcpClient();
DataRow dt = machinesdata[i];
Updata_Machines(Machines, Convert.ToInt16(dt["ID"]), "State", "800");
_ = AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接
, Selet_Machines(Machines, "IP", Convert.ToInt16(dt["ID"])).ToString()
, Selet_Machines(Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString());
}
}
public static string SYS_WorkNumder; //工单号
public static int SYS_AT1; //附缸1-3
public static int SYS_AT2;
public static int SYS_AT3;
public static object Selet_Machines(DataTable DB, string name, int key)//查询
{
try
{
lock (DB)
{
DataRow drEmployee = DB.Select("ID='" + key + "'").First();
object index = drEmployee.Field<object>(name);
return index;
}
}
catch (Exception)
{
// LogGing.LogGingDATA("SDTD:" + ex.ToString());
return "ERR";
}
}
public static void Updata_Machines(DataTable DB, int key, string name, string Value)//更新数据
{
try
{
lock (DB)
{
DataRow drEmployee = DB.Select("ID='" + key + "'").First();
drEmployee.BeginEdit();
drEmployee[name] = Value;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
drEmployee.ClearErrors();
}
}
catch (Exception)
{
// LogGing.LogGingDATA("SDTD:" + ex.ToString());
}
}
}
}