hurricane/arducam/resources/src/js/index.js

var VideoStream = function() {
var context = {}
/** -----------------------------
* Private variables
*/
var ws_api = GosWsDataApi()
var logger = Logger('[APP] ')
/** -----------------------------
* Public variables
*/
context.onConnected = null
context.onDisconnected = null
context.onSettingsUpdated = null
context.onImageAvailable = null
/** -----------------------------
* Public methods
*/
/** -----------------------------
* Start the stream connection
*/
context.start = function(host) {
ws_api.open(host)
}
/** -----------------------------
* Stop the stream connection
*/
context.stop = function() {
ws_api.close()
}
/** -----------------------------
* Update the device's camera settings.
* See settings_stream_handler() in the app.
*/
context.updateSettings = function(settings, handler) {
ws_api.write('settings', settings, handler, 5000)
}
/** -----------------------------
* Get the device's current camera settings.
* See settings_stream_handler() in the app.
*/
context.getSettings = function() {
var responseHandler = function(result, data) {
if(!result) {
logger.error('Failed to read settings, err: ' + data)
} else if(context.onSettingsUpdated) {
context.onSettingsUpdated(data)
}
}
ws_api.read('settings', responseHandler)
}
/** -----------------------------
* Return if the stream is connected
*/
context.isConnected = function() {
return ws_api.isConnected()
}
/** -----------------------------
* Return if the stream is attempting to connect
*/
context.isConnecting = function() {
return ws_api.isConnecting()
}
/** -----------------------------
* Private methods
*/
/** -----------------------------
* Handler called when client connects to device
*/
ws_api.onConnected = function() {
logger.info('Connected')
// Retrieve the current camera settings from the device
context.getSettings()
if(context.onConnected) {
context.onConnected()
}
}
/** -----------------------------
* Handler called when client disconnects from device
*/
ws_api.onDisconnected = function() {
logger.info('Disconnected')
if(context.onDisconnected) {
context.onDisconnected()
}
}
/** -----------------------------
* This is called when the device writes image data to this app.
* See: arducam_image_ready_callback() and arducam_data_writer_callback() in the example app.
*/
var imageStreamHandler = function(stream, action, data) {
if(context.onImageAvailable) {
context.onImageAvailable(data)
}
}
/** -----------------------------
* This is called when the device writes 'settings' stream to this app.
* See: broadcast_settings_to_all_clients() in the example app.
*/
var settingsStreamHandler = function(stream, action, data) {
if(context.onSettingsUpdated) {
logger.info('New settings: ' + JSON.stringify(data))
context.onSettingsUpdated(data)
}
}
ws_api.setLogger(console)
ws_api.setMsgpackLogger(console)
logger.info('Registering stream handlers ...')
ws_api.register('image', imageStreamHandler)
ws_api.register('settings', settingsStreamHandler)
return context
}