Пример #1
0
  private static OrderedMap<Long, Double[]> _MergeDataSets(boolean F, TemperatureSet TSList[]) {

    /* merge temperature data sets */
    OrderedMap<Long, Double[]> rowMap = new OrderedMap<Long, Double[]>();
    if (!ListTools.isEmpty(TSList)) {
      for (int d = 0; d < TSList.length; d++) {
        TemperatureSet TS = TSList[d];
        if (TS != null) {
          Collection<Temperature> TList = TS.getTemperatures();
          for (Temperature T : TList) {
            Long ts = new Long(T.getTimestamp());
            Double tmp = new Double(T.getTemperature(F));
            Double row[] = rowMap.get(ts);
            if (row == null) {
              row = new Double[TSList.length];
              rowMap.put(ts, row);
            }
            row[d] = tmp;
          }
        }
      }
    }

    /* sort by timestamp */
    rowMap.sortKeys(new ListTools.NumberComparator<Long>());

    /* return */
    return rowMap;
  }
Пример #2
0
  private static String CreateGoogleDataTableJSON(boolean F, TemperatureSet... TSList) {

    /* merge temperature data sets */
    OrderedMap<Long, Double[]> rowMap = _MergeDataSets(F, TSList);

    /* create JSON "cols" Object */
    JSON._Array dataTable_cols = new JSON._Array();
    // -- DateTime
    JSON._Object col_dateTime = (new JSON._Object()).setFormatIndent(false);
    col_dateTime.addKeyValue("id", "date");
    col_dateTime.addKeyValue("label", "Date/Time");
    col_dateTime.addKeyValue("type", "datetime");
    dataTable_cols.addValue(col_dateTime);
    // -- data set titles
    if (!ListTools.isEmpty(TSList)) {
      for (int d = 0; d < TSList.length; d++) {
        TemperatureSet TS = TSList[d];
        JSON._Object col_temp = (new JSON._Object()).setFormatIndent(false);
        col_temp.addKeyValue("id", "temp" + (d + 1));
        col_temp.addKeyValue("label", "Temp-" + (d + 1));
        col_temp.addKeyValue("type", "number");
        dataTable_cols.addValue(col_temp);
      }
    }

    /* create JSON "rows" Object */
    JSON._Array dataTable_rows = new JSON._Array();
    for (Long ts : rowMap.keySet()) {
      JSON._Object col = new JSON._Object();
      // TODO
    }

    /* return */
    return null; // TODO:
  }
Пример #3
0
 /**
  * ** A String reperesentation of this URI (with arguments) ** @param includeBlankValues True to
  * include keys for blank values. ** @return A String representation of this URI
  */
 public String toString(boolean includeBlankValues) {
   StringBuffer sb = new StringBuffer(this.getURI());
   if (!ListTools.isEmpty(this.getKeyValList())) {
     sb.append("?");
     this.getArgString(sb, includeBlankValues);
   }
   return sb.toString();
 }
Пример #4
0
 /** ** Constructor */
 public TemperatureSet(String title, Collection<Temperature> TList) {
   this.title = StringTools.trim(title);
   if (!ListTools.isEmpty(TList)) {
     for (Temperature T : TList) {
       this.addTemperature(T);
     }
   }
 }
Пример #5
0
 /**
  * ** A String reperesentation of this URI (with arguments) ** @return A String representation of
  * this URI
  */
 public String toString() {
   StringBuffer sb = new StringBuffer(this.getURI());
   if (!ListTools.isEmpty(this.getKeyValList())) {
     sb.append("?");
     this.getArgString(sb);
   }
   return sb.toString();
 }
