3.0.0 • Published 1 year ago

@mangar2/rules v3.0.0

Weekly downloads
10
License
LGPL-3.0-or-later
Repository
github
Last release
1 year ago

Abstract

The rule history stores the history for each rule and each topic in the rule For all rulename 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}

NameTypeDescription
firstFoundDatetime the message was found/generated first
lastSentDatetime the massage was sent last
valuestring, numbervalue of the message

Decision {Object}

NameTypeDescription
valuebooleanboolean value of the decision
reasonstringexplanation of the decision

Rule {Object}

NameTypeDescription
namestringname of the rule
timeobject, stringtime , when the rule will be activated
doLogintegertrue , 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

NameTypeDescription
timestampnumbertimestamp in milliseconds

Class ProcessRule

new ProcessRule(date, longitude, latitude)

Processes rules based on a set of variables

ProcessRule Parameters

NameTypeAttributeDefaultDescription
dateDateoptionalnowcurrent date
longitudenumberoptional51 . 476852geographical longitued poistion of the automation target in degrees
latitudenumberoptional-0 . 000500geographical latitued poistion of the automation target in degrees

ProcessRule Members

NameTypedescription
date@typeGets the current date/time
date@typeSets the current date/time
variables@typeSets all variables

ProcessRule Methods

_checkWeekday

_checkWeekday (rule, weekdays) => {}

Check , if the days of the week of the rule matches the current weekday

_checkWeekday Parameters
NameTypeDescription
ruleObjectrule to process
weekdaysstring, arraydays in the week to check
_checkWeekday returns
TypeDescription
| 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
NameTypeDescription
topicstringtopic of the message
valuePropertystring, number, objctrule value property
reasonstringreason so far
qos0, 1, 2Quality of service
_createMessage returns
TypeDescription
Messagemessage

_deriveAllMessagesFromRule

_deriveAllMessagesFromRule (rule, motionEvents, nonMotionEvents) => {Object.<messages:Message[], usedVariables>}

Derives all messages from the rule

_deriveAllMessagesFromRule Parameters
NameTypeAttributeDescription
ruleObjectrule to check
motionEventsObjectoptionalmap { topic : timestamp } list of events classified as motions
nonMotionEventsObjectoptional{ topic : true } list of events not classified as motions
_deriveAllMessagesFromRule throws
TypeDescription
Erroron error in the decision rule
_deriveAllMessagesFromRule returns
TypeDescription
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
NameTypeAttributeDescription
ruleObjectrule to check
motionEventsObjectoptionalmap { topic : timestamp } list of events classified as motions
nonMotionEventsObjectoptional{ topic : true } list of events not classified as motions
check throws
TypeDescription
Erroron error in the decision rule
check returns
TypeDescription
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
NameTypeDescription
ruleObjectrule to check
determineNeededVariables returns
TypeDescription
Object{ name : value } list of needed variables including current value

setVariable

setVariable (name, value)

Sets a single variable

setVariable Parameters
NameTypeDescription
namestringname 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

NameTypeDescription
rulesTreeObjectrules tree
checkCheckInputdefinition to check the validity of a rule

Rules Methods

_addToNodeRec

_addToNodeRec (node, nameChunks, rule)

Adds a rule to a tree node recursively

_addToNodeRec Parameters
NameTypeDescription
nodeObjectcurrent tree node
nameChunksArray.<string>remaining chunks of the rule name
ruleRulerule

_invalidateRule

_invalidateRule (name, messages, validFlag)

Invalidates a rule , because it generates an error

_invalidateRule Parameters
NameTypeDescription
namestringname of the rule to invalidate
messagesArray.<string>error messages
validFlagbooleanfalse 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
NameTypeDescription
processRuleProcessRulealgirithm to check the rules
checkRules returns
TypeDescription
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
NameTypeDescription
namestringname of the rule to delete

findRule

findRule (name) => {Rule, null}

Finds a rule in the rule list

findRule Parameters
NameTypeDescription
namestringname of the rule
findRule returns
TypeDescription
Rule, nullrule found or null

getInvalidRules

getInvalidRules () => {Array.<Rule>}

Get the invalid rules list

getInvalidRules returns
TypeDescription
Array.<Rule>array of invalid rules

getRuleTree

getRuleTree () => {}

Returns all rules in a tree of rules

getRuleTree returns
TypeDescription
| all rules organized in a tree based on the names

getRules

getRules () => {Array.<Rule>}

Get the valid rules list

getRules returns
TypeDescription
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
NameTypeDescription
ruleRuleto set
setRule returns
TypeDescription
| true , if the rule is valid

Class Variables

new Variables(date, longitude, latitude)

Manages a set of variables

Variables Parameters

NameTypeAttributeDefaultDescription
dateDateoptionalnowcurrent date
longitudenumberoptional51 . 476852geographical longitued poistion of the automation target in degrees
latitudenumberoptional-0 . 000500geographical 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
NameTypeDescription
namestringname of the variabl
valueanyvalue 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
TypeDescription
Object.<key:string, value:any>key/value map of all variables

getValue

getValue (name) => {any}

Returns the value of the variable

getValue Parameters
NameTypeDescription
namestringname of the variable
getValue returns
TypeDescription
anyvalue of the variable

isExternVariable

isExternVariable (name) => {boolean}

Returns true , if the variable is an external variable required from outside

isExternVariable Parameters
NameTypeDescription
namestringname of the variable
isExternVariable returns
TypeDescription
booleantrue , if the variable is external ( send to the service )

setValue

setValue (name, value)

Sets the value of an existing variable

setValue Parameters
NameTypeDescription
namestringname of the variable
valueanyvalue of the variable