diff --git a/handlers.js b/handlers.js index 1d53c7b..b9fc73b 100644 --- a/handlers.js +++ b/handlers.js @@ -25,44 +25,61 @@ * */ +'use strict'; + var url = require('url'); var querystring = require('querystring'); var fs = require('fs'); + var log = require('./log'); -function start(res, req) { - log.trace('start: begin'); +/** + * Handler for home page. + * @param res - result + * @param req - request + */ +function homepage(res, req) { + log.trace('homepage: begin'); - var body = ''+ - ''+ - ''+ - 'OpenKarotz Emulator'+ - ''+ - ''+ - '

OpenKarotz Emulator

'+ - '

API

'+ - ''+ - ''+ - ''; + var body = '' + + '' + + '' + + 'OpenKarotz Emulator' + + '' + + '' + + '

OpenKarotz Emulator

' + + '

API

' + + '' + '' + ''; - res.writeHead(200, {'Content-Type' : 'text/html'}); + res.writeHead(200, { + 'Content-Type' : 'text/html' + }); res.write(body); res.end(); - log.trace('start: end'); + log.trace('homepage: end'); } -exports.start = start; +exports.homepage = homepage; function sendResponse(res, data) { log.trace('sendResponse: ' + data); - res.writeHead(200, {'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*'}); + res.writeHead(200, { + 'Content-Type' : 'text/plain', + 'Access-Control-Allow-Origin' : '*' + }); res.write(data); res.end(); } @@ -78,9 +95,12 @@ function getParameter(req, param, defaultValue) { function sleep(res, req) { log.trace('sleep: begin'); + + // TODO: update sleep state var data1 = '{"return":"0"}'; - var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is already sleeping."}'; - // TODO: maintain sleep state + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // already sleeping."}'; + sendResponse(res, data1); log.trace('sleep: end'); } @@ -89,7 +109,10 @@ exports.sleep = sleep; function wakeup(res, req) { log.trace('wakeup: begin'); var silent = getParameter(req, "silent"); + + // TODO: update sleep state var data = '{"return":"0","silent":"' + silent + '"}'; + sendResponse(res, data); log.trace('wakeup: end'); } @@ -97,7 +120,10 @@ exports.wakeup = wakeup; function reboot(res, req) { log.trace('reboot: begin'); + + // TODO: update sleep state var data = '{"return":"0"}'; + sendResponse(res, data); log.trace('reboot: end'); } @@ -105,7 +131,10 @@ exports.reboot = reboot; function status(res, req) { log.trace('status: begin'); + + // TODO: check sleep state var data = '{"version":"201","ears_disabled":"0","sleep":"0","sleep_time":"0","led_color":"0000FF","led_pulse":"1","tts_cache_size":"4","usb_free_space":"-1","karotz_free_space":"148.4M","eth_mac":"00:00:00:00:00:00","wlan_mac":"01:23:45:67:89:AB","nb_tags":"4","nb_moods":"305","nb_sounds":"14","nb_stories":"0","karotz_percent_used_space":"37","usb_percent_used_space":""}'; + sendResponse(res, data); log.trace('status: end'); } @@ -129,20 +158,143 @@ exports.get_free_space = get_free_space; function leds(res, req) { log.trace('leds: begin'); + + // TODO: check sleep state + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // sleeping."}'; + var color = getParameter(req, "color", "00FF00"); - var secondary_color = getParameter(req, "secondary_color", "000000"); + var color2 = getParameter(req, "color2", "000000"); var pulse = getParameter(req, "pulse", "0"); var no_memory = getParameter(req, "no_memory", "0"); var speed = getParameter(req, "speed", ""); + // TODO: handle blink parameter? - var data = '{"color":"' + color + - '","secondary_color":"' + secondary_color + - '","pulse":"' + pulse + - '","no_memory":"' + no_memory + - '","speed":"' + speed + - '","return":"0"}'; - sendResponse(res, data); + var data1 = '{"color":"' + color + '","secondary_color":"' + color2 + + '","pulse":"' + pulse + '","no_memory":"' + no_memory + + '","speed":"' + speed + '","return":"0"}'; + sendResponse(res, data1); log.trace('leds: end'); } exports.leds = leds; +function ears(res, req) { + log.trace('ears: begin'); + + // TODO: check sleep state + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // sleeping."}'; + // var data3 = '{"return":"1","msg":"Unable to perform action, ears + // disabled."}'; + + var left = getParameter(req, "left", "0"); + var right = getParameter(req, "right", "0"); + // var noreset = getParameter(req, "noreset", "0"); // Unused + + var data1 = '{"left":"' + left + '","right":"' + right + '","return":"0"}'; + sendResponse(res, data1); + log.trace('ears: end'); +} +exports.ears = ears; + +function ears_reset(res, req) { + log.trace('ears_reset: begin'); + + // TODO: check sleep state + var data1 = '{"return":"0"}'; + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // sleeping."}'; + // var data3 = '{"return":"1","msg":"Unable to perform action, ears + // disabled."}'; + + sendResponse(res, data1); + log.trace('ears_reset: end'); +} +exports.ears_reset = ears_reset; + +function ears_random(res, req) { + log.trace('ears_random: begin'); + + // TODO: check sleep state + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // sleeping."}'; + // var data3 = '{"return":"1","msg":"Unable to perform action, ears + // disabled."}'; + + var left = Math.floor((Math.random() * 15) + 1); + var right = Math.floor((Math.random() * 15) + 1); + + var data1 = '{"left":"' + left + '","right":"' + right + '","return":"0"}'; + + sendResponse(res, data1); + log.trace('ears_random: end'); +} +exports.ears_random = ears_random; + +function sound(res, req) { + log.trace('sound: begin'); + + // TODO: check sleep state + var data = '{"return":"1"}'; + // var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is + // sleeping."}'; + + var id = getParameter(req, "id"); + var url = getParameter(req, "url"); + + if (id && url) { + data = '{"return":"1","msg":"You cannot use ID and URL parameters at the same time."}'; + } else if ((id === undefined) && (url === undefined)) { + data = '{"return":"1","msg":"No sound to play."}'; + } else if (id) { + data = '{"return":"0"}'; + // data = '{"return":"1","msg":"Unable to find sound : ' + id + '"}'; + } else { + data = '{"return":"0"}'; + } + + sendResponse(res, data); + log.trace('sound: end'); +} +exports.sound = sound; + +function sound_control(res, req) { + log.trace('sound_control: begin'); + + // TODO: check sleep state + var data = '{"return":"1","msg":"Unable to perform action, rabbit is sleeping."}'; + + var cmd = getParameter(req, "cmd"); + if (cmd === undefined) { + data = '{"return":"1","msg":"No command specified."}'; + } else { + data = '{"return":"0","cmd":"' + cmd + '"}'; + } + + sendResponse(res, data); + log.trace('sound_control: end'); +} +exports.sound_control = sound_control; + +function tts(res, req) { + log.trace('tts: begin'); + + // TODO: check sleep state + var data = '{"return":"1","msg":"Unable to perform action, rabbit is sleeping."}'; + + var text = getParameter(req, "text"); + var voice = getParameter(req, "voice", "margaux"); + // var speed = getParameter(req, "speed"); // Unused + var nocache = getParameter(req, "nocache", 0); + // var engine = getParameter(req, "engine"); // Unused + + if (text === undefined) { + data = '{"return":"1","msg":"Missing mandatory parameter(s)."}'; + } else { + data = '{"played":"1","cache":"' + (nocache == 0 ? 1 : 0) + '","return":"0","voice":"' + voice + '"}'; + } + + sendResponse(res, data); + log.trace('tts: end'); +} +exports.tts = tts; diff --git a/index.js b/index.js index 45cde57..e35feac 100644 --- a/index.js +++ b/index.js @@ -1,45 +1,53 @@ -/*! - * OpenKarotz Emulator - * - * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * http://opensource.org/licenses/MIT - * - */ - -var log = require('./log'); -var server = require('./server'); -var router = require('./router'); -var handlers = require('./handlers') - -var handle = {} -handle['/'] = handlers.start; -handle['/cgi-bin'] = handlers.start; -handle['/cgi-bin/sleep'] = handlers.sleep; -handle['/cgi-bin/wakeup'] = handlers.wakeup; -handle['/cgi-bin/reboot'] = handlers.reboot; -handle['/cgi-bin/status'] = handlers.status; -handle['/cgi-bin/get_version'] = handlers.get_version; -handle['/cgi-bin/get_free_space'] = handlers.get_free_space; -handle['/cgi-bin/leds'] = handlers.leds; - -log.info('OpenKarotz Emulator'); -server.start(router.route, handle); +/*! + * OpenKarotz Emulator + * + * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * http://opensource.org/licenses/MIT + * + */ + +'use strict'; + +var log = require('./log'); +var server = require('./server'); +var router = require('./router'); +var handlers = require('./handlers'); + +var handle = {}; +handle['/'] = handlers.homepage; +handle['/cgi-bin'] = handlers.homepage; +handle['/cgi-bin/sleep'] = handlers.sleep; +handle['/cgi-bin/wakeup'] = handlers.wakeup; +handle['/cgi-bin/reboot'] = handlers.reboot; +handle['/cgi-bin/status'] = handlers.status; +handle['/cgi-bin/get_version'] = handlers.get_version; +handle['/cgi-bin/get_free_space'] = handlers.get_free_space; +handle['/cgi-bin/leds'] = handlers.leds; +handle['/cgi-bin/ears'] = handlers.ears; +handle['/cgi-bin/ears_reset'] = handlers.ears_reset; +handle['/cgi-bin/ears_random'] = handlers.ears_random; +handle['/cgi-bin/sound'] = handlers.sound; +handle['/cgi-bin/sound_control'] = handlers.sound_control; +handle['/cgi-bin/tts'] = handlers.tts; + +log.info('OpenKarotz Emulator'); +server.start(router.route, handle); diff --git a/log.js b/log.js index ec1ca43..43ccff5 100644 --- a/log.js +++ b/log.js @@ -25,6 +25,8 @@ * */ +'use strict'; + function trace(message) { console.log(new Date() + ' [trace] ' + message); } diff --git a/package.json b/package.json index 48db20f..2d39e19 100644 --- a/package.json +++ b/package.json @@ -1,28 +1,28 @@ -{ - "name": "OpenKarotz Emulator", - "version": "0.1.0", - "description": "HTTP server to emulate OpenKarotz", - "main": "index.js", - "readmeFilename": "README.md", - "homepage": "http://github.com/hobbe/openkarotz-emulator", - "author": "Olivier Bagot (http://github.com/hobbe)", - "keywords": [ - "openkarotz", - "emulator", - "node.js" - ], - "contributors": [ - "Olivier Bagot (http://github.com/hobbe)" - ], - "bugs": { - "url": "http://github.com/hobbe/openkarotz-emulator/issues" - }, - "license": { - "type": "MIT", - "url": "http://github.com/hobbe/openkarotz-emulator/raw/master/LICENSE" - }, - "repository": { - "type": "git", - "url": "git://github.com/hobbe/openkarotz-emulator.git" - } -} +{ + "name": "OpenKarotz Emulator", + "version": "0.1.0", + "description": "HTTP server to emulate OpenKarotz", + "main": "index.js", + "readmeFilename": "README.md", + "homepage": "http://github.com/hobbe/openkarotz-emulator", + "author": "Olivier Bagot (http://github.com/hobbe)", + "keywords": [ + "openkarotz", + "emulator", + "node.js" + ], + "contributors": [ + "Olivier Bagot (http://github.com/hobbe)" + ], + "bugs": { + "url": "http://github.com/hobbe/openkarotz-emulator/issues" + }, + "license": { + "type": "MIT", + "url": "http://github.com/hobbe/openkarotz-emulator/raw/master/LICENSE" + }, + "repository": { + "type": "git", + "url": "git://github.com/hobbe/openkarotz-emulator.git" + } +} diff --git a/router.js b/router.js index 8a486af..5820591 100644 --- a/router.js +++ b/router.js @@ -1,44 +1,46 @@ -/*! - * OpenKarotz Emulator - * - * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * http://opensource.org/licenses/MIT - * - */ - -var log = require('./log'); - -function route(pathname, handle, res, req) { - - log.trace('Route a request for ' + pathname); - - if (typeof handle[pathname] === 'function') { - return handle[pathname](res, req); - } else { - log.failure('No request handler found for ' + pathname); - res.writeHead(404, {'Content-Type' : 'text/plain'}); - res.write('404 Not Found'); - res.end(); - } -} - -exports.route = route +/*! + * OpenKarotz Emulator + * + * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * http://opensource.org/licenses/MIT + * + */ + +'use strict'; + +var log = require('./log'); + +function route(pathname, handle, res, req) { + + log.trace('Route a request for ' + pathname); + + if (typeof handle[pathname] === 'function') { + return handle[pathname](res, req); + } else { + log.failure('No request handler found for ' + pathname); + res.writeHead(404, {'Content-Type' : 'text/plain'}); + res.write('404 Not Found'); + res.end(); + } +} + +exports.route = route; diff --git a/server.js b/server.js index aa6b225..6edaeb0 100644 --- a/server.js +++ b/server.js @@ -1,47 +1,49 @@ -/*! - * OpenKarotz Emulator - * - * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * http://opensource.org/licenses/MIT - * - */ - -var http = require('http') -var url = require('url'); -var log = require('./log'); -var port = 80; - -function start(route, handle) { - - function onRequest(req, res) { - var pathname = url.parse(req.url).pathname; - - if (pathname !== '/favicon.ico') { - route(pathname, handle, res, req) - } - } - - http.createServer(onRequest).listen(port) - log.info('Server has started on http://localhost:' + port) -} - -exports.start = start +/*! + * OpenKarotz Emulator + * + * Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * http://opensource.org/licenses/MIT + * + */ + +'use strict'; + +var http = require('http'); +var url = require('url'); +var log = require('./log'); +var port = 80; + +function start(route, handle) { + + function onRequest(req, res) { + var pathname = url.parse(req.url).pathname; + + if (pathname !== '/favicon.ico') { + route(pathname, handle, res, req); + } + } + + http.createServer(onRequest).listen(port); + log.info('Server has started on http://localhost:' + port); +} + +exports.start = start;