npm package: @jotiotech/node-red-status-parser
Description: Expect two inputs - `msg.payload` and `msg.statusSchema`. If `msg.statusSchema` is not provided, the node will just forward the input message. Payload is expected to contain `payload.outJson` which is an exit object from @jotiotech/node-red-bj-parser or a
Latest version: 0.0.4

# Status parser

## Input

Expect two inputs - `msg.payload` and `msg.statusSchema`. If `msg.statusSchema` is not provided, the node will just forward the input message. Payload is expected to contain `payload.outJson` which is an exit object from @jotiotech/node-red-bj-parser or at least follows convenctions defined by it.

## Output

Output is a copy of input message modified by the node.

# Schema format

```json
{
	"name" : "TEST-PATH",
		"version": "1.0.0",
		"schemaVersion" : "1.0.0",
		"maps" : [
		{
			"origin" : "path",
			"originPath" : {
				"path" : "network.port"
			},
			"mappings" : [
			{
				"binaryMask" : "1111'1111",
				"switch" : [
				{
					"code" : "I1",
					"description" : "No error"
				},
				{
					"code" : "C-1",
					"description" : "Flash memory or RAM error",
					"equals" : "0x08"
				},
				{
					"code" : "E-1",
					"description" : "Out of temperature range, short circuit or failure of temperature sensors",
					"equals" : "0x50"
				},
				{
					"code" : "E-3",
					"description" : "Temperature sensors swapped",
					"equals" : "0xB0"
				},
				{
					"code" : "E-4",
					"description" : "Hardware failure in ultrasonic section",
					"equals" : "0x28"
				},
				{
					"code" : "E-5",
					"description" : "Too frequent readings, M-Bus communication temporarily interrupted",
					"equals" : "0x10"
				},
				{
					"code" : "E-6",
					"description" : "Reverse flow direction",
					"equals" : "0xD0"
				},
				{
					"code" : "E-7",
					"description" : "Incorrect signal, measurement line airlock",
					"equals" : "0x70"
				},
				{
					"code" : "E-8",
					"description" : "Mains power failure",
					"equals" : "0x04"
				},
				{
					"code" : "E-9",
					"description" : "Battery almost empty",
					"equals" : "0x84"
				},
				{
					"code" : "E-L",
					"description" : "Leakage detected: pipe damage, device damage, impulse input 1 leakage",
					"equals" : "0xF0"
				}
				]
			}
			]
		}
	]
}
```

At the core of status parsing schema is a array named `maps`, which contains mapping information for status codes. Field `maps` is an array as there can be multiple sources of status codes in a single `message` (like if device reads data from multiple M-Bus meters). Each map object contains the following fields: `origin` and `mappings`. Field `origin` dictates from where status code is sourced and by extension where the output will be. Array `mappings` contains objects that define how to parse status code. Each mapping object contains the following fields: `binaryMask`, `switch` and `default`.

When processing status we first apply binary mask to the status code and than match it against cases outlined in the `switch` array. If no match is found, the `default` object is used. If not default is provided no entry into `quantities` array is added.

There is also special mapping used as a fallback if no other mapping matches. This mapping doesn't have `binaryMask` defined and has just a single case defined in `fallback` structure.
```json
{
	"fallback" : {
		"code" : "I1",
			"description" : "No error"
	}
}
```



### Available origin types

- `path` - absolute path to status code is provided by the user
* `originPath` - object containing path to status code
* `path` - absolute path to status code
* in case `path` option is used the parsed statys code will be placed into `deviceInfo.quantities` array
- `mdu` - code searched `mduList` for corresponding mdu field
* `originMDU` - object containing information about mdu field
* `mduType` - string that must match `mduType` field in `mduList`
*	`mduTypeDesc` - string that must match `mduTypeDesc` field in `mduList`
* in case `mdu` option is used the parsed status code will be placed into `deviceInfo.quantities` array
- `mbus` - code will search `mduList` for `mduType==mbus` fields and parse status code from them
* by `mduType==mbus` field is meant that the field is an output of `parseMBUS` from @jotiotech/node-red-bj-parser
* as there can be multiple `mduType==mbus` fields in `mduList` the output is placed into mbus field, not on higher level like in previous cases
* `originMBUS` - object containing information about where to look for status code
* `manId` - string that must match `manId` field in `mduList`
* `deviceType` - string that must match `deviceType` field in `mduList`
* `statusLocation` - picks between using standard M-Bus status byte or using some manufacturer specific field
* `statusByte` - if defined will use standard M-Bus status byte
* `custom` - contains information about custom field
* `dif` - array of numbers that must match `dif` field in `val.data`
* `vif` - array of numbers that must match `vif` field in `val.data`

#### PATH
```json
"origin" : "path",
	"originPath" : {
		"path" : "network.port"
	},
	```

#### MDU
	```json
	"origin" : "mdu",
	"originMDU" : {
		"mduType" : "sensor",
		"mduTypeDesc" : "pulse Ch1"
	},
	```

#### MBUS
	```json
	"origin" : "mbus",
	"originMBUS" : {
		"manId" : "TIP",
		"deviceType" : "Electricity meter",
		"statusLocation" : {
			"statusByte" : true,
			"custom" : {}
		}
	},

	"origin" : "mbus",
	"originMBUS" : {
		"manId" : "TIP",
		"deviceType" : "Electricity meter",
		"statusLocation" : {
			"custom" : {
				"dif" : [1],
				"vif" : [255, 97]
			}
		}
	}
```