using Hik.Api; using Newtonsoft.Json.Linq; using S7.Net; using System.Threading.Channels; using System.Windows.Forms; namespace xicheji { public partial class MainForm : Form { private readonly Config config; private readonly List washRecords = new(); private DateTime lastReceiveTime = DateTime.MinValue; private Plc? plc = null; private HikApi? hikApi = null; public MainForm() { InitializeComponent(); config = ConfigHelper.GetConfig(); } private void MainForm_Load(object sender, EventArgs e) { try { //测试数据 washRecords.Add(new WashRecord() { CarNum = "京123456", StartTime = new DateTime(2024, 6, 1, 12, 00, 00), EndTime = new DateTime(2024, 6, 1, 12, 30, 00), WashTime = 15, DryTime = 15, TotalElectricity = 10, TotalWater = 2, WaterPressure = 6, }); dataGridView1.DataSource = washRecords; ConnectPLC(); //ConnectCamera(); receivePLCTimer.Enabled = true; uploadTimer.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void btnConnectCamera_Click(object sender, EventArgs e) { try { ConnectCamera(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void nowTimer_Tick(object sender, EventArgs e) { try { var now = DateTime.Now; lblTime.Text = $"{now.Month}月 {now.Day}日 {now.Hour}:{now:mm}:{now:ss}"; } catch { } } private void receivePLCTimer_Tick(object sender, EventArgs e) { try { if (plc == null || !plc.IsConnected) { receivePLCTimer.Enabled = false; return; } ReadPLCData(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void ReadPLCData() { lblWashCount.Text = ReadPLCString("洗车次数"); return; var time = new DateTime( DateTime.Now.Year, ReadPLCInt("当前月"), ReadPLCInt("当前日"), ReadPLCInt("当前时"), ReadPLCInt("当前分"), ReadPLCInt("当前秒")); if (time <= lastReceiveTime) { //已经读过这个时间的了 return; } lastReceiveTime = time; ReadError(); ReadStatus(); lblWashCount.Text = ReadPLCString("洗车次数"); lblTotalWater.Text = ReadPLCString("累计用水量"); lblTotalElectricity.Text = ReadPLCString("累计用电量"); //.Text = ReadPLCStringValue("当前流量"); lblCurrentElectricity.Text = ReadPLCString("当前功率"); lblCurrentWaterP.Text = ReadPLCString("当前压力"); } private void ReadStatus() { lblStatus.Text = string.Empty; var statuses = new List() { "洗车中", "清洗中", "沥水中", "出车中", "风干中"}; foreach (var status in statuses) { if (ReadPLCBool(status)) { lblStatus.Text = status; break; } } } private void ReadError() { lblError.Text = string.Empty; var errors = new List() { "急停", "冲洗泵故障", "反洗泵故障", "清淤泵故障", "伴热故障", "风刀故障" }; foreach (var error in errors) { if (ReadPLCBool(error)) { lblError.Text = error; break; } } } private string ReadPLCString(string name) { var variable = ConfigHelper.FindVariable(name); if (plc == null || variable == null) return string.Empty; var value = plc.Read(variable.Key); return $"{value} {variable.Desc}"; } private int ReadPLCInt(string name) { var variable = ConfigHelper.FindVariable(name); if (plc == null || variable == null) return 0; var value = plc.Read(variable.Key); return int.Parse(value?.ToString() ?? "0"); } private bool ReadPLCBool(string name) { var variable = ConfigHelper.FindVariable(name); if (plc == null || variable == null) return false; var value = plc.Read(variable.Key); return value?.ToString()?.ToLower() == "true"; } private void uploadTimer_Tick(object sender, EventArgs e) { } private void btnSetting_Click(object sender, EventArgs e) { try { var settingForm = new SettingForm(); if (settingForm.ShowDialog() == DialogResult.OK) { ConnectPLC(); ConnectCamera(); receivePLCTimer.Enabled = true; uploadTimer.Enabled = true; } } catch { } } private void ConnectPLC() { plc = new Plc(config.PLCType, config.PLCIP, int.Parse(config.PLCPort), 0, 1); plc.Open(); if (plc == null || !plc.IsConnected) { MessageBox.Show("PLC连接失败!"); return; } } private void ConnectCamera() { try { hikApi = HikApi.Login(config.CameraIP, int.Parse(config.CameraPort), config.CameraUserName, config.CameraPassword) as HikApi; if (hikApi == null || !hikApi.Connected) { MessageBox.Show("摄像头连接失败!"); return; } hikApi.PlaybackService.StartPlayBack(hikApi.DefaultIpChannel, pictureBox1.Handle); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } public void UpdateCarInfo(DateTime now) { this.Invoke(() => { this.Text = now.ToString(); }); } } }