Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.6 KiB

  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. */
  6. #pragma once
  7. #include "espurna.h"
  8. #include "broker.h"
  9. //--------------------------------------------------------------------------------
  10. namespace sensor {
  11. namespace type {
  12. enum Type : unsigned char {
  13. Base = 0,
  14. Emon = 1 << 0,
  15. Analog = 1 << 1
  16. };
  17. } // namespace type
  18. enum class Unit : int {
  19. Min_,
  20. None,
  21. Celcius,
  22. Farenheit,
  23. Kelvin,
  24. Percentage,
  25. Hectopascal,
  26. Ampere,
  27. Volt,
  28. Voltampere,
  29. Kilovoltampere,
  30. VoltampereReactive,
  31. KilovoltampereReactive,
  32. Watt,
  33. Kilowatt,
  34. WattSecond,
  35. Joule = WattSecond,
  36. KilowattHour,
  37. PartsPerMillion,
  38. Ohm,
  39. MicrogrammPerCubicMeter, // The concentration of an air pollutant
  40. MilligrammPerCubicMeter, //
  41. Lux,
  42. UltravioletIndex, // "measurement of the strength of sunburn-producing ultraviolet (UV) radiation at a particular place and time"
  43. // (XXX: Not a unit. Distinguish from None and specify decimals)
  44. CountsPerMinute, // Unit of local dose rate (Geiger counting)
  45. MicrosievertPerHour, // 2nd unit of local dose rate (Geiger counting)
  46. Meter,
  47. Hertz,
  48. Ph,
  49. Max_
  50. };
  51. // Base units are 32 bit since we are the fastest with them.
  52. struct Ws {
  53. Ws();
  54. Ws(uint32_t);
  55. uint32_t value;
  56. };
  57. struct Wh {
  58. Wh();
  59. Wh(Ws);
  60. Wh(uint32_t);
  61. uint32_t value;
  62. };
  63. struct KWh {
  64. KWh();
  65. KWh(Ws);
  66. KWh(Wh);
  67. KWh(uint32_t);
  68. uint32_t value;
  69. };
  70. struct Energy {
  71. constexpr static uint32_t KwhMultiplier = 3600000ul;
  72. constexpr static uint32_t KwhLimit = ((1ul << 31ul) / KwhMultiplier);
  73. Energy() = default;
  74. // TODO: while we accept ws >= the kwh conversion limit,
  75. // should this be dealt with on the unit level?
  76. Energy(double);
  77. Energy(KWh, Ws);
  78. Energy(KWh);
  79. Energy(Wh);
  80. Energy(Ws);
  81. // Sets internal counters to zero
  82. void reset();
  83. // Check whether we have *any* energy recorded. Can be zero:
  84. // - on cold boot
  85. // - on overflow
  86. // - when we call `reset()`
  87. operator bool();
  88. // Generic conversion as-is
  89. double asDouble();
  90. // Convert back to input unit, with overflow mechanics when kwh values goes over 32 bit
  91. Ws asWs();
  92. // Generic sensors output energy in joules / watt-second
  93. Energy& operator +=(Ws);
  94. Energy operator +(Ws);
  95. // But sometimes we want to accept asDouble() value back
  96. Energy& operator =(double);
  97. // We are storing a kind-of integral and fractional parts
  98. // Using watt-second to avoid loosing precision, we don't expect these to be accessed directly
  99. KWh kwh;
  100. Ws ws;
  101. };
  102. }
  103. BrokerDeclare(SensorReadBroker, void(const String&, unsigned char, double, const char*));
  104. BrokerDeclare(SensorReportBroker, void(const String&, unsigned char, double, const char*));
  105. String magnitudeUnits(unsigned char index);
  106. String magnitudeDescription(unsigned char index);
  107. unsigned char magnitudeType(unsigned char index);
  108. unsigned char magnitudeIndex(unsigned char index);
  109. String magnitudeTopicIndex(unsigned char index);
  110. unsigned char magnitudeCount();
  111. double magnitudeValue(unsigned char index);
  112. // XXX: without param name it is kind of vague what exactly unsigned char is
  113. // consider adding stronger param type e.g. enum class
  114. String magnitudeTopic(unsigned char type);
  115. String magnitudeName(unsigned char type);
  116. String sensorError(unsigned char error);
  117. void sensorWebSocketMagnitudes(JsonObject& root, const String& prefix);
  118. unsigned char sensorCount();
  119. void sensorSetup();
  120. void sensorLoop();