Coding Help - BQL

83 views
Skip to first unread message

Scott B

unread,
Sep 9, 2012, 10:37:52 PM9/9/12
to hackerspac...@googlegroups.com
Ok, I'm trying to play out of my depth here again...

The following is a bit of  example code that searches through one of our 'stations' (a control system for a building), builds a table out of all the data returned from a BQL, and then does some simple math on it.

I have managed to do a bit of tinkering to also output the number of points it finds etc, but what I would like it to be able to do is output the name of the points that are the Max & Min as a string.

To do this, I would assume I first need to expand the Table so that it not only holds the value but also the name of the point (I have no Idea how to do this), then from there it shouldn't be too hard, each time a Min Or Max value is found I should be able to just store its point name in a similar way to how it stores its value at the moment. Then I can just update the output once it reaches the end of the table.

so, anyone know anything about creating tables from BQL searches?
  

// 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;

Reply all
Reply to author
Forward
0 new messages