Prog Obj data base retrieval example

Would anyone have an example for a data base trend log look up with a ProgramObject?

IE., could there be a slot defined on the Prog Object for trend log history of yesterdays data selector of an existing trended point and then a bool output slot created for “fault”?

IE., really really simplified logic and ability to drop down select an AHU mix temp, return temp, and outside air temp trend point and fault logic that the mixing temp needs to be in between return and outside air temp?

I have some more advanced fault logic but REAL curious if that could be accomplished at all?

Not really sure what you’re asking. You can write objects to references the min and max values of a history, and can look for times in which you want the history.

What do you mean for fault logic?

/* Auto-generated ProgramImpl Code */

import java.util.*;              /* java Predefined*/
import javax.baja.nre.util.*;    /* nre Predefined*/
import javax.baja.sys.*;         /* baja Predefined*/
import javax.baja.status.*;      /* baja Predefined*/
import javax.baja.util.*;        /* baja Predefined*/
import com.tridium.program.*;    /* program-rt Predefined*/
import javax.baja.history.*;     /* history-rt User Defined*/
import javax.baja.collection.*;  /* baja User Defined*/
import javax.baja.naming.*;      /* baja By Property*/

public class ProgramImpl
  extends com.tridium.program.ProgramBase
{

////////////////////////////////////////////////////////////////
// Getters
////////////////////////////////////////////////////////////////

  public BOrd getHistoryOrd() { return (BOrd)get("historyOrd"); }
  public BStatusEnum getTimeRange() { return (BStatusEnum)get("timeRange"); }
  public double getMinValue() { return getDouble("minValue"); }
  public double getMaxValue() { return getDouble("maxValue"); }

////////////////////////////////////////////////////////////////
// Setters
////////////////////////////////////////////////////////////////

  public void setHistoryOrd(javax.baja.naming.BOrd v) { set("historyOrd", v); }
  public void setTimeRange(javax.baja.status.BStatusEnum v) { set("timeRange", v); }
  public void setMinValue(double v) { setDouble("minValue", v); }
  public void setMaxValue(double v) { setDouble("maxValue", v); }

////////////////////////////////////////////////////////////////
// Program Source
////////////////////////////////////////////////////////////////

  public void progress(String msg) {
      System.out.println("[DEBUG] " + msg);
  }
  
  public void onExecute() throws Exception {
      progress("Starting execution...");
  
      BOrd baseOrd = getHistoryOrd();
      if (baseOrd == null) {
          progress("History ORD is not set.");
          return;
      }
  
      String range = getTimeRange().getValue().getTag().toLowerCase();
      progress("Using time range: " + range);
  
      String bqlOrdString = baseOrd.toString() +
          "?period=" + range + "|bql:select min(value), max(value)";
      progress("Constructed BQL ORD: " + bqlOrdString);
  
      BOrd bqlOrd = BOrd.make(bqlOrdString);
      BITable table;
  
      try {
          table = (BITable) bqlOrd.resolve().get();
          progress("BQL resolved successfully.");
      } catch (Exception e) {
          progress("Failed to resolve BQL ORD: " + e.getMessage());
          return;
      }
  
      ColumnList columns = table.getColumns();
      Column minCol = columns.get(0);  // min(value)
      Column maxCol = columns.get(1);  // max(value)
  
      TableCursor cursor = table.cursor();
      if (cursor.next()) {
          double min = ((BNumber) cursor.cell(minCol)).getDouble();
          double max = ((BNumber) cursor.cell(maxCol)).getDouble();
  
          progress("Min: " + min + ", Max: " + max);
  
          setMinValue(min);
          setMaxValue(max);
      } else {
          progress("No data returned from BQL query.");
      }
  }
}

Here’s an example to find the min and max histories of a time range

awesome kid in a candy store I can run with this thanks @Charles_Johnson !!!

1 Like

Hey Charles can you read in a CSV file from file location into ProgObj as well? IE., ord location picker or something from file system slot? If you had an example or something close of that it would be greatly appreciated as well

/* Auto-generated ProgramImpl Code */

import java.util.*;              /* java Predefined*/
import javax.baja.nre.util.*;    /* nre Predefined*/
import javax.baja.sys.*;         /* baja Predefined*/
import javax.baja.status.*;      /* baja Predefined*/
import javax.baja.util.*;        /* baja Predefined*/
import com.tridium.program.*;    /* program-rt Predefined*/
import javax.baja.naming.*;      /* baja User Defined*/
import javax.baja.file.*;        /* baja User Defined*/
import java.io.*;                /* java User Defined*/
import com.tridium.nd.*;         /* niagaraDriver-rt User Defined*/
import com.tridium.fox.sys.*;    /* fox-rt User Defined*/

