Esempio n. 1
0
  @Path("/writeAttribute")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response writeAttributeResponse(String input) throws Exception {

    JSONObject inputObj = new JSONObject(input);
    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class)
            .filter("client_bs_obj.device_id =", inputObj.getString("client_bs_obj.device_id"))
            .asList();

    ClientObject clientObject = clientObjects.get(0);
    resource.Object obj = clientObject.getObjectMap().get(inputObj.getInt("ObjectId"));
    obj.setPmin(inputObj.getJSONObject("value").getString("pmin"));
    obj.setPmax(inputObj.getJSONObject("value").getString("pmax"));
    obj.setLessThan(inputObj.getJSONObject("value").getString("lt"));
    obj.setGreaterThan(inputObj.getJSONObject("value").getString("gt"));
    obj.setStep(inputObj.getJSONObject("value").getString("st"));
    ds.save(clientObject);

    CountDownLatch signalRead =
        Info.getCountDownMessage(inputObj.getString("client_bs_obj.device_id"));
    signalRead.countDown();

    return Response.status(200).entity(inputObj.getString("message")).build();
  }
Esempio n. 2
0
  @Path("/read")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response readResponse(String input) throws Exception {
    JSONObject test = new JSONObject(input);
    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class)
            .filter("client_bs_obj.device_id =", test.getString("client_bs_obj.device_id"))
            .asList();
    Resource resourceO =
        clientObjects
            .get(0)
            .getObjectMap()
            .get(test.getInt("ObjectId"))
            .getObjInstanceMap()
            .get(test.getInt("ObjectInsId"))
            .getResourceMap()
            .get(test.getInt("resourceId"));
    ObjectMapper mapper = new ObjectMapper();
    Resource resourceN = mapper.readValue(test.getString("value"), Resource.class);

    resourceO.getValue().putAll(resourceN.getValue());

    ds.save(clientObjects.get(0));

    CountDownLatch signalRead = Info.getCountDownMessage(test.getString("client_bs_obj.device_id"));
    signalRead.countDown();

    return Response.status(200).entity("success").build();
  }
Esempio n. 3
0
  @Path("/clientObjects")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response sendObservecCancel() throws Exception {

    MongoCollection<Document> document = MongoConnection.getServerSideCLientObject();
    List<Document> foundDocument = document.find().into(new ArrayList<Document>());
    JSONArray jsonArray = new JSONArray(foundDocument);

    return Response.status(200).entity(jsonArray.toString()).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("/create")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response createResponse(String input) throws Exception {

    JSONObject inputObj = new JSONObject(input);
    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class)
            .filter("client_bs_obj.device_id =", inputObj.getString("client_bs_obj.device_id"))
            .asList();

    ClientObject clientObject = clientObjects.get(0);
    Map<Integer, ObjInstance> objInstanceMap =
        clientObject.getObjectMap().get(inputObj.getInt("ObjectId")).getObjInstanceMap();
    Map<Integer, Resource> resourceMap =
        objInstanceMap.get(inputObj.getInt("ObjectInsId")).getResourceMap();
    ObjInstance objInstance = new ObjInstance();
    objInstance.setObjInstance_id(inputObj.getInt("ObjectInsId") + 1);
    Resource resource = new Resource();
    resource.setRecourse_id(0);
    resource.setName(inputObj.getString("value"));
    Map<Integer, Resource> resourceMapNew = objInstance.getResourceMap();
    resourceMapNew.put(0, resource);
    objInstanceMap.put(objInstance.getObjInstance_id(), objInstance);

    // Map<Integer,Resource> resourceMap = objInstanceMap.get(ObjectInsId).getResourceMap();

    ds.save(clientObject);

    CountDownLatch signalRead =
        Info.getCountDownMessage(inputObj.getString("client_bs_obj.device_id"));
    signalRead.countDown();

    return Response.status(200).entity(inputObj.getString("message")).build();
  }
Esempio n. 6
0
  @Path("/delete")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.TEXT_PLAIN)
  public Response deleteResponse(String input) throws Exception {
    JSONObject inputObj = new JSONObject(input);
    Datastore ds = MongoConnection.getServer();
    List<ClientObject> clientObjects =
        ds.createQuery(ClientObject.class)
            .filter("client_bs_obj.device_id =", inputObj.getString("client_bs_obj.device_id"))
            .asList();

    ClientObject clientObject = clientObjects.get(0);
    Map<Integer, ObjInstance> objInstanceMap =
        clientObject.getObjectMap().get(inputObj.getInt("ObjectId")).getObjInstanceMap();
    objInstanceMap.remove(inputObj.getInt("ObjectInsId"));
    ds.save(clientObject);

    CountDownLatch signalRead =
        Info.getCountDownMessage(inputObj.getString("client_bs_obj.device_id"));
    signalRead.countDown();

    return Response.status(200).entity(inputObj.getString("message")).build();
  }
Esempio n. 7
0
  @Path("/rd")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(SseFeature.SERVER_SENT_EVENTS)
  public EventOutput respondToRegisterDevice(
      String test,
      @QueryParam("ep") final String endPoint,
      @QueryParam("lt") long lt,
      @QueryParam("sms") String sms,
      @QueryParam("lwm2m") String lwm2mVersion,
      @QueryParam("b") String bindingMode)
      throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    final ClientObject clientObject = mapper.readValue(test, ClientObject.class);

    final Datastore ds = MongoConnection.getServer();

    final Query<ClientObject> query =
        ds.createQuery(ClientObject.class).filter("client_bs_obj.device_id =", endPoint);
    final List<ClientObject> clientObjects = query.asList();

    final EventOutput eventOutput = new EventOutput();

    Thread rdThread =
        new Thread(
            new Runnable() {
              public void run() {
                try {

                  final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
                  if (clientObjects.size() == 0) {
                    ds.save(clientObject);

                    eventBuilder.name("register-message-to-client");
                    eventBuilder.data(String.class, "Register success!");
                    final OutboundEvent event = eventBuilder.build();
                    eventOutput.write(event);

                  } else {
                    ds.updateFirst(query, clientObject, true);

                    eventBuilder.name("register-message-to-client");
                    eventBuilder.data(String.class, "Update!");
                    final OutboundEvent event = eventBuilder.build();
                    eventOutput.write(event);
                  }
                  while (true) {

                    final OutboundEvent.Builder eventBuilder1 = new OutboundEvent.Builder();

                    eventBuilder1.name("register-message-to-client");
                    // wait for a signal
                    CountDownLatch signal = new CountDownLatch(1);
                    Info.setCountDownLatchMap(endPoint, signal);

                    signal.await();
                    System.out.println(endPoint);
                    System.out.println("hello");
                    eventBuilder1.data(String.class, Info.getInfo(endPoint));
                    final OutboundEvent event = eventBuilder1.build();
                    eventOutput.write(event);

                    signal = new CountDownLatch(1);
                    Info.setCountDownLatchMap(endPoint, signal);

                    //                                Info.setInfo(endPoint, null);

                  }

                } catch (IOException e) {
                  throw new RuntimeException("Bad Request", e);

                } catch (InterruptedException e) {
                  e.printStackTrace();
                } finally {
                  try {
                    eventOutput.close();
                  } catch (IOException ioClose) {
                    throw new RuntimeException("Error when closing the event output.", ioClose);
                  }
                }
              }
            });

    rdThread.start();

    return eventOutput;
  }
Esempio n. 8
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();
  }