Chronos Plugins  5.2.0
This documentation covers the plugin interfaces definitions and an example implementation.
MockPlugin.Device.MockDevice Class Reference

A chronos plugin implementation for a fake device. We pretend we are controlling a mixture of coffee machine and waiter robot. More...

+ Inheritance diagram for MockPlugin.Device.MockDevice:
+ Collaboration diagram for MockPlugin.Device.MockDevice:

Public Member Functions

 MockDevice ()
 
void Dispose ()
 
void BrewFrappuccino (BrewFrappuccino.CompositionData composition)
 Pretend we are doing some operation on a complex parameter set. More...
 
void SomeDummyMethod ()
 Just for testing if methods of this device can be called from some other point in our code. More...
 
void Connect ()
 Inform the user of our connect attempt / success. Instead of establishing a real connection, show some message box. More...
 
void Disconnect ()
 Pretend we are closing the connection. Actual operation substituted by a message box. More...
 
void ShowTheMessage (string messageText)
 Let our device set a status message and display some message box instead of doing real work. More...
 
void TriggerAbort (string reason, bool softStop)
 This will trigger the AbortSchedule-Event 5 seconds after it was called from a task. More...
 
void CheckForAbort ()
 
Task ApplyParametersAsync (bool waitUntilSetpointIsReached, CancellationToken canToken)
 
void WaitIfPaused ()
 
- Public Member Functions inherited from AxelSemrau.Chronos.Plugin.IHaveMachineParameters< CoffeMakerParams >
Task ApplyParametersAsync (bool waitUntilSetpointIsReached, CancellationToken cancelToken)
 Set the given parameters on the device. More...
 

Static Public Attributes

const string DeviceTypeName = "ACME Coffee Maker"
 

Properties

string DisplayedTypeName [get]
 Visible to the user on the instruments page of the settings editor. More...
 
string DeviceTypeDescription [get]
 Device class specification referred to by some messages. More...
 
string Name [get, set]
 
string Connection = "COM17" [get, set]
 Connection as set in the Chronos instrument settings. More...
 
bool IsConnected [get, set]
 Helper for our toolbox. More...
 
CoffeMakerParams Parameters = new CoffeMakerParams() [get, set]
 
bool Aborted [get, set]
 
bool Paused [get, set]
 
IEnumerable< string > LogPaths [get]
 
Func< StopRunArgs, Task > StopRun [get, set]
 
IScheduleEvents ScheduleEvents [set]
 
- Properties inherited from AxelSemrau.Chronos.Plugin.IDevice
string DisplayedTypeName [get]
 Text which is displayed in the instruments settings "Autosampler Type" column and in many other places. More...
 
string DeviceTypeDescription [get]
 Text which is displayed to make clear which kind of device a named sampler is. More...
 
string Name [get, set]
 User-selected name for the device instance. More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.INeedAConnection
string Connection [get, set]
 Name of a serial port, IP address, hostname.... More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.ICanInterrupt
bool Aborted [get, set]
 If set, abort execution as soon as possible. You can throw an OperationCanceledException. Well be set to false after termination of the run. More...
 
bool Paused [get, set]
 If paused, wait until paused is reset before executing the next command. More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.IHaveMachineParameters< CoffeMakerParams >
ParamType Parameters [get, set]
 Your parameter set. More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.IProvideDiagnosticLogs
IEnumerable< string > LogPaths [get]
 Provide full paths to each of your log files here. More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.IStopRuns
Func< StopRunArgs, Task > StopRun [set]
 Callback function returning a task that completes once the schedule queue was stopped. More...
 
- Properties inherited from AxelSemrau.Chronos.Plugin.IScheduleStateAware
IScheduleEvents ScheduleEvents [set]
 Hook up immediately or save this for later use. More...
 

Events

Action< ConnectionStateConnectionStateChanged
 
Action< string > SetStatusMessage
 
Action< string > DebugOutput
 
PropertyChangedEventHandler PropertyChanged
 
Action< string > AbortSchedule
 
- Events inherited from AxelSemrau.Chronos.Plugin.IDevice
Action< ConnectionStateConnectionStateChanged
 Raise this event when your connection state has changed. More...
 
