Class BabelAnyGestureMatrixDemo

java.lang.Object
pt.unl.fct.di.novasys.babel.core.GenericProtocol
pt.unl.fct.di.tardis.babel.iot.demos.BabelAnyGestureMatrixDemo
All Implemented Interfaces:
BabelDemo

public class BabelAnyGestureMatrixDemo extends pt.unl.fct.di.novasys.babel.core.GenericProtocol implements BabelDemo
Reactive Babel demo: shows an emoji on the Grove RGB LED matrix for any gesture the detector reports.

This is a textbook example of reactive input driving output: one input device asynchronously notifies the application, which then commands an output device. It wires together two of the IoT control protocols:

  • I2CInputControlProtocol (id 2100) reads the Grove gesture detector and, once asked for "reactive" gestures, emits a GestureNotification every time a hand movement is recognised.
  • I2COutputControlProtocol (id 2000) drives the Grove RGB LED matrix; the demo sends it a ShowEmojiRequest carrying the matrix's DeviceHandle whenever a gesture arrives.

The demo itself is a GenericProtocol: it never touches Pi4J, I²C or any GPIO line directly. It only exchanges Babel requests/replies/notifications with the control protocols, which own the hardware. This is the same "Pattern 1: driving a Grove device" flow documented in the module README.md, plus a notification subscription for the reactive input.

Run it with the command-line name anyGesture (see Main.java):

java -jar babel-raspberry-iot-examples.jar anyGesture

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    pt.unl.fct.di.novasys.babel.core.GenericProtocol.ProtocolMetricsBabelMetrics
  • Field Summary

    Fields inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    babel, babelSecurity

    Fields inherited from interface pt.unl.fct.di.tardis.babel.iot.demos.BabelDemo

    PROTO_ID, PROTO_NAME
  • Constructor Summary

    Constructors
    Constructor
    Description
    Builds the demo protocol.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Application entry point (invoked by Main for the anyGesture demo).
    void
    handleDemoTimer(DemoTimer t, long time)
    Timer handler driven by the periodic DemoTimer armed above.
    void
    handleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId)
    Reply handler for the GetReactiveGestureRequest we sent to arm the detector.
    void
    handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
    Reply handler for RegisterIoTDeviceReply.
    void
    Babel lifecycle hook: register every handler this protocol needs, then kick off the work.

    Methods inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    addSecret, addSecret, addSecret, addSecret, cancelTimer, closeConnection, closeConnection, closeConnection, closeConnection, closeConnection, closeConnection, createChannel, createSecureChannel, createSecureChannel, createSecureChannel, createSecureChannelWithAliases, createSecureChannelWithAliases, createSecureChannelWithIdentities, createSecureChannelWithIdentities, createSecureChannelWithProtoIdentities, enableGenericMetrics, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateSecret, generateSecret, generateSecret, generateSecret, generateSecretFromPassword, generateSecretFromPassword, generateSecretFromPassword, generateSecretFromPassword, getChannelOrThrow, getDefaultChannel, getDefaultProtoIdentity, getDefaultProtoIdentityCrypt, getDefaultProtoSecret, getMillisSinceBabelStart, getOrGenerateDefaultProtoIdentity, getProtoId, getProtoName, hasProtocolThreadStarted, openConnection, openConnection, openConnection, openConnection, registerChannelEventHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageSerializer, registerMetric, registerReplyHandler, registerRequestHandler, registerSharedChannel, registerTimerHandler, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendReply, sendRequest, setDefaultChannel, setDefaultProtoIdentity, setDefaultProtoIdentity, setDefaultProtoSecret, setupPeriodicTimer, setupTimer, startEventThread, subscribeNotification, triggerNotification, unsubscribeNotification

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BabelAnyGestureMatrixDemo

      public BabelAnyGestureMatrixDemo()
      Builds the demo protocol. Like every Babel GenericProtocol, it must declare a protocol name and a globally unique protocol id (here the shared BabelDemo constants); Babel uses the id to route events to this instance.
  • Method Details

    • init

      public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException
      Babel lifecycle hook: register every handler this protocol needs, then kick off the work. The golden rule is to register handlers BEFORE sending the request that triggers them, otherwise the reply/notification could arrive before there is anyone to receive it.
      Specified by:
      init in class pt.unl.fct.di.novasys.babel.core.GenericProtocol
      Throws:
      pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException
      IOException
    • handleRegisterIoTDeviceReply

      public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
      Reply handler for RegisterIoTDeviceReply. Babel calls it once for each RegisterIoTDeviceRequest we sent — here twice, one per device. The canonical pattern is: check RegisterIoTDeviceReply.isSuccessful(), and on success keep the DeviceHandle. Because both registrations share this one handler, we branch on the reply's device type to store the right handle.
    • handleGestureInputReply

      public void handleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId)
      Reply handler for the GetReactiveGestureRequest we sent to arm the detector. It confirms the input protocol accepted the reactive subscription; on success the demo paints the matrix blue as a visible "ready" acknowledgement. The actual per-gesture reaction happens later in handleGestureNotification(pt.unl.fct.di.tardis.babel.iot.controlprotocols.notifications.GestureNotification, short).
    • handleDemoTimer

      public void handleDemoTimer(DemoTimer t, long time)
      Timer handler driven by the periodic DemoTimer armed above. Each tick repaints the matrix green, giving the display a steady "idle" colour in between gesture-triggered emojis. Shows how a Babel protocol mixes timer-driven and event-driven work in the same single-threaded handler model.
    • execute

      public void execute() throws Exception
      Application entry point (invoked by Main for the anyGesture demo). It performs the standard Babel bootstrap:
      1. get the singleton Babel instance;
      2. load configuration from paradigmshift.config;
      3. instantiate the control protocols this demo depends on (I²C output 2000 + I²C input 2100) and the demo protocol itself;
      4. registerProtocol each one with Babel;
      5. init them in dependency order — the control protocols first, then the demo, so the targets of the demo's first requests already exist;
      6. start Babel, which spins up the event loops and begins delivering events to the registered handlers.
      Note the demo wires hardware access entirely through these protocols: it never constructs a Pi4J context or opens an I²C bus itself.
      Specified by:
      execute in interface BabelDemo
      Throws:
      Exception