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  package example.localservice;
16  
17  import org.fourthline.cling.binding.annotations.*;
18  
19  /**
20   * Explicitly naming related state variables
21   * <p>
22   * If your mapped action method does not match the name of a mapped state variable,
23   * you have to provide the name of (any) argument's related state variable:
24   * </p>
25   * <a class="citation" href="javacode://this" style="include:INC1"/>
26   * <p>
27   * Here the method has the name <code>retrieveStatus</code>, which
28   * you also have to override if you want it be known as a the
29   * <code>GetStatus</code> UPnP action. Because it is no longer a JavaBean
30   * accessor for <code>status</code>, it explicitly has to be linked with
31   * the related state variable <code>Status</code>. You always have to
32   * provide the related state variable name if your action has more than one
33   * output argument.
34   * </p>
35   * <p>
36   * The "related statevariable" detection algorithm in Cling has one more trick
37   * up its sleeve however. The UPnP specification says that a state variable which
38   * is only ever used to describe the type of an input or output argument should
39   * be named with the prefix <code>A_ARG_TYPE_</code>. So if you do not name the
40   * related state variable of your action argument, Cling will also
41   * look for a state variable with the name
42   * <code>A_ARG_TYPE_[Name Of Your Argument]</code>. In the example above, Cling
43   * is therefore also searching (unsuccessfully) for a state variable named
44   * <code>A_ARG_TYPE_ResultStatus</code>. (Given that direct querying
45   * of state variables is already deprecated in UDA 1.0, there are <em>NO</em>
46   * state variables which are anything but type declarations for action input/output
47   * arguments. This is a good example why UPnP is such a horrid specification.)
48   * </p>
49   */
50  @UpnpService(
51          serviceId = @UpnpServiceId("SwitchPower"),
52          serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
53  )
54  public class SwitchPowerNamedStateVariable {
55  
56      @UpnpStateVariable(defaultValue = "0", sendEvents = false)
57      private boolean target = false;
58  
59      @UpnpStateVariable(defaultValue = "0")
60      private boolean status = false;
61  
62      @UpnpAction
63      public void setTarget(@UpnpInputArgument(name = "NewTargetValue")
64                            boolean newTargetValue) {
65          target = newTargetValue;
66          status = newTargetValue;
67          System.out.println("Switch is: " + status);
68      }
69  
70      @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
71      public boolean getTarget() {
72          return target;
73      }
74  
75      @UpnpAction(                                    // DOC:INC1
76              name = "GetStatus",
77              out = @UpnpOutputArgument(
78                      name = "ResultStatus",
79                      stateVariable = "Status"
80              )
81      )
82      public boolean retrieveStatus() {
83          return status;
84      }                                               // DOC:INC1
85  
86  }