Ejemplo n.º 1
0
  @Override
  public void getAgents(DeviceWrapper wrapper, IAgentUPnPListener listener) {
    final DeviceWrapper refDevice = wrapper;
    final IAgentUPnPListener callback = listener;

    if (_upnpService != null) {
      Service service = wrapper.getDevice().findService(upnpAgentService);
      if (service == null) {
        EngineContext.log()
            .error("Agent JS action not found for " + wrapper.getDevice().getDisplayString());
        return;
      }
      Action getAgentListAction = service.getAction("GetAgentList");
      if (getAgentListAction == null) {
        EngineContext.log().error("Agent list action not found");
        return;
      }

      ActionInvocation getAgentListInvocation = new ActionInvocation(getAgentListAction);
      ActionCallback getAgentListCallback =
          new ActionCallback(getAgentListInvocation) {

            @Override
            public void success(ActionInvocation invocation) {
              List<AgentScript> scripts = new ArrayList<AgentScript>();

              ActionArgumentValue agents = invocation.getOutput("Agents");
              if (agents != null) {
                if (agents.getDatatype().getBuiltin() == Datatype.Builtin.STRING) {
                  String encodedJsonArray = String.valueOf(agents.getValue());

                  try {
                    JSONArray agentJsonArray = new JSONArray(encodedJsonArray);

                    for (int idx = 0; idx < agentJsonArray.length(); idx++) {
                      JSONObject agentObject = agentJsonArray.getJSONObject(idx);
                      AgentScript networkScript = new AgentNetworkScript(refDevice, agentObject);
                      scripts.add(networkScript);
                    }

                  } catch (JSONException e) {
                    e.printStackTrace();
                  }
                }
              }

              callback.setAvailableAgents(refDevice, scripts);
            }

            @Override
            public void failure(
                ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
              // don't callback
            }
          };
      _upnpService.getControlPoint().execute(getAgentListCallback);
    }
  }
Ejemplo n.º 2
0
  @Override
  public void getSourceCode(DeviceWrapper wrapper, String agentId, IAgentUPnPListener listener) {
    final DeviceWrapper refDevice = wrapper;
    final IAgentUPnPListener callback = listener;

    if (_upnpService != null) {
      Service service = wrapper.getDevice().findService(upnpAgentService);
      Action getAgentSourceAction = service.getAction("GetAgentSource");

      ActionInvocation getAgentSourceInvocation = new ActionInvocation(getAgentSourceAction);
      getAgentSourceInvocation.setInput("AgentId", agentId);

      ActionCallback getAgentSourceCallback =
          new ActionCallback(getAgentSourceInvocation) {

            @Override
            public void success(ActionInvocation invocation) {
              AgentScript networkScript = null;

              ActionArgumentValue agents = invocation.getOutput("Agent");
              if (agents != null) {
                if (agents.getDatatype().getBuiltin() == Datatype.Builtin.STRING) {
                  String encodedJsonArray = String.valueOf(agents.getValue());

                  try {
                    JSONObject agentObject = new JSONObject(encodedJsonArray);
                    networkScript = new AgentNetworkScript(refDevice, agentObject);

                  } catch (JSONException e) {
                    e.printStackTrace();
                  }
                }
              }

              callback.setAgentSourceCode(refDevice, networkScript);
            }

            @Override
            public void failure(
                ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {}
          };

      _upnpService.getControlPoint().execute(getAgentSourceCallback);
    }
  }