Class BabelAccelerometerLCDDemo
- All Implemented Interfaces:
BabelDemo
Two devices, two protocols:
- the accelerometer is read through
I2CInputControlProtocol(id 2100), which here answers point-in-timeGetAccelerometerDataRequests with anAccelerometerInputReply; - the LCD is driven through
I2COutputControlProtocol(id 2000) viaShowTextRequest.
RegisterIoTDeviceRequest carries no GPIO
line — just a device type and an alias.
The recurring IoT pattern this demo teaches is the request/reply
polling flow: the app periodically asks the input protocol for a fresh
measurement and renders the reply, rather than waiting on a pushed
notification. (For the push/notification style instead, see the gesture
demos, which subscribe to GestureNotification.)
The application never touches Pi4J or the I²C bus directly: it only sends Babel requests and consumes Babel replies; the two control protocols do all of the hardware work.
Run with the Accel command-line name (see Main).
-
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
ConstructorsConstructorDescriptionWires up the protocol identity (name + id) that Babel uses to route events to this protocol. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Application bootstrap (the entry pointMaincalls for theAcceldemo).voidhandleAccelerometerInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.AccelerometerInputReply rep, short protocolId) Reply handler that completes the polling flow: eachGetAccelerometerDataRequestsent inhandleDemoTimer(pt.unl.fct.di.tardis.babel.iot.demos.events.DemoTimer, long)comes back here as anAccelerometerInputReply.voidhandleDemoTimer(DemoTimer t, long time) Periodic timer handler: polls the accelerometer for one reading mode per tick, round-robin.voidhandleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceRequest: receives theDeviceHandlefor whichever device the control protocol just bound.voidinit(Properties props) Babel lifecycle hook: register handlers, then request the two devices.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
-
BabelAccelerometerLCDDemo
public BabelAccelerometerLCDDemo()Wires up the protocol identity (name + id) that Babel uses to route events to this protocol. Hardware registration happens later, ininit(java.util.Properties).
-
-
Method Details
-
init
public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException Babel lifecycle hook: register handlers, then request the two devices.This is the canonical IoT bootstrap. Handlers MUST be registered before the requests that will eventually trigger them, otherwise the reply could arrive with no handler installed:
- register the timer handler that will poll the accelerometer;
- register the
RegisterIoTDeviceReplyhandler that brings back theDeviceHandlefor each device; - register the
AccelerometerInputReplyhandler that carries the measurements; - only then send the two
RegisterIoTDeviceRequests, each addressed to the control protocol that owns that device.
handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply, short).- Specified by:
initin classpt.unl.fct.di.novasys.babel.core.GenericProtocol- Throws:
pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationExceptionIOException
-
handleDemoTimer
Periodic timer handler: polls the accelerometer for one reading mode per tick, round-robin. This is the request half of the request/reply polling flow — eachGetAccelerometerDataRequestis answered asynchronously byhandleAccelerometerInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.AccelerometerInputReply, short). -
handleRegisterIoTDeviceReply
public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceRequest: receives theDeviceHandlefor whichever device the control protocol just bound.Because both the LCD and the accelerometer share this one reply id, the handler demultiplexes on
RegisterIoTDeviceReply.getDeviceType()and stashes each handle in the matching field. The polling timer is only armed once the accelerometer (the device the timer reads) is ready. -
handleAccelerometerInputReply
public void handleAccelerometerInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.AccelerometerInputReply rep, short protocolId) Reply handler that completes the polling flow: eachGetAccelerometerDataRequestsent inhandleDemoTimer(pt.unl.fct.di.tardis.babel.iot.demos.events.DemoTimer, long)comes back here as anAccelerometerInputReply. The reply's measurement type matches the requested mode, so we cast accordingly, log it, and forward a rendered string to the LCD with aShowTextRequest. This is the read-then-display half of the input/output pairing. -
execute
Application bootstrap (the entry pointMaincalls for theAcceldemo). Standard Babel start-up sequence:- grab the
Babelsingleton and load configuration; - instantiate the two control protocols this demo depends on — the I²C output protocol (LCD) and the I²C input protocol (accelerometer);
- register all three protocols (the two controllers plus this demo) with Babel;
- call
initin dependency order — controllers before the demo, so they are ready to answer the demo's registration requests; b.start()hands control to the Babel event loop.
- grab the
-