Nunjucks Powers Mozilla

    @mangar2/rules

    2.1.0 • Public • Published

    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

    Rule {Object}

    Name Type Description
    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

    _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 Members

    Name Type description
    invalidRules @type Get the invalid rules list
    rules @type Get the valid rules list

    Rules Methods

    invalidateRule

    invalidateRule (name, messages)

    Invalidates a rule , because it generates an error

    invalidateRule Parameters
    Name Type Attribute Description
    name string name of the rule to invalidate
    messages Array.<string> optional error messages

    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

    Install

    npm i @mangar2/rules

    DownloadsWeekly Downloads

    4

    Version

    2.1.0

    License

    LGPL-3.0-or-later

    Unpacked Size

    1.17 MB

    Total Files

    55

    Last publish

    Collaborators

    • mangar2