DUDS
Distributed Update of Data from Something
Sensor.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2024 Jeff Jackowski
9  */
10 #ifndef SENSOR_HPP
11 #define SENSOR_HPP
12 
13 #include <duds/Something.hpp>
15 
16 namespace duds { namespace hardware { namespace devices {
17 
18 template<class SVT, class SQT, class TVT, class TQT>
19 class GenericDevice;
20 
21 /*
22 Consider conversion of voltage to another unit.
23 Make function that can return Unit used in a GenericValue, or a Measurement
24  containing a GenericValue.
25 */
26 
38 template<class SVT, class SQT, class TVT, class TQT>
39 class GenericSensor : public Something {
40  friend class GenericDevice<SVT, SQT, TVT, TQT>;
41 public:
49  typedef std::shared_ptr<Device> DeviceSptr;
53  typedef std::weak_ptr<Device> DeviceWptr;
61  typedef std::shared_ptr<Measurement> MeasurementSptr;
65  typedef std::shared_ptr<const Measurement> ConstMeasurementSptr;
66 private:
71  Device *dev;
75  ConstMeasurementSptr meas;
83  unsigned int idx;
84 protected:
85  struct Token { };
86 public:
92  GenericSensor(Device *pdev, Token) : dev(pdev) { }
101  Device *pdev,
102  const boost::uuids::uuid &id,
103  unsigned int i,
104  Token
105  ) : Something(id), dev(pdev), idx(i) { }
110  static std::shared_ptr< GenericSensor <SVT, SQT, TVT, TQT> > make(
111  Device *pdev
112  ) {
113  return std::make_shared< GenericSensor <SVT, SQT, TVT, TQT> >(
114  pdev, Token()
115  );
116  }
123  static std::shared_ptr< GenericSensor <SVT, SQT, TVT, TQT> > make(
124  Device *pdev,
125  const boost::uuids::uuid &id,
126  unsigned int i
127  ) {
128  return std::make_shared< GenericSensor <SVT, SQT, TVT, TQT> >(
129  pdev, id, i, Token()
130  );
131  }
135  unsigned int index() const {
136  return idx;
137  }
144  DeviceSptr device() const {
145  Device *d = dev;
146  if (d) {
147  return d->sharedPtr();
148  }
149  return DeviceSptr();
150  }
156  ConstMeasurementSptr &measurement() const {
157  return meas;
158  }
159 };
160 
161 typedef GenericSensor<
163  double,
165  float
167 
168 typedef std::shared_ptr<Sensor> SensorSptr;
169 typedef std::weak_ptr<Sensor> SensorWptr;
170 
171 } } }
172 
173 #endif // #ifndef SENSOR_HPP
boost::variant< std::string, duds::general::LanguageTaggedString, std::array< std::int32_t, 4 >, std::array< std::int64_t, 2 >, duds::data::int128_w, std::array< float, 4 >, std::array< double, 2 >, double, duds::data::Quantity, boost::recursive_wrapper< duds::data::QuantityNddArray >, duds::time::interstellar::Femtoseconds, duds::time::interstellar::Nanoseconds, duds::time::interstellar::FemtoTime, duds::time::interstellar::NanoTime, boost::uuids::uuid, boost::recursive_wrapper< duds::data::GenericValueTable >, std::shared_ptr< std::vector< char > >> GenericValue
A general value of a type can be serialized for transmission over a network and can be used with a re...
GenericSensor(Device *pdev, Token)
Make a sensor without setting the UUID.
Definition: Sensor.hpp:92
unsigned int idx
The sensor index within the owning Device.
Definition: Sensor.hpp:83
std::shared_ptr< GenericDevice< SVT, SQT, TVT, TQT > > sharedPtr()
Returns a shared pointer to this device object; will be empty if this object&#39;s destructor is running...
Definition: Device.hpp:147
std::shared_ptr< Sensor > SensorSptr
Definition: Sensor.hpp:168
std::shared_ptr< Device > DeviceSptr
A shared pointer type to the device type used by this sensor.
Definition: Sensor.hpp:49
Something specific; a base class for identifying things.
Definition: Something.hpp:26
GenericDevice< SVT, SQT, TVT, TQT > Device
The device type used by this sensor.
Definition: Sensor.hpp:45
static std::shared_ptr< GenericSensor< SVT, SQT, TVT, TQT > > make(Device *pdev)
Make a sensor without setting the UUID.
Definition: Sensor.hpp:110
ConstMeasurementSptr & measurement() const
Returns the current measurement object.
Definition: Sensor.hpp:156
ConstMeasurementSptr meas
The current measurement object.
Definition: Sensor.hpp:75
Represents a sensor on a specific Device; allows access to measurements without needing to use a Devi...
Definition: Sensor.hpp:39
std::shared_ptr< Measurement > MeasurementSptr
A shared pointer type to the measurement type used by this sensor.
Definition: Sensor.hpp:61
Represents something with one or more sensors that are sampled through the same hardware.
Definition: Device.hpp:34
std::weak_ptr< Sensor > SensorWptr
Definition: Sensor.hpp:169
GenericSensor(Device *pdev, const boost::uuids::uuid &id, unsigned int i, Token)
Make a new sensor.
Definition: Sensor.hpp:100
duds::data::GenericMeasurement< SVT, SQT, TVT, TQT > Measurement
The measurement type provided by the sensors of the parent device.
Definition: Sensor.hpp:57
std::weak_ptr< Device > DeviceWptr
A weak pointer type to the device type used by this sensor.
Definition: Sensor.hpp:53
Device * dev
The owning device object.
Definition: Sensor.hpp:71
Stores a sample of something along with a timestamp stored as a sample from a clock.
Definition: Measurement.hpp:24
static std::shared_ptr< GenericSensor< SVT, SQT, TVT, TQT > > make(Device *pdev, const boost::uuids::uuid &id, unsigned int i)
Make a new sensor.
Definition: Sensor.hpp:123
GenericSensor< duds::data::GenericValue, double, duds::time::interstellar::NanoTime, float > Sensor
Definition: Sensor.hpp:166
std::shared_ptr< const Measurement > ConstMeasurementSptr
A shared pointer type to the const measurement type used by this sensor.
Definition: Sensor.hpp:65
NanoClock::time_point NanoTime
A point in time in Interstellar Time stored in Nanoseconds.
unsigned int index() const
Returns the sensor&#39;s device specific index.
Definition: Sensor.hpp:135
DeviceSptr device() const
Returns a shared pointer to the owning device.
Definition: Sensor.hpp:144