aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Emergency
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2019-08-04 12:59:34 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2019-08-04 12:59:34 +0300
commitc2471c200471e62b80133542183eb14af7c79bc7 (patch)
treeb8171eb2bbc94cac456c4b653efad315e17ae681 /Software/Visual_Studio/Tango.Integration/Emergency
parenta21bffd43acbffcec36cc34bc8d03c2eb8358769 (diff)
downloadTango-c2471c200471e62b80133542183eb14af7c79bc7.tar.gz
Tango-c2471c200471e62b80133542183eb14af7c79bc7.zip
Implemented emergency switch.
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/Emergency')
-rw-r--r--Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatus.cs15
-rw-r--r--Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatusChangedEventArgs.cs14
-rw-r--r--Software/Visual_Studio/Tango.Integration/Emergency/IEmergencyNotificationProvider.cs34
-rw-r--r--Software/Visual_Studio/Tango.Integration/Emergency/UsbEmergencyNotificationProvider.cs139
4 files changed, 202 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatus.cs b/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatus.cs
new file mode 100644
index 000000000..85c35056f
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatus.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Integration.Emergency
+{
+ public enum EmergencyStatus
+ {
+ Error,
+ Off,
+ On,
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatusChangedEventArgs.cs b/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatusChangedEventArgs.cs
new file mode 100644
index 000000000..aad8ab4b7
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Integration/Emergency/EmergencyStatusChangedEventArgs.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Integration.Emergency
+{
+ public class EmergencyStatusChangedEventArgs
+ {
+ public EmergencyStatus Status { get; set; }
+ public Exception ErrorException { get; set; }
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Integration/Emergency/IEmergencyNotificationProvider.cs b/Software/Visual_Studio/Tango.Integration/Emergency/IEmergencyNotificationProvider.cs
new file mode 100644
index 000000000..9d8f0d421
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Integration/Emergency/IEmergencyNotificationProvider.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Integration.Emergency
+{
+ /// <summary>
+ /// Represents a machine emergency notification provider.
+ /// </summary>
+ public interface IEmergencyNotificationProvider
+ {
+ /// <summary>
+ /// Gets or sets a value indicating whether to enable emergency detection and notification.
+ /// </summary>
+ bool IsEnabled { get; set; }
+
+ /// <summary>
+ /// Gets or sets the address/port of the detection device.
+ /// </summary>
+ String Address { get; set; }
+
+ /// <summary>
+ /// Gets or sets the current emergency status.
+ /// </summary>
+ EmergencyStatus Status { get; set; }
+
+ /// <summary>
+ /// Occurs when the emergency status has changed.
+ /// </summary>
+ event EventHandler<EmergencyStatusChangedEventArgs> StatusChanged;
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Integration/Emergency/UsbEmergencyNotificationProvider.cs b/Software/Visual_Studio/Tango.Integration/Emergency/UsbEmergencyNotificationProvider.cs
new file mode 100644
index 000000000..ece9d39bb
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Integration/Emergency/UsbEmergencyNotificationProvider.cs
@@ -0,0 +1,139 @@
+using System;
+using System.Collections.Generic;
+using System.IO.Ports;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Timers;
+
+namespace Tango.Integration.Emergency
+{
+ /// <summary>
+ /// Represents a USB serial port emergency switch notification provider.
+ /// </summary>
+ /// <seealso cref="Tango.Integration.Emergency.IEmergencyNotificationProvider" />
+ public class UsbEmergencyNotificationProvider : IEmergencyNotificationProvider
+ {
+ private Timer _timer;
+ private bool _busy;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to enable emergency detection and notification.
+ /// </summary>
+ public bool IsEnabled { get; set; }
+
+ /// <summary>
+ /// Gets or sets the current emergency status.
+ /// </summary>
+ public EmergencyStatus Status { get; set; }
+
+ /// <summary>
+ /// Gets or sets the address/port of the detection device.
+ /// </summary>
+ public string Address { get; set; }
+
+ /// <summary>
+ /// Gets or sets the read timeout for the loop-back signal.
+ /// </summary>
+ public TimeSpan ReadTimeout { get; set; }
+
+ /// <summary>
+ /// Gets or sets the signal output.
+ /// </summary>
+ public String SignalOutput { get; set; }
+
+ /// <summary>
+ /// Occurs when the emergency status has changed.
+ /// </summary>
+ public event EventHandler<EmergencyStatusChangedEventArgs> StatusChanged;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="UsbEmergencyNotificationProvider"/> class.
+ /// </summary>
+ /// <param name="port">The port.</param>
+ public UsbEmergencyNotificationProvider(String port)
+ {
+ SignalOutput = "1";
+ Address = port;
+ ReadTimeout = TimeSpan.FromMilliseconds(500);
+ _timer = new Timer(2000);
+ _timer.Elapsed += _timer_Elapsed;
+ _timer.Start();
+ }
+
+ /// <summary>
+ /// Called when the status has been changed.
+ /// </summary>
+ /// <param name="status">The status.</param>
+ /// <param name="errorException">The error exception.</param>
+ protected virtual void OnStatusChanged(EmergencyStatus status, Exception errorException = null)
+ {
+ if (Status != status)
+ {
+ Status = status;
+ StatusChanged?.Invoke(this, new EmergencyStatusChangedEventArgs()
+ {
+ Status = status,
+ ErrorException = errorException,
+ });
+ }
+ }
+
+ /// <summary>
+ /// Handles the Elapsed event of the _timer.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
+ private void _timer_Elapsed(object sender, ElapsedEventArgs e)
+ {
+ if (IsEnabled && !_busy)
+ {
+ _busy = true;
+
+ SerialPort serial = new SerialPort(Address);
+ try
+ {
+ serial.ReadTimeout = (int)ReadTimeout.TotalMilliseconds;
+ serial.Open();
+
+ serial.Write(SignalOutput + "\n");
+
+ try
+ {
+ string output = serial.ReadLine();
+
+ if (output == SignalOutput)
+ {
+ OnStatusChanged(EmergencyStatus.On);
+ }
+ else
+ {
+ OnStatusChanged(EmergencyStatus.Off);
+ }
+ }
+ catch (TimeoutException)
+ {
+ OnStatusChanged(EmergencyStatus.Off);
+ }
+ catch (Exception ex)
+ {
+ OnStatusChanged(EmergencyStatus.Error, ex);
+ }
+ }
+ catch (Exception ex)
+ {
+ OnStatusChanged(EmergencyStatus.Error, ex);
+ }
+ finally
+ {
+ try
+ {
+ serial.Dispose();
+ _busy = false;
+ }
+ catch { }
+ }
+ }
+ }
+ }
+}