コード例 #1
0
  // Lee la configuracion que se encuentra en conf/RegistryConf
  private void readConfXml() {
    try {
      File inputFile = new File("src/java/Conf/RegistryConf.xml");
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(inputFile);
      doc.getDocumentElement().normalize();
      NodeList nList = doc.getElementsByTagName("RegistryConf");
      for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          RegistryConf reg = new RegistryConf();
          String aux;
          int idSensor;
          int value;
          aux = eElement.getElementsByTagName("idSensor").item(0).getTextContent();
          idSensor = Integer.parseInt(aux);
          reg.setIdSensor(idSensor);

          aux = eElement.getElementsByTagName("saveType").item(0).getTextContent();
          reg.setSaveTypeString(aux);

          aux = eElement.getElementsByTagName("value").item(0).getTextContent();
          value = Integer.parseInt(aux);
          reg.setValue(value);

          registryConf.add(reg);
          lastRead.put(idSensor, 0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
  // El string debe estar em formato json
  public boolean updateRegistry(String dataJson) {
    try {
      // Crea objeto json con string por parametro
      JSONObject json = new JSONObject(dataJson);
      // Obtiene el array de Registos
      JSONArray arr = json.getJSONArray("Registry");
      // Recorre el array
      for (int i = 0; i < arr.length(); i++) {
        // Obtiene los datos idSensor y value
        int idSensor = arr.getJSONObject(i).getInt("idSensor");

        int value = arr.getJSONObject(i).getInt("value");
        // Recorre la configuracion de registro
        for (RegistryConf reg : registryConf) {

          // Se fija si el registro corresponde a esta configuracion
          if (reg.getIdSensor() == idSensor) {

            // Checkea el criterio para guardar, o no en la BD
            // Checkea tambien si el valor es igual al anterior
            if (reg.getSaveTypeString() == "ONCHANGE" && lastRead.get(idSensor) != value) {
              // Actualizo la ultima lectura y guardo en la BD
              lastRead.put(idSensor, value);
              saveRegistry(idSensor, value);

            } else if (reg.getSaveTypeString() == "ONTIME") {
              // Variables auxiliares, para checkear tiempo
              Long auxLong = System.currentTimeMillis() / 1000;
              int now = auxLong.intValue();
              int timeToSave = lastRead.get(idSensor) + reg.getValue();
              // Checkea si ya es tiempo para guerdar un nuevo registro
              if (now >= timeToSave) {
                // Actualizo el ultimo guardado
                lastRead.put(idSensor, now);
                saveRegistry(idSensor, value);
              }
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }