demo/3d_demo/resources/src/js/index.js

var AccelerometerStream = function() {
var context = {}
/** -----------------------------
* Private variables
*/
var ws_api = GosWsDataApi()
var model_processor = ModelProcessor()
var logger = Logger('[APP] ')
var tempTimer = null
/** -----------------------------
* Public variables
*/
context.onConnected = null
context.onDisconnected = null
context.onTemperatureAvailable = null
context.onIntervalUpdated = null
context.temperatureUpdateInterval = 3000
/** -----------------------------
* Public methods
*/
/** -----------------------------
* Start the stream connection
*/
context.start = function(host) {
ws_api.open(host)
}
/** -----------------------------
* Stop the stream connection
*/
context.stop = function() {
ws_api.close()
}
/** -----------------------------
* Send a message to the device.
* See message_stream_handler() in the app.
*/
context.sendMessage = function(msg, handler) {
var request = {
value: msg
}
ws_api.write('msg', request, handler, 5000)
}
/** -----------------------------
* Update the device's update interval.
* See interval_stream_handler() in the app.
*/
context.setUpdateInterval = function(interval, handler) {
var request = {
value: interval
}
ws_api.write('interval', request, handler, 5000)
}
/** -----------------------------
* Get the device's current update interval.
* See interval_stream_handler() in the app.
*/
context.getUpdateInterval = function() {
var responseHandler = function(result, data) {
if(!result) {
logger.error('Failed to read interval, err: ' + data)
} else if(context.onIntervalUpdated) {
context.onIntervalUpdated(data.value)
}
}
ws_api.read('interval', responseHandler)
}
/** -----------------------------
* Return if the stream is connected
*/
context.isConnected = function() {
return ws_api.isConnected()
}
/** -----------------------------
* Private methods
*/
/** -----------------------------
* Handler called when client connects to device
*/
ws_api.onConnected = function() {
logger.info('Connected')
getTemperature()
if(context.onConnected) {
context.onConnected()
}
}
/** -----------------------------
* Handler called when client disconnects from device
*/
ws_api.onDisconnected = function() {
logger.info('Disconnected')
if(tempTimer) {
clearTimeout(tempTimer)
tempTimer = null
}
if(context.onDisconnected) {
context.onDisconnected()
}
}
/** -----------------------------
* This is called when the device writes accelerometer data to this app.
* See: accelerometer_data_handler() in the example app.
*/
var accelerometerClientStreamHandler = function(stream, action, data) {
logger.debug('Accelerometer data: ' + JSON.stringify(data))
model_processor.update(data.x, data.y, data.z)
}
/** -----------------------------
* This is called when the device writes 'interval' stream to this app.
* See: broadcast_interval_updated() in the example app.
*/
var intervalClientStreamHandler = function(stream, action, data) {
if(context.onIntervalUpdated) {
logger.info('New interval: ' + data.value)
context.onIntervalUpdated(data.value)
}
}
/** -----------------------------
* This is called periodically to read the device's temperature sensor.
* See temperature_stream_handler() in the app.
*/
var getTemperature = function() {
var responseHandler = function(result, data) {
if(!result) {
logger.error('Failed to read device temperature, err: ' + result)
} else {
context.onTemperatureAvailable(data)
}
tempTimer = setTimeout(getTemperature, context.temperatureUpdateInterval)
}
ws_api.read('temp', responseHandler)
}
ws_api.setLogger(console)
// ws_api.setMsgpackLogger(console)
logger.info('Registering stream handlers ...')
ws_api.register('accel', accelerometerClientStreamHandler)
ws_api.register('interval', intervalClientStreamHandler)
return context
}