From 4e4985432056ee30ad0ea972cc7f35a24a7cff32 Mon Sep 17 00:00:00 2001 From: nikzori Date: Fri, 24 Jan 2025 11:46:36 +0300 Subject: [PATCH] Premium Plus support added --- Datasheet.Designer.cs | 129 ++++----- Datasheet.cs | 373 +++++++++++++++++++++++---- Gidrolock Modbus Configurator.csproj | 5 +- Main.cs | 114 ++++---- Model.cs | 2 +- Properties/AssemblyInfo.cs | 10 +- Properties/Resources.Designer.cs | 46 ++-- Properties/Settings.Designer.cs | 22 +- 8 files changed, 499 insertions(+), 202 deletions(-) diff --git a/Datasheet.Designer.cs b/Datasheet.Designer.cs index 961fc21..f35620c 100644 --- a/Datasheet.Designer.cs +++ b/Datasheet.Designer.cs @@ -30,6 +30,8 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Datasheet)); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.labelBattery = new System.Windows.Forms.Label(); + this.label6 = new System.Windows.Forms.Label(); this.buttonPoll = new System.Windows.Forms.Button(); this.buttonSetID = new System.Windows.Forms.Button(); this.labelFirmware = new System.Windows.Forms.Label(); @@ -42,17 +44,15 @@ this.labelCleaning = new System.Windows.Forms.Label(); this.buttonCleaning = new System.Windows.Forms.Button(); this.label7 = new System.Windows.Forms.Label(); - this.label6 = new System.Windows.Forms.Label(); + this.labelAlarm = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.buttonAlarm = new System.Windows.Forms.Button(); this.buttonValve = new System.Windows.Forms.Button(); this.labelValve = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); - this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); + this.sensorPanel = new System.Windows.Forms.FlowLayoutPanel(); this.label9 = new System.Windows.Forms.Label(); - this.label10 = new System.Windows.Forms.Label(); - this.flowLayoutPanel2 = new System.Windows.Forms.FlowLayoutPanel(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).BeginInit(); this.groupBox2.SuspendLayout(); @@ -60,6 +60,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.labelBattery); + this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.buttonPoll); this.groupBox1.Controls.Add(this.buttonSetID); this.groupBox1.Controls.Add(this.labelFirmware); @@ -68,17 +70,35 @@ this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); - this.groupBox1.Location = new System.Drawing.Point(369, 12); + this.groupBox1.Location = new System.Drawing.Point(304, 12); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(211, 101); + this.groupBox1.Size = new System.Drawing.Size(221, 101); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инфо"; // + // labelBattery + // + this.labelBattery.AutoSize = true; + this.labelBattery.Location = new System.Drawing.Point(59, 48); + this.labelBattery.Name = "labelBattery"; + this.labelBattery.Size = new System.Drawing.Size(15, 13); + this.labelBattery.TabIndex = 9; + this.labelBattery.Text = "%"; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(6, 48); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(52, 13); + this.label6.TabIndex = 8; + this.label6.Text = "Батарея:"; + // // buttonPoll // this.buttonPoll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonPoll.Location = new System.Drawing.Point(130, 19); + this.buttonPoll.Location = new System.Drawing.Point(140, 43); this.buttonPoll.Name = "buttonPoll"; this.buttonPoll.Size = new System.Drawing.Size(75, 23); this.buttonPoll.TabIndex = 7; @@ -88,7 +108,7 @@ // // buttonSetID // - this.buttonSetID.Location = new System.Drawing.Point(130, 55); + this.buttonSetID.Location = new System.Drawing.Point(140, 68); this.buttonSetID.Name = "buttonSetID"; this.buttonSetID.Size = new System.Drawing.Size(75, 23); this.buttonSetID.TabIndex = 6; @@ -110,13 +130,13 @@ this.labelModel.AutoSize = true; this.labelModel.Location = new System.Drawing.Point(61, 16); this.labelModel.Name = "labelModel"; - this.labelModel.Size = new System.Drawing.Size(50, 13); + this.labelModel.Size = new System.Drawing.Size(46, 13); this.labelModel.TabIndex = 4; - this.labelModel.Text = "Standard"; + this.labelModel.Text = "Модель"; // // nudModbusID // - this.nudModbusID.Location = new System.Drawing.Point(74, 58); + this.nudModbusID.Location = new System.Drawing.Point(74, 71); this.nudModbusID.Name = "nudModbusID"; this.nudModbusID.Size = new System.Drawing.Size(50, 20); this.nudModbusID.TabIndex = 3; @@ -124,7 +144,7 @@ // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(6, 60); + this.label3.Location = new System.Drawing.Point(6, 73); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(62, 13); this.label3.TabIndex = 2; @@ -153,7 +173,7 @@ this.groupBox2.Controls.Add(this.labelCleaning); this.groupBox2.Controls.Add(this.buttonCleaning); this.groupBox2.Controls.Add(this.label7); - this.groupBox2.Controls.Add(this.label6); + this.groupBox2.Controls.Add(this.labelAlarm); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.button2); this.groupBox2.Controls.Add(this.buttonAlarm); @@ -162,7 +182,7 @@ this.groupBox2.Controls.Add(this.label4); this.groupBox2.Location = new System.Drawing.Point(13, 12); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(350, 100); + this.groupBox2.Size = new System.Drawing.Size(285, 100); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "Общее"; @@ -170,7 +190,7 @@ // labelCleaning // this.labelCleaning.AutoSize = true; - this.labelCleaning.Location = new System.Drawing.Point(52, 78); + this.labelCleaning.Location = new System.Drawing.Point(52, 77); this.labelCleaning.Name = "labelCleaning"; this.labelCleaning.Size = new System.Drawing.Size(33, 13); this.labelCleaning.TabIndex = 9; @@ -178,7 +198,7 @@ // // buttonCleaning // - this.buttonCleaning.Location = new System.Drawing.Point(192, 73); + this.buttonCleaning.Location = new System.Drawing.Point(125, 72); this.buttonCleaning.Name = "buttonCleaning"; this.buttonCleaning.Size = new System.Drawing.Size(73, 23); this.buttonCleaning.TabIndex = 8; @@ -189,20 +209,20 @@ // label7 // this.label7.AutoSize = true; - this.label7.Location = new System.Drawing.Point(6, 78); + this.label7.Location = new System.Drawing.Point(6, 77); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(48, 13); this.label7.TabIndex = 7; this.label7.Text = "Уборка:"; // - // label6 + // labelAlarm // - this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(61, 48); - this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(24, 13); - this.label6.TabIndex = 6; - this.label6.Text = "нет"; + this.labelAlarm.AutoSize = true; + this.labelAlarm.Location = new System.Drawing.Point(61, 48); + this.labelAlarm.Name = "labelAlarm"; + this.labelAlarm.Size = new System.Drawing.Size(24, 13); + this.labelAlarm.TabIndex = 6; + this.labelAlarm.Text = "нет"; // // label5 // @@ -215,7 +235,7 @@ // // button2 // - this.button2.Location = new System.Drawing.Point(271, 14); + this.button2.Location = new System.Drawing.Point(204, 14); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(73, 52); this.button2.TabIndex = 4; @@ -224,7 +244,7 @@ // // buttonAlarm // - this.buttonAlarm.Location = new System.Drawing.Point(192, 43); + this.buttonAlarm.Location = new System.Drawing.Point(125, 43); this.buttonAlarm.Name = "buttonAlarm"; this.buttonAlarm.Size = new System.Drawing.Size(73, 23); this.buttonAlarm.TabIndex = 3; @@ -234,7 +254,7 @@ // // buttonValve // - this.buttonValve.Location = new System.Drawing.Point(192, 14); + this.buttonValve.Location = new System.Drawing.Point(125, 14); this.buttonValve.Name = "buttonValve"; this.buttonValve.Size = new System.Drawing.Size(73, 23); this.buttonValve.TabIndex = 2; @@ -260,58 +280,41 @@ this.label4.TabIndex = 0; this.label4.Text = "Кран:"; // - // flowLayoutPanel1 + // sensorPanel // - this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + this.sensorPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); - this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; - this.flowLayoutPanel1.Location = new System.Drawing.Point(13, 135); - this.flowLayoutPanel1.Name = "flowLayoutPanel1"; - this.flowLayoutPanel1.Size = new System.Drawing.Size(280, 175); - this.flowLayoutPanel1.TabIndex = 4; + this.sensorPanel.AutoScroll = true; + this.sensorPanel.BackColor = System.Drawing.Color.Gainsboro; + this.sensorPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; + this.sensorPanel.Location = new System.Drawing.Point(13, 135); + this.sensorPanel.Name = "sensorPanel"; + this.sensorPanel.Size = new System.Drawing.Size(512, 175); + this.sensorPanel.TabIndex = 4; + this.sensorPanel.WrapContents = false; // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(10, 119); this.label9.Name = "label9"; - this.label9.Size = new System.Drawing.Size(108, 13); + this.label9.Size = new System.Drawing.Size(50, 13); this.label9.TabIndex = 5; - this.label9.Text = "Проводные датчики"; - // - // label10 - // - this.label10.AutoSize = true; - this.label10.Location = new System.Drawing.Point(297, 119); - this.label10.Name = "label10"; - this.label10.Size = new System.Drawing.Size(78, 13); - this.label10.TabIndex = 7; - this.label10.Text = "Радиодатчики"; - // - // flowLayoutPanel2 - // - this.flowLayoutPanel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left))); - this.flowLayoutPanel2.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; - this.flowLayoutPanel2.Location = new System.Drawing.Point(300, 135); - this.flowLayoutPanel2.Name = "flowLayoutPanel2"; - this.flowLayoutPanel2.Size = new System.Drawing.Size(280, 175); - this.flowLayoutPanel2.TabIndex = 6; + this.label9.Text = "Датчики"; // // Datasheet // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(592, 322); - this.Controls.Add(this.label10); - this.Controls.Add(this.flowLayoutPanel2); + this.ClientSize = new System.Drawing.Size(537, 322); this.Controls.Add(this.label9); - this.Controls.Add(this.flowLayoutPanel1); + this.Controls.Add(this.sensorPanel); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Datasheet"; + this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.Text = "Datasheet"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); @@ -344,10 +347,10 @@ private System.Windows.Forms.Label labelCleaning; private System.Windows.Forms.Button buttonCleaning; private System.Windows.Forms.Label label7; - private System.Windows.Forms.Label label6; - private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; + private System.Windows.Forms.Label labelAlarm; + private System.Windows.Forms.FlowLayoutPanel sensorPanel; private System.Windows.Forms.Label label9; - private System.Windows.Forms.Label label10; - private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel2; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.Label labelBattery; } } \ No newline at end of file diff --git a/Datasheet.cs b/Datasheet.cs index 7c644b1..5d25ed1 100644 --- a/Datasheet.cs +++ b/Datasheet.cs @@ -1,28 +1,21 @@ -using System; +using Newtonsoft.Json.Linq; +using System; +using System.Collections; using System.Collections.Generic; using System.Drawing; using System.IO.Ports; using System.Linq; -using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using System.Xml; - namespace Gidrolock_Modbus_Scanner { - /* - * This is more of a View than a View-Controller - * Relegate everything to interface functions for models - */ public partial class Datasheet : Form { byte modbusID; Device device; - byte[] message; - byte[] data; - EventHandler handler; + ModbusResponseEventArgs latestMessage; SerialPort port = Modbus.port; bool isPolling = false; @@ -33,86 +26,378 @@ namespace Gidrolock_Modbus_Scanner bool alarmStatus = false; bool cleaningStatus = false; - public Datasheet(byte modbusID, Device device) + List wiredSensors = new List(); + List wirelessSensors; + + + + public Datasheet(byte modbusID, Device device) : base() { + InitializeComponent(); + nudModbusID.Minimum = 1; nudModbusID.Maximum = 246; + nudModbusID.Value = modbusID; this.modbusID = modbusID; this.device = device; + labelModel.Text = device.name; + labelFirmware.Text = "v???"; + if (!device.hasCleaningMode) { labelCleaning.Text = "Недоступна."; buttonCleaning.Enabled = false; } + if (!device.hasBattery) + labelBattery.Text = "Нет"; + else labelBattery.Text = "???%"; - handler = (sndr, msg) => + Modbus.ResponseReceived += (sndr, msg) => { isAwaitingResponse = false; latestMessage = msg; }; + + for (int i = 0; i < device.wiredSensors; i++) { - message = msg.Message; - data = msg.Data; - }; + WiredSensor ws = new WiredSensor(i) { Width = 495, Height = 24 }; + sensorPanel.Controls.Add(ws); + ws.Visible = true; + + } + if (device.hasScenarioSensor) + { + ScenarioSensor scenSen = new ScenarioSensor() { Width = 495, Height = 24 }; + sensorPanel.Controls.Add(scenSen); + scenSen.Visible = true; + } + if (device.wiredSensors < device.sensorsAlarm.length) + { + wirelessSensors = new List(); + int wsrIndex = device.sensorsAlarm.length - device.wiredSensors - (device.hasScenarioSensor ? 1 : 0); + for (int i = 0; i < wsrIndex; i++) + { + WirelessSensor wsr = new WirelessSensor(i) { Width = 495, Height = 24 }; + sensorPanel.Controls.Add(wsr); + wsr.Visible = true; + } + } + for (int i = 0; i < sensorPanel.Controls.Count; i++) + sensorPanel.Controls[i].BackColor = i % 2 == 0 ? Color.White : Color.LightGray; + + + sensorPanel.Update(); } private async void buttonPoll_Click(object sender, EventArgs e) { // hardcoded for now, probably easier to keep it like this in the future - if (await PollEntry(device.valveStatus)) + try { - if (data.Last() > 0) - labelValve.Text = "Закрыт"; - else labelValve.Text = "Открыт"; + bool res = await PollEntry(device.valveStatus); // for some reason main thread doesn't go + Console.WriteLine("Polling for valve status, poll success: " + res); + if (res) + { + if (latestMessage.Data.Last() > 0) + { + isValveClosed = true; + labelValve.Text = "Закрыт"; + buttonValve.Text = "Открыть"; + } + else + { + isValveClosed = false; + labelValve.Text = "Открыт"; + buttonValve.Text = "Закрыть"; + } + } + + res = await PollEntry(device.alarmStatus); + Console.WriteLine("Polling for alarm status, poll success: " + res); + if (res) + { + if (latestMessage.Data.Last() > 0) + { + alarmStatus = true; + buttonAlarm.Text = "Выключить"; + labelAlarm.Text = "Протечка!"; + } + else + { + alarmStatus = false; + buttonAlarm.Text = "Авария"; + labelAlarm.Text = "нет"; + } + } + + if (device.hasCleaningMode) + { + res = await PollEntry(device.cleaningMode); + if (res) + { + if (latestMessage.Data.Last() > 0) + { + cleaningStatus = true; + buttonAlarm.Text = "Выключить"; + labelAlarm.Text = "вкл"; + } + else + { + cleaningStatus = false; + buttonAlarm.Text = "Включить"; + labelAlarm.Text = "выкл"; + } + } + } + + if (device.hasBattery) + { + res = await PollEntry(device.batteryCharge); + if (res) + { + labelBattery.Text = latestMessage.Data.Last().ToString(); + } + } + if (device.hasCleaningMode) + { + res = await PollEntry(device.cleaningMode); + if (res) + { + if (latestMessage.Data.Last() > 0) + { + cleaningStatus = true; + buttonAlarm.Text = "Выключить"; + labelAlarm.Text = "вкл"; + } + else + { + cleaningStatus = false; + buttonAlarm.Text = "Включить"; + labelAlarm.Text = "выкл"; + } + } + } + + res = await PollEntry(device.sensorsAlarm); + if (res) + { + BitArray bArray = new BitArray(latestMessage.Data); + bool[] bools = new bool[bArray.Length]; + bArray.CopyTo(bools, 0); + for (int i = 0; i < device.sensorsAlarm.length; i++) + { + Sensor snsr = sensorPanel.Controls[i] as Sensor; + snsr.labelLeak.Text = bools[i] ? "Протечка!" : "нет"; + } + } } + catch (Exception err) { MessageBox.Show(err.Message); } } async Task PollEntry(Entry entry) { - var send = Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length); + bool res = false; + Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length); isAwaitingResponse = true; - - Task delay = Task.WhenAny(Task.Delay(2000), Task.Run(() => { while (isAwaitingResponse) { } return true; })).ContinueWith((t) => + await Task.Delay(2000).ContinueWith(_ => { if (isAwaitingResponse) { - Console.WriteLine("Response timed out."); + MessageBox.Show("Превышено время ожидания ответа от устройства."); + isAwaitingResponse = false; + } + }); + while (isAwaitingResponse) { continue; } + + if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) + res = true; + + Console.WriteLine("Poll attempt finished"); + return res; + } + + private async void buttonSetID_Click(object sender, EventArgs e) + { + byte newID = (byte)nudModbusID.Value; // should prevent assigning wrong ID if UpDown is fiddled with in the middle of request + Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID); + await Task.Delay(2000).ContinueWith(_ => + { + if (isAwaitingResponse) + { + MessageBox.Show("Превышено время ожидания ответа от устройства."); isAwaitingResponse = false; } return false; }); + while (isAwaitingResponse) { continue; } - return await delay; + if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) + modbusID = newID; } - private async void buttonSetID_Click(object sender, EventArgs e) - { - Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 200, (byte)nudModbusID.Value); - await Task.Run(() => + private async void buttonValve_Click(object sender, EventArgs e) + { + ushort value = isValveClosed ? (ushort)0: (ushort)0xFF00; + Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value); + + await Task.Delay(2000).ContinueWith(_ => { - while (data is null) { continue; } - if (message[1] > 0x10) //exception code check - return; - this.modbusID = data[1]; //no exception, can just take the byte - message = null; - data = null; + if (isAwaitingResponse) + { + MessageBox.Show("Превышено время ожидания ответа от устройства."); + isAwaitingResponse = false; + } + return false; }); - + while (isAwaitingResponse) { continue; } + + if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) + { + isValveClosed = !isValveClosed; + labelValve.Text = isValveClosed ? "Закрыт" : "Открыт"; + buttonValve.Text = isValveClosed ? "Открыть" : "Закрыть"; + } } - private void buttonValve_Click(object sender, EventArgs e) + private async void buttonAlarm_Click(object sender, EventArgs e) { - ushort value = isValveClosed ? (ushort)0 : (ushort)1; - Modbus.WriteSingleAsync((FunctionCode)device.valveStatus.registerType, modbusID, device.valveStatus.address, value); + ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00; + Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value); + + await Task.Delay(2000).ContinueWith(_ => + { + if (isAwaitingResponse) + { + MessageBox.Show("Превышено время ожидания ответа от устройства."); + isAwaitingResponse = false; + } + return false; + }); + while (isAwaitingResponse) { continue; } + + if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) + { + alarmStatus = !alarmStatus; + labelAlarm.Text = alarmStatus ? "Протечка!" : "Нет"; + buttonAlarm.Text = alarmStatus ? "Выключить" : "Авария"; + } } - private void buttonAlarm_Click(object sender, EventArgs e) + private async void buttonCleaning_Click(object sender, EventArgs e) { - ushort value = alarmStatus ? (ushort)0 : (ushort)1; - Modbus.WriteSingleAsync((FunctionCode)device.valveStatus.registerType, modbusID, device.alarmStatus.address, value); + ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00; + Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value); + + await Task.Delay(2000).ContinueWith(_ => + { + if (isAwaitingResponse) + { + MessageBox.Show("Превышено время ожидания ответа от устройства."); + isAwaitingResponse = false; + } + return false; + }); + while (isAwaitingResponse) { continue; } + + if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) + { + cleaningStatus = !cleaningStatus; + labelCleaning.Text = cleaningStatus ? "вкл" : "выкл"; + buttonCleaning.Text = cleaningStatus ? "Выключить" : "Включить"; + } } + } + public class Sensor : FlowLayoutPanel + { + public Label labelName = new Label() { Width = 60, Height = 24 }; + public Label labelLeakFluff = new Label() { Width = 60, Height = 24 }; + public Label labelLeak = new Label() { Width = 50, Height = 24 }; + } + public class WiredSensor : Sensor + { - private void buttonCleaning_Click(object sender, EventArgs e) + public Label labelBreakFluff = new Label() { Width = 45, Height = 24 }; + public Label labelBreak = new Label() { Width = 55, Height = 24 }; // обрыв линии для WSP+ + + public Label labelWSPPlusFluff = new Label() { Width = 45, Height = 24 }; + public CheckBox wspPlusCheckbox = new CheckBox() { Width = 20, Height = 20 }; + public WiredSensor(int count) { + this.Margin = Padding.Empty; + this.Padding = new Padding(0, 5, 0, 0); + this.WrapContents = false; + this.BackColor = Color.White; + this.Height = 15; + this.FlowDirection = FlowDirection.LeftToRight; + this.Controls.Add(labelName); - } + this.Controls.Add(labelBreakFluff); + this.Controls.Add(labelBreak); + + this.Controls.Add(labelLeakFluff); + this.Controls.Add(labelLeak); + + this.Controls.Add(labelWSPPlusFluff); + this.Controls.Add(wspPlusCheckbox); + + labelName.Text = "WSP " + (count + 1); + + labelLeakFluff.Text = "Протечка:"; + labelLeak.Text = "неизвестно"; + + labelBreakFluff.Text = "Обрыв:"; + labelBreak.Text = "неизвестно"; + + labelWSPPlusFluff.Text = "WSP+:"; + } + } + + public class WirelessSensor : Sensor + { + + public Label labelBatteryFluff = new Label() { Width = 55, Height = 24 }; + public Label labelBattery = new Label() { Width = 45, Height = 24 }; + + public WirelessSensor(int count) + { + this.Margin = Padding.Empty; + this.Padding = new Padding(0, 5, 0, 0); + this.BackColor = Color.White; + this.FlowDirection = FlowDirection.LeftToRight; + this.WrapContents = false; + + this.Controls.Add(labelName); + this.Controls.Add(labelBatteryFluff); + this.Controls.Add(labelBattery); + this.Controls.Add(labelLeakFluff); + this.Controls.Add(labelLeak); + + labelName.Text = "WSR " + (count + 1); + + labelLeakFluff.Text = "Протечка:"; + labelLeak.Text = "неизвестно"; + + labelBatteryFluff.Text = "Батарея:"; + labelBattery.Text = "???%"; + } + } + + public class ScenarioSensor : Sensor + { + public ScenarioSensor() + { + labelName.Width = 172; + this.Margin = Padding.Empty; + this.Padding = new Padding(0, 5, 0, 0); + this.FlowDirection = FlowDirection.LeftToRight; + this.WrapContents = false; + + this.Controls.Add(labelName); + this.Controls.Add(labelLeakFluff); + this.Controls.Add(labelLeak); + + labelName.Text = "Сценарный датчик"; + + labelLeakFluff.Text = "Протечка:"; + labelLeak.Text = "неизвестно"; + } } } \ No newline at end of file diff --git a/Gidrolock Modbus Configurator.csproj b/Gidrolock Modbus Configurator.csproj index 553e1f6..24212c9 100644 --- a/Gidrolock Modbus Configurator.csproj +++ b/Gidrolock Modbus Configurator.csproj @@ -6,8 +6,8 @@ AnyCPU {47A399D3-30C1-4EF4-9E94-F3518919A59C} WinExe - Gidrolock_Modbus_Scanner - Gidrolock Modbus Scanner + Gidrolock_Modbus_Configurator + Gidrolock Modbus Configurator v4.8 512 true @@ -98,6 +98,7 @@ True Resources.resx + True diff --git a/Main.cs b/Main.cs index 45c4a1b..e8479bb 100644 --- a/Main.cs +++ b/Main.cs @@ -16,15 +16,11 @@ namespace Gidrolock_Modbus_Scanner public short[] res = new short[12]; SerialPort port = Modbus.port; public int expectedLength = 0; + ModbusResponseEventArgs latestMessage; - public byte[] latestMessage; public Dictionary models = new Dictionary(); - byte[] message = null; - byte[] data = null; - - - DateTime dateTime; + DateTime dateTime; Datasheet datasheet; #region Initialization @@ -38,9 +34,9 @@ namespace Gidrolock_Modbus_Scanner TextBox_Log.Text = "Приложение готово к работе."; cBoxDevice.Items.Add("Standard"); - cBoxDevice.Items.Add("Inteli"); cBoxDevice.Items.Add("Premium Plus"); - cBoxDevice.Items.Add("Premium"); + //cBoxDevice.Items.Add("Inteli"); + //cBoxDevice.Items.Add("Premium"); cBoxDevice.SelectedIndex = 0; checkboxID.Checked = false; @@ -50,8 +46,8 @@ namespace Gidrolock_Modbus_Scanner UpDown_ModbusID.Maximum = 247; models.Add("Standard", "STW485"); + models.Add("Premium Plus", "PRPLS1"); models.Add("Inteli", "INTELI"); - models.Add("Premuim Plus", "BUP485"); models.Add("Premium", "BUP485"); /* - Version Check - */ @@ -62,8 +58,8 @@ namespace Gidrolock_Modbus_Scanner Modbus.ResponseReceived += (sndr, msg) => { - message = msg.Message; - data = msg.Data; + latestMessage = msg; + isAwaitingResponse = false; }; } @@ -119,18 +115,43 @@ namespace Gidrolock_Modbus_Scanner // else parse response to unicode and go through every .json // if matching model is found, instantiate device window - // setup event listener - - // send message + string selectedModel = models[cBoxDevice.SelectedItem.ToString()]; + int selectedIndex = cBoxDevice.SelectedIndex; AddLog("Проверка модели устройства."); - - if (await PollModel()) + await Task.Run(async () => { - Console.WriteLine("true"); + // send message + latestMessage = null; + isAwaitingResponse = true; + var send = Modbus.ReadRegAsync((byte)UpDown_ModbusID.Value, FunctionCode.ReadInput, 200, 6); + await Task.Delay(2000).ContinueWith(_ => + { + if (isAwaitingResponse) + { + isAwaitingResponse = false; + MessageBox.Show("Истекло время ожидания ответа от устройства."); + } + return; + }); + + while (isAwaitingResponse) { continue; } + + if (latestMessage is null) + { + Console.WriteLine("latestMessage is still null"); + return; + } + + if (latestMessage.Status == ModbusStatus.Error) + return; + + // confirm the model - string response = ByteArrayToUnicode(data); - if (response != models[cBoxDevice.SelectedItem.ToString()]) + string response = ByteArrayToUnicode(latestMessage.Data); + Console.WriteLine("device model response: " + response); + Console.WriteLine("expected response: " + selectedModel); + if (response != selectedModel) { // response doesn't match expected model AddLog("Ответ устройства не соответствует выбранной модели. Поиск подходящей модели."); @@ -169,39 +190,22 @@ namespace Gidrolock_Modbus_Scanner else { // model is correct, instantiate config panel - int index = cBoxDevice.SelectedIndex; AddLog("Устройство соответстует модели, открываю панель конфигурации."); - Device device = GetDevice((DeviceType)index); + Device device = GetDevice((DeviceType)selectedIndex); StartDatasheet(device); } - } + }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } - async Task PollModel() - { - var send = Modbus.ReadRegAsync((byte)UpDown_ModbusID.Value, FunctionCode.ReadInput, 200, 6); - isAwaitingResponse = true; - Task delay = Task.WhenAny(Task.Delay(2000), Task.Run(() => { while (isAwaitingResponse) { } return true; })).ContinueWith((t) => - { - if (isAwaitingResponse) - { - Console.WriteLine("Response timed out."); - isAwaitingResponse = false; - } - return false; - }); - - return await delay; - } void StartDatasheet(Device device) { datasheet = new Datasheet((byte)UpDown_ModbusID.Value, device); - datasheet.Show(); + Application.Run(datasheet); } @@ -296,12 +300,14 @@ namespace Gidrolock_Modbus_Scanner // stupid fucking Encoding class does byte-by-byte conversion List result = new List(input.Length / 2); byte[] flip = input; - Array.Reverse(flip); // stupid fucking BitConverter is little-endian and spits out chinese nonsense otherwise - for (int i = 0; i < flip.Length; i += 2) + //Array.Reverse(flip); // stupid fucking BitConverter is little-endian and spits out chinese nonsense otherwise + for (int i = 0; i < flip.Length; i++) { - result.Add(BitConverter.ToChar(flip, i)); + if (flip[i] == 0x00) + continue; + else result.Add(Convert.ToChar(flip[i])); } - result.Reverse(); + //result.Reverse(); return new string(result.ToArray()); } @@ -338,8 +344,7 @@ namespace Gidrolock_Modbus_Scanner switch (dt) { case DeviceType.Standard: - d.modelName = "Standard"; - d.name = "Gidrolock Standard Wi-Fi RS485"; + d.name = "Standard Wi-Fi RS485"; d.id = 30; d.modelName = "STW485"; @@ -362,7 +367,22 @@ namespace Gidrolock_Modbus_Scanner d.modelName = "Inteli"; break; case DeviceType.PremiumPlus: - d.modelName = "Premium Plus"; + d.name = "Premium Plus Wi-Fi"; + d.id = 30; + d.modelName = "PRPLS1"; + + d.valveStatus = new Entry(RegisterType.Coil, 1202); + d.alarmStatus = new Entry(RegisterType.Coil, 1201); + + d.hasCleaningMode = true; + d.cleaningMode = new Entry(RegisterType.Coil, 3); + + d.hasBattery = true; + d.batteryCharge = new Entry(RegisterType.Input, 1207); + + d.wiredSensors = 7; + d.hasScenarioSensor = true; + d.sensorsAlarm = new Entry(RegisterType.Discrete, 1343, 29); break; case DeviceType.Premium: d.modelName = "Premium"; @@ -378,4 +398,4 @@ namespace Gidrolock_Modbus_Scanner public enum FunctionCode { ReadCoil = 1, ReadDiscrete = 2, ReadHolding = 3, ReadInput = 4, WriteCoil = 5, WriteRegister = 6, WriteMultCoils = 15, WriteMultRegisters = 16 }; //public enum SelectedPath { File, Folder }; -public enum DeviceType { Standard, Inteli, PremiumPlus, Premium }; +public enum DeviceType { Standard, PremiumPlus, Inteli, Premium }; diff --git a/Model.cs b/Model.cs index 3e6af8e..189d86f 100644 --- a/Model.cs +++ b/Model.cs @@ -23,7 +23,7 @@ namespace Gidrolock_Modbus_Scanner public bool hasScenarioSensor; public Entry sensorsAlarm; - public Entry wiredLineBreak; + public List wiredLineBreak; public Entry radioStatus; } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 9fb9ef1..aeb7d33 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -6,12 +6,12 @@ using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("Gidrolock Modbus Scanner")] -[assembly: AssemblyDescription("A collection of Modbus tools.")] +[assembly: AssemblyTitle("Gidrolock Modbus Configurator")] +[assembly: AssemblyDescription("A diagnosting modbus tool for Gidrolock valve controllers.")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("nikzori")] -[assembly: AssemblyProduct("Gidrolock Modbus Scanner")] -[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyCompany("Gidrolock")] +[assembly: AssemblyProduct("Gidrolock Modbus Configurator")] +[assembly: AssemblyCopyright("Copyright © 2025")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 3551e64..b9a4de3 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -8,10 +8,10 @@ // //------------------------------------------------------------------------------ -namespace Gidrolock_Modbus_Scanner.Properties -{ - - +namespace Gidrolock_Modbus_Configurator.Properties { + using System; + + /// /// A strongly-typed resource class, for looking up localized strings, etc. /// @@ -19,51 +19,43 @@ namespace Gidrolock_Modbus_Scanner.Properties // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources - { - + internal class Resources { + private static global::System.Resources.ResourceManager resourceMan; - + private static global::System.Globalization.CultureInfo resourceCulture; - + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() - { + internal Resources() { } - + /// /// Returns the cached ResourceManager instance used by this class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager - { - get - { - if ((resourceMan == null)) - { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Gidrolock_Modbus_Scanner.Properties.Resources", typeof(Resources).Assembly); + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Gidrolock_Modbus_Configurator.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } - + /// /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture - { - get - { + internal static global::System.Globalization.CultureInfo Culture { + get { return resourceCulture; } - set - { + set { resourceCulture = value; } } diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index 777d224..6b9c2f5 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -8,21 +8,17 @@ // //------------------------------------------------------------------------------ -namespace Gidrolock_Modbus_Scanner.Properties -{ - - +namespace Gidrolock_Modbus_Configurator.Properties { + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase - { - + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.11.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default - { - get - { + + public static Settings Default { + get { return defaultInstance; } }