- Events inherited from AxelSemrau.Chronos.Plugin.IProvideStatusMessages
Action< string > SetStatusMessage
 Raise this event to set your current status while doing something on the device. Messages passed to this event will be shown in the "Autosampler status" line. More...
 
- Events inherited from AxelSemrau.Chronos.Plugin.IHaveDebugOutput
Action< string > DebugOutput
 Messages passed to this event will be logged (including a timestamp) to the user configured debug logfile. More...
 
- Events inherited from AxelSemrau.Chronos.Plugin.IAbortSchedules
Action< string > AbortSchedule
 If you raise this event, the schedule will be aborted. More...
 

Private Member Functions

void OnIsConnectedChanged ()
 For thread-safe update of the toolbox's GUI elements representing the connection state. More...
 
void ScheduleStateChangedHandler (object sender, ScheduleStateEventArgs e)
 

Private Attributes

bool mIsConnected
 
readonly ManualResetEventSlim mPauseEnded = new ManualResetEventSlim(true)
 Using an event here instead of a simple bool helps us avoid polling while checking for the events. More...
 
readonly ManualResetEventSlim mAborted = new ManualResetEventSlim(false)
 
IScheduleEvents mScheduleEvents
 

Detailed Description

A chronos plugin implementation for a fake device. We pretend we are controlling a mixture of coffee machine and waiter robot.

Just an example implementation that doesn't do any real work.

Definition at line 37 of file MockDevice.cs.

Constructor & Destructor Documentation

◆ MockDevice()

MockPlugin.Device.MockDevice.MockDevice ( )

Definition at line 61 of file MockDevice.cs.

62  {
63  Instances.Add(this);
64  }

Member Function Documentation

◆ ApplyParametersAsync()

Task MockPlugin.Device.MockDevice.ApplyParametersAsync ( bool  waitUntilSetpointIsReached,
CancellationToken  canToken 
)

Definition at line 265 of file MockDevice.cs.

266  {
267 
268  SetStatusMessage?.Invoke($"Applying parameters: '{Parameters}' and {(!waitUntilSetpointIsReached ? "not " : "")} waiting for setpoint.");
269  if (waitUntilSetpointIsReached)
270  {
271  return Task.Delay(TimeSpan.FromSeconds(10),canToken);
272  }
273  return Task.CompletedTask;
274  }
Action< string > SetStatusMessage
Definition: MockDevice.cs:190

References MockPlugin.Device.MockDevice.SetStatusMessage.

◆ BrewFrappuccino()

void MockPlugin.Device.MockDevice.BrewFrappuccino ( BrewFrappuccino.CompositionData  composition)

Pretend we are doing some operation on a complex parameter set.

Parameters
composition

Definition at line 94 of file MockDevice.cs.

95  {
96  WaitIfPaused();
98  $"Making a {composition.Volume} mL frappuccino with cream type \"{composition.Cream}\"{(composition.DeCaffeinated ? ", decaffeinated." : "")}.");
99  }
void ShowTheMessage(string messageText)
Let our device set a status message and display some message box instead of doing real work.
Definition: MockDevice.cs:156

References MockPlugin.Device.MockDevice.ShowTheMessage(), and MockPlugin.Device.MockDevice.WaitIfPaused().

Referenced by MockPlugin.Tasks.BrewFrappuccino.Execute().

◆ CheckForAbort()

void MockPlugin.Device.MockDevice.CheckForAbort ( )

Definition at line 251 of file MockDevice.cs.

252  {
253  if (mAborted.IsSet)
254  {
255  throw new OperationCanceledException("Aborted");
256  }
257  }
readonly ManualResetEventSlim mAborted
Definition: MockDevice.cs:291

References MockPlugin.Device.MockDevice.mAborted.

Referenced by MockPlugin.Device.MockDevice.WaitIfPaused().

◆ Connect()

void MockPlugin.Device.MockDevice.Connect ( )

Inform the user of our connect attempt / success. Instead of establishing a real connection, show some message box.

Implements AxelSemrau.Chronos.Plugin.IDevice.

Definition at line 112 of file MockDevice.cs.

