NDEVR
API Documentation
Device.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Hardware
28File: Device
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/Model.h>
35#include <NDEVR/DesignObjectUpdateTarget.h>
36#include <NDEVR/TimeSpan.h>
37#include <NDEVR/String.h>
38#include <QObject>
39#if NDEVR_SUPPORTS_THREADING
40#include <QThread>
41#endif
42namespace NDEVR
43{
45 class InfoPipe;
53 class HARDWARE_API Device : public QObject, public Model, public DesignObjectUpdateTarget
54 {
55 Q_OBJECT
56 public:
63 Device(const Model& model, LogPtr log, QObject* parent = nullptr);
68 virtual void setLog(LogPtr log) { m_log = log; };
74 virtual void update(DesignObjectLookup* lookup, Time time) override;
79 virtual void update(Time) {};
84 [[nodiscard]] virtual TimeSpan updateInterval() const { return TimeSpan(1.0f); }
88 virtual void endUpdates() {}
93 [[nodiscard]] LogPtr deviceLog() const { return m_log; }
98 void setWarningCallback(const std::function<void(const Exception& warning, LogPtr log)>& warning_callback);
103 void startAutoUpdate(DesignObjectLookup* lookup = nullptr);
108 void stopAutoUpdate(DesignObjectLookup* lookup = nullptr);
114 signals:
118 void onUpdated();
119 protected:
124 virtual void showWarning(const TranslatedString& warning) const;
129 virtual void showWarning(const Exception& warning) const;
130 protected:
132 std::function<void(const Exception& warning, LogPtr log)> m_warning_callback;
134#if NDEVR_SUPPORTS_THREADING
136#endif
137 };
138#if NDEVR_SUPPORTS_THREADING
146 class HARDWARE_API DeviceUpdateThread : public QThread
147 {
148 public:
155 DeviceUpdateThread(const DeviceUpdateThread&) = delete;
157 virtual ~DeviceUpdateThread() override;
161 virtual void run() override;
165 void stop();
166 protected:
169 volatile bool m_is_running;
170 };
171#endif
172}
A core class where all Design Objects including models, materials, and geometries are stored.
An object that will be called to update each time the lookup is updated.
A class that updates a device by repeatedly calling update() until stop() is called.
Definition Device.h:147
DeviceUpdateThread(DesignObjectLookup *lookup, Device *sensor)
Constructs an update thread for the given device.
Device * m_sensor
The device being updated by this thread.
Definition Device.h:168
void stop()
Signals the update loop to stop and waits for the thread to finish.
DesignObjectLookup * m_lookup
Design object lookup for update context.
Definition Device.h:167
volatile bool m_is_running
Flag controlling the update loop execution.
Definition Device.h:169
virtual void run() override
The main thread loop that repeatedly calls the device's update method.
A root class which describes some physical object, such as a Motor, Sensor, or Connection.
Definition Device.h:54
virtual TimeSpan updateInterval() const
Returns the time interval between automatic updates.
Definition Device.h:84
void startAutoUpdate(DesignObjectLookup *lookup=nullptr)
Starts automatic periodic updating of this device on a background thread.
void setWarningCallback(const std::function< void(const Exception &warning, LogPtr log)> &warning_callback)
Sets a callback to be invoked when a warning is generated by this device.
virtual void endUpdates()
Called when the auto-update loop ends, allowing cleanup.
Definition Device.h:88
virtual void update(Time)
Called to update the device with only the current time.
Definition Device.h:79
LogPtr deviceLog() const
Returns the log associated with this device.
Definition Device.h:93
void stopAutoUpdate(DesignObjectLookup *lookup=nullptr)
Stops automatic periodic updating of this device.
virtual void showWarning(const TranslatedString &warning) const
Displays a warning message through the warning callback or log.
static DesignObjectLookup * s_default_lookup
Default design object lookup shared by all devices.
Definition Device.h:133
virtual void update(DesignObjectLookup *lookup, Time time) override
Called by the update system to update this device with design context.
std::function< void(const Exception &warning, LogPtr log)> m_warning_callback
Callback invoked when warnings are generated.
Definition Device.h:132
virtual void setLog(LogPtr log)
Sets the log used for device diagnostics.
Definition Device.h:68
LogPtr m_log
Log for device diagnostic messages.
Definition Device.h:131
static void SetDefaultDeviceLookup(DesignObjectLookup *lookup)
Sets the default DesignObjectLookup used by all devices when none is specified.
Device(const Model &model, LogPtr log, QObject *parent=nullptr)
Constructs a Device from a Model with the given log and optional Qt parent.
virtual void showWarning(const Exception &warning) const
Displays a warning exception through the warning callback or log.
DeviceUpdateThread * m_update_thread
Background thread for auto-updating this device.
Definition Device.h:135
void onUpdated()
Emitted after the device has been updated.
Provides consistent interface to handle errors through the throw expression.
Definition Exception.hpp:47
A light-weight base class for Log that allows processes to update, without the need for additional in...
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
Model()
Default constructor. Creates an uninitialized Model.
Definition Model.h:365
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.