Пример #6
0
  /* private constructor */
  private TrackServer(int tcpPorts[], int udpPorts[], int commandPort) throws Throwable {
    int listeners = 0;

    // Start TCP listeners
    if (!ListTools.isEmpty(tcpPorts)) {
      for (int i = 0; i < tcpPorts.length; i++) {
        int port = tcpPorts[i];
        if (ServerSocketThread.isValidPort(port)) {
          try {
            this._startTCP(port);
            listeners++;
          } catch (java.net.BindException be) {
            Print.logError("TCP: Error binding to port: %d", port);
          }
        } else {
          throw new Exception("TCP: Invalid port number: " + port);
        }
      }
    }

    // Start UDP listeners
    if (!ListTools.isEmpty(udpPorts)) {
      for (int i = 0; i < udpPorts.length; i++) {
        int port = udpPorts[i];
        if (ServerSocketThread.isValidPort(port)) {
          try {
            ServerSocketThread sst = this._startUDP(port);
            if (this.udpSocket == null) {
              this.udpSocket = sst.getDatagramSocket();
            }
            listeners++;
          } catch (java.net.BindException be) {
            Print.logError("UDP: Error binding to port: %d", port);
          }
        } else {
          throw new Exception("UDP: Invalid port number: " + port);
        }
      }
    }

    /* do we have any active listeners? */
    if (listeners <= 0) {
      Print.logWarn("No active device communication listeners!");
    }
  }
Пример #7
0
 public static String GetPropertyString_OBDII(long dtcFault[]) {
   if (ListTools.isEmpty(dtcFault)) {
     return GetPropertyString_OBDII("");
   } else {
     java.util.List<String> dtc = new Vector<String>();
     for (int i = 0; i < dtcFault.length; i++) {
       if (dtcFault[i] != 0L) {
         dtc.add(GetFaultString(dtcFault[i]));
       }
     }
     return GetPropertyString_OBDII(dtc);
   }
 }
Пример #8
0
 /**
  * ** Returns true if this key fully defines all key fields ** @return True if this key fully
  * defines all key fields
  */
 public boolean isFullKey() {
   DBField kfld[] = this.getKeyFields();
   if (ListTools.isEmpty(kfld)) {
     return false;
   } else {
     DBFieldValues fldVals = this.getFieldValues(); // hasPartialKey
     for (int i = 0; i < kfld.length; i++) {
       String fldName = kfld[i].getName();
       if (!fldVals.hasFieldValue(fldName)) {
         return false;
       }
     }
     return true;
   }
 }
Пример #9
0
  /**
   * ** Return the 'WHERE' clause for this key [CHECK] ** @param altIndexName The alternate index
   * name. If null or blank, uses ** primary keys instead ** @param whereKeyType The where key type.
   * One of the constants from DBWhere ** @return The 'WHERE' clause for this key
   */
  protected String _getWhereClause(
      String altIndexName, int whereKeyType) // boolean fullKeyRequired)
      throws DBException {

    /* key fields */
    boolean usePrimaryKey = StringTools.isBlank(altIndexName);
    DBField keyFlds[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName);
    if (ListTools.isEmpty(keyFlds)) {
      throw new DBException("No keys found!");
    }

    /* WHERE */
    DBWhere dwh = new DBWhere(this.getFactory());
    DBFieldValues fldVals = this.getFieldValues();
    int keyCnt = 0;
    boolean hasPartialKey = false;
    for (int i = 0; i < keyFlds.length; i++) {
      String fldName = keyFlds[i].getName();
      if (fldVals.hasFieldValue(fldName)) {
        if (!hasPartialKey || (whereKeyType == DBWhere.KEY_PARTIAL_ALL)) {
          String fev = dwh.EQ(fldName, fldVals.getFieldValueAsString(fldName));
          if (keyCnt > 0) {
            dwh.append(dwh.AND_(fev));
          } else {
            dwh.append(fev);
          }
          keyCnt++;
        } else {
          // whereKeyType == DBWhere.KEY_PARTIAL_FIRST, and we found a subsequent key
          String m =
              "Additional partial key in 'WHERE' clause! ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          // throw new DBException(m); // TODO:
          Print.logWarn("******************************************************************");
          Print.logWarn(m);
          // Print.logWarn(StringTools.join(keyFlds,","));
          // Print.logStackTrace(m);
          Print.logWarn("******************************************************************");
        }
      } else if ((i == 0) && (whereKeyType != DBWhere.KEY_PARTIAL_ALL_EMPTY)) { //
        // missing first key
        if (keyFlds[i].isAutoIncrement()) {
          // first key is an "auto_increment" and it is not present
          // assume that we are expecting the DB server to create this value for us, thus the key
          // dow not exist
          // However, there is nothing we can do about this here.
          String m =
              "First key field for 'WHERE' clause is 'auto_increment' and field is not present ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          throw new DBException(m);
        } else {
          String m =
              "Missing first key field for 'WHERE' clause! ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          throw new DBException(m);
        }
      } else if (whereKeyType == DBWhere.KEY_FULL) {
        // missing a key when all keys are required
        String m = "Missing key for 'WHERE' clause! [" + this.getTableName() + "." + fldName + "]";
        throw new DBException(m);
      } else {
        // only a portion of the key has been specified.
        // This is a common occurance deleting an Account/Device with sub-dependencies
        // Print.logWarn("Key field not specified: " + this.getTableName() + "." + fldName);
        hasPartialKey = true;
      }
    }

    return (keyCnt > 1) ? dwh.WHERE(dwh.toString()) : dwh.WHERE_(dwh.toString());
  }