113  {
115  MessageBox.Show(Helpers.Gui.MainWindow, $"Device {Name} was connected to {Connection}.");
117  mIsConnected = true;
119  }
ConnectionState
If your connectivity state changes, you should tell the user about it.
IWin32Window MainWindow
If you need to set the owner window yourself or want to show message boxes.
Definition: Helpers.cs:39
Static instance for access to utility functions and resources.
Definition: Helpers.cs:80
static IGuiHelper Gui
Utility functions for window handling
Definition: Helpers.cs:94
void OnIsConnectedChanged()
For thread-safe update of the toolbox's GUI elements representing the connection state.
Definition: MockDevice.cs:205
Action< ConnectionState > ConnectionStateChanged
Definition: MockDevice.cs:133

References MockPlugin.Device.MockDevice.ConnectionStateChanged, AxelSemrau.Chronos.Plugin.Helpers.Gui, AxelSemrau.Chronos.Plugin.IGuiHelper.MainWindow, MockPlugin.Device.MockDevice.mIsConnected, and MockPlugin.Device.MockDevice.OnIsConnectedChanged().

◆ Disconnect()

void MockPlugin.Device.MockDevice.Disconnect ( )

Pretend we are closing the connection. Actual operation substituted by a message box.

Implements AxelSemrau.Chronos.Plugin.IDevice.

Definition at line 124 of file MockDevice.cs.

125  {
127  MessageBox.Show(Helpers.Gui.MainWindow, $"Device {Name} was disconnected from {Connection}.");
129  mIsConnected = false;
131  }

References MockPlugin.Device.MockDevice.ConnectionStateChanged, AxelSemrau.Chronos.Plugin.Helpers.Gui, AxelSemrau.Chronos.Plugin.IGuiHelper.MainWindow, MockPlugin.Device.MockDevice.mIsConnected, and MockPlugin.Device.MockDevice.OnIsConnectedChanged().

◆ Dispose()

void MockPlugin.Device.MockDevice.Dispose ( )

Definition at line 68 of file MockDevice.cs.

69  {
70  Instances.Remove(this);
71  DebugOutput?.Invoke($"MockDevice {Name} disposed");
72  }
Action< string > DebugOutput
Definition: MockDevice.cs:196

References MockPlugin.Device.MockDevice.DebugOutput.

◆ OnIsConnectedChanged()

void MockPlugin.Device.MockDevice.OnIsConnectedChanged ( )
private

For thread-safe update of the toolbox's GUI elements representing the connection state.

Definition at line 205 of file MockDevice.cs.

206  {
207  var myHandler = PropertyChanged;
208  if (myHandler != null)
209  {
210  Helpers.Gui.GuiTaskFactory.StartNew(() => myHandler(this, new PropertyChangedEventArgs(nameof(IsConnected))));
211  }
212  }
System.Threading.Tasks.TaskFactory GuiTaskFactory
For your convenience, a default task factory for tasks running on the GUI thread.
Definition: Helpers.cs:28
PropertyChangedEventHandler PropertyChanged
Definition: MockDevice.cs:214
bool IsConnected
Helper for our toolbox.
Definition: MockDevice.cs:171

References AxelSemrau.Chronos.Plugin.Helpers.Gui, AxelSemrau.Chronos.Plugin.IGuiHelper.GuiTaskFactory, MockPlugin.Device.MockDevice.IsConnected, and MockPlugin.Device.MockDevice.PropertyChanged.

Referenced by MockPlugin.Device.MockDevice.Connect(), and MockPlugin.Device.MockDevice.Disconnect().

◆ ScheduleStateChangedHandler()

void MockPlugin.Device.MockDevice.ScheduleStateChangedHandler ( object  sender,
ScheduleStateEventArgs  e 
)
private

Definition at line 346 of file MockDevice.cs.

347  {
348  DebugOutput?.Invoke($"Schedule {e.PlanerName} ({e.PlanerID}) state {e.State}, abort reason {(string.IsNullOrEmpty(e.AbortReason) ? "N/A" : e.AbortReason)}");
349  }

References MockPlugin.Device.MockDevice.DebugOutput.

◆ ShowTheMessage()

void MockPlugin.Device.MockDevice.ShowTheMessage ( string  messageText)

Let our device set a status message and display some message box instead of doing real work.

Parameters
messageText

Definition at line 156 of file MockDevice.cs.

157  {
158  WaitIfPaused();
159  SetStatusMessage?.Invoke("The device just did something wonderful.");
160  MessageBox.Show(Helpers.Gui.MainWindow,
161  $"The following message was shown addressing the mock device {Name}:\r\n{messageText}",
162  "Mock Device",
163  MessageBoxButtons.OK);
164  DebugOutput?.Invoke($"Finished showing the message {messageText}");
165  }

