예제 #1
0
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public String getDevice(
      @PathParam("networkId") Long networkId, @PathParam("deviceId") String deviceIdStr) {

    HANetwork network = HANetwork.getInstance(networkId);

    if (network == null) {
      return "{\"response\":-1}";
    }

    DeviceDriver device = null;
    try {
      Long deviceId = new Long(deviceIdStr);
      for (DeviceDriver d : network.getDevices()) {
        if (d.getId().equals(deviceId)) {
          device = d;
          break;
        }
      }
    } catch (Exception e) {
      // ignore
    }
    if (device == null) {
      try {
        Address64 addr64 = new Address64(deviceIdStr);
        for (DeviceDriver d : network.getDevices()) {
          if (d.getAddress64().equals(addr64)) {
            device = d;
            break;
          }
        }
      } catch (Exception e) {
        // ignore
      }
    }

    if (device == null) {
      return "{\"response\":-1}";
    }

    StringBuffer ret = new StringBuffer();
    ret.append("{\"response\": 0");
    ret.append(",\"id\":" + device.getId());
    ret.append(",\"addr64\":" + device.getAddress64().toString());
    ret.append(",\"addr16\":" + device.getAddress16().toString());
    ret.append(",\"name\": \"" + device.getName() + "\"");
    ret.append(",\"lastRx\": " + device.getLastRxTime());
    ret.append("}");
    return ret.toString();
  }
예제 #2
0
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    log.info("API request received path=" + request.getPathInfo());

    String[] params = request.getPathInfo().split("/");

    Long networkId = new Long(params[1]);
    log.info("networkId=" + networkId);

    // EntityManager em = HibernateUtil.getEntityManager();
    // HANetwork network = em.find(HANetwork.class, networkId);
    HANetwork network = HANetwork.getInstance(networkId);

    String methodClass = "ie.wombat.ha.api.method." + params[2];

    Method method;
    try {
      method = (Method) Class.forName(methodClass).newInstance();
    } catch (InstantiationException e) {
      returnError(response, METHOD_ERROR);
      return;
    } catch (IllegalAccessException e) {
      returnError(response, METHOD_ERROR);
      return;
    } catch (ClassNotFoundException e) {
      returnError(response, METHOD_NOT_FOUND);
      return;
    }

    log.info("method=" + method);

    response.setContentType("application/json");

    MethodResponse mresp = method.invokeMethod(network, params);
    response
        .getOutputStream()
        .print("{\"status\":0,\"result\":" + JSONUtils.quote(mresp.getResponse()) + "}");
  }