@mangar2/rules
v3.0.0
Published
Extracts a list of rules from a configuration tree and stores it
Downloads
30
Readme
Abstract
The rule history stores the history for each rule and each topic in the rule For all [ rulename ] [ topic ] entries , it stores the
- value : The current value of the message created by the rule
- firstFound : The date the same value was first found
- lastSent : The date the message was last sent
Contents
Meta
Type definitions
HistoryEntry {Object}
| Name | Type | Description |
| ---- | ---- | ------- |
| firstFound
| Date
| time the message was found/generated first | |
| lastSent
| Date
| time the massage was sent last | |
| value
| string, number
| value of the message | |
Decision {Object}
| Name | Type | Description |
| ---- | ---- | ------- |
| value
| boolean
| boolean value of the decision | |
| reason
| string
| explanation of the decision | |
Rule {Object}
| Name | Type | Description |
| ---- | ---- | ------- |
| name
| string
| name of the rule | |
| time
| object, string
| time , when the rule will be activated | |
| doLog
| integer
| true , if the rule shall be logged | |
Global functions
timeInMillisecondsToLocalTimeString
timeInMillisecondsToLocalTimeString (timestamp)
Converts a timestamp to a string with the local time of day in brackets
timeInMillisecondsToLocalTimeString Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| timestamp
| number
| timestamp in milliseconds | |
Class ProcessRule
new ProcessRule(date, longitude, latitude)
Processes rules based on a set of variables
ProcessRule Parameters
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| date
| Date
| optional | now | current date | |
| longitude
| number
| optional | 51 . 476852 | geographical longitued poistion of the automation target in degrees | |
| latitude
| number
| optional | -0 . 000500 | geographical latitued poistion of the automation target in degrees | |
ProcessRule Members
| Name | Type | description |
| ------------ | ------------ | ------------ |
| date
| @type
| Gets the current date/time |
| date
| @type
| Sets the current date/time |
| variables
| @type
| Sets all variables |
ProcessRule Methods
_checkWeekday
_checkWeekday (rule, weekdays) => {}
Check , if the days of the week of the rule matches the current weekday
_checkWeekday Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| rule
| Object
| rule to process | |
| weekdays
| string, array
| days in the week to check | |
_checkWeekday returns
| Type | Description | | ---- | ----------- | | `` | true , if weekdays is not specified or the day of the week matches the expectation |
_createMessage
_createMessage (topic, valueProperty, reason, qos) => {Message}
Creates a message
_createMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| topic
| string
| topic of the message | |
| valueProperty
| string, number, objct
| rule value property | |
| reason
| string
| reason so far | |
| qos
| 0, 1, 2
| Quality of service | |
_createMessage returns
| Type | Description |
| ---- | ----------- |
| Message
| message |
_deriveAllMessagesFromRule
_deriveAllMessagesFromRule (rule, motionEvents, nonMotionEvents) => {Object.<messages:Message[], usedVariables>}
Derives all messages from the rule
_deriveAllMessagesFromRule Parameters
| Name | Type | Attribute | Description |
| ---------- | ------------ | ------------ | ----------------- |
| rule
| Object
| | rule to check | |
| motionEvents
| Object
| optional | map { topic : timestamp } list of events classified as motions | |
| nonMotionEvents
| Object
| optional | { topic : true } list of events not classified as motions | |
_deriveAllMessagesFromRule throws
| Type | Description |
| ---- | ----------- |
| Error
| on error in the decision rule |
_deriveAllMessagesFromRule returns
| Type | Description |
| ---- | ----------- |
| Object.<messages:Message[], usedVariables>
| Array of messages and object of variables used . |
check
check (rule, motionEvents, nonMotionEvents) => {Object.<messages:Message[], usedVariables>}
Processes a rule and if the rule demands , create a message
check Parameters
| Name | Type | Attribute | Description |
| ---------- | ------------ | ------------ | ----------------- |
| rule
| Object
| | rule to check | |
| motionEvents
| Object
| optional | map { topic : timestamp } list of events classified as motions | |
| nonMotionEvents
| Object
| optional | { topic : true } list of events not classified as motions | |
check throws
| Type | Description |
| ---- | ----------- |
| Error
| on error in the decision rule |
check returns
| Type | Description |
| ---- | ----------- |
| Object.<messages:Message[], usedVariables>
| Array of messages and object of variables used . |
determineNeededVariables
determineNeededVariables (rule) => {Object}
Gets a list of needed ( external ) variables for the rule
determineNeededVariables Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| rule
| Object
| rule to check | |
determineNeededVariables returns
| Type | Description |
| ---- | ----------- |
| Object
| { name : value } list of needed variables including current value |
setVariable
setVariable (name, value)
Sets a single variable
setVariable Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the variable to set | |
| value
| \*
| value of the variable to set | |
Class Rules
new Rules(rulesTree, check)
Creates an object holding automation rules
Example
const rulesTree =
{
location1: {
rules: {
rule1: {
title: 'hello'
},
rule2: {
title: 'world'
}
}
},
location2: {
rule1: {
error: 'faulty rule'
}
}
}
const checkRule = new CheckInput({ type: 'object', properties: { title: { type: string } }, required: ['title'] })
const rules = new Rules(rulesTree, checkRules)
console.log(rules.rules.length) // prints 1, we have one correct rule
console.log(rules.invalidRules[0].messages) // prints the error messages for rule 'location2'
Rules Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| rulesTree
| Object
| rules tree | |
| check
| CheckInput
| definition to check the validity of a rule | |
Rules Methods
_addToNodeRec
_addToNodeRec (node, nameChunks, rule)
Adds a rule to a tree node recursively
_addToNodeRec Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| node
| Object
| current tree node | |
| nameChunks
| Array.<string>
| remaining chunks of the rule name | |
| rule
| Rule
| rule | |
_invalidateRule
_invalidateRule (name, messages, validFlag)
Invalidates a rule , because it generates an error
_invalidateRule Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the rule to invalidate | |
| messages
| Array.<string>
| error messages | |
| validFlag
| boolean
| false to flag the rule as invalid to not process it again | |
checkRules
checkRules (processRule) => {Object.<variables:{[index:string]:string|number>Array.<, messages:Message>}
Checks all rules for errors , prints errors to console
checkRules Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| processRule
| ProcessRule
| algirithm to check the rules | |
checkRules returns
| Type | Description |
| ---- | ----------- |
| Object.<variables:{[index:string]:string|number>Array.<, messages:Message>
| } map of used variables ( variable name/variable value ) and error messages |
deleteRule
deleteRule (name)
Deletes a rule
deleteRule Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the rule to delete | |
findRule
findRule (name) => {Rule, null}
Finds a rule in the rule list
findRule Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the rule | |
findRule returns
| Type | Description |
| ---- | ----------- |
| Rule, null
| rule found or null |
getInvalidRules
getInvalidRules () => {Array.<Rule>}
Get the invalid rules list
getInvalidRules returns
| Type | Description |
| ---- | ----------- |
| Array.<Rule>
| array of invalid rules |
getRuleTree
getRuleTree () => {}
Returns all rules in a tree of rules
getRuleTree returns
| Type | Description | | ---- | ----------- | | `` | all rules organized in a tree based on the names |
getRules
getRules () => {Array.<Rule>}
Get the valid rules list
getRules returns
| Type | Description |
| ---- | ----------- |
| Array.<Rule>
| array of rules |
setRule
setRule (rule) => {}
Adds or updates a rule to the list of rules , checks its validity and sets a "isValid" property accordingly
setRule Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| rule
| Rule
| to set | |
setRule returns
| Type | Description | | ---- | ----------- | | `` | true , if the rule is valid |
Class Variables
new Variables(date, longitude, latitude)
Manages a set of variables
Variables Parameters
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| date
| Date
| optional | now | current date | |
| longitude
| number
| optional | 51 . 476852 | geographical longitued poistion of the automation target in degrees | |
| latitude
| number
| optional | -0 . 000500 | geographical latitued poistion of the automation target in degrees | |
Variables Methods
_addInternVariable
_addInternVariable (name, value)
Adds a variable ( coming from the service itself ) to the variable list
_addInternVariable Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the variabl | |
| value
| any
| value of the variable | |
calculateInternalVariables
calculateInternalVariables ()
Calculates all internal variables ( usually after a date change )
getAllVariables
getAllVariables () => {Object.<key:string, value:any>}
Gets a key/value map of all variables
getAllVariables returns
| Type | Description |
| ---- | ----------- |
| Object.<key:string, value:any>
| key/value map of all variables |
getValue
getValue (name) => {any}
Returns the value of the variable
getValue Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the variable | |
getValue returns
| Type | Description |
| ---- | ----------- |
| any
| value of the variable |
isExternVariable
isExternVariable (name) => {boolean}
Returns true , if the variable is an external variable required from outside
isExternVariable Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the variable | |
isExternVariable returns
| Type | Description |
| ---- | ----------- |
| boolean
| true , if the variable is external ( send to the service ) |
setValue
setValue (name, value)
Sets the value of an existing variable
setValue Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| name
| string
| name of the variable | |
| value
| any
| value of the variable | |