+#include "driver.h"
+#include "layout.h"
+#include "sensor.h"
+
+using namespace Msp;
+
+namespace R2C2 {
+
+Sensor::Sensor(Layout &l):
+ layout(l),
+ address(0),
+ state(INACTIVE)
+{
+ if(layout.has_driver())
+ layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Sensor::event));
+
+ layout.add_sensor(*this);
+}
+
+Sensor::~Sensor()
+{
+ layout.remove_sensor(*this);
+}
+
+void Sensor::tick(const Time::TimeDelta &dt)
+{
+ if(state_confirm_timeout)
+ {
+ state_confirm_timeout -= dt;
+ if(state_confirm_timeout<=Time::zero)
+ {
+ if(state==MAYBE_INACTIVE)
+ state = INACTIVE;
+ else if(state==MAYBE_ACTIVE)
+ state = ACTIVE;
+ state_confirm_timeout = Time::zero;
+ signal_state_changed.emit(state);
+ }
+ }
+}
+
+void Sensor::event(unsigned a, bool s)
+{
+ if(a==address)
+ {
+ if(s && state<MAYBE_ACTIVE)
+ {
+ state = MAYBE_ACTIVE;
+ state_confirm_timeout = 300*Time::msec;
+ signal_state_changed.emit(state);
+ }
+ else if(!s && state>MAYBE_INACTIVE)
+ {
+ state = MAYBE_INACTIVE;
+ state_confirm_timeout = 700*Time::msec;
+ signal_state_changed.emit(state);
+ }
+ }
+}
+
+} // namespace R2C2