State variables

The State rig manages "state variables". Each state variable has a unique descriptor, like:

    local:kitchen:moved
    local:living:front:temp
    reading:RF12-868.5.5:roomNode:temp
    switch:basement:back:left:button2

State variables have values which represent the state of, well... something. They can track information from sensors, as well as control the position of an actuator or the information shown on a display. Values can be anything - although integers are most common (and most efficient).

State variables reflect the known (or sometimes: desired) state of the house. The descriptor nesting with colons offers a way to organize lots of state variables. For a JeeNode with a Room Board, for example, there will usually be 5 state variables:

    reading:RF12-868.5.5:roomNode:temp
    reading:RF12-868.5.5:roomNode:humi
    reading:RF12-868.5.5:roomNode:light
    reading:RF12-868.5.5:roomNode:moved
    reading:RF12-868.5.5:roomNode:lobat

These variables can be set anywhere in a JeeMon application, and other parts of the code can subscribe to changes to one or more of these variables. Here is a way to pick up changes in any of the motion sensors:

    proc MyTrigger {name} {
      set value [State get $name]
      Log motion {> $name = $value}
    }

    State subscribe *:moved [namespace which MyTrigger]

The "State get ..." call returns the current value of a state variable. The "namespace which ..." bit is boilerplate to save the namespace context of the MyTrigger proc (with rigs, each source file has its own distinct namespace).

If you were to run this code, and assuming sensor data is coming in, you'd see lines like this in the log output:

   17:23:05.813   motion > reading:RF12-868.5.5:roomNode:moved = 1
   17:23:05.944   motion > reading:RF12-868.5.12:roomNode:moved = 1
   17:23:07.702   motion > reading:RF12-868.5.12:roomNode:moved = 0
   17:23:09.283   motion > reading:RF12-868.5.5:roomNode:moved = 0

Each state variable actually keeps track of a bit more information. Here's the complete list:

  • its current value (v)
  • its old value (o)
  • the last time its (current) value was set (t)
  • the previous time its value was set (p)
  • the last time it had its old value (m)

State variables are periodically saved to a text file called "stored/state.map". This file can be read by external programs, and lets JeeMon resume with the last-known values across restarts.

State variables act as the central nervous system of a JeeMon-/JeeRev-based application. This event-/trigger-based mechanism is what makes the system responsive and real-time.

For a description of all sub-commands available in the State rig, run "jeemon doc State".