public void mute(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   if (player.getVolume() > 0) {
     setVolume(playerId, 0);
   }
 }
 public void unMute(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   if (player.getVolume() == 0) {
     setVolume(playerId, player.getUnmuteVolume());
   }
 }
 private void handleStatusMessage(SqueezePlayer player, String[] messageParts) {
   for (String messagePart : messageParts) {
     // Parameter Power
     if (messagePart.startsWith("power%3A")) {
       String value = messagePart.substring("power%3A".length());
       player.setPowered(value.matches("1"));
     }
     // Parameter Volume
     else if (messagePart.startsWith("mixer%20volume%3A")) {
       String value = messagePart.substring("mixer%20volume%3A".length());
       player.setVolume(Integer.parseInt(value));
     }
     // Parameter Mode
     else if (messagePart.startsWith("mode%3A")) {
       String value = messagePart.substring("mode%3A".length());
       player.setMode(Mode.valueOf(value));
     }
     // Parameter Title
     else if (messagePart.startsWith("title%3A")) {
       String value = messagePart.substring("title%3A".length());
       player.setTitle(decode(value));
     }
     // Parameter Remote Title (radio)
     else if (messagePart.startsWith("remote_title%3A")) {
       String value = messagePart.substring("remote_title%3A".length());
       player.setRemoteTitle(decode(value));
     }
     // Parameter Artist
     else if (messagePart.startsWith("artist%3A")) {
       String value = messagePart.substring("artist%3A".length());
       player.setArtist(decode(value));
     }
     // Parameter Album
     else if (messagePart.startsWith("album%3A")) {
       String value = messagePart.substring("album%3A".length());
       player.setAlbum(decode(value));
     }
     // Parameter Genre
     else if (messagePart.startsWith("genre%3A")) {
       String value = messagePart.substring("genre%3A".length());
       player.setGenre(decode(value));
     }
     // Parameter Year
     else if (messagePart.startsWith("year%3A")) {
       String value = messagePart.substring("year%3A".length());
       player.setYear(Integer.parseInt(value));
     }
     // Parameter Artwork
     else if (messagePart.startsWith("artwork_track_id%3A")) {
       String value = messagePart.substring("artwork_track_id%3A".length());
       // NOTE: what is returned if not an artwork id? i.e. if a space?
       if (!value.startsWith(" ")) {
         value = "http://" + host + ":" + webPort + "/music/" + value + "/cover.jpg";
       }
       player.setCoverArt(decode(value));
     }
   }
 }
 public void showStringHuge(String playerId, String line, int duration) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(
       player.getMacAddress()
           + " show line1:"
           + line
           + " font:huge duration:"
           + String.valueOf(duration));
 }
 public void setVolume(String playerId, int volume) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   if (0 > volume) {
     volume = 0;
   } else if (volume > 100) {
     volume = 100;
   }
   sendCommand(player.getMacAddress() + " mixer volume " + String.valueOf(volume));
 }
    private void handlePlaylistMessage(SqueezePlayer player, String[] messageParts) {
      String action = messageParts[2];

      if (action.equals("newsong")) {
        player.setMode(Mode.play);
      } else if (action.equals("pause")) {
        player.setMode(messageParts[3].equals("0") ? Mode.play : Mode.pause);
      } else if (action.equals("stop")) {
        player.setMode(Mode.stop);
      }
    }
 public void showStrings(String playerId, String line1, String line2, int duration) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(
       player.getMacAddress()
           + " show line1:"
           + line1
           + " line2:"
           + line2
           + " duration:"
           + String.valueOf(duration));
 }
    private void handlePrefsetMessage(SqueezePlayer player, String[] messageParts) {
      if (messageParts.length < 5) return;

      // server prefsets
      if (messageParts[2].equals("server")) {
        String function = messageParts[3];
        String value = messageParts[4];

        if (function.equals("power")) {
          player.setPowered(value.equals("1"));
        } else if (function.equals("volume")) {
          player.setVolume(Integer.parseInt(value));
        }
      }
    }
    private void handlePlayerUpdate(String message) {
      String[] messageParts = message.split("\\s");
      if (messageParts.length < 2) {
        logger.warn("Invalid message - expecting at least 2 parts. Ignoring.");
        return;
      }

      // get the MAC address
      SqueezePlayer player = playersByMacAddress.get(decode(messageParts[0]));
      if (player == null) {
        logger.warn(
            "Status message received for MAC address {} which is not configured in openHAB. Ignoring.",
            messageParts[0]);
        return;
      }

      // get the message type
      String messageType = messageParts[1];

      if (messageType.equals("status")) {
        handleStatusMessage(player, messageParts);
      } else if (messageType.equals("playlist")) {
        handlePlaylistMessage(player, messageParts);
      } else if (messageType.equals("prefset")) {
        handlePrefsetMessage(player, messageParts);
      } else if (messageType.equals("ir")) {
        player.setIrCode(messageParts[2]);
      } else if (messageType.equals("power")) {
        // ignore these for now
        // player.setPowered(messageParts[1].equals("1"));
      } else if (messageType.equals("play")
          || messageType.equals("pause")
          || messageType.equals("stop")) {
        // ignore these for now
        // player.setMode(Mode.valueOf(messageType));
      } else if (messageType.equals("mixer")
          || messageType.equals("menustatus")
          || messageType.equals("button")) {
        // ignore these for now
      } else {
        logger.debug("Unhandled message type '{}'. Ignoring.", messageType);
      }
    }
    private void handlePlayersList(String message) {
      String[] playersList = decode(message).split("playerindex:\\d+\\s");
      for (String playerParams : playersList) {
        String[] parameterList = playerParams.split("\\s");

        // parse out the MAC address first
        String macAddress = null;
        for (String parameter : parameterList) {
          if (parameter.contains("playerid")) {
            macAddress = parameter.substring(parameter.indexOf(":") + 1);
            break;
          }
        }

        // if none found then ignore this set of params
        if (macAddress == null) continue;

        // see if this player exists in our config
        SqueezePlayer player = playersByMacAddress.get(macAddress);
        if (player == null) continue;

        // populate the player state
        for (String parameter : parameterList) {
          if (parameter.contains("ip")) {
            player.setIpAddr(parameter.substring(parameter.indexOf(":") + 1));
          } else if (parameter.contains("uuid")) {
            player.setUuid(parameter.substring(parameter.indexOf(":") + 1));
          } else if (parameter.contains("name")) {
            player.setName(parameter.substring(parameter.indexOf(":") + 1));
          } else if (parameter.contains("model")) {
            player.setModel(parameter.substring(parameter.indexOf(":") + 1));
          }
        }

        // tell the server we want to subscribe to player updates
        sendCommand(player.getMacAddress() + " status - 1 subscribe:10 tags:yagJlN");
      }
    }
 public void powerOff(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " power 0");
 }
 public void playUrl(String playerId, String url) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " playlist play " + url);
 }
 public void unPause(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " pause 0");
 }
 public void syncPlayer(String playerId1, String playerId2) {
   SqueezePlayer player1 = getPlayer(playerId1);
   SqueezePlayer player2 = getPlayer(playerId2);
   if (player1 == null || player2 == null) return;
   sendCommand(player1.getMacAddress() + " sync " + player2.getMacAddress());
 }
 public void volumeDown(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   setVolume(playerId, player.getVolume() - VOLUME_CHANGE_SIZE);
 }
 public void clearPlaylist(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " playlist clear");
 }
 public void next(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " playlist index +1");
 }
 public void stop(String playerId) {
   SqueezePlayer player = getPlayer(playerId);
   if (player == null) return;
   sendCommand(player.getMacAddress() + " stop");
 }