/* Supervise emon server process */ void pwrs_Node_SupEmon ( ) { int i = errh_eAnix_emon - 1; pwr_tTime current_time; pwr_tDeltaTime diff; static float timeout = 3; if ( !np) { pwr_tOid oid; pwr_tStatus sts; sts = gdh_GetNodeObject( 0, &oid); if ( ODD(sts)) gdh_ObjidToPointer( oid, (void **) &np); if ( EVEN(sts)) return; } if ( !np) return; if ( np->ProcStatus[i] != 0 && np->ProcStatus[i] != PWR__PTIMEOUT) { time_GetTime( ¤t_time); time_Adiff( &diff, ¤t_time, &np->ProcTimeStamp[i]); if ( time_DToFloat( 0, &diff) > timeout) { if ( errh_Severity( np->ProcStatus[i]) < errh_Severity(PWR__PTIMEOUT)) { np->ProcStatus[i] = PWR__PTIMEOUT; np->SystemStatus = PWR__PTIMEOUT; } } } }
/*----------------------------------------------------------------------------*\ Read method for the Modbus_TCP slave \*----------------------------------------------------------------------------*/ static pwr_tStatus IoRackRead ( io_tCtx ctx, io_sAgent *ap, io_sRack *rp ) { io_sRackLocal *local; pwr_sClass_Modbus_TCP_Slave *sp; pwr_tStatus sts; pwr_tTime now; pwr_tDeltaTime dt; local = rp->Local; sp = (pwr_sClass_Modbus_TCP_Slave *) rp->op; if (((sp->Status == MB__CONNDOWN) || (sp->Status == MB__CONNLOST)) && sp->DisableSlave != 1) { /* Reconnect */ time_GetTimeMonotonic( &now); time_Adiff(&dt, &now, &local->last_try_connect_time); if (dt.tv_sec >= (1 << MIN(sp->ReconnectCount, 6))) { sts = connect_slave(local, rp); if (sts >= 0) { sp->ReconnectCount = 0; sp->Status = MB__NORMAL; } else { sp->ReconnectCount++; memset(&sp->Inputs, 0, local->input_size); } } } /* Receive data */ if ((sp->Status == MB__NORMAL) && !sp->SingleOp) { sts = mb_recv_data(local, rp, sp); } if (sp->DisableSlave != 1) { if (sp->Status == MB__NORMAL) { sp->ErrorCount = 0; } else { sp->ErrorCount++; } if (sp->ErrorCount > sp->ErrorLimit) { memset(&sp->Inputs, 0, local->input_size); } } else { sp->ErrorCount = 0; sp->Status = MB__DISABLED; } return IO__SUCCESS; }
pwr_tDeltaTime * time_Uptime ( pwr_tStatus *status, pwr_tDeltaTime *tp, pwr_tDeltaTime *ap ) { pwr_tDeltaTime time; long tics; struct tms buff; static int tics_per_sec = 0; static pwr_tTime boot_time = {0,0}; static pwr_tDeltaTime max_diff = {0,20000000}; pwr_tDeltaTime uptime_tics; pwr_tTime current_time; pwr_tDeltaTime diff; pwr_dStatus(sts, status, TIME__SUCCESS); if ( !tics_per_sec) tics_per_sec = sysconf(_SC_CLK_TCK); if (tp == NULL) tp = &time; tics = times(&buff); uptime_tics.tv_sec = tics / tics_per_sec; uptime_tics.tv_nsec = (tics % tics_per_sec) * (1000000000 / tics_per_sec); // pwr_Assert(tp->tv_sec >= 0 && tp->tv_nsec >= 0); clock_gettime( CLOCK_REALTIME, ¤t_time); if ( !boot_time.tv_sec) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; } else { time_Adiff( tp, ¤t_time, &boot_time); time_Dsub( &diff, tp, &uptime_tics); time_Dabs(NULL, &diff); if ( time_Dcomp(&diff, &max_diff) > 0) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; *status = TIME__CLKCHANGE; } } if (ap != NULL) return time_Dadd(tp, tp, ap); else return tp; }
static int usbio_reconnect( io_tCtx ctx, io_sAgent *ap, io_sRack *rp, io_sCard *cp) { io_sLocal *local = cp->Local; io_sLocalUSB *localUSB = (io_sLocalUSB *)rp->Local; pwr_sClass_MotionControl_USBIO *op = (pwr_sClass_MotionControl_USBIO *)cp->op; pwr_tTime time; pwr_tDeltaTime diff; unsigned int snum; int handle; int i; int status; time_GetTime( &time); time_Adiff( &diff, &time, &local->ConnectRetry); if ( time_DToFloat( 0, &diff) > 1.0) { for ( i = 0; i < (int)sizeof(localUSB->USB_Handle); i++) { status = USBIO_Open( &handle); if ( status) break; status = USBIO_GetSerialNr( &handle, &snum); if ( snum == op->Super.Address) { local->USB_Handle = localUSB->USB_Handle[local->Idx] = handle; op->Status = status; op->Super.ErrorCount = 0; IoCardClose( ctx, ap, rp, cp); IoCardInit( ctx, ap, rp, cp); errh_Info( "USBIO card reconnected '%s'", cp->Name); return 1; } } } return 0; }
pwr_tDeltaTime * time_Uptime ( pwr_tStatus *status, pwr_tDeltaTime *tp, pwr_tDeltaTime *ap ) { pwr_tDeltaTime time; unsigned long tics; static pwr_tUInt64 tics_64; struct tms buff; static int tics_per_sec = 0; static pwr_tTime boot_time = {0,0}; static pwr_tDeltaTime max_diff = {0, 20000000}; pwr_tDeltaTime uptime_tics; pwr_tTime current_time; pwr_tDeltaTime diff; static pwr_tUInt16 msb_flips = 0; static pwr_tBoolean old_high_bit = 0; pwr_tBoolean high_bit; lldiv_t uptime_s; pwr_dStatus(sts, status, TIME__SUCCESS); if ( !tics_per_sec) tics_per_sec = sysconf(_SC_CLK_TCK); if (tp == NULL) tp = &time; tics = times(&buff); high_bit = tics >> (32 - 1); if (!high_bit && old_high_bit) msb_flips++; old_high_bit = high_bit; tics_64 = ((pwr_tUInt64) msb_flips << 32) | tics; uptime_s = lldiv(tics_64, (pwr_tInt64) tics_per_sec); uptime_tics.tv_sec = (pwr_tInt64) uptime_s.quot; uptime_tics.tv_nsec = ((pwr_tUInt64) uptime_s.rem) * (1000000000 / tics_per_sec); // pwr_Assert(tp->tv_sec >= 0 && tp->tv_nsec >= 0); time_GetTime( ¤t_time); if ( !boot_time.tv_sec) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; } else { time_Adiff( tp, ¤t_time, &boot_time); time_Dsub( &diff, tp, &uptime_tics); time_Dabs(NULL, &diff); if ( time_Dcomp(&diff, &max_diff) > 0) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; if (status != NULL) { *status = TIME__CLKCHANGE; } } } if (ap != NULL) return time_Dadd(tp, tp, ap); else return tp; }
int XttTCurve::get_data( pwr_tStatus *sts, pwr_tTime from, pwr_tTime to) { pwr_tDeltaTime trange; int from_idx, to_idx; pwr_tDeltaTime diff; pwr_tFloat32 timerange; int interval; load_data( sts, &arefv[0]); if ( EVEN(*sts)) return 0; // Calculate interval if ( time_Acomp( &from, &tc.last_time) > 0 || time_Acomp( &to, &tc.first_time) < 0) // No samples in this interval return 0; time_Adiff( &diff, &tc.last_time, &tc.first_time); timerange = time_DToFloat( 0, &diff); if ( time_Acomp( &from, &tc.first_time) < 0) { from = tc.first_time; from_idx = 0; } else { time_Adiff( &diff, &from, &tc.first_time); from_idx = time_DToFloat( 0, &diff) * tc.timebuf_samples / timerange; } if ( time_Acomp( &to, &tc.last_time) >= 0) to_idx = tc.timebuf_samples; else { time_Adiff( &diff, &tc.last_time, &to); to_idx = tc.timebuf_samples - time_DToFloat( 0, &diff) * tc.timebuf_samples / timerange; } interval = (to_idx - from_idx) / 1000 + 1; rows = (to_idx - from_idx) / interval; if ( from_idx + rows * interval > tc.timebuf_samples) rows = (tc.timebuf_samples - from_idx) / interval; // Create data for time axis gcd = new GeCurveData( curve_eDataType_DsTrend); gcd->x_data[0] = (double *) calloc( 1, 8 * rows); if ( tc.timeelement_size == 4) { for ( int i = 0; i < rows; i++) gcd->x_data[0][i] = (double) (*((unsigned int *)tc.tbuf + from_idx + i * interval)); } else { for ( int i = 0; i < rows; i++) gcd->x_data[0][i] = (double)(*((unsigned int *)tc.tbuf + 2*from_idx + 2*i*interval)) + (double)1e-9 * (*((unsigned int *)tc.tbuf + 2*from_idx + 2*i*interval + 1)); } strcpy( gcd->x_name, "Time"); gcd->x_axis_type[0] = curve_eAxis_x; strcpy( gcd->x_format[0], "%10t"); for ( int j = 0; j < tc.bufcnt; j++) { strncpy( gcd->y_name[j], tc.name[j], sizeof(gcd->y_name[0])); gcd->rows[j] = rows; gcd->y_data[j] = (double *) calloc( 1, 8 * rows); for ( int i = 0; i < rows; i++) { if ( i >= tc.buf_samples[j]) break; switch ( tc.type[j]) { case pwr_eType_Int64: gcd->y_data[j][i] = *((pwr_tInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Int32: gcd->y_data[j][i] = *((pwr_tInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Int16: gcd->y_data[j][i] = *((pwr_tInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Int8: gcd->y_data[j][i] = *((pwr_tInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_UInt64: gcd->y_data[j][i] = *((pwr_tUInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_UInt32: gcd->y_data[j][i] = *((pwr_tUInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_UInt16: gcd->y_data[j][i] = *((pwr_tUInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_UInt8: gcd->y_data[j][i] = *((pwr_tUInt32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Float32: gcd->y_data[j][i] = *((pwr_tFloat32 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Float64: gcd->y_data[j][i] = *((pwr_tFloat64 *)tc.vbuf[j] + from_idx + i*interval); break; case pwr_eType_Boolean: gcd->y_data[j][i] = *((pwr_tBoolean *)tc.vbuf[j] + from_idx + i*interval); break; default: *sts = SEV__CURVETYPE; return 0; } } } gcd->y_axis_type[0] = curve_eAxis_y; gcd->cols = tc.bufcnt; gcd->get_borders(); gcd->get_default_axis(); if ( to.tv_sec != 0 && from.tv_sec != 0) { time_Adiff( &trange, &to, &from); if ( time_DToFloat( 0, &trange) < 600) strcpy( gcd->x_format[0], "%10t"); else strcpy( gcd->x_format[0], "%11t"); } else strcpy( gcd->x_format[0], "%11t"); gcd->select_color( 0); if ( curve) { curve->set_curvedata( gcd); // This will free the old gcd curve->configure_curves(); curve->configure_axes(); curve->redraw(); } *sts = XNAV__SUCCESS; return 1; }
int main (int argc, char **argv) { pwr_tStatus sts; pwr_tObjid ObjId; pwr_sClass_DsTrendConf *TConfP; pwr_tBoolean InitOK; pwr_tTime CurrentTime, LastScan, NextScan; pwr_tDeltaTime ScanDeltaTime, WaitTime; qcom_sQid qini; qcom_sQattr qAttr; int tmo; char mp[2000]; qcom_sQid qid = qcom_cNQid; qcom_sGet get; int swap = 0; trend_tCtx ctx; errh_Init("pwr_trend", errh_eAnix_trend); errh_SetStatus( PWR__SRVSTARTUP); sts = gdh_Init("ds_trend"); If_Error_Log_Exit(sts, "gdh_Init"); if (!qcom_Init(&sts, 0, "pwr_trend")) { errh_Fatal("qcom_Init, %m", sts); exit(sts); } qAttr.type = qcom_eQtype_private; qAttr.quota = 100; if (!qcom_CreateQ(&sts, &qid, &qAttr, "events")) { errh_Fatal("qcom_CreateQ, %m", sts); exit(sts); } qini = qcom_cQini; if (!qcom_Bind(&sts, &qid, &qini)) { errh_Fatal("qcom_Bind(Qini), %m", sts); exit(-1); } ctx = (trend_tCtx) calloc( 1, sizeof(trend_sCtx)); /* Wait until local nethandler has started */ while(EVEN(gdh_NethandlerRunning())) sleep(1); /* Fetch ScanTime */ sts = gdh_GetClassList(pwr_cClass_DsTrendConf, &ObjId); if (EVEN(sts)) { errh_Info("Couldn't get the DsTrendConf object. Used ScanTime = 1 s"); ctx->scantime = 1; ctx->scantime_tc = 1.0; } else { gdh_ObjidToPointer(ObjId, (pwr_tAddress *)&TConfP); ctx->scantime = TConfP->ScanTime; if ( ctx->scantime > 3600) ctx->scantime = 3600; else if ( ctx->scantime < 1) ctx->scantime = 1; ctx->scantime_tc = TConfP->ScanTime; if ( ctx->scantime_tc > 3600) ctx->scantime_tc = 3600; } ctx->dstrend_multiple = (int) (ctx->scantime / ctx->scantime_tc + 0.5); aproc_RegisterObject( ObjId); InitOK = FALSE; sts = InitTrendList( ctx); if ( EVEN(sts)) { /* This should be removed when we can wait for init messages. */ errh_SetStatus(0); errh_Info("No DsTrend objects configured"); exit(0); } /* If even sts, just wait for init message */ time_GetTimeMonotonic(&LastScan); time_FloatToD( &ScanDeltaTime, ctx->scantime_tc); aproc_TimeStamp( ctx->scantime, 5.0); errh_SetStatus( PWR__SRUN); for (;;) { time_GetTimeMonotonic(&CurrentTime); time_Aadd(&NextScan, &LastScan, &ScanDeltaTime); if (time_Acomp(&CurrentTime, &NextScan) < 0) { time_Adiff(&WaitTime, &NextScan, &CurrentTime); tmo = 1000 * time_DToFloat( 0, &WaitTime); get.maxSize = sizeof(mp); get.data = mp; qcom_Get( &sts, &qid, &get, tmo); if (sts == QCOM__TMO || sts == QCOM__QEMPTY) { if ( !swap) StoreData( ctx); } else { ini_mEvent new_event; qcom_sEvent *ep = (qcom_sEvent*) get.data; new_event.m = ep->mask; if (new_event.b.oldPlcStop && !swap) { swap = 1; errh_SetStatus( PWR__SRVRESTART); CloseTrendList( ctx); } else if (new_event.b.swapDone && swap) { swap = 0; sts = InitTrendList( ctx); errh_SetStatus( PWR__SRUN); errh_Info("Warm restart completed"); } else if (new_event.b.terminate) { exit(0); } } } else if ( !swap) StoreData( ctx); LastScan = NextScan; aproc_TimeStamp( ctx->scantime, 5.0); } return 1; }
pwr_tStatus sev_db::tree_update_value(int item_idx, pwr_tTime time, void* buf) { float value; pwr_tTime prev_time; float interval = 0; if (!m_items[item_idx].ip) return SEV__SUCCESS; m_items[item_idx].ip->ReceiveCount++; if (m_items[item_idx].ip->ReceiveCount == 0) // Wrap around, reset write count m_items[item_idx].ip->WriteCount = 0; else m_items[item_idx].ip->WriteQuota = (pwr_tFloat32)m_items[item_idx].ip->WriteCount / m_items[item_idx].ip->ReceiveCount * 100; prev_time = m_items[item_idx].ip->LastTime; m_items[item_idx].ip->LastTime = time; switch (m_items[item_idx].attr[0].type) { case pwr_eType_Float32: ((pwr_sClass_SevItemFloat*)m_items[item_idx].ip)->LastValue = *(pwr_tFloat32*)buf; value = ((pwr_sClass_SevItemFloat*)m_items[item_idx].ip)->LastValue; break; case pwr_eType_Float64: ((pwr_sClass_SevItemFloat*)m_items[item_idx].ip)->LastValue = *(pwr_tFloat64*)buf; value = ((pwr_sClass_SevItemFloat*)m_items[item_idx].ip)->LastValue; break; case pwr_eType_Int32: case pwr_eType_UInt32: ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue = *(pwr_tInt32*)buf; value = ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue; break; case pwr_eType_Int16: case pwr_eType_UInt16: ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue = *(pwr_tInt16*)buf; value = ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue; break; case pwr_eType_Int8: case pwr_eType_UInt8: ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue = *(pwr_tInt8*)buf; value = ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue; break; case pwr_eType_Boolean: ((pwr_sClass_SevItemBoolean*)m_items[item_idx].ip)->LastValue = *(pwr_tBoolean*)buf; value = ((pwr_sClass_SevItemInt*)m_items[item_idx].ip)->LastValue; break; default: return 0; } if (m_items[item_idx].options & pwr_mSevOptionsMask_MeanValue1) interval = m_cnf.MeanValueInterval1; else if (m_items[item_idx].options & pwr_mSevOptionsMask_MeanValue2) interval = m_cnf.MeanValueInterval2; if (!feqf(interval, 0.0f)) { pwr_tDeltaTime dtime; float scantime; float prev_deviation; if (prev_time.tv_sec != 0) { time_Adiff(&dtime, &time, &prev_time); time_DToFloat(&scantime, &dtime); prev_deviation = value - m_items[item_idx].mean_value; } else { scantime = m_items[item_idx].scantime; prev_deviation = 0; } m_items[item_idx].mean_value = (value * scantime + m_items[item_idx].mean_value * m_items[item_idx].mean_acc_time) / (scantime + m_items[item_idx].mean_acc_time); m_items[item_idx].mean_acc_time += scantime; m_items[item_idx].variance_acc = prev_deviation * (value - m_items[item_idx].mean_value); m_items[item_idx].variance_cnt++; if (m_items[item_idx].mean_acc_time >= (interval - m_items[item_idx].scantime / 2)) { m_items[item_idx].ip->MeanValue = m_items[item_idx].mean_value; m_items[item_idx].ip->MeanValueTime = time; if (m_items[item_idx].variance_cnt > 1) m_items[item_idx].ip->StandardDeviation = sqrt(m_items[item_idx].variance_acc / (m_items[item_idx].variance_cnt - 1)); else m_items[item_idx].ip->StandardDeviation = 0; m_items[item_idx].mean_acc_time = 0; m_items[item_idx].variance_acc = 0; m_items[item_idx].variance_cnt = 0; } } return SEV__SUCCESS; }
void fastobject::scan() { int first = 0; if ( p->Active) { scan_cnt++; if ( scan_cnt >= scan_div) scan_cnt = 0; else return; } if ( p->Function & fast_mFunction_BeforeTrigg && p->Function & fast_mFunction_AlwaysPrepared && !p->New && !p->Prepare) { p->Prepare = 1; p->Active = 1; p->TriggIndex = fast_cNoTrigg; } if ( !p->Active || (p->Function & fast_mFunction_BeforeTrigg && p->Prepare && p->TriggIndex == fast_cNoTrigg)) { // Check trigg conditions if ( p->Function & fast_mFunction_BeforeTrigg && (p->Prepare && !old_prepare)) { // Start logging at index 0 current_index = 0; p->Active = 1; p->FirstIndex = 0; p->LastIndex = 0; p->New = 0; p->TriggIndex = fast_cNoTrigg; time_GetTime( &prepare_time); first = 1; scan_cnt = 0; } if ( (p->Function & fast_mFunction_ManTrigg && p->TriggMan) || (trigg && *trigg && !old_trigg)) { if ( !( p->Function & fast_mFunction_BeforeTrigg)) { current_index = 0; p->Active = 1; p->FirstIndex = 0; p->LastIndex = 0; p->New = 0; time_GetTime( &prepare_time); scan_cnt = 0; } else { // Calculate stop index int points_before = p->NoOfPointsBeforeTrigg; if ( !points_before) points_before++; p->TriggIndex = current_index; if ( (int)p->TriggIndex - points_before > 0) stop_index = p->TriggIndex - points_before; else stop_index = p->TriggIndex + p->NoOfPoints - points_before; } time_GetTime( &p->TriggTime); p->TriggMan = 0; } if ( p->Function & fast_mFunction_LevelTrigg && p->CurveValid[0] && p->AttributeType[0] == pwr_eType_Float32 && *(pwr_tFloat32 *)attributes[0] >= p->TriggLevel && old_level < p->TriggLevel) { if ( !( p->Function & fast_mFunction_BeforeTrigg)) { current_index = 0; p->Active = 1; p->FirstIndex = 0; p->LastIndex = 0; p->New = 0; time_GetTime( &prepare_time); scan_cnt = 0; } else { // Calculate stop index int points_before = p->NoOfPointsBeforeTrigg; if ( !points_before) points_before++; p->TriggIndex = current_index; if ( (int)p->TriggIndex - points_before > 0) stop_index = p->TriggIndex - points_before; else stop_index = p->TriggIndex + p->NoOfPoints - points_before; } time_GetTime( &p->TriggTime); } } if ( p->Function & fast_mFunction_LevelTrigg && p->CurveValid[0] && p->AttributeType[0] == pwr_eType_Float32) old_level = *(pwr_tFloat32 *)attributes[0]; if ( trigg) old_trigg = *trigg; old_prepare = p->Prepare; if ( p->New) { if ( new_cnt == int(2.0 / scan_base)) { new_cnt = 0; p->New = 0; } else new_cnt++; } if ( p->Active) { pwr_tTime now; pwr_tDeltaTime diff; pwr_tFloat32 fdiff; // Store time time_GetTime( &now); time_Adiff( &diff, &now, &prepare_time); time_DToFloat( &fdiff, &diff); ((pwr_tFloat32 *)time_buffer)[current_index] = fdiff; for ( int i = 0; i < FAST_CURVES; i++) { if ( p->CurveValid[i]) { switch ( p->AttributeType[i]) { case pwr_eType_Float32: ((pwr_tFloat32 *)buffers[i])[current_index] = *(pwr_tFloat32 *)attributes[i]; break; case pwr_eType_Float64: ((pwr_tFloat64 *)buffers[i])[current_index] = *(pwr_tFloat64 *)attributes[i]; break; case pwr_eType_UInt64: case pwr_eType_Int64: ((pwr_tInt64 *)buffers[i])[current_index] = *(pwr_tInt64 *)attributes[i]; break; case pwr_eType_UInt32: case pwr_eType_Int32: ((pwr_tInt32 *)buffers[i])[current_index] = *(pwr_tInt32 *)attributes[i]; break; case pwr_eType_UInt16: case pwr_eType_Int16: ((pwr_tInt16 *)buffers[i])[current_index] = *(pwr_tInt16 *)attributes[i]; break; case pwr_eType_UInt8: case pwr_eType_Int8: ((pwr_tInt8 *)buffers[i])[current_index] = *(pwr_tInt8 *)attributes[i]; break; case pwr_eType_Boolean: ((pwr_tBoolean *)buffers[i])[current_index] = *(pwr_tBoolean *)attributes[i]; break; default: ; } } } p->LastIndex = current_index; current_index++; if ( !( p->Function & fast_mFunction_BeforeTrigg)) { if ( current_index >= (int) p->NoOfPoints) { p->Active = 0; p->Prepare = 0; p->New = 1; new_cnt = 0; } } else { if ( p->TriggIndex == fast_cNoTrigg) { // No trigg yet if ( !first && current_index - 1 == (int) p->FirstIndex) { p->FirstIndex = current_index; if ( p->FirstIndex >= p->NoOfPoints) p->FirstIndex = 0; } if ( current_index >= (int) p->NoOfPoints) current_index = 0; if ( current_index >= (int) p->NoOfPoints) current_index = 0; } else { // Check if it is time to stop if ( current_index - 1 == stop_index) { p->Active = 0; p->Prepare = 0; p->New = 1; new_cnt = 0; p->LastIndex = current_index - 1; if ( current_index - 1 == (int) p->FirstIndex) { p->FirstIndex = current_index; if ( p->FirstIndex >= p->NoOfPoints) p->FirstIndex = 0; } } else { if ( current_index - 1 == (int) p->FirstIndex) { p->FirstIndex = current_index; if ( p->FirstIndex >= p->NoOfPoints) p->FirstIndex = 0; } if ( current_index >= (int) p->NoOfPoints) current_index = 0; } } } } }
pwr_tStatus rt_post::mh_alarm_bc( mh_sMessage *MsgP) { ala_uEvent *event = (ala_uEvent *) MsgP; int sts; char str[256]; char user[80]; char group[80]; char *s; pwr_tTime etime, current; pwr_tDeltaTime diff; int rcvnum; char rcv_array[10][80]; char text[400]; if ( !post || post->conf->Disable) return 1; if ( !event->Info.EventFlags & pwr_mEventFlagsMask_Email || !event->Info.EventFlags & pwr_mEventFlagsMask_SMS) return 1; // Skip events older than 10 minutes etime = net_NetTimeToTime( &event->Info.EventTime); time_GetTime( ¤t); time_Adiff( &diff, ¤t, &etime); if ( time_DToFloat( 0, &diff) > 600) return 1; // Check if already handled if ( event->Info.EventFlags & pwr_mEventFlagsMask_Email) { if ( post->email_check( &event->Info.Id)) return 1; else post->email_register( &event->Info.Id); } if ( event->Info.EventFlags & pwr_mEventFlagsMask_SMS) { if ( post->sms_check( &event->Info.Id)) return 1; else post->sms_register( &event->Info.Id); } post->replace_symbol( str, event->Msg.Receiver); rcvnum = dcli_parse( str, ",", "", (char *) rcv_array, sizeof( rcv_array)/sizeof( rcv_array[0]), sizeof( rcv_array[0]), 0); for ( int i = 0; i < rcvnum; i++) { dcli_trim( str, rcv_array[i]); s = strrchr( str, '.'); if ( s) { *s = 0; strncpy( group, post->systemgroup, sizeof(group)); strncat( group, ".", sizeof(group)); strncat( group, str, sizeof(group)); strncpy( user, s+1, sizeof(user)); } else { strncpy( group, post->systemgroup, sizeof(group)); strncpy( user, str, sizeof(user)); } SystemName *sn = new SystemName( group); sts = sn->parse(); if ( EVEN(sts)) { delete sn; errh_Error( "Post systemgroup parse error, %m", sts); continue; } SystemList *sl = post->udb->find_system( sn); delete sn; if ( !sl) { errh_Error( "No such system group, %s", group); continue; } UserList *ul = (UserList *) sl->find_user( user); if ( !ul) { errh_Error( "No such user, %s", user); continue; } if ( event->Info.EventFlags & pwr_mEventFlagsMask_Email && strcmp( post->conf->EmailCmd, "") != 0) { char address[40]; pwr_tCmd cmd; ul->get_email( address, sizeof(address)); post->format_email_text( MsgP, text, sizeof(text)); post->format_cmd( cmd, post->conf->EmailCmd, address, text); if ( post->conf->Options & pwr_mPostOptionsMask_Log) errh_Info( "Email: %s", cmd); system( cmd); post->conf->SentEmail++; } if ( event->Info.EventFlags & pwr_mEventFlagsMask_SMS && strcmp( post->conf->SMS_Cmd, "") != 0) { char sms[40]; pwr_tCmd cmd; ul->get_sms( sms, sizeof(sms)); post->format_sms_text( MsgP, text, sizeof(text)); post->format_cmd( cmd, post->conf->SMS_Cmd, sms, text); if ( post->conf->Options & pwr_mPostOptionsMask_Log) errh_Info( "SMS: %s", cmd); system( cmd); post->conf->SentSMS++; } } return 1; }
int XttLog::play(XNav* xnav, char* filename, double speed, int pid) { std::ifstream fp; pwr_tFileName fname; char line[1000]; int ind; int lpid; char t1[20], t2[20]; char type[40]; char user[40]; char attr[512]; char value[1000]; xttlog_eCategory category; int num; char timstr[40]; pwr_tTime log_time; pwr_tTime prev_time; pwr_tDeltaTime diff_time; float diff_time_f; int first = 1; if (speed < 0.01) speed = 1; dcli_translate_filename(fname, filename); fp.open(fname); if (!fp) return 0; CoWow::SetAutoRemove(1); while (fp.getline(line, sizeof(line))) { num = sscanf(line, "%d %s %s %d %s %s", &ind, t1, t2, &lpid, type, user); if (pid != 0 && pid != lpid) continue; strcpy(timstr, t1); strcat(timstr, " "); strcat(timstr, t2); time_AsciiToA(timstr, &log_time); if (!first) { time_Adiff(&diff_time, &log_time, &prev_time); diff_time_f = time_DToFloat(0, &diff_time); } else diff_time_f = 0; if (diff_time_f / speed > 0.01) { xnav->wow->Wait(diff_time_f / speed); } char* s; int cnt = 0; for (s = line; *s; s++) { if (s > line && (*(s - 1) == ' ' || *(s - 1) == ' ') && (*s != ' ' && *s != ' ')) cnt++; if (cnt == num) break; } string_to_category(type, &category); switch (category) { case xttlog_eCategory_Command: case xttlog_eCategory_OpenGraph: case xttlog_eCategory_CloseGraph: case xttlog_eCategory_ApplNew: case xttlog_eCategory_ApplDelete: { int sts = xnav->command(s); printf("%8.3f %-14s %9d %s\n", diff_time_f, type, sts, s); break; } case xttlog_eCategory_SetObjectInfo: { unsigned char buf[500]; unsigned int size; pwr_tStatus sts = 0; sscanf(s, "%s %s", attr, value); octstring_to_value(value, buf, sizeof(buf), &size); if (size) { sts = gdh_SetObjectInfo(attr, buf, size); if (EVEN(sts)) { printf("gdh_SetObjectInfo error: %s %s\n", attr, value); } } printf("%8.3f %-14s %9d %s %s\n", diff_time_f, type, sts, attr, value); break; } case xttlog_eCategory_Event: { unsigned char buf[500]; unsigned int size; pwr_tStatus sts; char graph[600]; pwr_tAName instance; char* t; sts = 0; sscanf(s, "%s %s", graph, value); octstring_to_value(value, buf, sizeof(buf), &size); t = strchr(graph, ':'); if (!t) break; *t = 0; strcpy(instance, t + 1); xnav->ge_event_exec(xttlog_eCategory_Event, graph, instance, buf, size); printf( "%8.3f %-14s %9d %s %10.10s\n", diff_time_f, type, sts, graph, value); break; } case xttlog_eCategory_GeConfirmOk: case xttlog_eCategory_GeConfirmCancel: { pwr_tStatus sts; char graph[600]; pwr_tAName instance; char* t; sts = 0; strcpy(graph, s); t = strchr(graph, ':'); if (!t) break; *t = 0; strcpy(instance, t + 1); xnav->ge_event_exec(category, graph, instance, 0, 0); printf("%8.3f %-14s %9d %s\n", diff_time_f, type, sts, graph); break; } case xttlog_eCategory_LogStart: printf("%8.3f %-14s\n", diff_time_f, type); break; default:; } prev_time = log_time; if (first) first = 0; } CoWow::SetAutoRemove(0); printf(" %-14s\n", "EndOfFile"); return 1; }
int XttSevHist::get_data( pwr_tStatus *sts, pwr_tTime from, pwr_tTime to) { if( sevhistobject ) { return get_objectdata(sts, from, to); } pwr_tTime *tbuf; void *vbuf; pwr_tDeltaTime trange; sevcli_get_itemdata( sts, scctx, oid, aname, from, to, 1000, &tbuf, &vbuf, &rows, &vtype, &vsize); if ( EVEN(*sts)) return 0; if( rows == 0 ) { return 0; } // Create data for time axis gcd = new GeCurveData( curve_eDataType_DsTrend); gcd->data[0] = (double *) calloc( 1, 8 * rows); for ( int i = 0; i < rows; i++) gcd->data[0][i] = (double)tbuf[i].tv_sec + (double)1e-9 * tbuf[i].tv_nsec; strcpy( gcd->name[0], "Time"); gcd->axis_type[0] = curve_eAxis_x; strcpy( gcd->format[0], "%10t"); strcpy( gcd->name[1], aname); gcd->data[1] = (double *) calloc( 1, 8 * rows); for ( int i = 0; i < rows; i++) { switch ( vtype) { case pwr_eType_Int64: gcd->data[1][i] = ((pwr_tInt32 *)vbuf)[i]; break; case pwr_eType_Int32: gcd->data[1][i] = ((pwr_tInt32 *)vbuf)[i]; break; case pwr_eType_Int16: gcd->data[1][i] = ((pwr_tInt32 *)vbuf)[i]; break; case pwr_eType_Int8: gcd->data[1][i] = ((pwr_tInt32 *)vbuf)[i]; break; case pwr_eType_UInt64: gcd->data[1][i] = ((pwr_tUInt32 *)vbuf)[i]; break; case pwr_eType_UInt32: gcd->data[1][i] = ((pwr_tUInt32 *)vbuf)[i]; break; case pwr_eType_UInt16: gcd->data[1][i] = ((pwr_tUInt32 *)vbuf)[i]; break; case pwr_eType_UInt8: gcd->data[1][i] = ((pwr_tUInt32 *)vbuf)[i]; break; case pwr_eType_Float32: gcd->data[1][i] = ((pwr_tFloat32 *)vbuf)[i]; break; case pwr_eType_Float64: gcd->data[1][i] = ((pwr_tFloat64 *)vbuf)[i]; break; case pwr_eType_Boolean: gcd->data[1][i] = ((pwr_tBoolean *)vbuf)[i]; break; default: *sts = SEV__CURVETYPE; return 0; } } free( tbuf); free( vbuf); gcd->axis_type[1] = curve_eAxis_y; gcd->cols = 1 + 1; gcd->rows = rows; gcd->get_borders(); gcd->get_default_axis(); if ( to.tv_sec != 0 && from.tv_sec != 0) { time_Adiff( &trange, &to, &from); if ( time_DToFloat( 0, &trange) < 600) strcpy( gcd->format[0], "%10t"); else strcpy( gcd->format[0], "%11t"); } else strcpy( gcd->format[0], "%11t"); gcd->select_color( 0); if ( curve) { curve->set_curvedata( gcd); // This will free the old gcd curve->configure_curves(); curve->configure_axes(); curve->redraw(); } *sts = SEV__SUCCESS; return 1; }
int XttSevHist::get_objectdata( pwr_tStatus *sts, pwr_tTime from, pwr_tTime to) { pwr_tTime *tbuf; void *vbuf; sevcli_sHistAttr *histattrbuf; int numAttributes; pwr_tDeltaTime trange; sevcli_get_objectitemdata( sts, scctx, oid, aname, from, to, 1000, &tbuf, &vbuf, &rows, &histattrbuf, &numAttributes); if ( EVEN(*sts)) return 0; if( rows == 0 ) { return 0; } // Create data for time axis gcd = new GeCurveData( curve_eDataType_DsTrend); gcd->data[0] = (double *) calloc( 1, 8 * rows); for ( int i = 0; i < rows; i++) gcd->data[0][i] = (double)tbuf[i].tv_sec + (double)1e-9 * tbuf[i].tv_nsec; strcpy( gcd->name[0], "Time"); gcd->axis_type[0] = curve_eAxis_x; strcpy( gcd->format[0], "%10t"); //todo linesize bör vi kunna få från sevcli_get_objectitemdata int linesize = 0; for( int i = 0; i < numAttributes; i++) { linesize += histattrbuf[i].size; } void *dataptr = vbuf; int tmp = 0; gcd->cols = 1; for( int i = 0; i < numAttributes; i++) { switch ( histattrbuf[i].type ) { case pwr_eType_Int64: case pwr_eType_Int32: case pwr_eType_Int16: case pwr_eType_Int8: case pwr_eType_UInt64: case pwr_eType_UInt32: case pwr_eType_UInt16: case pwr_eType_UInt8: case pwr_eType_Float32: case pwr_eType_Float64: case pwr_eType_Boolean: break; default: tmp += histattrbuf[i].size; continue; } if( gcd->cols >= CURVE_MAX_COLS ) { printf("To many columns for curve class max:%d\n", CURVE_MAX_COLS); break; } gcd->cols++; strcpy( gcd->name[gcd->cols-1], histattrbuf[i].aname); gcd->data[gcd->cols-1] = (double *) calloc( 1, 8 * rows); gcd->axis_type[gcd->cols-1] = curve_eAxis_y; dataptr = (char *)vbuf + tmp; //tmp += histattrbuf[i].size; for ( int j = 0; j < rows; j++) { //dataptr = (char *)dataptr + linesize*j; dataptr = ((char *)vbuf)+ j * linesize + tmp; switch ( histattrbuf[i].type ) { case pwr_eType_Int64: gcd->data[gcd->cols-1][j] = *(pwr_tInt64 *)dataptr; break; case pwr_eType_Int32: gcd->data[gcd->cols-1][j] = *(pwr_tInt32 *)dataptr; break; case pwr_eType_Int16: gcd->data[gcd->cols-1][j] = *(pwr_tInt16 *)dataptr; break; case pwr_eType_Int8: gcd->data[gcd->cols-1][j] = *(pwr_tInt8 *)dataptr; break; case pwr_eType_UInt64: gcd->data[gcd->cols-1][j] = *(pwr_tUInt64 *)dataptr; break; case pwr_eType_UInt32: gcd->data[gcd->cols-1][j] = *(pwr_tUInt32 *)dataptr; break; case pwr_eType_UInt16: gcd->data[gcd->cols-1][j] = *(pwr_tUInt16 *)dataptr; break; case pwr_eType_UInt8: gcd->data[gcd->cols-1][j] = *(pwr_tUInt8 *)dataptr; break; case pwr_eType_Float32: gcd->data[gcd->cols-1][j] = *(pwr_tFloat32 *)dataptr; break; case pwr_eType_Float64: gcd->data[gcd->cols-1][j] = *(pwr_tFloat64 *)dataptr; break; case pwr_eType_Boolean: gcd->data[gcd->cols-1][j] = *(pwr_tBoolean *)dataptr; break; default: *sts = SEV__CURVETYPE; return 0; } } tmp += histattrbuf[i].size; } free( tbuf); free( vbuf); free( histattrbuf); gcd->rows = rows; gcd->get_borders(); gcd->get_default_axis(); if ( to.tv_sec != 0 && from.tv_sec != 0) { time_Adiff( &trange, &to, &from); if ( time_DToFloat( 0, &trange) < 600) strcpy( gcd->format[0], "%10t"); else strcpy( gcd->format[0], "%11t"); } else strcpy( gcd->format[0], "%11t"); gcd->select_color( 0); if ( curve) { curve->set_curvedata( gcd); // This will free the old gcd curve->configure_curves(); curve->configure_axes(); curve->redraw(); } *sts = SEV__SUCCESS; return 1; }
void pwrs_Node_Exec ( ) { int i; pwr_tTime current_time; pwr_tDeltaTime diff; errh_eSeverity severity; errh_eSeverity system_severity; errh_eSeverity plc_severity; int new_idx = -1; static int supervise[80] = { 0,0,0,0,1,1,1,0,1,1, 1,1,1,0,0,1,0,1,1,1, 1,1,1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1}; static int reboot_done = 0; if ( !np) { pwr_tOid oid; pwr_tStatus sts; sts = gdh_GetNodeObject( 0, &oid); if ( ODD(sts)) gdh_ObjidToPointer( oid, (void **) &np); if ( EVEN(sts)) return; } if ( !np) return; if ( np->EmergBreakTrue) { switch ( np->EmergBreakSelect) { case 1: { /* Reboot */ int sts; if ( !reboot_done) { errh_Fatal( "Emergency break action: reboot"); sts = system("rt_prio --reboot"); if ( sts != 0) errh_Fatal("Unable to reboot, sts %d", sts); reboot_done = 1; } break; } default: ; } } else reboot_done = 0; /* Calculate plc status */ new_idx = -1; plc_severity = errh_Severity( np->ProcStatus[errh_eAnix_plc-1]); for ( i = errh_eAnix_plc1 - 1; i < errh_eAnix_plc1 - 1 + errh_cAnix_PlcSize; i++) { severity = errh_Severity( np->ProcStatus[i]); if ( np->ProcStatus[i] != 0 && EVEN(np->ProcStatus[i])) { if ( severity >= plc_severity) { new_idx = i; plc_severity = severity; } } } if ( new_idx != -1) np->ProcStatus[errh_eAnix_plc-1] = np->ProcStatus[new_idx]; else if ( EVEN(np->ProcStatus[errh_eAnix_plc-1])) np->ProcStatus[errh_eAnix_plc-1] = PWR__SRUN; /* Calculate system status and check timestamp */ new_idx = -1; system_severity = errh_Severity( np->SystemStatus); time_GetTime( ¤t_time); for ( i = 0; i < sizeof(np->ProcStatus)/sizeof(np->ProcStatus[0]); i++) { if ( np->ProcStatus[i] != 0 && supervise[i]) { time_Adiff( &diff, &np->ProcTimeStamp[i], ¤t_time); if ( time_Dcomp( &diff, 0) < 0) { if ( errh_Severity( np->ProcStatus[i]) < errh_Severity(PWR__PTIMEOUT)) np->ProcStatus[i] = PWR__PTIMEOUT; } else if ( np->ProcStatus[i] == PWR__PTIMEOUT) { np->ProcStatus[i] = (i < errh_cAnix_SrvSize) ? PWR__SRUN : PWR__ARUN; } } severity = errh_Severity( np->ProcStatus[i]); if ( np->ProcStatus[i] != 0 && EVEN(np->ProcStatus[i])) { if ( severity >= system_severity) { new_idx = i; system_severity = severity; } } } if ( new_idx != -1) np->SystemStatus = np->ProcStatus[new_idx]; else if ( EVEN(np->SystemStatus)) np->SystemStatus = PWR__RUNNING; }