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(id2100) reads the Grove gesture detector and, once asked for "reactive" gestures, emits aGestureNotificationevery time a hand movement is recognised.I2COutputControlProtocol(id2000) drives the Grove RGB LED matrix; the demo sends it aShowEmojiRequestcarrying the matrix'sDeviceHandlewhenever 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
-
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, babelSecurityFields inherited from interface pt.unl.fct.di.tardis.babel.iot.demos.BabelDemo
PROTO_ID, PROTO_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Application entry point (invoked byMainfor theanyGesturedemo).voidhandleDemoTimer(DemoTimer t, long time) Timer handler driven by the periodicDemoTimerarmed above.voidhandleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId) Reply handler for theGetReactiveGestureRequestwe sent to arm the detector.voidhandleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceReply.voidinit(Properties props) 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
-
Constructor Details
-
BabelAnyGestureMatrixDemo
public BabelAnyGestureMatrixDemo()Builds the demo protocol. Like every BabelGenericProtocol, it must declare a protocol name and a globally unique protocol id (here the sharedBabelDemoconstants); 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:
initin classpt.unl.fct.di.novasys.babel.core.GenericProtocol- Throws:
pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationExceptionIOException
-
handleRegisterIoTDeviceReply
public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceReply. Babel calls it once for eachRegisterIoTDeviceRequestwe sent — here twice, one per device. The canonical pattern is: checkRegisterIoTDeviceReply.isSuccessful(), and on success keep theDeviceHandle. 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 theGetReactiveGestureRequestwe 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 inhandleGestureNotification(pt.unl.fct.di.tardis.babel.iot.controlprotocols.notifications.GestureNotification, short). -
handleDemoTimer
Timer handler driven by the periodicDemoTimerarmed 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
Application entry point (invoked byMainfor theanyGesturedemo). It performs the standard Babel bootstrap:- get the singleton
Babelinstance; - load configuration from
paradigmshift.config; - instantiate the control protocols this demo depends on (I²C output 2000 + I²C input 2100) and the demo protocol itself;
registerProtocoleach one with Babel;initthem in dependency order — the control protocols first, then the demo, so the targets of the demo's first requests already exist;startBabel, which spins up the event loops and begins delivering events to the registered handlers.
- get the singleton
-