fbp-protocol-client
Version:
Client library for the FBP protocol
182 lines (146 loc) • 4.72 kB
text/coffeescript
Base = require './base'
microflo = require 'microflo'
parseQueryString = (queryString) ->
queries = queryString.split "&"
params = {}
queries.forEach (query, i) ->
kv = query.split '='
params[kv[0]] = kv[1]
return params
parseAddress = (address) ->
info =
type: null
device: null
baudrate: "9600"
if address.indexOf('serial://') == 0
info.type = 'serial'
if address.indexOf('simulator://') == 0
info.type = 'simulator'
if info.type
start = address.indexOf('://')+'://'.length
end = address.indexOf('?')
end = address.length if end < 0
d = address.substring start, end
info.device = d if d
queryStart = address.indexOf('?')
if queryStart != -1
query = address.substring queryStart+1
params = parseQueryString query
for k, v of params
info[k] = v
return info
# TODO: make this runtime be for every device that supports the same FBCS protocol as MicroFlo
class MicroFloRuntime extends Base
constructor: (definition) ->
= false
= []
= null
# MicroFlo things
= null
'connected',
super definition
isConnected: -> isnt null
getElement: ->
setParentElement: (parent) ->
= document.createElement 'container'
parent.appendChild
setMain: (graph) ->
if
# Unsubscribe from previous main graph
.removeListener 'changeProperties',
# Update contents on property changes
graph.on 'changeProperties',
super graph
openComm: () ->
getRuntime = null
info = parseAddress
if info.type == 'serial'
getRuntime = (callback) =>
microflo.serial.openTransport info.device, parseInt(info.baudrate), (err, transport) ->
return callback err if err
dev = new microflo.runtime.Runtime transport
if process?.env? and process.env['MICROFLO_COMPONENT_MAP']
fs = require 'fs'
filename = process.env['MICROFLO_COMPONENT_MAP']
dev.library.definition = JSON.parse fs.readFileSync(filename)
return callback null, dev
else if info.type == 'simulator'
getRuntime = (callback) =>
build = require 'microflo-emscripten'
sim = new microflo.simulator.RuntimeSimulator build.runtime
sim.library.definition = build.library # TODO: should be passed/looked up automatically
sim.start()
#sim.device.graph = sim.graph
return callback null, sim
getRuntime (err, runtime) =>
return 'error', err if err
runtime.on 'message', (response) =>
{ data: response }
runtime.device.open () =>
= false
if err
console.log 'MicroFlo error:', err
'error', err
return
= runtime
# Perform capability discovery
'getruntime', {}
'status',
online: true
label: 'connected'
'connected'
connect: ->
return if
= true
transport = runtime?.transport
?.stop()
= null
# Make sure serial transport is closed before reopening
if transport
transport.close () =>
else
f = () =>
setTimeout f, 0
disconnect: ->
onClosed = (success) =>
= null
'status',
online: false
label: 'disconnected'
'disconnected'
if
.transport.close onClosed
else
onClosed false
updatecontainer: =>
return unless
# Set an ID for targeting purposes
.id = 'preview-container'
send: (protocol, command, payload) ->
msg =
protocol: protocol
command: command
payload: payload
if
.push msg
return
try
.handleMessage msg
catch e
console.log e.stack
console.log e
onMessage: (message) =>
switch message.data.protocol
when 'runtime' then message.data.command, message.data.payload
when 'graph' then message.data.command, message.data.payload
when 'network' then message.data.command, message.data.payload
when 'component' then message.data.command, message.data.payload
flush: ->
for item in
item.protocol, item.command, item.payload
= []
module.exports = MicroFloRuntime
MicroFloRuntime.parseAddress = parseAddress