Exemplo n.º 1
0
    /** Main processing loop */
    @Override
    public void run() {
      running = true;
      logger.debug("OmniConnectionThread running");
      while (running) {
        connected = false;

        /*
         * Connect to the system
         */

        logger.debug("OmniConnectionThread trying to connect");

        try {
          c = new Connection(host, port, key);
          connected = true;
          logger.debug("OmniConnectionThread connected");
        } catch (Exception e) {
          logger.error("Could not connect", e);
        }

        /*
         * If we fail to connect sleep a bit before trying again
         */
        if (!connected) {
          try {
            Thread.currentThread();
            Thread.sleep(10 * 1000);
          } catch (InterruptedException ignored) {
          }

        } else {

          /*
           * If we get disconnected then do nothing
           */
          c.addDisconnectListener(
              new DisconnectListener() {
                @Override
                public void notConnectedEvent(Exception e) {
                  logger.error("OmniConnectionThread was disconnected, will try again", e);
                  connected = false;
                }
              });

          /*
           * Real time device changes get processed here
           */
          c.addNotificationListener(listener);

          /*
           * Load everything and main audio source text loop
           */
          try {
            SystemStatus sysstatus = c.reqSystemStatus();
            logger.info("System: " + sysstatus.toString());

            omni = c.reqSystemInformation().getModel() < 36;

            /*
             * We need to explicitly tell the controller to send us
             * real time notifications
             */
            c.enableNotifications();

            if (generateItems) {
              OmnilinkItemGenerator gen = new OmnilinkItemGenerator(c);
              logger.info(gen.generateItemsAndGroups());
            }

            // update known items with state
            populateRefreshMapFromAllProviders();

            // load audio sources, we won't get updates on these
            readAllAudioSources();

            /*
             * if we get disconnected then refresh any devices that
             * we have to keep them up to date.
             */

            while (running && c.connected()) {
              updateRefreshItems();
              /*
               * Audio source text is not pushed in real time, so
               * we poll for it
               */
              updateAudioSourceTexts();
              try {
                synchronized (audioUpdateLock) {
                  audioUpdateLock.wait(5000);
                }
              } catch (InterruptedException ignored) {
              }
            }
          } catch (IOException ex) {
            logger.error("Could not connect to system", ex);
          } catch (OmniNotConnectedException ex) {
            logger.error("Could not connect to system", ex.getNotConnectedReason());
          } catch (OmniInvalidResponseException ex) {
            logger.error("Could not connect to system", ex);
          } catch (OmniUnknownMessageTypeException ex) {
            logger.error("Could not connect to system", ex);
            // is this needed? I just added this without looking at
            // the code for 2 years
          } catch (Exception ex) {
            logger.error("Could not connect to system", ex);
          } finally {
            c.disconnect();
            c = null;
          }
        }
      }
    }