Search in sources :

Example 26 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class MqttTransportHandler method checkGatewaySession.

private void checkGatewaySession() {
    Device device = deviceSessionCtx.getDevice();
    JsonNode infoNode = device.getAdditionalInfo();
    if (infoNode != null) {
        JsonNode gatewayNode = infoNode.get("gateway");
        if (gatewayNode != null && gatewayNode.asBoolean()) {
            gatewaySessionCtx = new GatewaySessionCtx(processor, deviceService, authService, relationService, deviceSessionCtx);
        }
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) GatewaySessionCtx(org.thingsboard.server.transport.mqtt.session.GatewaySessionCtx) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 27 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class RestClient method createDevice.

public Device createDevice(String name, String type) {
    Device device = new Device();
    device.setName(name);
    device.setType(type);
    return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
}
Also used : Device(org.thingsboard.server.common.data.Device)

Example 28 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class DeviceController method assignDeviceToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/device/{deviceId}", method = RequestMethod.POST)
@ResponseBody
public Device assignDeviceToPublicCustomer(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(device.getTenantId());
        Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(deviceId, publicCustomer.getId()));
        logEntityAction(deviceId, savedDevice, savedDevice.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strDeviceId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strDeviceId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 29 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class DeviceController method saveDeviceCredentials.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/device/credentials", method = RequestMethod.POST)
@ResponseBody
public DeviceCredentials saveDeviceCredentials(@RequestBody DeviceCredentials deviceCredentials) throws ThingsboardException {
    checkNotNull(deviceCredentials);
    try {
        Device device = checkDeviceId(deviceCredentials.getDeviceId());
        DeviceCredentials result = checkNotNull(deviceCredentialsService.updateDeviceCredentials(deviceCredentials));
        actorService.onCredentialsUpdate(getCurrentUser().getTenantId(), deviceCredentials.getDeviceId());
        logEntityAction(device.getId(), device, device.getCustomerId(), ActionType.CREDENTIALS_UPDATED, null, deviceCredentials);
        return result;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.CREDENTIALS_UPDATED, e, deviceCredentials);
        throw handleException(e);
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 30 with Device

use of org.thingsboard.server.common.data.Device 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)

Aggregations

Device (org.thingsboard.server.common.data.Device)53 Test (org.junit.Test)31 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)18 ArrayList (java.util.ArrayList)11 DeviceId (org.thingsboard.server.common.data.id.DeviceId)11 CustomerId (org.thingsboard.server.common.data.id.CustomerId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Customer (org.thingsboard.server.common.data.Customer)7 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)7 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 TenantId (org.thingsboard.server.common.data.id.TenantId)6 UUID (java.util.UUID)5 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 Tenant (org.thingsboard.server.common.data.Tenant)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 List (java.util.List)2 Before (org.junit.Before)2