Skip to content

Commit

Permalink
small refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Feb 8, 2025
1 parent 4f74d8a commit 88322c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/MycilaJSY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ void Mycila::JSY::end() {
_baudRate = BaudRate::UNKNOWN;
_lastAddress = MYCILA_JSY_ADDRESS_UNKNOWN;
_model = MYCILA_JSY_MK_UNKNOWN;
_time = 0;
data.clear();
_serial->end();
}
Expand All @@ -748,7 +749,7 @@ void Mycila::JSY::end() {
// read
///////////////////////////////////////////////////////////////////////////////

bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
bool Mycila::JSY::_readMetrics(const uint8_t address, uint16_t model) {
if (!_enabled)
return false;

Expand Down Expand Up @@ -1122,12 +1123,13 @@ bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
break;
}

bool changed = data != parsed;
_time = millis();

bool changed = data != parsed;
if (changed)
data = parsed;

_time = millis();


_mutexOp.unlock();

Expand Down
27 changes: 20 additions & 7 deletions src/MycilaJSY.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ namespace Mycila {

class Data {
public:
/**
* @brief lock the data for reading.
* This must be called before reading the data.
*/
void lock() const { _mutexData.lock_shared(); }

/**
* @brief unlock the data after reading.
*/
void unlock() const { _mutexData.unlock_shared(); }

uint8_t address = MYCILA_JSY_ADDRESS_UNKNOWN; // device address
uint16_t model = MYCILA_JSY_MK_UNKNOWN; // device model

Expand Down Expand Up @@ -317,6 +328,8 @@ namespace Mycila {
// clear all values
void clear();

bool isConnected() const { return aggregate.frequency > 0; }

// compare two data
bool operator==(const Data& other) const;
// compare two data
Expand Down Expand Up @@ -470,19 +483,19 @@ namespace Mycila {
bool setMode(uint8_t address, Mode mode) { return _setMode(address, readModel(address), mode); }

/**
* @brief Read the JSY values.
* @brief Read the JSY metrics.
* @return true if the read was successful
* @note This function is blocking until the data is read or the timeout is reached.
*/
bool read() { return _read(_destinationAddress, _model); }
bool readMetrics() { return _readMetrics(_destinationAddress, _model); }

/**
* @brief Read the JSY values.
* @brief Read the JSY metrics.
* @param address The address of the device to read (1-255) or MYCILA_JSY_ADDRESS_BROADCAST for all devices
* @return true if the read was successful
* @note This function is blocking until the data is read or the timeout is reached.
*/
bool read(uint8_t address) { return _read(address, readModel(address)); }
bool readMetrics(uint8_t address) { return _readMetrics(address, readModel(address)); }

/**
* @brief Reset the energy counters of the JSY.
Expand Down Expand Up @@ -563,9 +576,9 @@ namespace Mycila {
uint32_t getTime() const { return _time; }

// check if the device is connected to the grid, meaning if last read was successful
bool isConnected() const { return data.aggregate.frequency > 0; }
bool isConnected() const { return data.isConnected(); }

void setCallback(Callback callback) { _callback = callback; }
void onEvent(Callback callback) { _callback = callback; }

public:
/**
Expand Down Expand Up @@ -602,7 +615,7 @@ namespace Mycila {
};

bool _set(uint8_t address, uint8_t newAddress, BaudRate newBaudRate);
bool _read(uint8_t address, uint16_t model);
bool _readMetrics(uint8_t address, uint16_t model);
Mode _readMode(uint8_t address, uint16_t model);
bool _setMode(uint8_t address, uint16_t model, Mode mode);

Expand Down

0 comments on commit 88322c4

Please sign in to comment.