Search in sources :

Example 1 with TransportContext

use of org.thingsboard.server.common.transport.TransportContext in project thingsboard by thingsboard.

the class DeviceApiController method claimDevice.

@ApiOperation(value = "Save claiming information (claimDevice)", notes = "Saves the information required for user to claim the device. " + "See more info about claiming in the corresponding 'Claiming devices' platform documentation." + "\n Example of the request payload: " + MARKDOWN_CODE_BLOCK_START + "{\"secretKey\":\"value\", \"durationMs\":60000}" + MARKDOWN_CODE_BLOCK_END + "Note: both 'secretKey' and 'durationMs' is optional parameters. " + "In case the secretKey is not specified, the empty string as a default value is used. In case the durationMs is not specified, the system parameter device.claim.duration is used.\n\n" + REQUIRE_ACCESS_TOKEN, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/{deviceToken}/claim", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> claimDevice(@ApiParam(value = ACCESS_TOKEN_PARAM_DESCRIPTION, required = true, defaultValue = "YOUR_DEVICE_ACCESS_TOKEN") @PathVariable("deviceToken") String deviceToken, @RequestBody(required = false) String json) {
    DeferredResult<ResponseEntity> responseWriter = new DeferredResult<>();
    transportContext.getTransportService().process(DeviceTransportType.DEFAULT, ValidateDeviceTokenRequestMsg.newBuilder().setToken(deviceToken).build(), new DeviceAuthCallback(transportContext, responseWriter, sessionInfo -> {
        TransportService transportService = transportContext.getTransportService();
        DeviceId deviceId = new DeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
        transportService.process(sessionInfo, JsonConverter.convertToClaimDeviceProto(deviceId, json), new HttpOkCallback(responseWriter));
    }));
    return responseWriter;
}
Also used : JsonObject(com.google.gson.JsonObject) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ApiOperation(io.swagger.annotations.ApiOperation) SubscribeToAttributeUpdatesMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) GetAttributeResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) UUID(java.util.UUID) TransportService(org.thingsboard.server.common.transport.TransportService) RestController(org.springframework.web.bind.annotation.RestController) ToServerRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SubscribeToRPCMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg) TbTransportService(org.thingsboard.server.common.data.TbTransportService) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) GetAttributeRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg) ProvisionDeviceResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) AttributeUpdateNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg) ToDeviceRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg) ToServerRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcRequestMsg) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) DeferredResult(org.springframework.web.context.request.async.DeferredResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) JsonParser(com.google.gson.JsonParser) RequestBody(org.springframework.web.bind.annotation.RequestBody) SessionMsgListener(org.thingsboard.server.common.transport.SessionMsgListener) ConditionalOnExpression(org.springframework.boot.autoconfigure.condition.ConditionalOnExpression) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionCloseNotificationProto(org.thingsboard.server.gen.transport.TransportProtos.SessionCloseNotificationProto) ValidateDeviceTokenRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceTokenRequestMsg) DataConstants(org.thingsboard.server.common.data.DataConstants) TransportContext(org.thingsboard.server.common.transport.TransportContext) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) Consumer(java.util.function.Consumer) HttpStatus(org.springframework.http.HttpStatus) ResponseEntity(org.springframework.http.ResponseEntity) StringUtils(org.springframework.util.StringUtils) ResponseEntity(org.springframework.http.ResponseEntity) TransportService(org.thingsboard.server.common.transport.TransportService) TbTransportService(org.thingsboard.server.common.data.TbTransportService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) UUID(java.util.UUID) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with TransportContext

use of org.thingsboard.server.common.transport.TransportContext in project thingsboard by thingsboard.

the class DeviceApiController method postRpcRequest.