References MockPlugin.Device.MockDevice.DebugOutput, AxelSemrau.Chronos.Plugin.Helpers.Gui, AxelSemrau.Chronos.Plugin.IGuiHelper.MainWindow, MockPlugin.Device.MockDevice.SetStatusMessage, and MockPlugin.Device.MockDevice.WaitIfPaused().

Referenced by MockPlugin.Device.MockDevice.BrewFrappuccino(), MockPlugin.Device.MockDeviceToolbox.btnShowMessage_Click(), and MockPlugin.Tasks.BrewCoffee.Execute().

◆ SomeDummyMethod()

void MockPlugin.Device.MockDevice.SomeDummyMethod ( )

Just for testing if methods of this device can be called from some other point in our code.

Definition at line 104 of file MockDevice.cs.

105  {
106  DebugOutput?.Invoke($"Dummy method of {Name} was called.");
107  }

References MockPlugin.Device.MockDevice.DebugOutput.

◆ TriggerAbort()

void MockPlugin.Device.MockDevice.TriggerAbort ( string  reason,
bool  softStop 
)

This will trigger the AbortSchedule-Event 5 seconds after it was called from a task.

Parameters
reason
softStop

For a real device, this would be something like a leak, a failed pump or heater or anything else that makes it impossible to operate the device properly.

Definition at line 229 of file MockDevice.cs.

230  {
231  Task.Run(() =>
232  {
233  Thread.Sleep(5000);
234  if (softStop)
235  {
236  StopRun?.Invoke(new StopRunArgs()
237  {
238  How = StopRunArgs.StopMode.NoNewJobs, StopQueue = true, Reason = reason,
239  RestartRemainingJobs = false
240  });
241  }
242  else
243  {
244  AbortSchedule?.Invoke(reason);
245  }
246  });
247 }
Options for stopping the schedule/queue.
StopMode
Details how to stop the run
Action< string > AbortSchedule
Definition: MockDevice.cs:249
Func< StopRunArgs, Task > StopRun
Definition: MockDevice.cs:323

References MockPlugin.Device.MockDevice.AbortSchedule, and MockPlugin.Device.MockDevice.StopRun.

Referenced by MockPlugin.Tasks.PretendCoffeeMachineIsBroken.Execute().

◆ WaitIfPaused()

void MockPlugin.Device.MockDevice.WaitIfPaused ( )

Definition at line 278 of file MockDevice.cs.

279  {
280  WaitHandle.WaitAny(new[] { mPauseEnded.WaitHandle, mAborted.WaitHandle });
281  CheckForAbort();
282  }
readonly ManualResetEventSlim mPauseEnded
Using an event here instead of a simple bool helps us avoid polling while checking for the events.
Definition: MockDevice.cs:289

References MockPlugin.Device.MockDevice.CheckForAbort(), MockPlugin.Device.MockDevice.mAborted, and MockPlugin.Device.MockDevice.mPauseEnded.

Referenced by MockPlugin.Device.MockDevice.BrewFrappuccino(), and MockPlugin.Device.MockDevice.ShowTheMessage().

Member Data Documentation

◆ DeviceTypeName

const string MockPlugin.Device.MockDevice.DeviceTypeName = "ACME Coffee Maker"
static

Definition at line 52 of file MockDevice.cs.

◆ mAborted

readonly ManualResetEventSlim MockPlugin.Device.MockDevice.mAborted = new ManualResetEventSlim(false)
private

◆ mIsConnected

bool MockPlugin.Device.MockDevice.mIsConnected
private

◆ mPauseEnded

readonly ManualResetEventSlim MockPlugin.Device.MockDevice.mPauseEnded = new ManualResetEventSlim(true)
private

Using an event here instead of a simple bool helps us avoid polling while checking for the events.

Definition at line 289 of file MockDevice.cs.

Referenced by MockPlugin.Device.MockDevice.WaitIfPaused().

◆ mScheduleEvents

IScheduleEvents MockPlugin.Device.MockDevice.mScheduleEvents
private

Definition at line 327 of file MockDevice.cs.

Property Documentation

◆ Aborted

