forked from microflo/microflo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroflo.coffee
131 lines (114 loc) · 5.62 KB
/
microflo.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# MicroFlo - Flow-Based Programming for microcontrollers
# Copyright (c) 2013 Jon Nordby <[email protected]>
# MicroFlo may be freely distributed under the MIT license
require "coffee-script/register"
fs = require("fs")
path = require("path")
cmdFormat = require("./microflo/commandformat.json")
microflo = require("./lib/microflo")
commander = require("commander")
pkginfo = require("pkginfo")(module)
defaultLibrary = 'microflo-core/components/arduino-standard.json'
setupRuntimeCommand = (env) ->
serialPortToUse = env.serial or "auto"
port = env.port or 3569
debugLevel = env.debug or "Error"
ip = env.ip or "127.0.0.1"
baud = parseInt(env.baudrate) or 9600
library = env.library or defaultLibrary
if env.file
file = path.resolve env.file
microflo.runtime.setupSimulator file, baud, port, debugLevel, ip, (err, runtime) ->
throw err if err
else
microflo.runtime.setupRuntime serialPortToUse, baud, port, debugLevel, ip, library, (err, runtime) ->
throw err if err
uploadGraphCommand = (graphPath, env) ->
serialPortName = env.serial or "auto"
debugLevel = env.debug
baud = parseInt(env.baudrate) or 9600
microflo.runtime.uploadGraphFromFile graphPath, serialPortName, baud, debugLevel
generateFwCommand = (inputFile, outputDir, env) ->
target = env.target or "arduino"
outputFile = outputDir + "/main.cpp"
library = env.library or defaultLibrary
componentLib = new microflo.componentlib.ComponentLibrary()
componentLib.loadSetFile library, (err) ->
throw err if err
componentLib.loadFile inputFile
microflo.generate.updateComponentLibDefinitions componentLib, outputDir, "createComponent"
microflo.generate.generateOutput componentLib, inputFile, outputFile, target
registerRuntimeCommand = (user, env) ->
ip = env.ip or "auto"
port = parseInt(env.port) or 3569
label = env.label or "MicroFlo"
id = env.id or process.env["MICROFLO_RUNTIME_ID"]
user = process.env["FLOWHUB_USER_ID"] unless user
rt = microflo.runtime.createFlowhubRuntime(user, ip, port, label)
unless id
microflo.runtime.registerFlowhubRuntime rt, (err, ok) ->
if err
console.log "Could not register runtime with Flowhub", err
process.exit 1
else
console.log "Runtime registered with id:", rt.runtime.id
generateComponentLib = (componentlibJsonFile, componentlibOutputPath, factoryMethodName, env) ->
componentLibraryDefinition = undefined
componentLibrary = undefined
# load specified component library Json definition
componentLibraryDefinition = require(componentlibJsonFile)
componentLibrary = new microflo.componentlib.ComponentLibrary(componentLibraryDefinition, componentlibOutputPath)
componentLibrary.load()
# write component library definitions to external source or inside microflo project
microflo.generate.updateComponentLibDefinitions componentLibrary, componentlibOutputPath, factoryMethodName
flashCommand = (file, env) ->
upload = require("./lib/flash.coffee")
tty = env.serial
baud = parseInt(env.baudrate) or 115200
upload.avrUploadHexFile file, tty, baud, (err, written) ->
console.log err, written
updateDefsCommand = (directory) ->
microflo.generate.updateDefinitions directory
main = ->
commander.version module.exports.version
commander.command("componentlib <JsonFile> <OutputPath> <FactoryMethodName>")
.description("Generate compilable sources of specified component library from .json definition")
.action generateComponentLib
commander.command("update-defs")
.description("Update internal generated definitions")
.action updateDefsCommand
commander.command("generate <INPUT> <OUTPUT>")
.description("Generate MicroFlo firmware code, with embedded graph.")
.option("-l, --library <FILE.json>", "Component library file")
.option("-t, --target <platform>", "Target platform: arduino|linux|avr8")
.action generateFwCommand
commander.command("upload")
.option("-s, --serial <PORT>", "which serial port to use")
.option("-b, --baudrate <RATE>", "baudrate for serialport")
.option("-d, --debug <LEVEL>", "set debug level")
.description("Upload a new graph to a device running MicroFlo firmware")
.action uploadGraphCommand
commander.command("runtime")
.description("Run as a server, for use with the NoFlo UI.")
.option("-s, --serial <PORT>", "which serial port to use")
.option("-b, --baudrate <RATE>", "baudrate for serialport")
.option("-d, --debug <LEVEL>", "set debug level")
.option("-p, --port <PORT>", "which port to use for WebSocket")
.option("-i, --ip <IP>", "which IP to use for WebSocket")
.option("-f, --file <FILE>", "Firmware file to run (.js or binary)")
.action setupRuntimeCommand
commander.command("register [USER]")
.description("Register the runtime with Flowhub registry")
.option("-p, --port <PORT>", "WebSocket port")
.option("-i, --ip <IP>", "WebSocket IP")
.option("-l, --label <PORT>", "Label to show in UI for this runtime")
.option("-r, --id <RUNTIME-ID>", "UUID for the runtime")
.action registerRuntimeCommand
commander.command("flash <FILE.hex>")
.description("Flash runtime onto device")
.option("-s, --serial <PORT>", "which serial port to use")
.option("-b, --baudrate <RATE>", "baudrate for serialport")
.action flashCommand
commander.parse process.argv
commander.help() if process.argv.length <= 2
exports.main = main