Skip to content

Commit

Permalink
WIP: experimental code to extract energy direction from status word a…
Browse files Browse the repository at this point in the history
…nd apply to power reading
  • Loading branch information
r00t- committed Mar 31, 2021
1 parent d4a0c9b commit a893a6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/protocols/MeterSML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MeterSML : public vz::protocol::Protocol {
parity_type_t _parity;
std::string _pull;
bool _use_local_time;
bool _direction_from_status;

int _fd; /* file descriptor of port */
struct termios _old_tio; /* required to reset port */
Expand All @@ -84,7 +85,7 @@ class MeterSML : public vz::protocol::Protocol {
* @param rd the reading to store to
* @return true if it is a valid entry
*/
bool _parse(sml_list *list, Reading *rd);
bool _parse(sml_list *list, Reading *rd, bool *direction_from_status);

/**
* Open serial port by device
Expand Down
20 changes: 18 additions & 2 deletions src/protocols/MeterSML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ MeterSML::MeterSML(std::list<Option> options)
/* using default value if not specified */
_use_local_time = false;
}
try {
_direction_from_status = optlist.lookup_bool(options, "direction_from_status");
} catch (vz::OptionNotFoundException &e) {
/* using default value if not specified */
_direction_from_status = false;
}

/* baudrate */
int baudrate = 9600; /* default to avoid compiler warning */
Expand Down Expand Up @@ -244,6 +250,7 @@ ssize_t MeterSML::read(std::vector<Reading> &rds, size_t n) {
sml_file *file;
sml_get_list_response *body;
sml_list *entry;
bool direction_from_status = false;

if (_fd < 0) {
if (!reopen()) {
Expand Down Expand Up @@ -289,7 +296,7 @@ ssize_t MeterSML::read(std::vector<Reading> &rds, size_t n) {

/* iterating through linked list */
for (; m < n && entry != NULL;) {
if (_parse(entry, &rds[m]))
if (_parse(entry, &rds[m], &direction_from_status))
m++;
entry = entry->next;
}
Expand All @@ -302,7 +309,7 @@ ssize_t MeterSML::read(std::vector<Reading> &rds, size_t n) {
return m; // return number of successful readings
}

bool MeterSML::_parse(sml_list *entry, Reading *rd) {
bool MeterSML::_parse(sml_list *entry, Reading *rd, bool *direction_from_status) {
// int unit = (entry->unit) ? *entry->unit : 0;
int scaler = (entry->scaler) ? *entry->scaler : 1;

Expand All @@ -321,9 +328,18 @@ bool MeterSML::_parse(sml_list *entry, Reading *rd) {
rd->value(sml_value_to_double(entry->value) * pow(10, scaler));
}

if (_direction_from_status && entry->status) {
*direction_from_status = (*(entry->status->data.status16) & (1 << 5)) == (1 << 5);
}

ReadingIdentifier *rid(new ObisIdentifier(obis));
rd->identifier(rid);

if ( _direction_from_status && *direction_from_status &&
obis == Obis(1, 0, 15, 7, 0, 0xff)){
rd->value(-1 * sml_value_to_double(entry->value) * pow(10, scaler));
}

// TODO handle SML_TIME_SEC_INDEX or time by SML File/Message
struct timeval tv;
if (!_use_local_time && entry->val_time) { /* use time from meter */
Expand Down

0 comments on commit a893a6c

Please sign in to comment.