@Override
 public void run() {
   try {
     System.out.println("Emulator started...");
     System.out.println();
     System.out.println();
     System.out.println("Type values of buttons and potentiometer. (Space separated values)");
     System.out.println("  Buttons: 0 or 1");
     System.out.println("  Potentiometer: 0 - 1023");
     System.out.println(
         "  Example: 0 1 0 200  -->  only Button 2 pushed and the value of the Potentiometer is 200");
     System.out.println("Button 1\tButton 2\tButton 3\tPotentiometer");
     System.out.println("  OFF\t\t  OFF\t\t  OFF\t\t       0");
     BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
     while (isRunning) {
       String input = consoleReader.readLine();
       SensorValues values = parseInputLine(input);
       values.print();
       queue.put(values.createJSONString());
     }
   } catch (InterruptedException e) {
     LOGGER.error("Error occurred.", e);
   } catch (IOException e) {
     LOGGER.error("Error occurred.", e);
   }
 }
Esempio n. 2
0
 /**
  * This is the constructor of the callback class. It got an IoTSystem from the MIDL created model
  * and use it to initialize the IncQuery Base Indexer.
  *
  * @param system
  */
 public Callback(IoTSystem system) {
   this.system = system;
   try {
     navigationHelper =
         IncQueryBaseFactory.getInstance().createNavigationHelper(this.system, true, null);
   } catch (IncQueryBaseException e) {
     LOGGER.error(e.getMessage());
   }
 }
Esempio n. 3
0
  @Override
  public void messageArrived(String topic, MqttMessage message) {
    try {

      Stopwatch sw = Stopwatch.createStarted();

      String msg = new String(message.getPayload());
      JsonParser parser = new JsonParser();
      JsonObject object = parser.parse(msg).getAsJsonObject();

      // XXX: workaround for demo!!!!
      String sensorId = topic;
      String[] segments = topic.split("/");
      if (segments.length > 2) {
        sensorId = segments[1] + segments[2];
      }

      long elapsed = sw.elapsed(TimeUnit.MILLISECONDS);
      if (elapsed > 5) {
        System.out.println("Phase parse: " + elapsed);
      }
      sw.reset();

      // Use IncQuery Base Indexer to find the sensor
      Sensor selectedSensor =
          (Sensor) navigationHelper.findByAttributeValue(sensorId).iterator().next().getEObject();

      elapsed = sw.elapsed(TimeUnit.MILLISECONDS);
      if (elapsed > 10) {
        System.out.println("Phase indexer: " + elapsed);
      }

      Payload sensorPayload = selectedSensor.getLastReceivedPayload();

      NavigationHelper paramNavHelper =
          IncQueryBaseFactory.getInstance().createNavigationHelper(sensorPayload, true, null);
      // Get parameters from model using the JSON file content
      for (Entry<String, JsonElement> parameter : object.entrySet()) {
        // Use IncQuery Base Indexer to find parameter
        Set<Setting> settings = paramNavHelper.findByAttributeValue(parameter.getKey());
        if (settings.isEmpty()) continue;

        DataParameter paramValue = (DataParameter) settings.iterator().next().getEObject();
        // Get the parameter new value from the message
        JsonElement newValue = parameter.getValue();
        // Find parameter type, and set the new value
        if (paramValue.getType().equals("int")) {
          ((IntParameter) paramValue).setValue(newValue.getAsInt());
        } else if (paramValue.getType().equals("double")) {
          ((DoubleParameter) paramValue).setValue(newValue.getAsDouble());
        } else if (paramValue.getType().equals("string")) {
          ((StringParameter) paramValue).setValue(newValue.getAsString());
        } else if (paramValue.getType().equals("boolean")) {
          ((BooleanParameter) paramValue).setValue(newValue.getAsBoolean());
        } else if (paramValue.getType().equals("long")) {
          ((LongParameter) paramValue).setValue(newValue.getAsLong());
        }
      }

      elapsed = sw.elapsed(TimeUnit.MILLISECONDS);
      if (elapsed > 10) {
        System.out.println("Phase update: " + elapsed);
      }

    } catch (Exception e) {
      LOGGER.error(e.getMessage());
    }
  }
Esempio n. 4
0
 @Override
 public void deliveryComplete(IMqttDeliveryToken token) {
   LOGGER.info("The delivery completed.");
 }
Esempio n. 5
0
 @Override
 public void connectionLost(Throwable t) {
   LOGGER.error("The connection lost.", t);
 }