@wethecurious/curios-reduxmiddleware
v0.0.8
Published
A redux middleware for communication with Curios services
Downloads
47
Readme
CuriOs redux middleware
This collection of middlewares is designed to provide a redux friendly connection to We The Curious's CuriOs services for exhibit's implementing the redux pattern. The middleware handles connection to and data retrival from:
- mqtt iot devices
- phidget physical input devices
- exhibit analytics service
Usage
The createCuriosMiddleware export both a middleware and wrapped version of the redux combineReducer function which provides the '@@Curios' reducer to track connection state in the redux store
Changes
v0.0.8
- Added options to useCurios hook to optionally attempt to parse bus data as json
v0.0.6
Changed phidget actions to correctly handle disconnect and reconnection of devices
TODO: make sure the reducer updates to reflect both channel and connection status
Config
{
app: 'example', //app name as supplied by wtc
appInstance: 0, //instance of the app, used when more than one app connects to the same broker
service: 'test', //service name
serviceInstance: 0 //instance of the service, used in there is more than one instance of the service in the app
serviceVersion: '0.1', //version of the service
env: 'production', //initialise the middleware in development or production mode
mqtt: { // mqtt broker connection details
host: 'localhost',
port: 3000,
},
phidget: [ //phidget details, mulitple phidgets may be connected by providing and array on connection details
{
host: 'localhost',
port: 5166,
isMqttMock: false //Optional: If a phidget is not avaliable use mqtt as a mock, in this case it will subscribe to mock/phidget/{name}
devices: [{
type: 'ENCODER'
name: 'encoder1'
channel: 0
}] //an array of device connected to the VINT ports of the phidget. Only 'ENCODER' and 'VOLTAGE RATIO ARE currently avliable.
}
]
}
Create the Functions and the Store
const [useCurios, middleware, combineReducers] = createCuriosMiddleware(config)
const store = createStore(
combineReducers({...otherReducers}),
applyMiddleware(...middleware)
)
Initalise the middleware
store.dispatch({type: '@@CURIOS_INIT'})
Reset the session
Once an app has been initialised a new session should be created everytime the application resets to attract mode
store.dispatch({type: '@@CURIOS_SESSION'})
To get the data from the mqtt or phidget bus
The useCurios hook can be used inside a component to get data coming on the bus.
const busData = useCurios()
You can select a component to only update on new data by using a selector function, eg:
const busData = useCurios(state => state[selectedDevice])
You can optionally pass options
const options = {
parseAsJson: true
}
const busData = useCurios(state => state[selectedDevice], options)
To mock phidget info with mqtt
If a phidget is not avaliable you can use mqtt as a mock by setting isMqttMock: true
in the phidget device config. In this case it will subscribe to mock/phidget/{name}
Coming next
[x] ~~Better selection of params from useCurios~~
[x] ~~More control over phidget devices and ports. Currently it is not easy to choose which device is running on which VINT port~~
[x] ~~MQTT mock system for when phidget hardware is not avaliable~~
AWS IoT intergration for analytics
Less pollution of redux dispatch list when mqtt can't connect
Support for more phidget devices