// MinMaxAvg - Points Selected by BQL
// Daniel Drury / 2007
//
// Return BQL result with 1st column is a BStatusNumeric
// Edit property sheet with BQL, get BQL by using query builder
// Example: Avg all Numeric Points in station
// station:|slot:/|bql:select out from control:NumericPoint
// Example: Avg all Numeric Points starting with ZN-T
// station:|slot:/|bql:select out from control:NumericPoint where displayName like 'ZN-T*'
public void onStart() throws Exception
{
updateTimer();
}
public void onExecute() throws Exception
{
String ord = getInBqlOrd();
BStatusNumeric Value;
double num=0;
double avg=0;
double min=0;
double max=0;
double first=1;
updateTimer();
BITable result = (BITable)BOrd.make(ord).resolve(Sys.getStation()).get(); // execute bql into table
ColumnList columns = result.getColumns(); // get table cols data
TableCursor c = (TableCursor)result.cursor(); // setup table cursor
first = 1; // init stuff on 1st valid found
num = 0; // init number points found
while (c.next()) // walk bql rows
{
Column valueColumn = columns.get(0); // data is in 1st col
Value = (BStatusNumeric) c.get(valueColumn); // get BStatusNumeric from table
if (Value.getStatus().isValid()) { // get Status of point, check valid
if (first != 0) { // 1st valid point, use it for everything
min = Value.getValue();
max = Value.getValue();
avg = Value.getValue();
num = 1; // Init number points found
first = 0;
} else { // 2nd+ valid point, check min/max, and avg it
if (Value.getValue() <= min) min = Value.getValue();
if (Value.getValue() >= max) max = Value.getValue();
avg += Value.getValue();
num++;
}
}
}
getNum().setValue(num); // set number points found
if (num > 0) { // only use avg/min/max if something valid found
if (num != 0) getAvg().setValue(avg/num);
getMin().setValue(min);
getMax().setValue(max);
getNumber().setValue(num);
} else { // No Valid Return, Return Default
getAvg().setValue(getDefault().getValue());
getMin().setValue(getDefault().getValue());
getMax().setValue(getDefault().getValue());
}
}
public void onStop() throws Exception
{
if (ticket != null) ticket.cancel();
}
void updateTimer()
{
if (ticket != null) ticket.cancel();
ticket = Clock.schedule(getProgram(), getExecutePeriod(), BProgram.execute, null);
}
Clock.Ticket ticket;
long lastOnExecuteTicks;