5 namespace uponor_smatrix {
7 static const char *
const TAG =
"uponor_smatrix";
18 ESP_LOGCONFIG(TAG,
"Uponor Smatrix");
19 ESP_LOGCONFIG(TAG,
" System address: 0x%04X", this->
address_);
22 ESP_LOGCONFIG(TAG,
" Time synchronization: YES");
30 ESP_LOGCONFIG(TAG,
" Detected unknown device addresses:");
32 ESP_LOGCONFIG(TAG,
" 0x%04X", device_address);
38 const uint32_t now =
millis();
42 ESP_LOGD(TAG,
"Discarding %d bytes of unparsed data", this->
rx_buffer_.size());
69 auto packet = std::move(this->
tx_queue_.front());
82 const uint8_t *packet = this->
rx_buffer_.data();
90 uint16_t system_address =
encode_uint16(packet[0], packet[1]);
91 uint16_t device_address =
encode_uint16(packet[2], packet[3]);
92 uint16_t
crc =
encode_uint16(packet[packet_len - 1], packet[packet_len - 2]);
94 uint16_t computed_crc =
crc16(packet, packet_len - 2);
95 if (crc != computed_crc) {
100 ESP_LOGV(TAG,
"Received packet: sys=%04X, dev=%04X, data=%s, crc=%04X", system_address, device_address,
101 format_hex(&packet[4], packet_len - 6).c_str(), crc);
105 ESP_LOGI(TAG,
"Using detected system address 0x%04X", system_address);
107 }
else if (this->
address_ != system_address) {
109 ESP_LOGW(TAG,
"Received packet from unknown system address 0x%04X", system_address);
114 size_t data_len = (packet_len - 6) / 3;
116 if (packet[4] == UPONOR_ID_REQUEST)
117 ESP_LOGVV(TAG,
"Ignoring request packet for device 0x%04X", device_address);
123 for (
int i = 0; i < data_len; i++) {
124 data[i].
id = packet[(i * 3) + 4];
134 bool found_temperature =
false;
135 bool found_time =
false;
136 for (
int i = 0; i < data_len; i++) {
137 if (data[i].
id == UPONOR_ID_ROOM_TEMP)
138 found_temperature =
true;
139 if (data[i].
id == UPONOR_ID_DATETIME1)
141 if (found_temperature && found_time) {
142 ESP_LOGI(TAG,
"Using detected time device address 0x%04X", device_address);
152 for (
auto *device : this->
devices_) {
153 if (device->address_ == device_address) {
155 device->on_device_data(data, data_len);
161 ESP_LOGI(TAG,
"Received packet for unknown device address 0x%04X ", device_address);
170 if (this->
address_ == 0 || device_address == 0 || data ==
nullptr || data_len == 0)
174 std::vector<uint8_t> packet;
175 packet.reserve(6 + 3 * data_len);
177 packet.push_back(this->
address_ >> 8);
178 packet.push_back(this->
address_ >> 0);
179 packet.push_back(device_address >> 8);
180 packet.push_back(device_address >> 0);
182 for (
int i = 0; i < data_len; i++) {
183 packet.push_back(data[i].
id);
184 packet.push_back(data[i].value >> 8);
185 packet.push_back(data[i].value >> 0);
188 auto crc =
crc16(packet.data(), packet.size());
189 packet.push_back(
crc >> 0);
190 packet.push_back(
crc >> 8);
214 uint16_t time1 = (year & 0x7F) << 7 | (month & 0x0F) << 3 | (day_of_week & 0x07);
215 uint16_t time2 = (day_of_month & 0x1F) << 11 | (hour & 0x1F) << 6 | (minute & 0x3F);
221 UponorSmatrixData data[] = {{UPONOR_ID_DATETIME1, time1}, {UPONOR_ID_DATETIME2, time2}, {UPONOR_ID_DATETIME3, time3}};
ESPTime now()
Get the time in the currently defined timezone.
std::set< uint16_t > unknown_devices_
void write_array(const uint8_t *data, size_t len)
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
A more user-friendly version of struct tm from time.h.
void add_on_time_sync_callback(std::function< void()> callback)
bool parse_byte_(uint8_t byte)
std::vector< UponorSmatrixDevice * > devices_
time::RealTimeClock * time_id_
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
uint32_t IRAM_ATTR HOT millis()
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
uint8_t second
seconds after the minute [0-60]
bool read_byte(uint8_t *data)
std::queue< std::vector< uint8_t > > tx_queue_
uint8_t minute
minutes after the hour [0-59]
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
uint8_t day_of_week
day of the week; sunday=1 [1-7]
bool send_time_requested_
Implementation of SPI Controller mode.
uint8_t month
month; january=1 [1-12]
bool send(uint16_t device_address, const UponorSmatrixData *data, size_t data_len)
void dump_config() override
uint8_t hour
hours since midnight [0-23]
std::vector< uint8_t > rx_buffer_
uint16_t time_device_address_
uint8_t day_of_month
day of the month [1-31]