AskSin++
Weather.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2019-09-05 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __WEATHER_H__
7 #define __WEATHER_H__
8 
9 #include <Message.h>
10 #include <Channel.h>
11 
12 namespace as {
13 
14 class WeatherEventMsg : public Message {
15 public:
16  void init(uint8_t msgcnt,int16_t temp,uint8_t humidity, bool batlow) {
17  uint8_t t1 = (temp >> 8) & 0x7f;
18  uint8_t t2 = temp & 0xff;
19  if( batlow == true ) {
20  t1 |= 0x80; // set bat low bit
21  }
22  Message::init(0xc,msgcnt,AS_MESSAGE_WEATHER_EVENT,BIDI,t1,t2);
23  pload[0] = humidity;
24  }
25 };
26 
27 template <class HAL,class CLOCKTYPE,class SENSORSTYPE,int PEERS_PER_CHANNEL,int EXTRADELAY,class LIST0,class LIST1=List1,class LIST4=List4>
28 class WeatherChannel : public Channel<HAL,LIST1,EmptyList,LIST4,PEERS_PER_CHANNEL,LIST0>, protected RTCAlarm {
29 
30 private:
31  SENSORSTYPE sens;
32 
33 public:
35  virtual ~WeatherChannel () {}
36 
37  virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
38  // check if delay for millis is active - only if using RTC
39  if( delayMillis() == false ) {
40  uint8_t msgcnt = this->device().nextcount();
41  // send
42  WeatherEventMsg& msg = (WeatherEventMsg&)this->device().message();
43  msg.init(msgcnt,sens.temperature(),sens.humidity(),this->device().battery().low());
44  this->device().broadcastEvent(msg);
45  // reactivate for next send
46  reactivate(msg);
47  }
48  }
49 
50  void reactivate (Message& msg) {
51  uint32_t nextsend = AskSinBase::nextSendSlot(msg.from(),msg.count()) + EXTRADELAY;
52  CLOCKTYPE::instance().add(*this,nextsend);
53  // reactive measure before send
54  CLOCKTYPE::instance().add(sens,nextsend-sens.before());
55  }
56 
57  void setup(Device<HAL,LIST0>* dev,uint8_t number,uint16_t addr) {
59  sens.init();
60  CLOCKTYPE::instance().add(sens,1000);
61  CLOCKTYPE::instance().add(*this,sens.before()+1000);
62  }
63 
64  uint8_t status () const { return 0; }
65 
66  uint8_t flags () const { return this->device().battery().low() ? 0x80 : 0x00; }
67 
68  SENSORSTYPE& sensors () { return sens; }
69 };
70 
71 }
72 
73 #endif
as::RTCAlarm
Definition: Alarm.h:52
as::Channel
Definition: Channel.h:21
as::Message
Definition: Message.h:51
as::AlarmClock
Definition: AlarmClock.h:32
as::Device< HAL, LIST0 >
as::WeatherChannel
Definition: Weather.h:28
as::AskSinBase::nextSendSlot
static uint32_t nextSendSlot(const HMID &id, uint8_t msgcnt)
Definition: AskSinPP.h:169
as::WeatherEventMsg
Definition: Weather.h:14