Aruna
Water.cpp
Go to the documentation of this file.
1 //
2 // Created by noeel on 16-04-20.
3 //
4 
5 #include <cstdio>
6 #include "aruna/sensor/Water.h"
7 
8 using namespace aruna::sis;
9 using namespace aruna::sensor;
10 
11 Water::Water() : status() {
13  status.level = level_t::NOTIFY;
14 // update description
16 }
17 
18 aruna::err_t Water::is_wet(bool &water_detected) {
19  err_t e;
20  uint16_t i;
21  e = get_water_level(i);
22  water_detected = i != 0;
23  return e;
24 }
25 
27  uint16_t mm;
28  err_t e;
29  e = get_water_level(mm);
30  if ((uint8_t) e)
31  log->error("Error retrieving water level: %s", err_to_char.at(e));
33  status.level = mm ? level_t::CRITICAL : level_t::NOTIFY;
35  return &status;
36 }
37 
38 void Water::set_sis_status_location(char *location) {
39  strcpy(status.location, location);
40 }
41 
42 
43 void water_status_t::update_description(uint8_t water_level) {
44  snprintf(description, sizeof(description), "%imm water in %s", water_level, location);
45 }
46 
47 void water_status_t::encode(uint8_t *buffer) {
48 // standard encoding
49  status_t::encode(buffer);
50  uint8_t start_byte = status_t::get_encode_size();
51  buffer[start_byte] = water_level_mm;
52  buffer[start_byte + 1] = strlen(location);
53  memcpy((char *) &buffer[start_byte + 2], location, strlen(location));
54 }
55 
56 void water_status_t::decode(uint8_t *to_decode) {
57  status_t::decode(to_decode);
58  uint8_t start_byte = status_t::get_encode_size();
59  water_level_mm = to_decode[start_byte];
60 // TODO decoding of location is not always correct.
61  memcpy(location, (char *) &to_decode[start_byte + 2], to_decode[start_byte + 1]);
62 }
63 
65  return status_t::get_encode_size() + strlen(location) + 2;
66 }
67 
69 }
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
type_t
Type of status.
Definition: sisTypes.h:26
err_t is_wet(bool &water_detected)
Is water detected at the sensor?
Definition: Water.cpp:18
uint8_t get_encode_size() override
Definition: Water.cpp:64
status_t status
stores the comm status
Definition: comm.cpp:40
log::channel_t * log
Definition: Performer.h:24
void update_description(uint8_t water_level)
updates description with water level.
Definition: Water.cpp:43
void set_sis_status_location(char *location)
Set the location of the sensor.
Definition: Water.cpp:38
sis::status_t * update_status() override
Perform check to see if system is secure and return updated status.
Definition: Water.cpp:26
void encode(uint8_t *buffer) override
Definition: Water.cpp:47
int error(const char *format,...)
log error message
Definition: log.cpp:90
status that is to be reported to the watcher
Definition: sisTypes.h:36
water_status_t status
Definition: Water.h:42
void decode(uint8_t *to_decode) override
Definition: Water.cpp:56
water_status_t()
water-specific SIS status
Definition: Water.cpp:68
virtual err_t get_water_level(uint16_t &water_level_in_mm)=0
Get water level in millimeters of the sensor.