Home Manual Reference Source Test Repository

src/collection/devices/collect/Datapoint.js

'use strict';



/**
 * This is a base object that allows the user to create a Datapoint.
 */
export default class Datapoint {

    constructor() {
        this._from = undefined;
        this._at = undefined;
        this._value = undefined;
        this._tags = undefined;
    }

    /**
     * Sets the from attribute
     * @param {number} from - optional field
     * @return {Datapoint}
     */
    withFrom(from) {
        if (from !== null) {
            if (typeof from !== 'number')
                throw new Error('OGAPI_MUST_BE_NUMBER_PARAMETER');
            this._from = from;
        }
        return this;
    }

    /**
     * Sets the at attribute
     * @param {number} at - optional field
     * @return {Datapoint}
     */
    withAt(at) {
        if (at !== null) {
            if (typeof at !== 'number')
                throw new Error('OGAPI_MUST_BE_NUMBER_PARAMETER');
            this._at = at;
        }
        return this;
    }

    /**
     * Sets the value attribute
     * @param {string} value - required field 
     * @return {Datapoint}
     */
    withValue(value) {
        if (value === undefined || value.length === 0)
            throw new Error('OGAPI_DEFINED_PARAMETER');
        this._value = value;
        return this;
    }

    /**
     * Sets the source attribute
     * @param {string} value - optional field
     * @return {Datapoint}
     */
    withSource(value) {
        if (value === undefined || value.length === 0)
            throw new Error('OGAPI_DEFINED_PARAMETER');
        this._source = value;
        return this;
    }

    /**
     * Sets the source attribute
     * @param {string} value - optional field
     * @return {Datapoint}
     */
    withSourceInfo(value) {
        if (value === undefined || value.length === 0)
            throw new Error('OGAPI_DEFINED_PARAMETER');
        this._sourceInfo = value;
        return this;
    }

    /**
     * Sets the tags attribute
     * @param {Array} tags 
     * @return {Datapoint}
     */
    withTags(tags) {
        if (tags !== null) {
            if (tags.constructor !== Array || tags.length === 0)
                throw new Error({ message: 'OGAPI_ARRAY_PARAMETER', parameter: 'tags' });
            this._tags = tags;
        }
        return this;
    }

    composeElement() {
        if (this._value === undefined || this._value.length === 0) {
            throw new Error({ message: 'OGAPI_DEFINED_PARAMETER', parameter: 'value' });
        }
        var datapoint = {
            'from': this._from || undefined,
            'at': this._at || undefined,
            'value': this._value,
            'tags': this._tags || undefined,
            'source': this._source || undefined,
            'sourceInfo': this._sourceInfo || undefined
        };
        return datapoint;
    }



}