Aruna
watcher.cpp
Go to the documentation of this file.
1 //
2 // Created by noeel on 15-04-20.
3 //
4 
5 #include "aruna/sis/watcher.h"
6 #include "aruna/log.h"
7 #include <pthread.h>
8 #include <aruna/sis/reporter.h>
9 #include <aruna/sensor/Water.h>
10 
11 using namespace aruna;
12 namespace {
15  pthread_t watcher_thread;
17 }
18 
21 // TODO error check
22  log_channel = new log::channel_t("SIS watcher");
23  for (auto &i : level) {
24  i = level_t::WARNING;
25  }
26  pthread_create(&watcher_thread, NULL, watch, NULL);
27  return comm_channel->register_err;
28 }
29 
30 void *sis::watcher::watch(void *) {
32  sis::status_t *status_buffer;
33  sis::type_t received_type;
34  while (1) {
35  comm_channel->receive(&tp);
36  if (tp.from_port != sis::reporter::port) {
38  log_channel->warning("Port: %i sending us packages, dropping:", tp.from_port);
40  continue;
41  }
42  received_type = (sis::type_t) tp.data_received[0];
43 
44 // TODO this feels off, is there any better way of handling this?
45  switch (received_type) {
46  case type_t::WATER:
47  status_buffer = reinterpret_cast<status_t *>(new sensor::water_status_t());
48  break;
49  case type_t::UNKNOWN:
50  case type_t::MAX:
51  default:
52  received_type = type_t::UNKNOWN;
54  status_buffer = new sis::status_t(received_type);
55  break;
56  }
57 
58  status_buffer->decode(tp.data_received);
59  if ((uint8_t) level[(uint8_t) received_type] <= (uint8_t) status_buffer->level) {
60 // TODO error on critical and warning on warning.
61 // TODO better log containing type and level.
62 // TODO water type specific handler
63  log_channel->error(status_buffer->description);
64 // TODO log NOTIFY after >WARNING
65  } else {
66 // TODO log in verbose
67  }
68  delete status_buffer;
70  }
71  return nullptr;
72 }
73 
74 void sis::watcher::set_level(type_t type, level_t new_level) {
75  level[(uint8_t) type] = new_level;
76 }
Definition: comm.cpp:14
type_t
Type of status.
Definition: sisTypes.h:26
level_t
SIS urgency level.
Definition: sisTypes.h:16
uint8_t data_lenght
size of the data
Definition: commTypes.h:70
err_t start()
start SIS report watcher thread
Definition: watcher.cpp:19
port_t from_port
channel who is sending the data.
Definition: commTypes.h:44
bool receive(transmitpackage_t *tpp)
handeler to handle incomming connections
Definition: commTypes.h:171
void set_level(type_t type, level_t new_level)
set level of alert on SIS level.
Definition: watcher.cpp:74
sis::level_t level[(uint8_t) sis::type_t::MAX]
Definition: watcher.cpp:16
void * watch(void *)
Watch SIS reports task.
Definition: watcher.cpp:30
comm::channel_t * comm_channel
Definition: watcher.cpp:13
char description[32]
Definition: sisTypes.h:40
log::channel_t * log_channel
Definition: watcher.cpp:14
endpoint type of a comm channel
Definition: commTypes.h:150
int warning(const char *format,...)
log warning message
Definition: log.cpp:77
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
virtual void decode(uint8_t *to_decode)
Definition: sisTypes.h:56
static const comm::port_t port
Definition: reporter.h:15
uint8_t * data_received
pointer to where incoming data must be stored.
Definition: commTypes.h:65
int dump(level_t level, uint8_t *bin, size_t size)
dump array of bin data
Definition: log.cpp:135
transmit ready package.
Definition: commTypes.h:39