<commit_before>/**
 * Copyright (c) 2010-2014, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.squeezebox.internal;

import java.util.ArrayList;
import java.util.List;

import org.openhab.binding.squeezebox.SqueezeboxBindingConfig;
import org.openhab.binding.squeezebox.SqueezeboxBindingProvider;
import org.openhab.core.binding.AbstractBinding;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.types.Command;
import org.openhab.io.squeezeserver.SqueezePlayer;
import org.openhab.io.squeezeserver.SqueezePlayer.PlayerEvent;
import org.openhab.io.squeezeserver.SqueezePlayerEventListener;
import org.openhab.io.squeezeserver.SqueezeServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Binding which communicates with (one or many) Squeezeboxes. 
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 1.3.0
 */
public class SqueezeboxBinding extends AbstractBinding<SqueezeboxBindingProvider> implements SqueezePlayerEventListener {
	
	private static final Logger logger = LoggerFactory.getLogger(SqueezeboxBinding.class);

	private SqueezeServer squeezeServer;
	
	/**
	 * @{inheritDoc}
	 */
	@Override
	public void internalReceiveCommand(String itemName, Command command) {
		if (squeezeServer == null) {
			logger.warn("Squeeze Server not initialised or configured yet, ignoring command '{}' for item '{}'", command.toString(), itemName);
			return;
		}

		logger.trace("internalReceiveCommand(itemname = {}, command = {})", itemName, command.toString());
		for (SqueezeboxBindingProvider provider : providers) {
			SqueezeboxBindingConfig bindingConfig = provider.getSqueezeboxBindingConfig(itemName);

			String playerId = bindingConfig.getPlayerId();
			SqueezePlayer player = squeezeServer.getPlayer(playerId);
			if (player == null) {
				logger.warn("No Squeezebox player configured with id '{}'. Ignoring.", playerId);
				continue;
			}
		
			try {
				switch (bindingConfig.getCommandType()) {
					case POWER:
						if (command.equals(OnOffType.ON))
							squeezeServer.powerOn(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.powerOff(playerId);						
						break;
					
					case MUTE:
						if (command.equals(OnOffType.ON))
							squeezeServer.mute(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unMute(playerId);						
						break;
					
					case VOLUME: 
						if (command.equals(IncreaseDecreaseType.INCREASE))						
							squeezeServer.volumeUp(playerId);
						else if (command.equals(IncreaseDecreaseType.DECREASE))
							squeezeServer.volumeDown(playerId); 
						else if (command.equals(UpDownType.UP))						
							squeezeServer.volumeUp(playerId);
						else if (command.equals(UpDownType.DOWN))
							squeezeServer.volumeDown(playerId); 
						else if (command instanceof DecimalType)
							squeezeServer.setVolume(playerId, ((DecimalType)command).intValue()); 
						break;

					case PLAY:
						if (command.equals(OnOffType.ON))
							squeezeServer.play(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					case PAUSE:
						if (command.equals(OnOffType.ON))
							squeezeServer.pause(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unPause(playerId);
						break;
					case STOP:
						if (command.equals(OnOffType.ON))
							squeezeServer.stop(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.play(playerId);
						break;
					case NEXT:
						if (command.equals(OnOffType.ON))
							squeezeServer.next(playerId);
						break;						
					case PREV:
						if (command.equals(OnOffType.ON))
							squeezeServer.prev(playerId);
						break;
						
					case HTTP: 
						if (command.equals(OnOffType.ON))
							squeezeServer.playUrl(playerId, "http://" + bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					case FILE: 
						if (command.equals(OnOffType.ON))
							squeezeServer.playUrl(playerId, "file://" + bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					
					case SYNC: 
						if (command.equals(OnOffType.ON))
							squeezeServer.syncPlayer(playerId, bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unSyncPlayer(playerId);
						break;

					default:
						logger.warn("Unsupported command type '{}'", bindingConfig.getCommandType()); 
				}
			}
			catch (Exception e) {
				logger.warn("Error executing command type '" + bindingConfig.getCommandType() + "'", e);
			}	
		}
	}

	@Override
	public void powerChangeEvent(PlayerEvent event) {<commit_msg>Squeezebox Binding: Sync "remove" players incorrect<commit_after>/**
 * Copyright (c) 2010-2014, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.squeezebox.internal;

import java.util.ArrayList;
import java.util.List;

import org.openhab.binding.squeezebox.SqueezeboxBindingConfig;
import org.openhab.binding.squeezebox.SqueezeboxBindingProvider;
import org.openhab.core.binding.AbstractBinding;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.types.Command;
import org.openhab.io.squeezeserver.SqueezePlayer;
import org.openhab.io.squeezeserver.SqueezePlayer.PlayerEvent;
import org.openhab.io.squeezeserver.SqueezePlayerEventListener;
import org.openhab.io.squeezeserver.SqueezeServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Binding which communicates with (one or many) Squeezeboxes. 
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 1.3.0
 */
public class SqueezeboxBinding extends AbstractBinding<SqueezeboxBindingProvider> implements SqueezePlayerEventListener {
	
	private static final Logger logger = LoggerFactory.getLogger(SqueezeboxBinding.class);

	private SqueezeServer squeezeServer;
	
	/**
	 * @{inheritDoc}
	 */
	@Override
	public void internalReceiveCommand(String itemName, Command command) {
		if (squeezeServer == null) {
			logger.warn("Squeeze Server not initialised or configured yet, ignoring command '{}' for item '{}'", command.toString(), itemName);
			return;
		}

		logger.trace("internalReceiveCommand(itemname = {}, command = {})", itemName, command.toString());
		for (SqueezeboxBindingProvider provider : providers) {
			SqueezeboxBindingConfig bindingConfig = provider.getSqueezeboxBindingConfig(itemName);

			String playerId = bindingConfig.getPlayerId();
			SqueezePlayer player = squeezeServer.getPlayer(playerId);
			if (player == null) {
				logger.warn("No Squeezebox player configured with id '{}'. Ignoring.", playerId);
				continue;
			}
		
			try {
				switch (bindingConfig.getCommandType()) {
					case POWER:
						if (command.equals(OnOffType.ON))
							squeezeServer.powerOn(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.powerOff(playerId);						
						break;
					
					case MUTE:
						if (command.equals(OnOffType.ON))
							squeezeServer.mute(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unMute(playerId);						
						break;
					
					case VOLUME: 
						if (command.equals(IncreaseDecreaseType.INCREASE))						
							squeezeServer.volumeUp(playerId);
						else if (command.equals(IncreaseDecreaseType.DECREASE))
							squeezeServer.volumeDown(playerId); 
						else if (command.equals(UpDownType.UP))						
							squeezeServer.volumeUp(playerId);
						else if (command.equals(UpDownType.DOWN))
							squeezeServer.volumeDown(playerId); 
						else if (command instanceof DecimalType)
							squeezeServer.setVolume(playerId, ((DecimalType)command).intValue()); 
						break;

					case PLAY:
						if (command.equals(OnOffType.ON))
							squeezeServer.play(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					case PAUSE:
						if (command.equals(OnOffType.ON))
							squeezeServer.pause(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unPause(playerId);
						break;
					case STOP:
						if (command.equals(OnOffType.ON))
							squeezeServer.stop(playerId);
						else if (command.equals(OnOffType.OFF))
							squeezeServer.play(playerId);
						break;
					case NEXT:
						if (command.equals(OnOffType.ON))
							squeezeServer.next(playerId);
						break;						
					case PREV:
						if (command.equals(OnOffType.ON))
							squeezeServer.prev(playerId);
						break;
						
					case HTTP: 
						if (command.equals(OnOffType.ON))
							squeezeServer.playUrl(playerId, "http://" + bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					case FILE: 
						if (command.equals(OnOffType.ON))
							squeezeServer.playUrl(playerId, "file://" + bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.stop(playerId);
						break;
					
					case SYNC: 
						if (command.equals(OnOffType.ON))
							squeezeServer.syncPlayer(playerId, bindingConfig.getExtra()); 
						else if (command.equals(OnOffType.OFF))
							squeezeServer.unSyncPlayer(bindingConfig.getExtra());
						break;

					default:
						logger.warn("Unsupported command type '{}'", bindingConfig.getCommandType()); 
				}
			}
			catch (Exception e) {
				logger.warn("Error executing command type '" + bindingConfig.getCommandType() + "'", e);
			}	
		}
	}

	@Override
	public void powerChangeEvent(PlayerEvent event) {
{'fraction_non_alphanumeric': 0.1121867881548975, 'fraction_numerical': 0.0032270311313591497, 'mean_word_length': 3.5713665943600867, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5666084', 'n_tokens_mistral': 4290, 'n_tokens_neox': 3306, 'n_words': 611}