Skip to content

Commit 6fcd6d6

Browse files
committed
add logging facility for dsx
1 parent 57fbe80 commit 6fcd6d6

File tree

1 file changed

+69
-5
lines changed

1 file changed

+69
-5
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ DS18X20Controller::~DS18X20Controller() { delete _DS18X20_model; }
4343
*/
4444
/***********************************************************************/
4545
bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
46+
unsigned long total_start_time = millis();
47+
4648
// Attempt to decode the incoming message into a Ds18x20Add message
49+
unsigned long decode_start_time = millis();
4750
if (!_DS18X20_model->DecodeDS18x20Add(stream)) {
4851
WS_DEBUG_PRINTLN("ERROR: Unable to decode Ds18x20Add message");
4952
return false;
5053
}
54+
unsigned long decode_end_time = millis();
55+
WS_DEBUG_PRINT("Decode time: ");
56+
WS_DEBUG_PRINTLN(decode_end_time - decode_start_time);
5157

5258
// If we receive no sensor types to configure, bail out
5359
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types_count == 0) {
@@ -59,17 +65,26 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
5965
uint8_t pin_name = atoi(_DS18X20_model->GetDS18x20AddMsg()->onewire_pin + 1);
6066

6167
// Initialize the DS18X20Hardware object
68+
unsigned long init_start_time = millis();
6269
auto new_dsx_driver = std::make_unique<DS18X20Hardware>(pin_name);
6370
// Attempt to get the sensor's ID on the OneWire bus to show it's been init'd
6471
bool is_initialized = new_dsx_driver->GetSensor();
72+
unsigned long init_end_time = millis();
73+
WS_DEBUG_PRINT("Initialization time: ");
74+
WS_DEBUG_PRINTLN(init_end_time - init_start_time);
75+
6576
if (is_initialized) {
6677
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
78+
6779
// Set the sensor's resolution
6880
new_dsx_driver->SetResolution(
6981
_DS18X20_model->GetDS18x20AddMsg()->sensor_resolution);
82+
7083
// Set the sensor's period
7184
new_dsx_driver->SetPeriod(_DS18X20_model->GetDS18x20AddMsg()->period);
85+
7286
// Configure the types of sensor reads to perform
87+
unsigned long config_types_start_time = millis();
7388
for (int i = 0; i < _DS18X20_model->GetDS18x20AddMsg()->sensor_types_count;
7489
i++) {
7590
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types[i] ==
@@ -81,31 +96,44 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
8196
new_dsx_driver->is_read_temp_f = true;
8297
}
8398
}
99+
84100
// Add the DS18X20Hardware object to the vector of hardware objects
85-
//_DS18X20_pins.push_back(new_dsx_driver);
86101
_DS18X20_pins.push_back(std::move(new_dsx_driver));
87102
} else {
88103
WS_DEBUG_PRINTLN(
89104
"ERROR: Unable to get ds18x sensor ID, ds18x sensor not initialized");
90105
}
91106

92107
// Encode and publish a Ds18x20Added message back to the broker
108+
unsigned long encode_start_time = millis();
93109
if (!_DS18X20_model->EncodeDS18x20Added(
94110
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin, is_initialized)) {
95111
WS_DEBUG_PRINTLN("ERROR: Unable to encode Ds18x20Added message");
96112
return false;
97113
}
114+
unsigned long encode_end_time = millis();
115+
WS_DEBUG_PRINT("Encode message time: ");
116+
WS_DEBUG_PRINTLN(encode_end_time - encode_start_time);
98117

99118
// Publish the AnalogIO message to the broker
119+
unsigned long publish_start_time = millis();
100120
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
101121
_DS18X20_model->GetDS18x20AddedMsg())) {
102122
WS_DEBUG_PRINTLN("ERROR: Unable to publish Ds18x20Added message");
103123
return false;
104124
}
125+
unsigned long publish_end_time = millis();
126+
WS_DEBUG_PRINT("Publish message time: ");
127+
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
128+
129+
unsigned long total_end_time = millis();
130+
WS_DEBUG_PRINT("Total function execution time: ");
131+
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
105132

106133
return true;
107134
}
108135

