Stop devices that were already started in case of an exception when starting devices --> avoid problems when closing the program.

This commit is contained in:
Milan Hanajik 2017-01-06 13:05:27 +01:00
parent dd85f03328
commit 21ee6b7a65
5 changed files with 70 additions and 18 deletions

View File

@ -58,6 +58,9 @@ namespace DeviceTest
{
compClasses.Add(componentFactory.ClassName);
}
tbfComponents = new List<TBF.BenchControl.Generic.IComponent>();
tbfDevices = new List<IDevice>();
}
private void DeviceTestDlg_Load(object sender, EventArgs e)
@ -262,8 +265,8 @@ namespace DeviceTest
private void startButton_Click(object sender, EventArgs e)
{
tbfComponents = new List<TBF.BenchControl.Generic.IComponent>();
tbfDevices = new List<IDevice>();
tbfComponents.Clear();
tbfDevices.Clear();
tbfComponent4Op = null;
operation1 = null;
operation2 = null;
@ -278,24 +281,30 @@ namespace DeviceTest
{
TBF.BenchControl.Generic.IComponent component = parentFactory.GetComponent(parentCfg, tbfComponents);
tbfComponents.Add(component);
if (component is IDevice) tbfDevices.Add(component as IDevice);
IDevice dev = component as IDevice;
if (dev != null)
{
initializedDeviceName = dev.Name;
dev.Initialize();
tbfDevices.Add(dev);
}
}
if (componentFactory != null && componentCfg != null)
{
TBF.BenchControl.Generic.IComponent component = componentFactory.GetComponent(componentCfg, tbfComponents);
tbfComponents.Add(component);
tbfComponent4Op = component;
if (component is IDevice) tbfDevices.Add(component as IDevice);
IDevice dev = component as IDevice;
if (dev != null)
{
initializedDeviceName = dev.Name;
dev.Initialize();
tbfDevices.Add(dev);
}
}
if (tbfComponents.Count == 0) throw new Exception("There are no components");
foreach (var d in tbfDevices)
{
initializedDeviceName = d.Name;
d.Initialize();
}
SaveSettings(); /// Save settings after successful initialization
workerThread = new Thread(Worker);
@ -319,6 +328,9 @@ namespace DeviceTest
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
foreach (var dev in tbfDevices) dev.StopDevice();
tbfDevices.Clear();
startButton.Enabled = true;
stopButton.Enabled = false;
configureButton.Enabled = true;
@ -399,7 +411,7 @@ namespace DeviceTest
Cursor = Cursors.WaitCursor;
workerThreadRunning = false;
workerThread.Join(3000);
workerThread.Join(5000);
workerThread = null;
Cursor = Cursors.Default;
@ -421,6 +433,8 @@ namespace DeviceTest
workerThread = null;
}
Cursor = Cursors.Default;
SaveSettings();
Cursor = Cursors.Default;

View File

@ -14,6 +14,9 @@ namespace TBF.BenchControl.Network.Adapter
return string.Format("Component={0} IPAddress={1} NetMask={2} Description={3}", Cfg.Name, IPAddress, NetMask, (Cfg as NetadapterCfg).Description);
}
public bool Running { get { return running; } }
bool running;
readonly NetadapterCfg netadapterCfg;
readonly IPAddress ipAddress;
readonly IPAddress netMask;
@ -39,6 +42,8 @@ namespace TBF.BenchControl.Network.Adapter
netMask = netadapter.NetMask;
broadcastAddress = netadapter.GetBroadcastAddress();
running = false;
log.Debug(this.ToString());
}
@ -57,6 +62,10 @@ namespace TBF.BenchControl.Network.Adapter
tftpServer = null;
}
}
running = true;
log.FatalFormat("Successfully initialized device {0}", ToString());
}
public void StopDevice()
@ -65,6 +74,8 @@ namespace TBF.BenchControl.Network.Adapter
{
if (tftpServer != null) tftpServer.Stop();
}
running = false;
}
public void RunDeviceBefore() {}

View File

@ -15,10 +15,6 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
{
public class Camera : ComponentBase, Generic.IDevice
{
const string ClpUserName = "tbf";
const string ClpPassword = "C1ern4V0d4";
const string ClpPrompt = "$ ";
private static readonly ILog log = LogManager.GetLogger(typeof(Camera));
public override string ToString()
{
@ -31,6 +27,14 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
string.IsNullOrEmpty(image) ? "not detected" : image);
}
public bool Running { get { return running; } }
bool running;
const string ClpUserName = "tbf";
const string ClpPassword = "C1ern4V0d4";
const string ClpPrompt = "$ ";
readonly CameraCfg cameraCfg;
readonly GenericDevices.INetworkAdapter netAdapter;
@ -65,6 +69,11 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
netAdapter = TbfComponents.FindComponent(cfg.ParentName, components) as GenericDevices.INetworkAdapter;
if (netAdapter == null) throw new ArgumentNullException("no network adapter");
telnet = null;
terminalDlg = null;
running = false;
log.Debug(this.ToString());
}
@ -74,10 +83,12 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
{
if (cameraCfg.DebugLevel == DebugMode.Simulate) return;
/// Detect a camera
bool cameraDetected = DetectCamera(cameraCfg.HardwareAddress, true,
out ipAddress, out hardware, out revision, out serial, out image);
if (!cameraDetected) throw new Exception("No camera detected");
/// Open telnet clint and start the log-in process
telnet = new Telnet.TelnetClient(this, cameraCfg.Name, ClpUserName, ClpPassword, ClpPrompt);
if (cameraCfg.DisplayTerminal)
@ -88,6 +99,8 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.CONNECT, ipAddress.ToString(), 10, 30));
running = true;
log.FatalFormat("Successfully initialized device {0}", ToString());
}
@ -104,6 +117,8 @@ namespace TBF.BenchControl.Network.Camera.CLP1611
telnet.Dispose();
telnet = null;
}
running = false;
}
public void RunDeviceBefore() {}

View File

@ -279,8 +279,8 @@ namespace TBF.BenchControl
if (cmpnt is IDevice)
{
AddDevice(cmpnt as IDevice);
(cmpnt as IDevice).Initialize();
AddDevice(cmpnt as IDevice); /// Only components that were initialized are added
}
}
@ -303,6 +303,17 @@ namespace TBF.BenchControl
return null;
}
/// <summary>
/// Stop devices that were started by InitializeDevices().
/// Works correctly also after exeption from InitializeDevices() as only ...
/// ... correctly started devices were added in 'devices' list.
/// </summary>
public static void StopDevices()
{
foreach (var dev in devices) dev.StopDevice();
}
/// <summary>
/// Start the state machine
/// </summary>

View File

@ -478,6 +478,7 @@ namespace TBF
MessageBoxIcon.Question))
{
BenchControl.StateMachine.Stop();
BenchControl.StateMachine.StopDevices();
UiBridge.Bridge.OnEmergencyStopChanged(this, UiBridge.EmergencyStopEventArgs.State.CloseForm);
new Forms.ClosingProgram().ShowDialog();
DialogResult = DialogResult.OK;