msgflo
Version:
Polyglot FBP runtime based on message queues
104 lines (87 loc) • 2.9 kB
text/coffeescript
msgflo = require 'msgflo-nodejs'
# Emulate a physical toggle switch
# Sends a boolean. Switches between on and off every second
class Switch
constructor: (address) ->
= msgflo.transport.getClient address
= null
= true
= '/myswitch/baar/1/status'
start: (callback) ->
sendStatus = () =>
= not # toggle
.sendTo 'outqueue', , , () ->
.connect (err) =>
return callback err if err
.createQueue 'outqueue', , (err) =>
return callback err if err
= setInterval sendStatus, 1000
return callback null
stop: (callback) ->
clearInterval if
.removeQueue 'outqueue', , (err) =>
return .disconnect callback
# Emulate a physical lightbubl
# Takes a boolean on/off signal
class LightBulb
constructor: (address) ->
= msgflo.transport.getClient address
= '/mylightbulb/ffo/1/set-on'
= '/mylightbulb/ffo/1/is-on'
start: (callback) ->
onInput = (message) =>
state = if message then 'ON' else 'OFF'
console.log "turning lightbulb #{state}"
.sendTo 'outqueue', , message, () ->
.connect (err) =>
return callback err if err
.createQueue 'outqueue', , (err) =>
return callback err if err
.createQueue 'inqueue', , (err) =>
return callback err if err
.subscribeToQueue , onInput, callback
stop: (callback) ->
clearInterval if
.removeQueue 'outqueue', , (err) =>
return .disconnect callback
exports.LightBulb = LightBulb
exports.ToggleSwitch = Switch
exports.sendDeclarations = (address, callback) ->
# TODO: use the foreign participants declaration tool
lightbulb =
component: 'my/LightBulb'
icon: 'file-word-o'
role: 'mybulb'
label: 'A lightbubl that can be on or off'
inports: [
id: 'enable'
type: 'boolean'
description: ''
queue: '/mylightbulb/ffo/1/set-on'
]
outports: [
id: 'confirm'
type: 'boolean'
description: 'ff'
queue: '/mylightbulb/ffo/1/is-on'
]
toggleswitch =
component: 'my/ToggleSwitch'
role: 'myswitch'
icon: 'file-word-o'
label: 'A toggle switch which can turn things on or off'
inports: []
outports: [
id: 'state'
type: 'boolean'
description: 'ff'
queue: '/myswitch/baar/1/status'
]
client = msgflo.transport.getClient address
client.connect (err) ->
return callback err if err
client.registerParticipant lightbulb, (err) ->
return callback err if err
client.registerParticipant toggleswitch, (err) ->
return callback err if err
client.disconnect callback