Esempio n. 1
0
  @Path("/create/{ObjID}/{ObjInsId}")
  @POST
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public Response sendCreate(
      @PathParam("ObjID") String objectId,
      @PathParam("ObjInsId") String objectInstanceId,
      @QueryParam("ep") String endPoint,
      String value)
      throws InterruptedException {

    String string = objectId + "/" + objectInstanceId;

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("operation", "create");
    jsonObject.addProperty("directory", string);
    jsonObject.addProperty("value", value);

    Info.setInfo(endPoint, jsonObject.toString());
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registered yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();
    return Response.status(200).entity("creation success").build();
  }
Esempio n. 2
0
  @Path("/writeAttribute/{ObjID}/{ObjInsId}/{resourceId}")
  @PUT
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public Response sendWriteAttribute(
      @PathParam("ObjID") String objectId,
      @PathParam("ObjInsId") String objectInstanceId,
      @PathParam("resourceId") String resourceID,
      @QueryParam("ep") String endPoint,
      @QueryParam("pmin") String pmin,
      @QueryParam("pmax") String pmax,
      @QueryParam("gt") String gt,
      @QueryParam("lt") String lt,
      @QueryParam("st") String st)
      throws InterruptedException {

    String string = objectId + "/" + objectInstanceId + "/" + resourceID;

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("operation", "writeAttribute");
    jsonObject.addProperty("directory", string);
    JsonObject value = new JsonObject();
    if (pmin != null) {
      value.addProperty("pmin", pmin);
    }
    if (pmax != null) {
      value.addProperty("pmax", pmax);
    }
    if (gt != null) {
      value.addProperty("gt", gt);
    }
    if (lt != null) {
      value.addProperty("lt", lt);
    }
    if (st != null) {
      value.addProperty("st", st);
    }
    jsonObject.addProperty("value", value.toString());

    //        String directory =
    // "{\"operation\":\"discover\""+","+"\""+"directory\":"+"\""+objectId+"/"+objectInstanceId+"/"+resourceID+"\"}";
    Info.setInfo(endPoint, jsonObject.toString());
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registered yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();

    return Response.status(200).entity("writeAttribute success").build();
  }
Esempio n. 3
0
  @Path("/write/{ObjID}/{ObjInsId}/{resourceId}")
  @POST
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public Response sendWrite(
      @PathParam("ObjID") String objectId,
      @PathParam("ObjInsId") String objectInstanceId,
      @PathParam("resourceId") String resourceID,
      @QueryParam("ep") String endPoint,
      String value)
      throws InterruptedException {
    String directory =
        "{\"operation\":\"write\""
            + ","
            + "\""
            + "directory\":"
            + "\""
            + objectId
            + "/"
            + objectInstanceId
            + "/"
            + resourceID
            + "\""
            + ","
            + "\""
            + "value\":"
            + "\""
            + value
            + "\""
            + "}";
    Info.setInfo(endPoint, directory);
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registerd yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();

    return Response.status(200).entity("write success").build();
  }
Esempio n. 4
0
  @Path("/discover/{ObjID}/{ObjInsId}/{resourceId}")
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public Response sendDiscover(
      @PathParam("ObjID") String objectId,
      @PathParam("ObjInsId") String objectInstanceId,
      @PathParam("resourceId") String resourceID,
      @QueryParam("ep") String endPoint)
      throws Exception {
    String directory =
        "{\"operation\":\"discover\""
            + ","
            + "\""
            + "directory\":"
            + "\""
            + objectId
            + "/"
            + objectInstanceId
            + "/"
            + resourceID
            + "\"}";
    Info.setInfo(endPoint, directory);
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registerd yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();

    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class).filter("client_bs_obj.device_id =", endPoint).asList();
    resource.Object obj = clientObjects.get(0).getObjectMap().get(Integer.parseInt(objectId));
    String message = "discover success! " + "pmin:" + obj.getPmin() + " pmax:" + obj.getPmax();

    return Response.status(200).entity(message).build();
  }
Esempio n. 5
0
  @Path("/execute/{ObjID}/{ObjInsId}/{resourceId}")
  @POST
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public Response sendExecute(
      @PathParam("ObjID") String objectId,
      @PathParam("ObjInsId") String objectInstanceId,
      @PathParam("resourceId") String resourceID,
      @QueryParam("ep") String endPoint,
      String value)
      throws InterruptedException {

    String string = objectId + "/" + objectInstanceId + "/" + resourceID;

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("operation", "execute");
    jsonObject.addProperty("directory", string);
    jsonObject.addProperty("value", value);

    Info.setInfo(endPoint, jsonObject.toString());
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registered yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();
    String temp = Info.getClientSendToServerInfoMap(endPoint);
    Double result = Double.valueOf(temp);
    Double result1 = Double.valueOf(String.format("%.2f", result));
    String temp1 = String.valueOf(result1);

    return Response.status(200).entity("execution success!The temperature is " + temp1).build();
  }
Esempio n. 6
0
  @Path("/read/{ObjID}/{ObjInsId}/{resourceId}")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response sendRead(
      @PathParam("ObjID") Integer objectId,
      @PathParam("ObjInsId") Integer objectInstanceId,
      @PathParam("resourceId") Integer resourceID,
      @QueryParam("ep") String endPoint)
      throws Exception {
    String directory =
        "{"
            + "\"operation\":\"read\""
            + ","
            + "\""
            + "directory\":"
            + "\""
            + objectId
            + "/"
            + objectInstanceId
            + "/"
            + resourceID
            + "\"}";
    Info.setInfo(endPoint, directory);
    CountDownLatch signal = Info.getCountDown(endPoint);
    if (signal == null) {
      return Response.status(200).entity("Devices not registerd yet").build();
    }
    signal.countDown();

    CountDownLatch signalRead = new CountDownLatch(1);
    Info.setCountDownLatchMessageMap(endPoint, signalRead);
    signalRead.await();

    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class).filter("client_bs_obj.device_id =", endPoint).asList();

    JSONArray jsonArray = new JSONArray();
    if (objectInstanceId == 0) {

      Map<Date, Double> hashMap;
      hashMap =
          clientObjects
              .get(0)
              .getObjectMap()
              .get(objectId)
              .getObjInstanceMap()
              .get(objectInstanceId)
              .getResourceMap()
              .get(resourceID)
              .getValue();
      Map<Date, Double> sortedMap = new TreeMap<Date, Double>(hashMap);

      Iterator it = sortedMap.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        JSONArray jsonArray1 = new JSONArray();
        Date date = (Date) pair.getKey();
        jsonArray1.put(date.getTime()).put(pair.getValue());
        jsonArray.put(jsonArray1);
        it.remove(); // avoids a ConcurrentModificationException
      }
    } else if (objectInstanceId == 1) {

      Map<Date, Integer> hashMap;
      hashMap =
          clientObjects
              .get(0)
              .getObjectMap()
              .get(objectId)
              .getObjInstanceMap()
              .get(objectInstanceId)
              .getResourceMap()
              .get(resourceID)
              .getMvalue();
      Map<Date, Integer> sortedMap = new TreeMap<Date, Integer>(hashMap);
      Iterator it = sortedMap.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        JSONArray jsonArray1 = new JSONArray();
        Date date = (Date) pair.getKey();

        jsonArray1.put(date.getTime()).put(pair.getValue());
        jsonArray.put(jsonArray1);
        it.remove(); // avoids a ConcurrentModificationException
      }
    }

    return Response.status(200).entity(jsonArray.toString()).build();
  }