<commit_before>/**
 * Copyright (c) 2010-2017 by the respective copyright holders.
 *
 * 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.heos.handler;

import static org.openhab.binding.heos.HeosBindingConstants.*;
import static org.openhab.binding.heos.internal.resources.HeosConstants.*;

import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.PlayPauseType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.heos.internal.api.HeosAPI;
import org.openhab.binding.heos.internal.api.HeosSystem;
import org.openhab.binding.heos.internal.resources.HeosEventListener;
import org.openhab.binding.heos.internal.resources.HeosPlayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@link HeosPlayerHandler} handles the actions for a HEOS player.
 * Channel commands are received and send to the dedicated channels
 *
 * @author <NAME> - Initial contribution
 */

public class HeosPlayerHandler extends BaseThingHandler implements HeosEventListener {
    private HeosAPI api;
    private HeosSystem heos;
    private String pid;
    private HashMap<String, HeosPlayer> playerMap;
    private HeosPlayer player = new HeosPlayer();
    private HeosBridgeHandler bridge;

    private Logger logger = LoggerFactory.getLogger(HeosPlayerHandler.class);

    public HeosPlayerHandler(Thing thing, HeosSystem heos, HeosAPI api) {
        super(thing);
        this.heos = heos;
        this.api = api;
        pid = thing.getConfiguration().get(PID).toString();
    }

    @Override
    public void handleCommand(ChannelUID channelUID, Command command) {
        if (command.toString().equals("REFRESH")) {
            return;
        }
        if (channelUID.getId().equals(CH_ID_CONTROL)) {
            String com = command.toString();

            switch (com) {
                case "PLAY":
                    api.play(pid);
                    break;
                case "PAUSE":
                    api.pause(pid);
                    break;
                case "NEXT":
                    api.next(pid);
                    break;
                case "PREVIOUS":
                    api.prevoious(pid);
                    break;
                case "ON":
                    api.play(pid);
                    break;
                case "OFF":
                    api.pause(pid);
                    break;
            }
        } else if (channelUID.getId().equals(CH_ID_VOLUME)) {
            api.volume(command.toString(), pid);
        } else if (channelUID.getId().equals(CH_ID_MUTE)) {
            if (command.toString().equals("ON")) {
                api.muteON(pid);
            } else {
                api.muteOFF(pid);
            }
            // Allows playing external sources like Aux In
            // If no player on bridge is selected play input from this this player
            // Only one source can be selected
        } else if (channelUID.getId().equals(CH_ID_INPUTS)) {
            if (bridge.getSelectedPlayer().isEmpty()) {
                api.playInputSource(pid, null, command.toString());
            } else if (bridge.getSelectedPlayer().size() > 1) {
                logger.warn("Only one source can be selected for HEOS Input. Selected amount of sources: {} ",<commit_msg>Refresh channel command reworked<commit_after>/**
 * Copyright (c) 2010-2017 by the respective copyright holders.
 *
 * 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.heos.handler;

import static org.openhab.binding.heos.HeosBindingConstants.*;
import static org.openhab.binding.heos.internal.resources.HeosConstants.*;

import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.PlayPauseType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.openhab.binding.heos.internal.api.HeosFacade;
import org.openhab.binding.heos.internal.api.HeosSystem;
import org.openhab.binding.heos.internal.resources.HeosEventListener;
import org.openhab.binding.heos.internal.resources.HeosPlayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@link HeosPlayerHandler} handles the actions for a HEOS player.
 * Channel commands are received and send to the dedicated channels
 *
 * @author <NAME> - Initial contribution
 */

public class HeosPlayerHandler extends BaseThingHandler implements HeosEventListener {
    private HeosFacade api;
    private HeosSystem heos;
    private String pid;
    private HashMap<String, HeosPlayer> playerMap;
    private HeosPlayer player = new HeosPlayer();
    private HeosBridgeHandler bridge;

    private Logger logger = LoggerFactory.getLogger(HeosPlayerHandler.class);

    public HeosPlayerHandler(Thing thing, HeosSystem heos, HeosFacade api) {
        super(thing);
        this.heos = heos;
        this.api = api;
        pid = thing.getConfiguration().get(PID).toString();
    }

    @Override
    public void handleCommand(ChannelUID channelUID, Command command) {
        if (command instanceof RefreshType) {
            return;
        }
        if (channelUID.getId().equals(CH_ID_CONTROL)) {
            String com = command.toString();

            switch (com) {
                case "PLAY":
                    api.play(pid);
                    break;
                case "PAUSE":
                    api.pause(pid);
                    break;
                case "NEXT":
                    api.next(pid);
                    break;
                case "PREVIOUS":
                    api.previous(pid);
                    break;
                case "ON":
                    api.play(pid);
                    break;
                case "OFF":
                    api.pause(pid);
                    break;
            }
        } else if (channelUID.getId().equals(CH_ID_VOLUME)) {
            api.volume(command.toString(), pid);
        } else if (channelUID.getId().equals(CH_ID_MUTE)) {
            if (command.toString().equals("ON")) {
                api.muteON(pid);
            } else {
                api.muteOFF(pid);
            }
            // Allows playing external sources like Aux In
            // If no player on bridge is selected play input from this this player
            // Only one source can be selected
        } else if (channelUID.getId().equals(CH_ID_INPUTS)) {
            if (bridge.getSelectedPlayer().isEmpty()) {
                api.playInputSource(pid, command.toString());
            } else if (bridge.getSelectedPlayer().size() > 1) {
                logger.warn("Only one source can be selected for HEOS Input. Selected amount of sources: {} ",
{'fraction_non_alphanumeric': 0.10029534826482894, 'fraction_numerical': 0.0036918533103618015, 'mean_word_length': 2.44364406779661, 'pattern_counts': {'":': 12, '<': 7, '<?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': '4045644', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2194, 'n_words': 526}