public class ProgramImpl
  extends com.tridium.program.ProgramBase
{

////////////////////////////////////////////////////////////////
// Getters
////////////////////////////////////////////////////////////////

  public BOrd getFileOrd() { return (BOrd)get("fileOrd"); }
  public boolean getStationType() { return getBoolean("stationType"); }
  public int getFoxPort() { return getInt("foxPort"); }
  public boolean getUseFoxS() { return getBoolean("useFoxS"); }

////////////////////////////////////////////////////////////////
// Setters
////////////////////////////////////////////////////////////////

  public void setFileOrd(javax.baja.naming.BOrd v) { set("fileOrd", v); }
  public void setStationType(boolean v) { setBoolean("stationType", v); }
  public void setFoxPort(int v) { setInt("foxPort", v); }
  public void setUseFoxS(boolean v) { setBoolean("useFoxS", v); }

////////////////////////////////////////////////////////////////
// Program Source
////////////////////////////////////////////////////////////////

  public void onExecute() throws Exception
  {
      // Resolve the BOrd reference to a file, cast it to BIFile, and open an input stream reader for its contents.
      // Assumes this is a CSV file that contains station information (name, address).
      BIFile file = (BIFile) getFileOrd().getOrd().resolve().get();
      InputStreamReader reader = new InputStreamReader(file.getInputStream());
  
      // Read all lines from the CSV file into a string array.
      String[] rows = FileUtil.readLines(reader);
  
      // Resolve the NiagaraNetwork driver component from the station's component space.
      // This is the parent container for all Niagara Edge Lite stations to be added.
      BNiagaraNetwork niagaraNetwork = (BNiagaraNetwork) BOrd.make("station:|slot:/Drivers/NiagaraNetwork").resolve().get();
  
      // Loop through each row in the CSV (skipping the first row, assuming it's a header).
      for (int i = 1; i < rows.length; i++) 
      {
          // Split each line by comma, trimming whitespace. Expected format: Name,IPAddress
          String[] properties = TextUtil.splitAndTrim(rows[i], ',');
          String name = properties[0];     // Station name (used as slot name in NiagaraNetwork)
          String address = properties[1];  // IP address of the station
  
          // Construct BOrd references for where the station will reside and how it will connect.
          BOrd stationOrd = BOrd.make("station:|slot:/Drivers/NiagaraNetwork/" + name);
          BOrd stationAddress = BOrd.make("ip:" + address);
          
          if (getStationType())
          {
            // Add a new instance of BNiagaraStation to the NiagaraNetwork using the station name as the slot.
            niagaraNetwork.add(name, new BNiagaraStation());
  
            // Resolve the newly added station component by its BOrd.
            BNiagaraStation station = (BNiagaraStation) stationOrd.resolve().get();
  
            // Set the IP address of the remote station so it knows how to communicate.
            station.setAddress(stationAddress);
  
            // Retrieve the Fox client connection for this station.
            // This connection manages communication over the Niagara Fox protocol.
            BFoxClientConnection foxConnection = (BFoxClientConnection) station.getClientConnection();
  
            // Set the Fox TCP port (usually 1911 or custom port) and whether secure FoxS should be used.
            foxConnection.setPort(getFoxPort());       // Typically pulled from user config or slot property
            foxConnection.setUseFoxs(getUseFoxS());    // Boolean toggle for secure FoxS
            
          } 
          else 
          { 
            // Add a new instance of BNiagaraEdgeLiteStation to the NiagaraNetwork using the station name as the slot.
            niagaraNetwork.add(name, new BNiagaraEdgeLiteStation());
  
            // Resolve the newly added station component by its BOrd.
            BNiagaraEdgeLiteStation station = (BNiagaraEdgeLiteStation) stationOrd.resolve().get();
  
            // Set the IP address of the remote station so it knows how to communicate.
            station.setAddress(stationAddress);
  
            // Retrieve the Fox client connection for this station.
            // This connection manages communication over the Niagara Fox protocol.
            BFoxClientConnection foxConnection = (BFoxClientConnection) station.getClientConnection();
  
            // Set the Fox TCP port (usually 1911 or custom port) and whether secure FoxS should be used.
            foxConnection.setPort(getFoxPort());       // Typically pulled from user config or slot property
            foxConnection.setUseFoxs(getUseFoxS());    // Boolean toggle for secure FoxS
          } 
      }
  }
}