Crestron Components Lib - Showcase App

Utility function subscribeState

Emulator Scenario (click to expand) (click to collapse)
{
  "cues": [
    {
      "type": "b",
      "event": "trig1",
      "trigger": true,
      "actions": [
        {
          "state": "city",
          "type": "string",
          "logic": "set",
          "value": "Las Vegas"
        },
        {
          "state": "sig_n",
          "type": "n",
          "logic": "decrement",
          "offset": 3
        },
        {
          "state": "sig_b",
          "type": "b",
          "logic": "toggle"
        }
      ]
    },
    {
      "type": "b",
      "event": "trig2",
      "trigger": true,
      "actions": [
        {
          "state": "city",
          "type": "string",
          "logic": "set",
          "value": "New York"
        },
        {
          "state": "sig_n",
          "type": "n",
          "logic": "increment",
          "offset": 5
        },
        {
          "state": "sig_b",
          "type": "b",
          "logic": "set",
          "value": true
        }
      ]
    }
  ],
  "onStart": [
    {
      "type": "s",
      "state": "city",
      "value": "Hong Kong"
    }
  ]
}
Emulator actions

Press the Load button to load the scenario. This will also reload all of the existing examples on the page.

Press the Run button to run the scenario. You only need to do this if the scenario (the json) has an onStart key.

Events and states

The Emulator Scenario JSON contains an array of cues and an optional array onStart.

→ A cue is defined by: type:string, event:string, trigger:boolean|string|number, actions:array
→ An action is defined by: type:string, state:string, logic:string ... plus other fields ( depending on type)

The value of the state or event for a cue or for an action is a string which represents the name of that state or action.

Type field

The type for cue or for action can be the string:

  • b or boolean - for a boolean state or event
  • n or number or numeric - for a number state or event
  • s or string - for a string state or event
  • o or object - for an object state or event
Trigger field

The trigger can be either a fixed value of the same type as the state or event, or the string "&change". If it is a fixed value then, when the emulator detects that the state or event has changed to the value given in trigger, it also executes the actions for that state or event. If the value is the string is "&change", regardless of state or event type, the actions are executed on any change in the value of the state or event.

Logic Field

The logic can be:

  • link - passes the value received on the incoming join through to the outgoing join. Using this between incompatible join types will attempt to cast the value. In the event of a cast failure, the value will be set to false, 0 or "" (empty string).
  • set - assigns a specific value to the join. This requires a 5th key of 'value' and the specific value to assign. ie. {"state":"light_level", "type":"n", "logic": "set","value":5}. If the 5th key is omitted, the default value will be sent (false, 0, "" or {}).
  • toggle - (boolean Only) reads the current state and changes it to the opposite state.
  • pulse - (boolean Only) value goes high and then low.
  • increment - (Numeric Only) reads the current value of the analog and adds an offset. This logic accepts an optional 5th key of 'offset which determines how many times to increment by. ie. {"event":"volume_level","type":"n", "logic":"increment","offset":5}. If this 5th key is omitted, the default offset of 1 will be applied.
  • decrement - (Numeric Only) reads the current value of the analog and subtracts an offset. This logic accepts an optional 5th key of 'offset which determines how many times to decrement by. ie. {"event":"volume_level","type":"n", "logic":"decrement","offset":5}. If this 5th key is omitted, the default offset of 1 will be applied.
  • RCB logic can be accomplished by defining an action containing an object state or event of RCB type. A 5th key is expected as the amount of time in milliseconds to reach the level. {"signal":"light_level", "type":"n", "logic": "rcb","value":65535, "time":2000}
    { "rcb":{ "value": numeric_value, "time": duration_in_ms } }
Scenario example:

    {
      "cues": [
        {
          "type": "boolean",
          "event": "trigger",
          "trigger": true,
          "actions": [
            {
              "state": "action1",
              "type": "boolean",
              "logic": "toggle"
            },
            {
                "state": "rcb_signal",
                "type": "object",
                "logic": "set",
                "value": {
                    "rcb":{
                        "value": 101,
                        "time": 1500
                    }
                }
            }
          ]
        }
      ],
      "onStart": [
      ]
    }
    

Utility function: subscribeState

subscribeState(signalType:string, signalName:string, callback:Function, errCalback:Function):string

signalType - a string with the type of the signal valid values: "boolean" | "b" | "number" | "numeric" | "n" | "string" | "s" | "object" | "o"

signalName - a string with the name of the signal

callback - the callback function that will be called on the signal change. Receives one parameter, which is the new value of the signal.

errCallback - the callback function that will be called on error.

Returns a string - the subscription id

See the JS tab for the code and browser console for messages

Click this button to change the value of the city signal to "Las Vegas", will decrement sig_n by 3 and will toggle sig_b.

Click this button to change the value of the city signal to "New York", will increment sig_n by 5 and will set sig_b to true.

The following table will display the values of the signals and update them as they change. The update is done using the subscribeState utility function ( see the JS tab).

signal value
sig_b
sig_n
city

The errCallback is called if something goes wrong, for example if the signalType provided to the subscribeState function is invalid:

<p>See the JS tab for the code and browser console for messages</p>

<p>
    Click this button to change the value of the <b>city</b> signal to "Las Vegas", will decrement <b>sig_n</b> by 3 and
    will toggle <b>sig_b</b>.
    <ch5-button label="Btn1" sendEventOnClick="trig1"></ch5-button>
</p>
<p>
    Click this button to change the value of the <b>city</b> signal to "New York", will increment <b>sig_n</b> by 5 and
    will set <b>sig_b</b> to true.
    <ch5-button label="Btn2" sendEventOnClick="trig2"></ch5-button>
</p>

<p>
    The following table will display the values of the signals and update them as they change. The update is done using
    the subscribeState utility function ( see the JS tab).
</p>
<table class="table">
    <tr>
        <th>signal</th>
        <th>value</th>
    </tr>
    <tr>
        <td>sig_b</td>
        <td id="s1"></td>
    </tr>
    <tr>
        <td>sig_n</td>
        <td id="s2"></td>
    </tr>
    <tr>
        <td>city</td>
        <td id="s3"></td>
    </tr>
</table>

<p>The <b>errCallback</b> is called if something goes wrong, for example if the <i>signalType</i> provided to the <i>subscribeState</i>
    function
    is invalid:</p>
<p id="errCallback-example"></p>