Commit e99072bc authored by Claes Sjofors's avatar Claes Sjofors

sev_server, linkear regr for boolean added

parent 565034de
...@@ -128,7 +128,17 @@ class sev_item { ...@@ -128,7 +128,17 @@ class sev_item {
strncpy( oname, x.oname, sizeof(oname)); strncpy( oname, x.oname, sizeof(oname));
strncpy( description, x.description, sizeof(description)); strncpy( description, x.description, sizeof(description));
if ( x.cache) if ( x.cache)
cache = new sev_valuecache(*x.cache); switch ( attr[0].type) {
case pwr_eType_Float32:
case pwr_eType_Float64:
case pwr_eType_Int32:
cache = new sev_valuecache_double(*(sev_valuecache_double *)x.cache);
break;
case pwr_eType_Boolean:
cache = new sev_valuecache_bool(*(sev_valuecache_bool *)x.cache);
break;
default: ;
}
} }
~sev_item() { ~sev_item() {
if ( cache) if ( cache)
......
...@@ -1015,16 +1015,25 @@ int sev_dbms::store_value( pwr_tStatus *sts, int item_idx, int attr_idx, ...@@ -1015,16 +1015,25 @@ int sev_dbms::store_value( pwr_tStatus *sts, int item_idx, int attr_idx,
pwr_tTime time, void *buf, unsigned int size) pwr_tTime time, void *buf, unsigned int size)
{ {
if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandLinearRegr) { if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandLinearRegr) {
double value; void *value;
double dval;
pwr_tBoolean bval;
switch ( m_items[item_idx].attr[0].type) { switch ( m_items[item_idx].attr[0].type) {
case pwr_eType_Float32: case pwr_eType_Float32:
value = *(pwr_tFloat32 *)buf; dval = *(pwr_tFloat32 *)buf;
value = &dval;
break; break;
case pwr_eType_Float64: case pwr_eType_Float64:
value = *(pwr_tFloat64 *)buf; dval = *(pwr_tFloat64 *)buf;
value = &dval;
break; break;
case pwr_eType_Int32: case pwr_eType_Int32:
value = *(pwr_tInt32 *)buf; dval = *(pwr_tInt32 *)buf;
value = &dval;
break;
case pwr_eType_Boolean:
bval = *(pwr_tBoolean *)buf;
value = &bval;
break; break;
default: default:
return 0; return 0;
...@@ -4015,24 +4024,29 @@ int sev_dbms::store_stat( sev_sStat *stat) ...@@ -4015,24 +4024,29 @@ int sev_dbms::store_stat( sev_sStat *stat)
return 1; return 1;
} }
void sev_dbms::write_db_cb( void *data, int idx, double value, pwr_tTime *time) void sev_dbms::write_db_cb( void *data, int idx, void *value, pwr_tTime *time)
{ {
pwr_tStatus sts; pwr_tStatus sts;
sev_dbms *dbms = (sev_dbms *)data; sev_dbms *dbms = (sev_dbms *)data;
switch( dbms->m_items[idx].attr[0].type) { switch( dbms->m_items[idx].attr[0].type) {
case pwr_eType_Float32: { case pwr_eType_Float32: {
pwr_tFloat32 v = value; pwr_tFloat32 v = *(double *)value;
dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v)); dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v));
break; break;
} }
case pwr_eType_Float64: { case pwr_eType_Float64: {
pwr_tFloat64 v = value; pwr_tFloat64 v = *(double *)value;
dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v)); dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v));
break; break;
} }
case pwr_eType_Int32: { case pwr_eType_Int32: {
pwr_tInt32 v = value; pwr_tInt32 v = *(double *)value;
dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v));
break;
}
case pwr_eType_Boolean: {
pwr_tBoolean v = *(pwr_tBoolean *)value;
dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v)); dbms->write_value( &sts, idx, 0, *time, &v, sizeof(v));
break; break;
} }
...@@ -4042,13 +4056,24 @@ void sev_dbms::write_db_cb( void *data, int idx, double value, pwr_tTime *time) ...@@ -4042,13 +4056,24 @@ void sev_dbms::write_db_cb( void *data, int idx, double value, pwr_tTime *time)
void sev_dbms::add_cache( int item_idx) void sev_dbms::add_cache( int item_idx)
{ {
if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandMeanValue) switch ( m_items[item_idx].attr[0].type) {
m_items[item_idx].cache = new sev_valuecache( sev_eCvType_Mean, m_items[item_idx].deadband, case pwr_eType_Float32:
m_items[item_idx].scantime); case pwr_eType_Float64:
else case pwr_eType_Int32:
m_items[item_idx].cache = new sev_valuecache( sev_eCvType_Point, m_items[item_idx].deadband, if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandMeanValue)
m_items[item_idx].scantime); m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Mean, m_items[item_idx].deadband,
m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx); m_items[item_idx].scantime);
else
m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Point, m_items[item_idx].deadband,
m_items[item_idx].scantime);
m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx);
break;
case pwr_eType_Boolean:
m_items[item_idx].cache = new sev_valuecache_bool( sev_eCvType_Point);
m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx);
break;
default: ;
}
} }
int sev_dbms::begin_transaction() int sev_dbms::begin_transaction()
......
...@@ -163,7 +163,7 @@ class sev_dbms : public sev_db { ...@@ -163,7 +163,7 @@ class sev_dbms : public sev_db {
char *dbName() { return sev_dbms_env::dbName();} char *dbName() { return sev_dbms_env::dbName();}
char *pwrtype_to_type( pwr_eType type, unsigned int size); char *pwrtype_to_type( pwr_eType type, unsigned int size);
static int timestr_to_time( char *tstr, pwr_tTime *ts); static int timestr_to_time( char *tstr, pwr_tTime *ts);
static void write_db_cb( void *data, int idx, double value, pwr_tTime *time); static void write_db_cb( void *data, int idx, void *value, pwr_tTime *time);
int check_objectitem( pwr_tStatus *sts, char *tablename, pwr_tOid oid, char *oname, char *aname, int check_objectitem( pwr_tStatus *sts, char *tablename, pwr_tOid oid, char *oname, char *aname,
pwr_tDeltaTime storagetime, pwr_tDeltaTime storagetime,
char *description, pwr_tFloat32 scantime, char *description, pwr_tFloat32 scantime,
......
...@@ -45,9 +45,9 @@ ...@@ -45,9 +45,9 @@
#include "pwr_baseclasses.h" #include "pwr_baseclasses.h"
#include "sev_valuecache.h" #include "sev_valuecache.h"
const int sev_valuecache::m_size = VALUECACHE_SIZE; const int sev_valuecache_double::m_size = VALUECACHE_SIZE;
int sev_valuecache::idx( int index) int sev_valuecache_double::idx( int index)
{ {
if ( !m_length) if ( !m_length)
return 0; return 0;
...@@ -59,13 +59,14 @@ int sev_valuecache::idx( int index) ...@@ -59,13 +59,14 @@ int sev_valuecache::idx( int index)
return i; return i;
} }
sev_sCacheValue& sev_valuecache::operator[]( const int index) sev_sCacheValueDouble& sev_valuecache_double::operator[]( const int index)
{ {
return m_val[idx(index)]; return m_val[idx(index)];
} }
void sev_valuecache::add( double val, pwr_tTime *t) void sev_valuecache_double::add( void *value, pwr_tTime *t)
{ {
double val = *(double *)value;
double time; double time;
pwr_tDeltaTime dt; pwr_tDeltaTime dt;
...@@ -109,7 +110,7 @@ void sev_valuecache::add( double val, pwr_tTime *t) ...@@ -109,7 +110,7 @@ void sev_valuecache::add( double val, pwr_tTime *t)
calculate_epsilon(0); calculate_epsilon(0);
} }
void sev_valuecache::evaluate() void sev_valuecache_double::evaluate()
{ {
int value_added = 1; int value_added = 1;
...@@ -128,7 +129,7 @@ void sev_valuecache::evaluate() ...@@ -128,7 +129,7 @@ void sev_valuecache::evaluate()
} }
} }
void sev_valuecache::calculate_k() void sev_valuecache_double::calculate_k()
{ {
double xysum = 0; double xysum = 0;
double x2sum = 0; double x2sum = 0;
...@@ -151,7 +152,7 @@ void sev_valuecache::calculate_k() ...@@ -151,7 +152,7 @@ void sev_valuecache::calculate_k()
} }
} }
void sev_valuecache::write( int index) void sev_valuecache_double::write( int index)
{ {
int ii = idx(index); int ii = idx(index);
double wval, wtime; double wval, wtime;
...@@ -189,12 +190,12 @@ void sev_valuecache::write( int index) ...@@ -189,12 +190,12 @@ void sev_valuecache::write( int index)
if ( m_write_cb) { if ( m_write_cb) {
pwr_tTime time; pwr_tTime time;
time_Aadd( &time, &m_start_time, time_Float64ToD( 0, wtime)); time_Aadd( &time, &m_start_time, time_Float64ToD( 0, wtime));
(m_write_cb)( m_userdata, m_useridx, wval, &time); (m_write_cb)( m_userdata, m_useridx, &wval, &time);
} }
} }
// Calculate epsilon for all // Calculate epsilon for all
void sev_valuecache::calculate_epsilon() void sev_valuecache_double::calculate_epsilon()
{ {
if ( m_length == 1) { if ( m_length == 1) {
m_val[m_first].epsilon = 0; m_val[m_first].epsilon = 0;
...@@ -206,7 +207,7 @@ void sev_valuecache::calculate_epsilon() ...@@ -206,7 +207,7 @@ void sev_valuecache::calculate_epsilon()
} }
// Calculate epsilon for one index // Calculate epsilon for one index
void sev_valuecache::calculate_epsilon( int index) void sev_valuecache_double::calculate_epsilon( int index)
{ {
int ii = idx(index); int ii = idx(index);
if ( m_k >= 1E32) { if ( m_k >= 1E32) {
...@@ -219,7 +220,7 @@ void sev_valuecache::calculate_epsilon( int index) ...@@ -219,7 +220,7 @@ void sev_valuecache::calculate_epsilon( int index)
// Check deadband for one index // Check deadband for one index
// Returns true if all values inside deadband. // Returns true if all values inside deadband.
bool sev_valuecache::check_deadband( int index) bool sev_valuecache_double::check_deadband( int index)
{ {
if ( m_val[idx(index)].epsilon > m_deadband) if ( m_val[idx(index)].epsilon > m_deadband)
return false; return false;
...@@ -228,7 +229,7 @@ bool sev_valuecache::check_deadband( int index) ...@@ -228,7 +229,7 @@ bool sev_valuecache::check_deadband( int index)
// Check deadband for all values // Check deadband for all values
// Returns true if all values inside deadband. // Returns true if all values inside deadband.
bool sev_valuecache::check_deadband() bool sev_valuecache_double::check_deadband()
{ {
for ( int i = 0; i < m_length; i++) { for ( int i = 0; i < m_length; i++) {
int ii = idx(i); int ii = idx(i);
...@@ -238,7 +239,7 @@ bool sev_valuecache::check_deadband() ...@@ -238,7 +239,7 @@ bool sev_valuecache::check_deadband()
return true; return true;
} }
int sev_valuecache::get_optimal_write() int sev_valuecache_double::get_optimal_write()
{ {
if ( m_type == sev_eCvType_Mean) { if ( m_type == sev_eCvType_Mean) {
m_last_k = m_k; m_last_k = m_k;
...@@ -267,3 +268,30 @@ int sev_valuecache::get_optimal_write() ...@@ -267,3 +268,30 @@ int sev_valuecache::get_optimal_write()
return min_idx; return min_idx;
} }
void sev_valuecache_bool::add( void *value, pwr_tTime *t)
{
m_val.val = *(pwr_tBoolean *)value;
m_val.time = *t;
if ( m_inited) {
// Store valeu
write( 0);
m_inited = true;
}
}
void sev_valuecache_bool::evaluate()
{
if ( m_val.val != m_wval.val) {
write(0);
}
}
void sev_valuecache_bool::write( int index)
{
m_wval.val = m_val.val;
m_wval.time = m_val.time;
if ( m_write_cb)
(m_write_cb)( m_userdata, m_useridx, &m_wval.val, &m_wval.time);
}
...@@ -49,21 +49,45 @@ typedef struct { ...@@ -49,21 +49,45 @@ typedef struct {
double val; double val;
double time; double time;
double epsilon; double epsilon;
} sev_sCacheValue; } sev_sCacheValueDouble;
typedef struct {
pwr_tBoolean val;
pwr_tTime time;
} sev_sCacheValueBool;
#define VALUECACHE_SIZE 20 #define VALUECACHE_SIZE 20
class sev_valuecache { class sev_valuecache {
static const int m_size; public:
sev_eCvType m_type; sev_eCvType m_type;
void *m_userdata; void *m_userdata;
int m_useridx; int m_useridx;
void (*m_write_cb)( void *, int , void *, pwr_tTime *);
sev_valuecache( sev_eCvType type) : m_type(type), m_userdata(0), m_useridx(0), m_write_cb(0) {}
sev_valuecache( const sev_valuecache& x) : m_type(x.m_type), m_userdata(x.m_userdata), m_useridx(x.m_useridx),
m_write_cb(x.m_write_cb) {}
virtual ~sev_valuecache() {};
virtual void add( void *value, pwr_tTime *time) {};
virtual void evaluate() {};
virtual void write( int index) {};
virtual void set_write_cb( void (*write_cb)( void *, int, void *, pwr_tTime *), void *userdata, int idx) {
m_write_cb = write_cb;
m_userdata = userdata;
m_useridx = idx;
}
};
class sev_valuecache_double : public sev_valuecache {
static const int m_size;
int m_length; int m_length;
int m_first; int m_first;
int m_last; int m_last;
bool m_inited; bool m_inited;
sev_sCacheValue m_val[VALUECACHE_SIZE]; sev_sCacheValueDouble m_val[VALUECACHE_SIZE];
sev_sCacheValue m_wval; sev_sCacheValueDouble m_wval;
double m_k; double m_k;
double m_m; double m_m;
double m_deadband; double m_deadband;
...@@ -72,29 +96,28 @@ class sev_valuecache { ...@@ -72,29 +96,28 @@ class sev_valuecache {
int m_last_opt_write; int m_last_opt_write;
pwr_tTime m_start_time; pwr_tTime m_start_time;
double m_last_k; double m_last_k;
void (*m_write_cb)( void *, int , double, pwr_tTime *);
public: public:
sev_valuecache( sev_eCvType type, double deadband_value, double deadband_time) : sev_valuecache_double( sev_eCvType type, double deadband_value, double deadband_time) :
m_type(type), m_userdata(0), m_useridx(0), m_length(0), m_first(0), m_last(0), m_inited(false), sev_valuecache(type), m_length(0), m_first(0), m_last(0), m_inited(false),
m_deadband_value(deadband_value), m_deadband_time(deadband_time), m_write_cb(0) { m_deadband_value(deadband_value), m_deadband_time(deadband_time) {
memset( &m_wval, 0, sizeof(m_wval)); memset( &m_wval, 0, sizeof(m_wval));
time_GetTime(&m_start_time); time_GetTime(&m_start_time);
} }
sev_valuecache( const sev_valuecache& x) : m_type(x.m_type), m_userdata(x.m_userdata), m_useridx(x.m_useridx), sev_valuecache_double( const sev_valuecache_double& x) :
m_length(x.m_length), sev_valuecache(x), m_length(x.m_length),
m_first(x.m_first), m_last(x.m_last), m_inited(x.m_inited), m_k(x.m_k), m_deadband(x.m_deadband), m_first(x.m_first), m_last(x.m_last), m_inited(x.m_inited), m_k(x.m_k), m_deadband(x.m_deadband),
m_deadband_value(x.m_deadband_value), m_deadband_time(x.m_deadband_time), m_last_opt_write(x.m_last_opt_write), m_deadband_value(x.m_deadband_value), m_deadband_time(x.m_deadband_time), m_last_opt_write(x.m_last_opt_write),
m_start_time(x.m_start_time), m_last_k(x.m_last_k), m_write_cb(x.m_write_cb) { m_start_time(x.m_start_time), m_last_k(x.m_last_k) {
memcpy( m_val, x.m_val, sizeof(m_val)); memcpy( m_val, x.m_val, sizeof(m_val));
memcpy( &m_wval, &x.m_wval, sizeof(m_wval)); memcpy( &m_wval, &x.m_wval, sizeof(m_wval));
} }
~sev_valuecache() {} ~sev_valuecache_double() {}
int length() { return m_length;} int length() { return m_length;}
int idx( int index); int idx( int index);
sev_sCacheValue& operator[]( const int index); sev_sCacheValueDouble& operator[]( const int index);
sev_sCacheValue& wval() { return m_wval;} sev_sCacheValueDouble& wval() { return m_wval;}
void add( double value, pwr_tTime *time); void add( void *value, pwr_tTime *time);
void evaluate(); void evaluate();
void calculate_k(); void calculate_k();
void write( int index); void write( int index);
...@@ -105,11 +128,27 @@ class sev_valuecache { ...@@ -105,11 +128,27 @@ class sev_valuecache {
int get_optimal_write(); int get_optimal_write();
double epsilon(int index) { return m_val[idx(index)].epsilon;} double epsilon(int index) { return m_val[idx(index)].epsilon;}
double get_k() { return m_k;} double get_k() { return m_k;}
void set_write_cb( void (*write_cb)( void *, int, double, pwr_tTime *), void *userdata, int idx) { };
m_write_cb = write_cb;
m_userdata = userdata; class sev_valuecache_bool : public sev_valuecache {
m_useridx = idx; bool m_inited;
sev_sCacheValueBool m_val;
sev_sCacheValueBool m_wval;
public:
sev_valuecache_bool( sev_eCvType type) :
sev_valuecache(type), m_inited(false) {
memset( &m_wval, 0, sizeof(m_wval));
}
sev_valuecache_bool( const sev_valuecache_bool& x) :
sev_valuecache(x), m_inited(x.m_inited) {
memcpy( &m_wval, &x.m_wval, sizeof(m_wval));
} }
~sev_valuecache_bool() {}
sev_sCacheValueBool& wval() { return m_wval;}
void add( void *value, pwr_tTime *time);
void evaluate();
void write( int index);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment