All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.relayr.java.model.Device Maven / Gradle / Ivy

package io.relayr.java.model;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;

import io.relayr.java.RelayrJavaSdk;
import io.relayr.java.model.action.Command;
import io.relayr.java.model.action.Configuration;
import io.relayr.java.model.action.Reading;
import io.relayr.java.model.models.DeviceFirmware;
import io.relayr.java.model.models.DeviceModel;
import io.relayr.java.model.models.error.DeviceModelsException;
import io.relayr.java.model.models.schema.ValueSchema;
import io.relayr.java.model.models.transport.Transport;
import io.relayr.java.model.state.State;
import io.relayr.java.model.state.StateCommands;
import io.relayr.java.model.state.StateConfigurations;
import io.relayr.java.model.state.StateMetadata;
import io.relayr.java.model.state.StateReadings;
import rx.Observable;

/**
 * The Device class is a representation of the device entity.
 * A device entity is any external entity capable of gathering measurements
 * or one which is capable of receiving information from the relayr platform.
 * Examples would be a thermometer, a gyroscope or an infrared sensor.
 */
public class Device implements Serializable {

    private static final long serialVersionUID = 1L;

    private final String id;
    private final String modelId;
    private String name;
    private String description;
    private final String owner;
    private final String firmwareVersion;
    private final String secret;
    private final String externalId;
    @SerializedName("public") private boolean isPublic;
    @SerializedName("integrationType") private String accountType;
    private Model model;

    public Device(String accountType, boolean isPublic, String externalId, String secret,
                  String firmwareVersion, String owner, Model model, String name, String id) {
        this.accountType = accountType;
        this.isPublic = isPublic;
        this.externalId = externalId;
        this.secret = secret;
        this.firmwareVersion = firmwareVersion;
        this.owner = owner;
        this.model = model;
        this.modelId = model.getId();
        this.name = name;
        this.id = id;
    }

    public Device(String id, String owner, String modelId, String name) {
        this(id, owner, modelId, name, "");
    }

