View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.support.renderingcontrol.callback;
17  
18  import org.fourthline.cling.controlpoint.ActionCallback;
19  import org.fourthline.cling.model.action.ActionException;
20  import org.fourthline.cling.model.action.ActionInvocation;
21  import org.fourthline.cling.model.meta.Service;
22  import org.fourthline.cling.model.types.ErrorCode;
23  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
24  import org.fourthline.cling.support.model.Channel;
25  
26  import java.util.logging.Logger;
27  
28  /**
29   *
30   * @author Christian Bauer
31   */
32  public abstract class GetVolume extends ActionCallback {
33  
34      private static Logger log = Logger.getLogger(GetVolume.class.getName());
35  
36      public GetVolume(Service service) {
37          this(new UnsignedIntegerFourBytes(0), service);
38      }
39  
40      public GetVolume(UnsignedIntegerFourBytes instanceId, Service service) {
41          super(new ActionInvocation(service.getAction("GetVolume")));
42          getActionInvocation().setInput("InstanceID", instanceId);
43          getActionInvocation().setInput("Channel", Channel.Master.toString());
44      }
45  
46      public void success(ActionInvocation invocation) {
47          boolean ok = true;
48          int currentVolume = 0;
49          try {
50              currentVolume = Integer.valueOf(invocation.getOutput("CurrentVolume").getValue().toString()); // UnsignedIntegerTwoBytes...
51          } catch (Exception ex) {
52              invocation.setFailure(
53                      new ActionException(ErrorCode.ACTION_FAILED, "Can't parse ProtocolInfo response: " + ex, ex)
54              );
55              failure(invocation, null);
56              ok = false;
57          }
58          if (ok) received(invocation, currentVolume);
59      }
60  
61      public abstract void received(ActionInvocation actionInvocation, int currentVolume);
62  
63  }