136+
109137
/***********************************************************************/
110138
/*!
111139
@brief Handles a Ds18x20Remove message from the broker. Attempts to
@@ -146,22 +174,31 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
146174
*/
147175
/***********************************************************************/
148176
void DS18X20Controller::update() {
177+
unsigned long total_start_time = millis();
178+
149179
// Bail out if there are no OneWire pins to poll
150180
if (_DS18X20_pins.empty())
151181
return;
152182

153183
// Iterate through the vector
154184
for (uint8_t i = 0; i < _DS18X20_pins.size(); i++) {
155-
// Create a temporary DS18X20Hardware driver
185+
unsigned long sensor_start_time = millis();
186+
187+
// Create a reference to the DS18X20Hardware object
156188
DS18X20Hardware &temp_dsx_driver = *(_DS18X20_pins[i]);
189+
157190
// Check if the driver's timer has not expired yet
158191
if (!temp_dsx_driver.IsTimerExpired()) {
159192
continue;
160193
}
194+
161195
// Create a new sensor_event
162196
_DS18X20_model->InitDS18x20EventMsg();
197+
163198
// Are we reading the temperature in Celsius, Fahrenheit, or both?
164199
if (temp_dsx_driver.is_read_temp_c) {
200+
unsigned long temp_c_start_time = millis();
201+
165202
WS_DEBUG_PRINTLN("Reading temperature in Celsius"); // TODO: Debug remove
166203
// Attempt to read the temperature in Celsius
167204
if (!temp_dsx_driver.ReadTemperatureC()) {
@@ -172,10 +209,15 @@ void DS18X20Controller::update() {
172209
_DS18X20_model->addSensorEvent(
173210
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE,
174211
temp_c);
212+
213+
unsigned long temp_c_end_time = millis();
214+
WS_DEBUG_PRINT("Read temperature Celsius time: ");
215+
WS_DEBUG_PRINTLN(temp_c_end_time - temp_c_start_time);
175216
}
176217
if (temp_dsx_driver.is_read_temp_f) {
177-
WS_DEBUG_PRINTLN(
178-
"Reading temperature in Fahrenheit"); // TODO: Debug remove
218+
unsigned long temp_f_start_time = millis();
219+
220+
WS_DEBUG_PRINTLN("Reading temperature in Fahrenheit"); // TODO: Debug remove
179221
// Attempt to read the temperature in Fahrenheit
180222
if (!temp_dsx_driver.ReadTemperatureF()) {
181223
WS_DEBUG_PRINTLN("ERROR: Unable to read temperature in Fahrenheit");
@@ -185,7 +227,12 @@ void DS18X20Controller::update() {
185227
_DS18X20_model->addSensorEvent(
186228
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT,
187229
temp_f);
230+
231+
unsigned long temp_f_end_time = millis();
232+
WS_DEBUG_PRINT("Read temperature Fahrenheit time: ");
233+
WS_DEBUG_PRINTLN(temp_f_end_time - temp_f_start_time);
188234
}
235+
189236
// Get and print out the SensorEvent message's contents
190237
wippersnapper_ds18x20_Ds18x20Event event_msg =
191238
*_DS18X20_model->GetDS18x20EventMsg();
@@ -202,11 +249,17 @@ void DS18X20Controller::update() {
202249
}
203250

204251
// Encode the Ds18x20Event message
252+
unsigned long encode_start_time = millis();
205253
if (!_DS18X20_model->EncodeDs18x20Event()) {
206254
WS_DEBUG_PRINTLN("ERROR: Failed to encode Ds18x20Event message");
207255
continue;
208256
}
257+
unsigned long encode_end_time = millis();
258+
WS_DEBUG_PRINT("Encode event message time: ");
259+
WS_DEBUG_PRINTLN(encode_end_time - encode_start_time);
260+
209261
// Publish the Ds18x20Event message to the broker
262+
unsigned long publish_start_time = millis();
210263
WS_DEBUG_PRINT("Publishing Ds18x20Event message to broker...");
211264
if (!WsV2.PublishSignal(
212265
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
@@ -215,5 +268,16 @@ void DS18X20Controller::update() {
215268
continue;
216269
}
217270
WS_DEBUG_PRINTLN("Published!");
271+
unsigned long publish_end_time = millis();
272+
WS_DEBUG_PRINT("Publish event message time: ");
273+
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
274+
275+
unsigned long sensor_end_time = millis();
276+
WS_DEBUG_PRINT("Total sensor processing time: ");
277+
WS_DEBUG_PRINTLN(sensor_end_time - sensor_start_time);
218278
}
219-
}
279+
280+
unsigned long total_end_time = millis();
281+
WS_DEBUG_PRINT("Total update() execution time: ");
282+
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
283+
}

0 commit comments

Comments
 (0)