# Notifications utils

### Notification

The `notification` object is the main object for managing notifications.

#### notification.addEmailNotification(emailConfig)

Sends an email notification to defined recipients.

**Returns**: void

This function takes as parameter an object with the following fields:

| Property         | Type   | Default | Mandatory | Description                                                                           |
|------------------|--------|---------|-----------|---------------------------------------------------------------------------------------|
| jsRecipients     | List   |         | Yes       | Array of recipients to send email. This param has same format as defined in easy mode |
| notificationName | String |         | Yes       | Name of notification that will be received in subject field                           |          
| notificationBody | String |         | Yes       | Mustache template with email's body                                                   |            
| ruleName         | String |         | Yes       | Name of rule that launch email request                                                |        
| mailParameters   | Object |         | No        | Json object with key-value pairs that will be replaces in mustache evaluation         |  

Example of use:

The example send email to _example@recipient.es_ with _highTemperature_ notification.

```javascript
emailNotifConfig = {
    jsRecipients: ['example@recipient.es'],
    notificationName: 'highTemperature',
    notificationBody: 'Received high temperature from {{deviceIdentifier}} with value {{deviceTemperature}}',
    ruleName: 'highTemperatureRule',
    mailParameters: {
        deviceIdentifier: getDatastreamFromEntity('device.identifier')._current.value,
        deviceTemperature: getDatastreamFromEntity('device.temperature')._current.value
    }
}
    
notification.addEmailNotification(emailNotifConfig);
```

#### notification.addTrapNotification(trapConfig)

Sends an SNMP trap notification to defined recipients.

This function takes as parameter an object with the following fields:

| Property         | Type   | Default | Mandatory | Description                                                                                       |
|------------------|--------|---------|-----------|---------------------------------------------------------------------------------------------------|
| jsRecipients     | List   |         | Yes       | Array of recipients (`<ip>:<port>`) to send trap. This param has same format as defined in easy mode |
| jsVariables      | Object |         | Yes       | Map of variables. Each pair defines OID variable and sent value for this OID                      |
| notificationName | String |         | Yes       | Name of notification                                                                              |          
| trapOID          | String |         | Yes       | OID of trap                                                                                       |              
| enterpriseOID    | String |         | Yes       | OID of enterprise                                                                                 |  
| ruleName         | String |         | Yes       | Name of rule                                                                                      |      

Example of use:

The example send trap to _25.35.98.5:8585_ with _highTemperature_ notification.

```javascript
trapNotifConfig = {
    jsRecipients: ['25.35.98.5:8585'],
    jsVariables: {
      '1.1.1': entity['provision.administration.organization']._value._current.value,
      '1.1.2': entity['provision.administration.channel']._value._current.value,
      '1.1.3': entity['provision.administration.identifier']._value._current.value,
      '1.1.4': entity['device.temperature']._value._current.value,
      '1.1.5': entity['device.temperature']._value._previous.value,
      '1.1.6': Date.now()
    },
    notificationName: 'highTemperature',
    trapOID: '1.100.1',
    enterpriseOID: '1.2.7.3.1.2.25841',
    ruleName: 'highTemperatureRule'
}

notification.addTrapNotification(trapNotifConfig);
```

#### notification.sendHttp(httpConfig)

Sends an HTTP request notification to a specified URL.

**Returns**: void

This function takes as parameter an object with the following fields:

| Property    | Type   | Default | Mandatory | Description                                   |
|-------------|--------|---------|-----------|-----------------------------------------------|
| url         | String |         | Yes       | URL to which the HTTP request will be sent    |
| method      | String |         | Yes       | HTTP method to be used for the request        |          
| headers     | Object |         | No        | Key-value pairs representing HTTP headers     |            
| queryParams | Object |         | No        | Key-value pairs representing query parameters |        
| body        | String |         | No        | Body of the HTTP request                      |            

Example of use:

The example sends http request to _http://myService/request_ with _highTemperature_ notification.

```javascript
httpJson = {
  'url': 'http://myService/request',
  'method' : 'POST',
  'headers': {
    'Content-type': 'application/json'
  },
  'queryParams' : {
    'deviceId' : entity['provision.device.identifier']._value._current.value
  },
  'body' : 'New alert received by high temperature received. Current value: ' + entity['device.temperature']._value._current.value
};

notification.sendHttp(httpJson);
```

