gt:         String source = command.toString();
context: /**
   * Copyright (c) 2014-2016 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.samsungtv.internal.service;
  
  import static org.openhab.binding.samsungtv.SamsungTvBindingConstants.*;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.ScheduledFuture;
  import java.util.concurrent.TimeUnit;
  
  import org.eclipse.smarthome.core.library.types.DecimalType;
  import org.eclipse.smarthome.core.library.types.StringType;
  import org.eclipse.smarthome.core.types.Command;
  import org.eclipse.smarthome.core.types.UnDefType;
  import org.eclipse.smarthome.io.transport.upnp.UpnpIOParticipant;
  import org.eclipse.smarthome.io.transport.upnp.UpnpIOService;
  import org.openhab.binding.samsungtv.internal.service.api.SamsungTvService;
  import org.openhab.binding.samsungtv.internal.service.api.ValueReceiver;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  
  /**
   * The {@link MainTVServerService} is responsible for handling MainTVServer
   * commands.
   *
   * @author Pauli Anttila - Initial contribution
   */
  public class MainTVServerService implements UpnpIOParticipant, SamsungTvService {
  
      public static final String SERVICE_NAME = "MainTVServer2";
      private static final List<String> supportedCommands = Arrays.asList(SOURCE_NAME, BROWSER_URL, STOP_BROWSER);
  
      private Logger logger = LoggerFactory.getLogger(MainTVServerService.class);
  
      private UpnpIOService service;
  
      private ScheduledExecutorService scheduler;
      private ScheduledFuture<?> pollingJob;
  
      private String udn;
      private int pollingInterval;
  
      private Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<String, String>());
  
      private List<ValueReceiver> listeners = new ArrayList<ValueReceiver>();
  
      public MainTVServerService(UpnpIOService upnpIOService, String udn, int pollingInterval) {
          logger.debug("Create a Samsung TV MainTVServer service");
  
          if (upnpIOService != null) {
              service = upnpIOService;
          } else {
              logger.debug("upnpIOService not set.");
          }
  
          this.udn = udn;
          this.pollingInterval = pollingInterval;
  
          scheduler = Executors.newScheduledThreadPool(1);
      }
  
      @Override
      public String getServiceName() {
          return SERVICE_NAME;
      }
  
      @Override
      public List<String> getSupportedChannelNames() {
          return supportedCommands;
      }
  
      @Override
      public void addEventListener(ValueReceiver listener) {
          listeners.add(listener);
      }
  
      @Override
      public void removeEventListener(ValueReceiver listener) {
          listeners.remove(listener);
      }
  
      @Override
      public void start() {
          if (pollingJob == null || pollingJob.isCancelled()) {
              logger.debug("Start refresh task, interval={}", pollingInterval);
              pollingJob = scheduler.scheduleAtFixedRate(pollingRunnable, 0, pollingInterval, TimeUnit.MILLISECONDS);
          }
      }
  
      @Override
      public void stop() {
          if (pollingJob != null && !pollingJob.isCancelled()) {
              pollingJob.cancel(true);
              pollingJob = null;
          }
      }
  
      @Override
      public void clearCache() {
          stateMap.clear();
      }
  
      private Runnable pollingRunnable = new Runnable() {
  
          @Override
          public void run() {
              if (isRegistered()) {
                  try {
                      updateResourceState("MainTVAgent2", "GetCurrentMainTVChannel", null);
  
                      updateResourceState("MainTVAgent2", "GetCurrentExternalSource", null);
  
                      updateResourceState("MainTVAgent2", "GetCurrentContentRecognition", null);
  
                      updateResourceState("MainTVAgent2", "GetCurrentBrowserURL", null);
                  } catch (Exception e) {
                      logger.debug("Exception during poll : {}", e);
                  }
              }
          }
      };
  
      @Override
      public void handleCommand(String channel, Command command) {
          logger.debug("Received channel: {}, command: {}", channel, command);
  
          switch (channel) {
              case SOURCE_NAME:
                  setSourceName(command);
                  // Clear value on cache to force update
                  stateMap.put("CurrentExternalSource", "");
                  break;
              case BROWSER_URL:
                  setBrowserUrl(command);
                  // Clear value on cache to force update
                  stateMap.put("BrowserURL", "");
                  break;
              case STOP_BROWSER:
                  stopBrowser(command);
                  break;
              default:
                  logger.warn("Samsung TV doesn't support transmitting for channel '{}'", channel);
          }
      }
  
      private boolean isRegistered() {
          return service.isRegistered(this);
      }
  
      @Override
      public String getUDN() {
          return udn;
      }
  
      @Override
      public void onServiceSubscribed(String service, boolean succeeded) {
      }
  
      @Override
      public void onValueReceived(String variable, String value, String service) {
  
          String oldValue = stateMap.get(variable);
          if (value.equals(oldValue)) {
              logger.trace("Value haven't been changed, ignore update");
              return;
          }
  
          stateMap.put(variable, value);
  
          for (ValueReceiver listener : listeners) {
  
              switch (variable) {
                  case "ProgramTitle":
                      listener.valueReceived(PROGRAM_TITLE, (value != null) ? new StringType(value) : UnDefType.UNDEF);
                      break;
  
                  case "ChannelName":
                      listener.valueReceived(CHANNEL_NAME, (value != null) ? new StringType(value) : UnDefType.UNDEF);
                      break;
  
                  case "CurrentExternalSource":
                      listener.valueReceived(SOURCE_NAME, (value != null) ? new StringType(value) : UnDefType.UNDEF);
                      break;
  
                  case "CurrentChannel":
                      String currentChannel = parseCurrentChannel(value);
                      listener.valueReceived(CHANNEL,
                              (value != null) ? new DecimalType(currentChannel) : UnDefType.UNDEF);
                      break;
  
                  case "ID":
                      listener.valueReceived(SOURCE_ID, (value != null) ? new DecimalType(value) : UnDefType.UNDEF);
                      break;
  
                  case "BrowserURL":
                      listener.valueReceived(BROWSER_URL, (value != null) ? new StringType(value) : UnDefType.UNDEF);
                      break;
              }
          }
      }
  
      protected Map<String, String> updateResourceState(String serviceId, String actionId, Map<String, String> inputs) {
  
          Map<String, String> result = service.invokeAction(this, serviceId, actionId, inputs);
  
          for (String variable : result.keySet()) {
              onValueReceived(variable, result.get(variable), serviceId);
          }
  
          return result;
      }
  
      private void setSourceName(Command command) {
  
          Map<String, String> result = updateResourceState("MainTVAgent2", "GetSourceList", null);
  
  