bool MockPlugin.Device.MockDevice.Aborted
getset

Definition at line 293 of file MockDevice.cs.

294  {
295  get => mAborted.IsSet;
296  set
297  {
298  if (value) { mAborted.Set(); } else { mAborted.Reset(); }
299  }
300  }

◆ Connection

string MockPlugin.Device.MockDevice.Connection = "COM17"
getset

Connection as set in the Chronos instrument settings.

If this gets a bit more complicated, you can also use a UITypeEditor here.

Definition at line 146 of file MockDevice.cs.

146 { get; set; } = "COM17";

◆ DeviceTypeDescription

string MockPlugin.Device.MockDevice.DeviceTypeDescription
get

Device class specification referred to by some messages.

Definition at line 82 of file MockDevice.cs.

◆ DisplayedTypeName

string MockPlugin.Device.MockDevice.DisplayedTypeName
get

Visible to the user on the instruments page of the settings editor.

Definition at line 77 of file MockDevice.cs.

◆ IsConnected

bool MockPlugin.Device.MockDevice.IsConnected
getset

Helper for our toolbox.

Definition at line 170 of file MockDevice.cs.

171  {
172  get => mIsConnected;
173  set
174  {
175  if (!mIsConnected && value)
176  {
177  Connect();
178  }
179  else if (mIsConnected && !value)
180  {
181  Disconnect();
182  }
183  }
184  }
void Connect()
Inform the user of our connect attempt / success. Instead of establishing a real connection,...
Definition: MockDevice.cs:112
void Disconnect()
Pretend we are closing the connection. Actual operation substituted by a message box.
Definition: MockDevice.cs:124

Referenced by MockPlugin.Device.MockDevice.OnIsConnectedChanged().

◆ LogPaths

IEnumerable<string> MockPlugin.Device.MockDevice.LogPaths
get

Definition at line 320 of file MockDevice.cs.

◆ Name

string MockPlugin.Device.MockDevice.Name
getset

◆ Parameters

CoffeMakerParams MockPlugin.Device.MockDevice.Parameters = new CoffeMakerParams()
getset

Definition at line 263 of file MockDevice.cs.

263 { get; set; } = new CoffeMakerParams();

◆ Paused

bool MockPlugin.Device.MockDevice.Paused
getset

Definition at line 302 of file MockDevice.cs.

303  {
304  get => !mPauseEnded.IsSet;
305  set
306  {
307  if (value)
308  {
309  mPauseEnded.Reset();
310  }
311  else
312  {
313  mPauseEnded.Set();
314  }
315  }
316  }

◆ ScheduleEvents

IScheduleEvents MockPlugin.Device.MockDevice.ScheduleEvents
set

Definition at line 329 of file MockDevice.cs.

330  {
331  set
332  {
333  if (mScheduleEvents != null)
334  {
336  }
337 
338  mScheduleEvents = value;
339  if (value != null)
340  {
342  }
343  }
344  }
EventHandler< ScheduleStateEventArgs > ScheduleStateChanged
Notification about schedule state changes
void ScheduleStateChangedHandler(object sender, ScheduleStateEventArgs e)
Definition: MockDevice.cs:346
IScheduleEvents mScheduleEvents
Definition: MockDevice.cs:327

◆ StopRun

Func<StopRunArgs, Task> MockPlugin.Device.MockDevice.StopRun
getset

Definition at line 323 of file MockDevice.cs.

323 { get; set; }

Referenced by MockPlugin.Device.MockDevice.TriggerAbort().

Event Documentation

◆ AbortSchedule

Action<string> MockPlugin.Device.MockDevice.AbortSchedule

Definition at line 249 of file MockDevice.cs.

Referenced by MockPlugin.Device.MockDevice.TriggerAbort().

◆ ConnectionStateChanged

Action<ConnectionState> MockPlugin.Device.MockDevice.ConnectionStateChanged

◆ DebugOutput

◆ PropertyChanged

PropertyChangedEventHandler MockPlugin.Device.MockDevice.PropertyChanged

Definition at line 214 of file MockDevice.cs.

Referenced by MockPlugin.Device.MockDevice.OnIsConnectedChanged().

◆ SetStatusMessage

Action<string> MockPlugin.Device.MockDevice.SetStatusMessage

The documentation for this class was generated from the following file: