137 lines
3.5 KiB
C++
137 lines
3.5 KiB
C++
/*
|
||
* ValueWatchDog.hpp
|
||
*
|
||
* Created on: 12 окт. 2019 г.
|
||
* Author: titov
|
||
*/
|
||
|
||
#ifndef SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_
|
||
#define SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_
|
||
|
||
#include "../systemic/Timer.hh"
|
||
#include "../systemic/IValue.hpp"
|
||
#include "../systemic/IStatus.hh"
|
||
|
||
namespace schematic {
|
||
|
||
template<typename BaseType>
|
||
struct ValueWatchDogBase : public systemic::IStatus {
|
||
protected:
|
||
typedef systemic::IValue<BaseType> Value;
|
||
typedef BaseType Base;
|
||
|
||
|
||
ValueWatchDogBase( Value & value, float timeout_in_second );
|
||
|
||
public:
|
||
virtual operator bool() const;
|
||
|
||
protected:
|
||
Value & value;
|
||
const systemic::time_t timeout;
|
||
|
||
mutable Base previous_value;
|
||
mutable systemic::Timer timer;
|
||
|
||
};
|
||
|
||
|
||
|
||
template<typename BaseType>
|
||
struct ValueWatchDogObser2Obser : public ValueWatchDogBase<BaseType> {
|
||
|
||
typedef systemic::IValue<BaseType> Value;
|
||
typedef BaseType Base;
|
||
|
||
ValueWatchDogObser2Obser( Value & value, float timeout_in_second ) : ValueWatchDogBase<BaseType>( value, timeout_in_second ) {}
|
||
operator bool() const;
|
||
|
||
};
|
||
|
||
template<typename BaseType>
|
||
struct ValueWatchDogProcess2Obser : public ValueWatchDogBase<BaseType> {
|
||
|
||
typedef systemic::IValue<BaseType> Value;
|
||
typedef BaseType Base;
|
||
|
||
ValueWatchDogProcess2Obser( Value & value, float timeout_in_second ) : ValueWatchDogBase<BaseType>( value, timeout_in_second ) {}
|
||
void process();
|
||
|
||
};
|
||
|
||
//template<typename BaseType>
|
||
//struct ValueWatchDogAsync2Obser : public systemic::IStatus, public IValueSet<BaseType> {
|
||
//
|
||
// ValueWatchDogAsync2Obser( float timeout_in_second );
|
||
//
|
||
// operator bool() const;
|
||
// IValueSet<BaseType> & operator=( BaseType val );
|
||
//
|
||
//protected:
|
||
// const systemic::time_t timeout;
|
||
//
|
||
// Base previous_value;
|
||
// systemic::Timer timer;
|
||
//};
|
||
|
||
//template<typename BaseType>
|
||
//struct ValueWatchDogAsync2Process : public IValueSet<BaseType> {
|
||
//
|
||
// ValueWatchDogAsync2Obser( IValueSet<BaseType> & output, float timeout_in_second );
|
||
//
|
||
// void process();
|
||
// IValueSet<BaseType> & operator=( BaseType val );
|
||
//
|
||
//protected:
|
||
// IValueSet<BaseType> & output;
|
||
// const systemic::time_t timeout;
|
||
//
|
||
// Base previous_value;
|
||
// systemic::Timer timer;
|
||
//};
|
||
|
||
}
|
||
|
||
template<typename BaseType>
|
||
inline schematic::ValueWatchDogBase<BaseType>::ValueWatchDogBase(
|
||
Value & value, float timeout_in_second ) : value(value), timeout(systemic::seconds2time(timeout_in_second)), previous_value(value), timer() {
|
||
|
||
timer.start(timeout);
|
||
|
||
}
|
||
|
||
template<typename BaseType>
|
||
inline schematic::ValueWatchDogBase<BaseType>::operator bool() const {
|
||
|
||
return timer.delayElapsed();
|
||
|
||
}
|
||
|
||
template<typename BaseType>
|
||
inline schematic::ValueWatchDogObser2Obser<BaseType>::operator bool() const {
|
||
|
||
Base temp_value = ValueWatchDogBase<BaseType>::value;
|
||
|
||
if( ValueWatchDogBase<BaseType>::previous_value != temp_value ) {
|
||
ValueWatchDogBase<BaseType>::previous_value = temp_value;
|
||
ValueWatchDogBase<BaseType>::timer.start( ValueWatchDogBase<BaseType>::timeout );
|
||
}
|
||
|
||
return ValueWatchDogBase<BaseType>::operator bool();
|
||
|
||
}
|
||
|
||
template<typename BaseType>
|
||
inline void schematic::ValueWatchDogProcess2Obser<BaseType>::process() {
|
||
|
||
typename ValueWatchDogBase<BaseType>::Base temp_value = ValueWatchDogBase<BaseType>::value;
|
||
|
||
if( ValueWatchDogBase<BaseType>::previous_value != temp_value ) {
|
||
ValueWatchDogBase<BaseType>::previous_value = temp_value;
|
||
ValueWatchDogBase<BaseType>::timer.start(ValueWatchDogBase<BaseType>::timeout);
|
||
}
|
||
|
||
}
|
||
|
||
#endif /* SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_ */
|