@ApiOperation(value = "Send the RPC command (postRpcRequest)", notes = "Send the RPC request to server. The request payload is a JSON document that contains 'method' and 'params'. For example:" + MARKDOWN_CODE_BLOCK_START + "{\"method\": \"sumOnServer\", \"params\":{\"a\":2, \"b\":2}}" + MARKDOWN_CODE_BLOCK_END + "The response contains arbitrary JSON with the RPC reply. For example: " + MARKDOWN_CODE_BLOCK_START + "{\"result\": 4}" + MARKDOWN_CODE_BLOCK_END + REQUIRE_ACCESS_TOKEN, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/{deviceToken}/rpc", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postRpcRequest(@ApiParam(value = ACCESS_TOKEN_PARAM_DESCRIPTION, required = true, defaultValue = "YOUR_DEVICE_ACCESS_TOKEN") @PathVariable("deviceToken") String deviceToken, @ApiParam(value = "The RPC request JSON", required = true) @RequestBody String json) {
    DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
    transportContext.getTransportService().process(DeviceTransportType.DEFAULT, ValidateDeviceTokenRequestMsg.newBuilder().setToken(deviceToken).build(), new DeviceAuthCallback(transportContext, responseWriter, sessionInfo -> {
        JsonObject request = new JsonParser().parse(json).getAsJsonObject();
        TransportService transportService = transportContext.getTransportService();
        transportService.registerSyncSession(sessionInfo, new HttpSessionListener(responseWriter, transportContext.getTransportService(), sessionInfo), transportContext.getDefaultTimeout());
        transportService.process(sessionInfo, ToServerRpcRequestMsg.newBuilder().setRequestId(0).setMethodName(request.get("method").getAsString()).setParams(request.get("params").toString()).build(), new SessionCloseOnErrorCallback(transportService, sessionInfo));
    }));
    return responseWriter;
}
Also used : JsonObject(com.google.gson.JsonObject) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ApiOperation(io.swagger.annotations.ApiOperation) SubscribeToAttributeUpdatesMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) GetAttributeResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) UUID(java.util.UUID) TransportService(org.thingsboard.server.common.transport.TransportService) RestController(org.springframework.web.bind.annotation.RestController) ToServerRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SubscribeToRPCMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg) TbTransportService(org.thingsboard.server.common.data.TbTransportService) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) GetAttributeRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg) ProvisionDeviceResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) AttributeUpdateNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg) ToDeviceRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg) ToServerRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcRequestMsg) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) DeferredResult(org.springframework.web.context.request.async.DeferredResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) JsonParser(com.google.gson.JsonParser) RequestBody(org.springframework.web.bind.annotation.RequestBody) SessionMsgListener(org.thingsboard.server.common.transport.SessionMsgListener) ConditionalOnExpression(org.springframework.boot.autoconfigure.condition.ConditionalOnExpression) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionCloseNotificationProto(org.thingsboard.server.gen.transport.TransportProtos.SessionCloseNotificationProto) ValidateDeviceTokenRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceTokenRequestMsg) DataConstants(org.thingsboard.server.common.data.DataConstants) TransportContext(org.thingsboard.server.common.transport.TransportContext) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) Consumer(java.util.function.Consumer) HttpStatus(org.springframework.http.HttpStatus) ResponseEntity(org.springframework.http.ResponseEntity) StringUtils(org.springframework.util.StringUtils) ResponseEntity(org.springframework.http.ResponseEntity) TransportService(org.thingsboard.server.common.transport.TransportService) TbTransportService(org.thingsboard.server.common.data.TbTransportService) JsonObject(com.google.gson.JsonObject) DeferredResult(org.springframework.web.context.request.async.DeferredResult) JsonParser(com.google.gson.JsonParser) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiParam (io.swagger.annotations.ApiParam)2 Arrays (java.util.Arrays)2 List (java.util.List)2 UUID (java.util.UUID)2 Consumer (java.util.function.Consumer)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 ConditionalOnExpression (org.springframework.boot.autoconfigure.condition.ConditionalOnExpression)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpStatus (org.springframework.http.HttpStatus)2 MediaType (org.springframework.http.MediaType)2 ResponseEntity (org.springframework.http.ResponseEntity)2 StringUtils (org.springframework.util.StringUtils)2 PathVariable (org.springframework.web.bind.annotation.PathVariable)2