# Datastreams and values

### Getting and formatting datastreams and values

#### getVariableValue(variable)

This method reads a variable value.

**Kind**: global function  
**Returns**: The read value, or undefined if the variable is not found.

| Param            | Type    | Description               |
| ---------------- |---------| --------------------------|
| variable         | String  | Variable to obtain value. |

Examples of use:

```javascript
var myVar = undefined;
var finalVar = getVariableValue(myVar);
```

Result is '';

This function returns the same value

```javascript
var myVar = 2;
var finalVar = getVariableValue(myVar);
```

Result is 2;

#### getDatastreamFromEntity(datastreamId)

This method returns completed datastream of received entity.

**Kind**: global function  
**Returns**: Completed datastream or undefined if the datastream is not found.

| Param            | Type    | Description               |
| ---------------- |---------| --------------------------|
| datastreamId     | String  | Datastream to obtain value. |

Example of use:

```javascript
var deviceTemperature = getDatastreamFromEntity('device.temperature');
```

Result:
```json
{
  "_received": [
    {
      "value": 25.3,
      "date": "2017-12-01T08:52:37.64Z",
      "at": "2017-12-01T08:52:37.64Z",
      "source": "DEVICE_OPENGATE_HTTP",
      "sourceInfo": "IoT Data Message Received"
    }
  ],
  "_current": {
    "value": 25.3,
    "date": "2017-12-01T08:52:37.64Z",
    "at": "2017-12-01T08:52:37.64Z",
    "source": "DEVICE_OPENGATE_HTTP",
    "sourceInfo": "IoT Data Message Received"
  },
  "_previous": {
    "value": 23.3,
    "date": "2017-12-01T05:52:37.64Z",
    "at": "2017-12-01T05:52:37.64Z",
    "source": "DEVICE_OPENGATE_HTTP",
    "sourceInfo": "IoT Data Message Received"
  }
}
```

#### getDatastreamValueFromEntity(datastreamObject)

Returns `_value._current._value` from datastream object

**Kind**: global function  
**Returns**: Value object from datastream object.

| Param            | Type    | Description               |
| ---------------- |---------| --------------------------|
| datastreamObject | Object  | Datastream object.        |

Example of use:

```javascript
var deviceTemperature = getDatastreamFromEntity('device.temperature');
var deviceTemperatureValue = getDatastreamValueFromEntity(deviceTemperature);
```

Result:

```json
{
  "value": 25.3,
  "date": "2017-12-01T08:52:37.64Z",
  "at": "2017-12-01T08:52:37.64Z",
  "source": "DEVICE_OPENGATE_HTTP",
  "sourceInfo": "IoT Data Message Received"
}
```

#### getCommsDatastreamFromEntity(datastreamId, commsId)

Returns complete datastream in selected communication module of received entity.

**Kind**: global function  
**Returns**: Complete datastream or undefined if the datastream is not found.

| Param            | Type    | Description               |
| ---------------- |---------| --------------------------|
| datastreamId     | String  | Datastream to obtain value. |
| commsId          | String  | Communication module identifier. |

Example of use:

```javascript
datastream = getCommsDatastreamFromEntity('device.communicationModules[].subscription.address', 'commsMod_battery_id');
```

Result:

```json
{
  "_current": {
    "value": {
      "type": "IPV4",
      "value": "99.1.1.71",
      "apn": "myapn.es"
    },
    "date": "2017-12-01T08:52:37.624Z",
    "at": "2017-12-01T08:52:37.624Z",
    "source": "DEVICE_OPENGATE_HTTP",
    "sourceInfo": "IoT Data Message Received"
  },
  "_previous": {
    "value": {
      "type": "IPV4",
      "value": "99.1.1.71",
      "apn": "myapn.es"
    },
    "date": "2017-12-01T08:52:37.624Z",
    "at": "2017-12-01T08:52:37.624Z",
    "source": "DEVICE_OPENGATE_HTTP",
    "sourceInfo": "IoT Data Message Received"
  }
}
```

#### getCounterValue(datastreamValue, incValue, resetDate)

Obtains `incValue` if `datastreamValue` date is before than `resetDate` or increments received value in datastream to `incValue` if the date is after.

**Kind**: global function  
**Returns**: Incremented value or reset value.

| Param            | Type    | Description               |
| ---------------- |---------| --------------------------|
| datastreamValue  | Object  | Datastream value.         |
| incValue         | Number  | Value to increment.       |
| resetDate        | Date    | Date of reset.            |

Example of use:

```javascript
var datastream = {'value':3, 'date': toDate('2021-06-30T11:20:21.352Z')}
var resetDate = toDate('2021-06-30T00:00:0.000Z');
var counter = getCounterValue(datastream, 1, resetDate); 
```

Result: 4

This example get reseted daily counter to _myCounterDatastream_ datastream:

```javascript
var datastream = {'value':3, 'date': toDate('2021-06-29T11:20:21.352Z')}
var resetDate = toDate('2021-06-30T00:00:0.000Z');
var counter = getCounterValue(datastream, 1, resetDate); 
```

Result: 1
