Fix metrics from original project
This commit is contained in:
parent
9c874adfaa
commit
9a451be72a
2 changed files with 2128 additions and 1024 deletions
|
@ -41,7 +41,6 @@
|
|||
#include <set>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include "ovms_utils.h"
|
||||
#include "ovms_mutex.h"
|
||||
#include "dbc_number.h"
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
|
@ -59,6 +58,8 @@ typedef enum : uint8_t
|
|||
{
|
||||
Other = 0,
|
||||
Native = Other,
|
||||
ToMetric = 1,
|
||||
ToImperial = 2,
|
||||
|
||||
Kilometers = 10,
|
||||
Miles = 11,
|
||||
|
@ -71,8 +72,6 @@ typedef enum : uint8_t
|
|||
kPa = 30,
|
||||
Pa = 31,
|
||||
PSI = 32,
|
||||
Bar = 33,
|
||||
mBar = 34,
|
||||
|
||||
Volts = 40,
|
||||
Amps = 41,
|
||||
|
@ -81,7 +80,6 @@ typedef enum : uint8_t
|
|||
kWh = 44,
|
||||
Watts = 45,
|
||||
WattHours = 46,
|
||||
kVA = 47,
|
||||
|
||||
Seconds = 50,
|
||||
Minutes = 51,
|
||||
|
@ -98,21 +96,30 @@ typedef enum : uint8_t
|
|||
KphPS = 71, // Kph per second
|
||||
MphPS = 72, // Mph per second
|
||||
MetersPSS = 73, // Meters per second^2
|
||||
FeetPSS = 74, // Feet per second^2
|
||||
|
||||
dbm = 80, // Signal Quality (in dBm)
|
||||
sq = 81, // Signal Quality (in SQ units)
|
||||
|
||||
Percentage = 90,
|
||||
rpm = 91,
|
||||
|
||||
// Energy consumption:
|
||||
WattHoursPK = 100, // Wh/km
|
||||
WattHoursPM = 101, // Wh/mi
|
||||
kWhP100K = 102, // Kwh/100km
|
||||
KPkWh = 103, // Km/kwH
|
||||
MPkWh = 104, // mi/kwH
|
||||
|
||||
// Torque:
|
||||
Nm = 110,
|
||||
// ^^^^ Last ^^^^ MetricUnitLast below.
|
||||
|
||||
UnitNotFound = 255 // Used for errors in Search.
|
||||
} metric_unit_t;
|
||||
|
||||
const metric_unit_t MetricUnitFirst = Other;
|
||||
const metric_unit_t MetricUnitLast = Nm;
|
||||
|
||||
typedef enum : uint8_t
|
||||
{
|
||||
NeverDefined = 0,
|
||||
|
@ -121,6 +128,9 @@ typedef enum : uint8_t
|
|||
} metric_defined_t;
|
||||
|
||||
extern const char* OvmsMetricUnitLabel(metric_unit_t units);
|
||||
extern const char* OvmsMetricUnitName(metric_unit_t units);
|
||||
extern metric_unit_t OvmsMetricUnitFromName(const char* unit);
|
||||
|
||||
extern int UnitConvert(metric_unit_t from, metric_unit_t to, int value);
|
||||
extern float UnitConvert(metric_unit_t from, metric_unit_t to, float value);
|
||||
|
||||
|
@ -157,9 +167,9 @@ class OvmsMetric
|
|||
virtual std::string AsJSON(const char* defvalue = "", metric_unit_t units = Other, int precision = -1);
|
||||
virtual float AsFloat(const float defvalue = 0, metric_unit_t units = Other);
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
virtual void DukPush(DukContext &dc);
|
||||
virtual void DukPush(DukContext &dc, metric_unit_t units = Other);
|
||||
#endif
|
||||
virtual bool SetValue(std::string value);
|
||||
virtual bool SetValue(std::string value, metric_unit_t units = Other);
|
||||
virtual bool SetValue(dbcNumber& value);
|
||||
virtual void operator=(std::string value);
|
||||
virtual uint32_t LastModified();
|
||||
|
@ -205,12 +215,12 @@ class OvmsMetricBool : public OvmsMetric
|
|||
float AsFloat(const float defvalue = 0, metric_unit_t units = Other);
|
||||
int AsBool(const bool defvalue = false);
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc);
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override;
|
||||
#endif
|
||||
bool SetValue(bool value);
|
||||
void operator=(bool value) { SetValue(value); }
|
||||
bool SetValue(std::string value);
|
||||
bool SetValue(dbcNumber& value);
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override;
|
||||
bool SetValue(dbcNumber& value) override;
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
void Clear();
|
||||
bool CheckPersist();
|
||||
|
@ -233,12 +243,12 @@ class OvmsMetricInt : public OvmsMetric
|
|||
float AsFloat(const float defvalue = 0, metric_unit_t units = Other);
|
||||
int AsInt(const int defvalue = 0, metric_unit_t units = Other);
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc);
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override;
|
||||
#endif
|
||||
bool SetValue(int value, metric_unit_t units = Other);
|
||||
void operator=(int value) { SetValue(value); }
|
||||
bool SetValue(std::string value);
|
||||
bool SetValue(dbcNumber& value);
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override;
|
||||
bool SetValue(dbcNumber& value) override;
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
void Clear();
|
||||
bool CheckPersist();
|
||||
|
@ -261,12 +271,12 @@ class OvmsMetricFloat : public OvmsMetric
|
|||
float AsFloat(const float defvalue = 0, metric_unit_t units = Other);
|
||||
int AsInt(const int defvalue = 0, metric_unit_t units = Other);
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc);
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override;
|
||||
#endif
|
||||
bool SetValue(float value, metric_unit_t units = Other);
|
||||
void operator=(float value) { SetValue(value); }
|
||||
bool SetValue(std::string value);
|
||||
bool SetValue(dbcNumber& value);
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override;
|
||||
bool SetValue(dbcNumber& value) override;
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
void Clear();
|
||||
virtual bool CheckPersist();
|
||||
|
@ -286,9 +296,9 @@ class OvmsMetricString : public OvmsMetric
|
|||
public:
|
||||
std::string AsString(const char* defvalue = "", metric_unit_t units = Other, int precision = -1);
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc);
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override;
|
||||
#endif
|
||||
bool SetValue(std::string value);
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override;
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
void Clear();
|
||||
virtual bool IsString() { return true; };
|
||||
|
@ -342,7 +352,7 @@ class OvmsMetricBitset : public OvmsMetric
|
|||
return json;
|
||||
}
|
||||
|
||||
bool SetValue(std::string value)
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override
|
||||
{
|
||||
std::bitset<N> n_value;
|
||||
std::istringstream vs(value);
|
||||
|
@ -356,7 +366,7 @@ class OvmsMetricBitset : public OvmsMetric
|
|||
if (elem >= 0 && elem < N)
|
||||
n_value[elem] = 1;
|
||||
}
|
||||
return SetValue(n_value);
|
||||
return SetValue(n_value, units);
|
||||
}
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
|
||||
|
@ -369,7 +379,7 @@ class OvmsMetricBitset : public OvmsMetric
|
|||
}
|
||||
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc)
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override
|
||||
{
|
||||
std::bitset<N> value;
|
||||
{
|
||||
|
@ -454,7 +464,7 @@ class OvmsMetricSet : public OvmsMetric
|
|||
return json;
|
||||
}
|
||||
|
||||
bool SetValue(std::string value)
|
||||
bool SetValue(std::string value, metric_unit_t units = Other) override
|
||||
{
|
||||
std::set<ElemType> n_value;
|
||||
std::istringstream vs(value);
|
||||
|
@ -466,7 +476,7 @@ class OvmsMetricSet : public OvmsMetric
|
|||
ts >> elem;
|
||||
n_value.insert(elem);
|
||||
}
|
||||
return SetValue(n_value);
|
||||
return SetValue(n_value, units);
|
||||
}
|
||||
void operator=(std::string value) { SetValue(value); }
|
||||
|
||||
|
@ -479,7 +489,7 @@ class OvmsMetricSet : public OvmsMetric
|
|||
}
|
||||
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc)
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override
|
||||
{
|
||||
std::set<ElemType> value;
|
||||
{
|
||||
|
@ -739,7 +749,7 @@ class OvmsMetricVector : public OvmsMetric
|
|||
}
|
||||
|
||||
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
|
||||
void DukPush(DukContext &dc)
|
||||
void DukPush(DukContext &dc, metric_unit_t units = Other) override
|
||||
{
|
||||
std::vector<ElemType, Allocator> value;
|
||||
{
|
||||
|
@ -917,16 +927,16 @@ typedef std::function<void(OvmsMetric*)> MetricCallback;
|
|||
class MetricCallbackEntry
|
||||
{
|
||||
public:
|
||||
MetricCallbackEntry(const char* caller, MetricCallback callback);
|
||||
MetricCallbackEntry(std::string caller, MetricCallback callback);
|
||||
virtual ~MetricCallbackEntry();
|
||||
|
||||
public:
|
||||
const char *m_caller;
|
||||
std::string m_caller;
|
||||
MetricCallback m_callback;
|
||||
};
|
||||
|
||||
typedef std::list<MetricCallbackEntry*> MetricCallbackList;
|
||||
typedef std::map<const char*, MetricCallbackList*, CmpStrOp> MetricCallbackMap;
|
||||
typedef std::map<std::string, MetricCallbackList*> MetricCallbackMap;
|
||||
|
||||
class OvmsMetrics
|
||||
{
|
||||
|
@ -939,10 +949,11 @@ class OvmsMetrics
|
|||
void DeregisterMetric(OvmsMetric* metric);
|
||||
|
||||
public:
|
||||
bool Set(const char* metric, const char* value);
|
||||
bool Set(const char* metric, const char* value, const char *unit = NULL);
|
||||
bool SetInt(const char* metric, int value);
|
||||
bool SetBool(const char* metric, bool value);
|
||||
bool SetFloat(const char* metric, float value);
|
||||
std::string GetUnitStr(const char* metric, const char *unit = NULL);
|
||||
OvmsMetric* Find(const char* metric);
|
||||
|
||||
OvmsMetricInt *InitInt(const char* metric, uint16_t autostale=0, int value=0, metric_unit_t units = Other, bool persist = false);
|
||||
|
@ -978,8 +989,8 @@ class OvmsMetrics
|
|||
}
|
||||
|
||||
public:
|
||||
void RegisterListener(const char* caller, const char* name, MetricCallback callback);
|
||||
void DeregisterListener(const char* caller);
|
||||
void RegisterListener(std::string caller, std::string name, MetricCallback callback);
|
||||
void DeregisterListener(std::string caller);
|
||||
void NotifyModified(OvmsMetric* metric);
|
||||
|
||||
protected:
|
||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue