예제 #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();
  }
예제 #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());
   }
 }
예제 #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");
 }
예제 #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));
   }
 }
예제 #5
0
 /**
  * Determine if this listener is still connected
  *
  * @return true when connected
  */
 public boolean isConnected() {
   return !connection.getSocket().isClosed();
 }