Beispiel #1
0
  @Override
  public void run() {
    while (running) {
      try {
        Socket socket = connection.getSocket();
        if (!socket.isClosed()) {
          BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          String line = in.readLine();
          while (running && line != null) {
            if (!StringUtils.isEmpty(line)) {
              if (line.startsWith("{\"config\":{")) {
                connection.setConfig(getInputMapper().readValue(line, Config.class));
                configAction(ConfigModifyAction.ConfigReceived, null);
              } else if (line.equals("1")) {
                // pilight stopping
                connection.getSocket().close();
                throw new IOException("Connection to pilight lost");
              } else {
                Status status = getInputMapper().readValue(line, Status.class);
                callback.messageReceived(connection, status);
              }
            }
            line = in.readLine();
          }
        }
      } catch (IOException e) {
        logger.error("Error in pilight listener thread", e);
      }

      // empty line received (socket closed) or pilight stopped, try to reconnect
      reconnect();
    }

    cleanup();
  }
Beispiel #2
0
 private void reconnect() {
   try {
     Thread.sleep(RECONNECT_DELAY);
   } catch (InterruptedException e) {
     // noop
   }
   logger.debug("pilight reconnecting");
   if (connection.connect(getInputMapper(), getOutputMapper())) {
     logger.info(
         "Established connection to pilight server at {}:{}",
         connection.getHostname(),
         connection.getPort());
   }
 }
Beispiel #3
0
 private void cleanup() {
   try {
     connection.getSocket().close();
   } catch (IOException e) {
     logger.error("Error while closing pilight socket", e);
   }
   logger.info("Thread pilight listener stopped");
 }
Beispiel #4
0
 private void internalRefreshConfig()
     throws JsonGenerationException, JsonMappingException, IOException {
   if (!updatingConfig) {
     updatingConfig = true;
     logger.info("Updating pilight config");
     Socket socket = connection.getSocket();
     getOutputMapper()
         .writeValue(socket.getOutputStream(), new Identification(Identification.REQUEST_CONFIG));
   }
 }
Beispiel #5
0
 /**
  * Determine if this listener is still connected
  *
  * @return true when connected
  */
 public boolean isConnected() {
   return !connection.getSocket().isClosed();
 }