# @mangar2/message

> Object to deploy MQTT type messages

Latest version **1.4.2** (published 2020-12-11) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @mangar2/message
pnpm add @mangar2/message
yarn add @mangar2/message
bun add @mangar2/message
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.2 |
| Published | 2020-12-11 |
| First published | 2019-09-22 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Volker Böhm |
| Maintainers | mangar2 |
| Keywords | message, MQTT, topic |

## Links

- npm: https://www.npmjs.com/package/@mangar2/message
- Repository: https://github.com/Mangar2/yaha/tree/master/node_modules/@mangar2/message
- Homepage: https://github.com/Mangar2/yaha.git
- npm.io page: https://npm.io/package/@mangar2/message

## Dependencies (1)

- [@mangar2/checkinput](https://npm.io/package/@mangar2/checkinput.md) latest

## Recent versions

- 1.4.2 (latest) — 2020-12-11
- 1.4.1 — 2020-12-10
- 1.4.0 — 2020-03-15
- 1.3.1 — 2020-03-14
- 1.3.0 — 2020-03-14
- 1.2.1 — 2020-01-12
- 1.2.0 — 2020-01-12
- 1.1.0 — 2019-10-27
- 1.0.8 — 2019-10-15
- 1.0.7 — 2019-10-06
- 1.0.6 — 2019-10-06
- 1.0.5 — 2019-10-06
- 1.0.4 — 2019-10-06
- 1.0.3 — 2019-10-06
- 1.0.2 — 2019-10-06
- … 2 more at https://npm.io/package/@mangar2/message/versions

## README

<!-- This file is generated by jsmddoc version 0.1 -->

# Abstract

This is the basic class holding MQTT message information The yaha home automation is based on messages . Nothing happens without a message . The message runs from creator to broker and then to the receiver . On every step , a reason will added to the mesage including a timestamp to get a full trace .

## Contents

- [Meta](#Meta)
- [Type definitions](#Type-definitions)
- [Class Message](#Class-Message)
  - [Parameters](#Message-Parameters)
  - [Members](#Message-Members)
  - [Methods](#Message-Methods)
    - [addReason](#addReason)
    - [getDateOfNewestChange](#getDateOfNewestChange)
    - [isOn](#isOn)
    - [toJSON](#toJSON)
    - [validate](#validate)

## Meta

| | |
| --- | --- |
| **File** | message.js |
| **Abstract** | This is the basic class holding MQTT message information The yaha home automation is based on messages . Nothing happens without a message . The message runs from creator to broker and then to the receiver . On every step , a reason will added to the mesage including a timestamp to get a full trace . |
| **Author** | Volker Böhm |
| **Copyright** | Copyright ( c ) 2020 Volker Böhm |
| **License** | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |

## Type definitions

### ReasonEntry {Object}

| Name | Type | Description |
| ---- | ---- | ------- |
| `timestamp` | `string` | Timestamp of the event in ISO format | |
| `message` | `string` | Event message | |

### Reason {Array.<ReasonEntry>}

| Name | Type | Description |
| ---- | ---- | ------- |
| `tst` | `test` | |

## Class Message

`new Message(topic, value, reason, now)`

Creates a message for MQTT

### Example

```javascript
const message = new Message('this/is/a/topic', 'a value', 'a reason')
```

### Message Parameters

| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `topic` | `string` |  |  | topic string | |
| `value` | `string, number` | optional | '' | value to set topic to | |
| `reason` | `string, Reason` | optional | null | explaining , why the topic will be set to value | |
| `now` | `Date` | optional | new Date ( ) | current time | |

### Message Members

| Name | Type | description |
| ------------ | ------------ | ------------ |
| `qos` | `integer` | Quality of service ( 0 , 1 or 2 ) the message shall be delivered |
| `retain` | `boolean` | True , if the message shall be retained |
| `value` | `string, bool, number` | Value of the message |

### Message Methods

#### addReason

`addReason (reason, now)`

Adds a reason to the reason array

##### addReason Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `reason` | `string` | explaining , why the topic will be set to value | |
| `now` | `Date, undefined` | current time . If not provided , it will be calculated . usually only passed for debugging reasons . | |

#### getDateOfNewestChange

`getDateOfNewestChange () => {Date}`

Gets the latest Date of the message by browsing the reasons

##### getDateOfNewestChange returns

| Type | Description |
| ---- | ----------- |
| `Date` |  |

#### isOn

`readonly isOn ()`

returns true , if the value is "on" ( 1 , 'on' , 'true' , true )

#### toJSON

`toJSON ()`

ensures that the JSON format is well formatted ( value instead of _value )

#### validate

`static validate (object)`

Checks the consistence of a message object

##### validate Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `object` | `Object` | object to check or this | |

##### validate throws

| Type | Description |
| ---- | ----------- |
| `error` |  |

---
_Source: https://npm.io/package/@mangar2/message · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
