Search in sources :

Example 1 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceConnect.

private void onDeviceConnect(String deviceName, String deviceType) {
    if (!devices.containsKey(deviceName)) {
        Device device = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
        if (device == null) {
            device = new Device();
            device.setTenantId(gateway.getTenantId());
            device.setName(deviceName);
            device.setType(deviceType);
            device = deviceService.saveDevice(device);
            relationService.saveRelationAsync(new EntityRelation(gateway.getId(), device.getId(), "Created"));
        }
        GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device);
        devices.put(deviceName, ctx);
        log.debug("[{}] Added device [{}] to the gateway session", gatewaySessionId, deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Device(org.thingsboard.server.common.data.Device) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 2 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceRpcResponse.

public void onDeviceRpcResponse(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload());
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        String deviceName = checkDeviceConnected(jsonObj.get(DEVICE_PROPERTY).getAsString());
        Integer requestId = jsonObj.get("id").getAsInt();
        String data = jsonObj.get("data").toString();
        GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, new ToDeviceRpcResponseMsg(requestId, data))));
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 3 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class DeviceApiController method process.

private void process(HttpSessionCtx ctx, FromDeviceMsg request) {
    AdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ctx, request);
    processor.process(new BasicToDeviceActorSessionMsg(ctx.getDevice(), msg));
}
Also used : BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 4 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceTelemetry.

public void onDeviceTelemetry(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload());
    int requestId = mqttMsg.variableHeader().messageId();
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) {
            String deviceName = checkDeviceConnected(deviceEntry.getKey());
            if (!deviceEntry.getValue().isJsonArray()) {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
            }
            BasicTelemetryUploadRequest request = new BasicTelemetryUploadRequest(requestId);
            JsonArray deviceData = deviceEntry.getValue().getAsJsonArray();
            for (JsonElement element : deviceData) {
                JsonConverter.parseWithTs(request, element.getAsJsonObject());
            }
            GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
            processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request)));
        }
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 5 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceAttributesRequest.

public void onDeviceAttributesRequest(MqttPublishMessage msg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, msg.payload());
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        int requestId = jsonObj.get("id").getAsInt();
        String deviceName = jsonObj.get(DEVICE_PROPERTY).getAsString();
        boolean clientScope = jsonObj.get("client").getAsBoolean();
        Set<String> keys;
        if (jsonObj.has("key")) {
            keys = Collections.singleton(jsonObj.get("key").getAsString());
        } else {
            JsonArray keysArray = jsonObj.get("keys").getAsJsonArray();
            keys = new HashSet<>();
            for (JsonElement keyObj : keysArray) {
                keys.add(keyObj.getAsString());
            }
        }
        BasicGetAttributesRequest request;
        if (clientScope) {
            request = new BasicGetAttributesRequest(requestId, keys, null);
        } else {
            request = new BasicGetAttributesRequest(requestId, null, keys);
        }
        GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request)));
        ack(msg);
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg) JsonArray(com.google.gson.JsonArray) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)

Aggregations

BasicAdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)6 BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)6 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 JsonArray (com.google.gson.JsonArray)2 Device (org.thingsboard.server.common.data.Device)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)1