Пример #10
0
  /**
   * ** Returns true if the specified key attribute exists in the table ** @param altIndexName The
   * alternate index name, or null to use the primary index ** @param whereKeyType The partial key
   * match type ** @return True if the specified key attribute exists in the table, false otherwise
   */
  protected boolean _exists(String altIndexName, int whereKeyType)
      throws SQLException, DBException {

    /* key fields */
    boolean usePrimaryKey = StringTools.isBlank(altIndexName);
    DBField kfld[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName);
    if (ListTools.isEmpty(kfld)) {
      throw new DBException("No keys found!");
    }

    /* check last key for "auto_increment" */
    if (whereKeyType == DBWhere.KEY_FULL) {
      DBField lastField = kfld[kfld.length - 1];
      if (lastField.isAutoIncrement()
          && !this.getFieldValues().hasFieldValue(lastField.getName())) {
        // full key requested and last key is auto_increment, which is missing
        return false;
      }
    }

    // DBSelect: SELECT <Keys> FROM <TableName> <KeyWhere>
    String firstKey = kfld[0].getName();
    DBSelect<gDBR> dsel = new DBSelect<gDBR>(this.getFactory());
    dsel.setSelectedFields(firstKey);
    dsel.setWhere(this._getWhereClause(altIndexName, whereKeyType));

    /* get keyed record */
    DBConnection dbc = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean exists = false;
    try {
      dbc = DBConnection.getDefaultConnection();
      stmt = dbc.execute(dsel.toString()); // may throw DBException
      rs = stmt.getResultSet();
      exists = rs.next();
    } catch (SQLException sqe) {
      if (sqe.getErrorCode() == DBFactory.SQLERR_TABLE_NOTLOCKED) {
        // MySQL: This case has been seen on rare occasions.  Not sure what causes it.
        Print.logError("SQL Lock Error: " + sqe);
        Print.logError("Hackery! Forcing lock on table: " + this.getTableName());
        if (DBProvider.lockTableForRead(this.getTableName(), true)) { // may throw DBException
          stmt = dbc.execute(dsel.toString()); // may throw SQLException, DBException
          rs = stmt.getResultSet(); // SQLException
          exists = rs.next(); // SQLException
          DBProvider.unlockTables(); // DBException
        }
      } else {
        throw sqe;
      }
    } finally {
      if (rs != null) {
        try {
          rs.close();
        } catch (Throwable t) {
        }
      }
      if (stmt != null) {
        try {
          stmt.close();
        } catch (Throwable t) {
        }
      }
      DBConnection.release(dbc);
    }

    return exists;
  }
Пример #11
0
 /**
  * ** Returns true if any tagged field names have been defined ** @return True if any tagged field
  * names have been defined
  */
 public boolean hasTaggedFields() {
   return !ListTools.isEmpty(this.taggedFields);
 }
Пример #12
0
 public static String GetPropertyString_OBDII(java.util.List<String> dtc) {
   String dtcStr = !ListTools.isEmpty(dtc) ? StringTools.join(dtc, ",") : "";
   return GetPropertyString_OBDII(dtcStr);
 }
Пример #13
0
 public static String GetPropertyString_OBDII(String dtc[]) {
   String dtcStr = !ListTools.isEmpty(dtc) ? StringTools.join(dtc, ",") : "";
   return GetPropertyString_OBDII(dtcStr);
 }