diff --git a/charger-gui/Logger.cs b/charger-gui/Logger.cs
new file mode 100644
index 0000000..2fb7088
--- /dev/null
+++ b/charger-gui/Logger.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace charger_gui
+{
+ class Logger
+ {
+ private string fileName;
+
+ public Logger(string fileName)
+ {
+ this.fileName = fileName;
+ }
+
+ public void Log(string s)
+ {
+ var toLog = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + s + "\n";
+ File.AppendAllText(fileName, toLog);
+ }
+ }
+}
diff --git a/charger-gui/MainWindow.xaml b/charger-gui/MainWindow.xaml
index 9e4789b..fd02aa5 100644
--- a/charger-gui/MainWindow.xaml
+++ b/charger-gui/MainWindow.xaml
@@ -16,6 +16,7 @@
+
@@ -30,15 +31,37 @@
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/charger-gui/MainWindow.xaml.cs b/charger-gui/MainWindow.xaml.cs
index 39c24ad..9f8e026 100644
--- a/charger-gui/MainWindow.xaml.cs
+++ b/charger-gui/MainWindow.xaml.cs
@@ -1,4 +1,5 @@
-using System;
+using charger_gui.operations;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -22,33 +23,61 @@ namespace charger_gui
public partial class MainWindow : Window
{
private UpdateWorker worker;
- private DispatcherTimer updateTimer;
+ private Logger log;
MainWindowViewModel model;
public MainWindow()
{
InitializeComponent();
+ log = new Logger("charger.log");
model = new MainWindowViewModel();
DataContext = model;
worker = new UpdateWorker(model);
- updateTimer = new DispatcherTimer();
- updateTimer.Tick += OnTimer;
- updateTimer.Interval = TimeSpan.FromMilliseconds(50);
- updateTimer.Start();
- }
-
- private void OnTimer(object sender, EventArgs e)
- {
- model.OnPropertyChanged("IAcc");
- model.OnPropertyChanged("UAcc");
- model.OnPropertyChanged("ILamp");
- model.OnPropertyChanged("ULamp");
- model.OnPropertyChanged("ConnectionStatus");
}
private void Ports_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
worker.SetPort(Ports.SelectedItem.ToString());
}
+
+ private void LampButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (model.CurrentOperation != null)
+ {
+ model.CurrentOperation.Cancel();
+ }
+ model.CurrentOperation = null;
+ model.IsLampOn = !model.IsLampOn;
+ }
+
+ private void FlipChargeButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (model.CurrentOperation != null)
+ {
+ model.CurrentOperation.Cancel();
+ }
+ model.CurrentOperation = null;
+ model.IsCharging = !model.IsCharging;
+ }
+
+ private void DischargeButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (model.CurrentOperation != null)
+ {
+ model.CurrentOperation.Cancel();
+ }
+ model.CurrentOperation = new DischargeOperation(log, model);
+ model.CurrentOperation.Start();
+ }
+
+ private void ChargeButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (model.CurrentOperation != null)
+ {
+ model.CurrentOperation.Cancel();
+ }
+ model.CurrentOperation = new ChargeOperation(log, model);
+ model.CurrentOperation.Start();
+ }
}
}
diff --git a/charger-gui/MainWindowViewModel.cs b/charger-gui/MainWindowViewModel.cs
index a943864..73ae1ff 100644
--- a/charger-gui/MainWindowViewModel.cs
+++ b/charger-gui/MainWindowViewModel.cs
@@ -1,4 +1,5 @@
-using System;
+using charger_gui.operations;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
@@ -23,43 +24,102 @@ namespace charger_gui
}
}
- public float iAcc;
- public string IAcc
+ private float iAcc;
+ public float IAcc
{
get
{
- return string.Format("{0,8:0.0000}", iAcc);
+ return iAcc;
+ }
+ set
+ {
+ iAcc = value;
+ OnPropertyChanged("IAcc");
}
}
- public float uAcc;
- public string UAcc
+ private float u1Acc;
+ public float U1Acc
{
get
{
- return string.Format("{0,8:0.0000}", uAcc);
+ return u1Acc;
}
+ set
+ {
+ u1Acc = value;
+ OnPropertyChanged("U1Acc");
+ }
}
- public float iLamp;
- public string ILamp
+ private float u2Acc;
+ public float U2Acc
{
get
{
- return string.Format("{0,8:0.0000}", iLamp);
+ return u2Acc;
+ }
+ set
+ {
+ u2Acc = value;
+ OnPropertyChanged("U2Acc");
}
}
- public float uLamp;
- public string ULamp
+ private float iLamp;
+ public float ILamp
{
get
{
- return string.Format("{0,8:0.0000}", uLamp);
+ return iLamp;
+ }
+ set
+ {
+ iLamp = value;
+ OnPropertyChanged("ILamp");
}
}
- public bool isConnected;
+ private float u1Lamp;
+ public float U1Lamp
+ {
+ get
+ {
+ return u1Lamp;
+ }
+ set
+ {
+ u1Lamp = value;
+ OnPropertyChanged("U1Lamp");
+ }
+ }
+
+ private float u2Lamp;
+ public float U2Lamp
+ {
+ get
+ {
+ return u2Lamp;
+ }
+ set
+ {
+ u2Lamp = value;
+ OnPropertyChanged("U2Lamp");
+ }
+ }
+
+ private bool isConnected;
+ public bool IsConnected
+ {
+ set
+ {
+ if (isConnected != value)
+ {
+ isConnected = value;
+ OnPropertyChanged("ConnectionStatus");
+ }
+ }
+ }
private Brush connectedBrush = new SolidColorBrush(Color.FromRgb(0, 255, 0));
private Brush notConnectedBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0));
@@ -72,17 +132,92 @@ namespace charger_gui
}
}
+ private bool isCharging;
+ public bool IsCharging
+ {
+ get { return isCharging; }
+ set
+ {
+ if (isCharging != value)
+ {
+ isCharging = value;
+ OnPropertyChanged("ChargeStatus");
+ }
+ }
+
+ }
+ public Brush ChargeStatus
+ {
+ get
+ {
+ if (isCharging) return connectedBrush;
+ else return notConnectedBrush;
+ }
+ }
+ private bool isLampOn;
+ public bool IsLampOn
+ {
+ get { return isLampOn; }
+ set
+ {
+ if (isLampOn != value)
+ {
+ isLampOn = value;
+ OnPropertyChanged("LampStatus");
+ }
+ }
+ }
+ public Brush LampStatus
+ {
+ get
+ {
+ if (isLampOn) return connectedBrush;
+ else return notConnectedBrush;
+ }
+ }
+
+ private AutomaticOperation currentOperation;
+ public AutomaticOperation CurrentOperation
+ {
+ get
+ {
+ return currentOperation;
+ }
+ set
+ {
+ if (currentOperation != null)
+ {
+ currentOperation.StatusChanged -= OnCurrentOperationStatusChanged;
+ }
+ currentOperation = value;
+ currentOperation.StatusChanged += OnCurrentOperationStatusChanged;
+ OnPropertyChanged("OperationStatus");
+ }
+ }
+
+ private void OnCurrentOperationStatusChanged()
+ {
+ OnPropertyChanged("OperationStatus");
+ }
+
+ public string OperationStatus
+ {
+ get
+ {
+ if (currentOperation == null) return "MANUAL MODE";
+ return currentOperation.Status;
+ }
+ }
+
private void FillPorts()
{
ports = new List(SerialPort.GetPortNames());
}
public event PropertyChangedEventHandler PropertyChanged;
- public void OnPropertyChanged(string propertyName)
+ public void OnPropertyChanged(string name = null)
{
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler == null) return;
- handler(this, new PropertyChangedEventArgs(propertyName));
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
diff --git a/charger-gui/UpdateWorker.cs b/charger-gui/UpdateWorker.cs
index 5f5260b..14aed1f 100644
--- a/charger-gui/UpdateWorker.cs
+++ b/charger-gui/UpdateWorker.cs
@@ -11,6 +11,8 @@ namespace charger_gui
class UpdateWorker
{
const int SLAVE_ADDRESS = 1;
+ const int CHARGE_REGISTER = 4;
+ const int LAMP_REGISTER = 5;
private MainWindowViewModel model;
private Thread workingThread;
@@ -21,7 +23,7 @@ namespace charger_gui
isWorking = false;
shouldRestart = false;
workingThread = new Thread(Work);
- model.isConnected = false;
+ model.IsConnected = false;
}
private bool isWorking;
@@ -47,6 +49,7 @@ namespace charger_gui
private void Work()
{
+
while (true)
{
try
@@ -57,6 +60,7 @@ namespace charger_gui
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
+ serialPort.ReadTimeout = 50;
serialPort.Open();
// create modbus master
@@ -64,19 +68,23 @@ namespace charger_gui
while (!shouldRestart)
{
var r1 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 0, 4);
- model.iAcc = ToFloat(r1[0], r1[1]);
- model.iLamp = ToFloat(r1[2], r1[3]);
- var r2 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 6, 4);
- model.uAcc = ToFloat(r2[0], r2[1]);
- model.uLamp = ToFloat(r2[2], r2[3]);
+ model.IAcc = ToFloat(r1[0], r1[1]);
+ model.ILamp = ToFloat(r1[2], r1[3]);
+ var r2 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 6, 8);
+ model.U1Acc = ToFloat(r2[0], r2[1]);
+ model.U2Acc = ToFloat(r2[2], r2[3]);
+ model.U1Lamp = ToFloat(r2[4], r2[5]);
+ model.U2Lamp = ToFloat(r2[6], r2[7]);
+ master.WriteSingleRegister(SLAVE_ADDRESS, CHARGE_REGISTER, (ushort)(model.IsCharging ? 1 : 0));
+ master.WriteSingleRegister(SLAVE_ADDRESS, LAMP_REGISTER, (ushort)(model.IsLampOn ? 1 : 0));
// ничего не упало, коннект есть значит
- model.isConnected = true;
+ model.IsConnected = true;
}
}
}
catch (Exception e)
{
- model.isConnected = false;
+ model.IsConnected = false;
Thread.Sleep(100);
// will continue
}
diff --git a/charger-gui/charger-gui.csproj b/charger-gui/charger-gui.csproj
index 4218f60..e9b4428 100644
--- a/charger-gui/charger-gui.csproj
+++ b/charger-gui/charger-gui.csproj
@@ -56,6 +56,10 @@
MSBuild:Compile
Designer
+
+
+
+
MSBuild:Compile
diff --git a/charger-gui/operations/AutomaticOperation.cs b/charger-gui/operations/AutomaticOperation.cs
new file mode 100644
index 0000000..f0751ec
--- /dev/null
+++ b/charger-gui/operations/AutomaticOperation.cs
@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace charger_gui.operations
+{
+ class AutomaticOperation
+ {
+
+ private Thread workingThread;
+
+ public bool IsComplete
+ {
+ get
+ {
+ return workingThread == null || workingThread.IsAlive;
+ }
+ }
+
+ public event Action StatusChanged;
+ public virtual string Status
+ {
+ get
+ {
+ return "UNKNOWN";
+ }
+ }
+
+ protected Logger log;
+ protected MainWindowViewModel model;
+
+ public AutomaticOperation(Logger log, MainWindowViewModel model)
+ {
+ this.log = log;
+ this.model = model;
+ }
+
+ public void Start()
+ {
+ workingThread = new Thread(Work);
+ workingThread.Start();
+ }
+
+ public void Cancel()
+ {
+ if (workingThread != null)
+ {
+ workingThread.Abort();
+ }
+ }
+
+ protected void OnStatusChanged()
+ {
+ StatusChanged?.Invoke();
+ }
+
+ protected virtual void Work()
+ {
+ }
+ }
+}
diff --git a/charger-gui/operations/ChargeOperation.cs b/charger-gui/operations/ChargeOperation.cs
new file mode 100644
index 0000000..91b6734
--- /dev/null
+++ b/charger-gui/operations/ChargeOperation.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace charger_gui.operations
+{
+ class ChargeOperation : AutomaticOperation
+ {
+ const float MAX_VOLTAGE = 14.5f;
+
+ private int passed;
+ private bool isComplete = false;
+ public override string Status
+ {
+ get
+ {
+ if (isComplete) return "Charging complete in " + passed + " seconds";
+ else return "Charging, passed " + passed + " seconds";
+ }
+ }
+
+ public ChargeOperation(Logger log, MainWindowViewModel model) : base(log, model)
+ {
+ }
+
+ protected override void Work()
+ {
+ log.Log("Charging started");
+ var start = DateTime.Now;
+ model.IsCharging = true;
+ model.IsLampOn = false;
+ Thread.Sleep(500);
+ while (model.U2Acc < MAX_VOLTAGE)
+ {
+ Thread.Sleep(1000);
+ passed = (int)((DateTime.Now - start).TotalSeconds);
+ OnStatusChanged();
+ log.Log("Charging " + passed + "s " + model.U2Acc.ToString("F4"));
+ if (model.U1Acc < 10f)
+ {
+ log.Log("Charging error: NO POWER. " + model.U1Acc.ToString("F4"));
+ }
+ }
+ log.Log("Charging complete in " + passed + " seconds");
+ isComplete = true;
+ OnStatusChanged();
+ }
+ }
+}
diff --git a/charger-gui/operations/DischargeOperation.cs b/charger-gui/operations/DischargeOperation.cs
new file mode 100644
index 0000000..26a8b56
--- /dev/null
+++ b/charger-gui/operations/DischargeOperation.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace charger_gui.operations
+{
+ class DischargeOperation : AutomaticOperation
+ {
+ const float MIN_VOLTAGE = 10.8f;
+
+ private int passed;
+ private bool isComplete = false;
+ public override string Status
+ {
+ get
+ {
+ if (isComplete) return "Discharging complete in " + passed + " seconds";
+ else return "Discharging, passed " + passed + " seconds";
+ }
+ }
+
+ public DischargeOperation(Logger log, MainWindowViewModel model) : base(log, model)
+ {
+ }
+
+ protected override void Work()
+ {
+ log.Log("Discharging started");
+ var start = DateTime.Now;
+ model.IsCharging = false;
+ model.IsLampOn = true;
+ Thread.Sleep(500);
+ while (model.U2Acc > MIN_VOLTAGE)
+ {
+ Thread.Sleep(1000);
+ passed = (int)((DateTime.Now - start).TotalSeconds);
+ OnStatusChanged();
+ log.Log("Discharging " + passed + "s " + model.U2Acc.ToString("F4"));
+ }
+ log.Log("Discharging complete in " + passed + " seconds");
+ isComplete = true;
+ OnStatusChanged();
+ }
+ }
+}