    public Device(String id, String owner, String modelId, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.owner = owner;
        this.modelId = modelId;
        this.externalId = null;
        this.secret = null;
        this.firmwareVersion = null;
        this.model = new Model(modelId);
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getId() {
        return id;
    }

    public String getFirmwareVersion() {
        return firmwareVersion;
    }

    public String getSecret() {
        return secret;
    }

    public String getExternalId() {
        return externalId;
    }

    public boolean isPublic() {
        return isPublic;
    }

    public String getOwner() {
        return owner;
    }

    public String getAccountType() {
        return accountType;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /** Subscribes an app to a device channel. Enables the app to receive data from the device. */
    public Observable subscribeToCloudReadings() {
        return RelayrJavaSdk.getWebSocketClient().subscribe(id);
    }

    /** Unsubscribes an app from a device channel, stopping and cleaning up the connection. */
    public void unSubscribeToCloudReadings() {
        RelayrJavaSdk.getWebSocketClient().unSubscribe(id);
    }

    /**
     * Returns whole state with all latest readings, configurations, commands and metadata for device.
     * Readings, commands and configurations are persisted automatically when received on the server.
     */
    public Observable getState() {
        return RelayrJavaSdk.getDeviceApi().getState(id);
    }

    /**
     * Returns all latest readings saved in device state.
     * For filtering readings use {@link #getReadings(String, String)}
     */
    public Observable getReadings() {
        return getReadings(null, null);
    }

    /**
     * Returns latest readings saved in device state optionally filtered with path and meaning
     * @param path    see {@link io.relayr.java.model.models.transport.DeviceReading#path} in {@link DeviceModel} for more details
     * @param meaning see {@link io.relayr.java.model.models.transport.DeviceReading#meaning} in  {@link DeviceModel} for more details
     */
    public Observable getReadings(String path, String meaning) {
        return RelayrJavaSdk.getDeviceApi().getReadings(id, path, meaning);
    }

    /**
     * Sends a configuration to device.
     * Check possible configuration in {@link DeviceModel}.
     * Command will be automatically saved in device state and  can be fetched with {@link #getStateConfigurations()}
     */
    public Observable sendConfiguration(Configuration configuration) {
        return RelayrJavaSdk.getDeviceApi().sendConfiguration(id, configuration);
    }

    /**
     * Returns all latest configurations saved in device state.
     * For filtering readings use {@link #getStateConfigurations(String, String)}
     */
    public Observable getStateConfigurations() {
        return getStateConfigurations(null, null);
    }

    /**
     * Returns latest readings saved in device state optionally filtered with path and meaning
     * @param path see {@link io.relayr.java.model.models.transport.DeviceConfiguration#path} in {@link DeviceModel} for more details
     * @param name see {@link io.relayr.java.model.models.transport.DeviceConfiguration#name} in  {@link DeviceModel} for more details
     */
    public Observable getStateConfigurations(String path, String name) {
        return RelayrJavaSdk.getDeviceApi().getConfigurations(id, path, name);
    }

    /**
     * Sends a command to the this device.
     * Check possible commands in {@link DeviceModel}.
     * Command will be automatically saved in device state and  can be fetched with {@link #getStateCommands()}
     */
    public Observable sendCommand(Command command) {
        return RelayrJavaSdk.getDeviceApi().sendCommand(id, command);
    }

    /**
     * Returns all latest commands saved in device state.
     * For filtering readings use {@link #getStateCommands(String, String)}
     */
    public Observable getStateCommands() {
        return getStateCommands(null, null);
    }

    /**
     * Returns latest readings saved in device state optionally filtered with path and meaning
     * @param path see {@link io.relayr.java.model.models.transport.DeviceCommand#path} in {@link DeviceModel} for more details
     * @param name see {@link io.relayr.java.model.models.transport.DeviceCommand#name} in  {@link DeviceModel} for more details
     */
    public Observable getStateCommands(String path, String name) {
        return RelayrJavaSdk.getDeviceApi().getCommands(id, path, name);
    }

    /**
     * Returns metadata saved in device state.
     * @param key if null whole metadata is returned, otherwise object with specified key is returned (if one exists)
     */
    public Observable getStateMetadata(String key) {
        return RelayrJavaSdk.getDeviceApi().getMetadata(id, key);
    }

    /**
     * Saves object with corresponding key into device metadata.
     * @param key    separated with dots '.' to create nicer dictionary structure for easier navigation.
     * @param object object to save under specified key
     */
    public Observable saveStateMetadata(String key, Object object) {
        return RelayrJavaSdk.getDeviceApi().setMetadata(id, key, object);
    }


    public String getModelId() {
        if (modelId != null) return modelId;
        if (model != null) return model.getId();
        return null;
    }

    /**
     * Returns {@link DeviceModel} that defines readings, commands and configurations for
     * specific device depending on device firmware version.
     * Use if {@link RelayrJavaSdk#getDeviceModelsCache()} is initialized.
     * @return {@link DeviceModel}
     */
    public DeviceModel getDeviceModel() throws DeviceModelsException {
        return RelayrJavaSdk.getDeviceModelsCache().getModelById(getModelId());
    }

    /**
     * Returns {@link ValueSchema} for specified meaning and path from received {@link io.relayr.java.model.action.Reading} object.
     * {@link ValueSchema} defines received data and a way to parse it.
     * @return {@link DeviceModel}
     */
    public ValueSchema getValueSchema(String meaning, String path) throws DeviceModelsException {
        DeviceModel model = RelayrJavaSdk.getDeviceModelsCache().getModelById(getModelId());
        if (model == null) throw DeviceModelsException.deviceModelNotFound();

        DeviceFirmware firmware = model.getFirmwareByVersion(firmwareVersion);
        if (firmware == null) throw DeviceModelsException.firmwareNotFound();

        Transport defaultTransport = firmware.getDefaultTransport();
        if (defaultTransport == null) throw DeviceModelsException.transportNotFound();

        return defaultTransport.getReadingByMeaning(meaning, path).getValueSchema();
    }

    /** Used only for Wunderbar devices. */
    public TransmitterDevice toTransmitterDevice() {
        return new TransmitterDevice(id, secret, owner, name, modelId != null ? modelId : model.getId());
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof TransmitterDevice && ((TransmitterDevice) o).getId().equals(id) ||
                o instanceof Device && ((Device) o).id.equals(id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override public String toString() {
        return "Device{" +
                "id='" + id + '\'' +
                ", modelId='" + modelId + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", owner='" + owner + '\'' +
                ", firmwareVersion='" + firmwareVersion + '\'' +
                ", secret='" + secret + '\'' +
                ", externalId='" + externalId + '\'' +
                ", isPublic=" + isPublic +
                ", accountType='" + accountType + '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy