Permalink
Browse files

updated playground page

  • Loading branch information...
1 parent d6752a2 commit 24e31b57af253d3097c16a4e142e310ed58fd100 Tobias Bielohlawek committed Dec 30, 2012
View
@@ -1,3 +1,2 @@
web: bundle exec rackup config.ru -p $PORT
dev: bundle exec rackup config.ru -p 9292
-tunnel: ssh ssh-21560@warteschlange.de -N -R 9092:localhost:9292
View
@@ -1,12 +1,12 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift ::File.expand_path(::File.dirname(__FILE__) + '/lib')
-require 'nabaztag_hack_kit/server'
+require './server'
require 'nabaztag_hack_kit/redirect'
$stdout.sync = true
use Rack::Reloader, 0
use NabaztagHackKit::Redirect
-run NabaztagHackKit::Server.new
+run Basic::Server.new
View
@@ -0,0 +1,14 @@
+require "nabaztag_hack_kit/server"
+
+require 'nabaztag_hack_kit/mods/callback'
+# require 'nabaztag_hack_kit/mods/logger'
+require 'nabaztag_hack_kit/mods/playground'
+
+module Basic
+ class Server < NabaztagHackKit::Server
+ register NabaztagHackKit::Mods::Callback
+ # register Mods::Logger
+ register NabaztagHackKit::Mods::Playground
+
+ end
+end
@@ -1,45 +1,54 @@
-class Bunny
- @bunnies = {}
+require 'nabaztag_hack_kit/message'
- attr_reader :id, :last_seen, :queued_commands
+module NabaztagHackKit
+ class Bunny
+ @bunnies = {}
- class << self
- def all
- @bunnies.values
- end
+ attr_reader :id, :last_seen, :queued_commands
- def find(id)
- @bunnies[id]
- end
+ class << self
+ def all
+ @bunnies.values
+ end
- def find_or_initialize_by_id(id)
- find(id) || Bunny.new(id)
- end
+ def find(id)
+ @bunnies[id]
+ end
+
+ def find_or_initialize_by_id(id)
+ find(id) || Bunny.new(id)
+ end
- def add(bunny)
- @bunnies[bunny.id] = bunny
+ def add(bunny)
+ @bunnies[bunny.id] = bunny
+ end
end
- end
- def initialize(id)
- @id = id
- @queued_commands = []
+ def initialize(id)
+ @id = id
+ @queued_commands = []
- Bunny.add(self)
- end
+ Bunny.add(self)
+ end
- def seen
- @last_seen = Time.now
- end
+ def seen!
+ @last_seen = Time.now
+ end
- def queue_command(command, value)
- @queued_commands << (Array(command) << value)
- end
+ def queue_commands(commands)
+ @queued_commands << commands
+ end
- def to_json(a,b)
- {
- :id => id,
- :last_seen => last_seen,
- }.to_json
+ def next_message!
+ Message.build(*@queued_commands.shift || Message::Api::OK)
+ end
+
+ def to_json(state = nil, deepth = nil)
+ {
+ :id => id,
+ :last_seen => last_seen,
+ :queued_commands_size => queued_commands.size,
+ }.to_json
+ end
end
end
@@ -13,7 +13,7 @@ def build(*commands)
pack full_message commands.map { |cmd, *data|
data = convert_data(data)
- [cmd] + to_3b(data.size) + data
+ [cmd.to_i] + to_3b(data.size) + data.map(&:to_i)
}
end
@@ -38,15 +38,11 @@ module Api
F = 1
B = 2
- def send_nabaztag(*data)
- Message.build(*data)
- end
-
- def rgb(values)
- values.map do |value|
- Message.to_3b(value)
- end.flatten
- end
+ # def rgb(values)
+ # values.map do |value|
+ # Message.to_3b(value)
+ # end.flatten
+ # end
end
end
@@ -0,0 +1,13 @@
+module NabaztagHackKit
+ module Mods
+ module Button
+
+ def self.registered(app)
+ app.on "button-pressed" do |bunny, data, request, run|
+ callback("button-pressed", bunny, params[:duration], request, run+1)
+ end
+ end
+
+ end
+ end
+end
@@ -4,19 +4,11 @@
module NabaztagHackKit
module Mods
module Callback
- PREFIX = "/api/:bunnyid"
module Helpers
- include Message::Api
-
- def callback(action, data, request, run = 0)
+ def callback(action, bunny, data, request, run = 0)
if (cb = self.class.callbacks[action.to_s]) && (callback = cb[run])
- unless instance_exec(data, request, run, &callback)
- callback(action, data, request, run+1)
- end
- else
- logger.warn "no callback found for #{action}-#{run}"
- send_nabaztag OK
+ instance_exec(bunny, data, request, run, &callback) || callback(action, bunny, data, request, run+1)
end
end
end
@@ -29,15 +21,18 @@ def on(callback, &block)
def self.registered(app)
app.helpers Callback::Helpers
- app.on "button-pressed" do |data, request, run|
- callback("button-pressed", params[:duration], request, run+1)
- end
-
# generic api callback
%w(get post).each do |method|
- app.send(method, "#{PREFIX}/:action.jsp") do
- callback('request', params, request)
- callback(params[:action], params, request)
+ app.send(method, "/api/:bunnyid/:action.jsp") do
+ bunny = Bunny.find(params[:bunnyid])
+ callback('request', bunny, params, request)
+ callback(params[:action], bunny, params, request).tap do |response|
+ unless response
+ logger.warn "no successful callback found for #{params[:action]}"
+ status 404
+ return
+ end
+ end
end
end
end
@@ -1,81 +1,69 @@
require "json"
require 'nabaztag_hack_kit/bunny'
+require 'nabaztag_hack_kit/message/helper'
module NabaztagHackKit
module Mods
module Playground
- module Helpers
- def cmds
- @@cmds ||= {}
- end
-
- def add_commands(bunny_sn, command, values)
- bunnies.each do |sn, value|
- if params[:bunny] == "*" || params[:bunny] == sn
- cmds[sn] ||= {}
- cmds[sn][command] = values
- end
- end
- end
-
- ########
-
- def distance_of_time(from_time, to_time = Time.now)
- hours = ( to_time - from_time) / 3600
- minutes = ((to_time - from_time) % 3600) / 60
- seconds = ((to_time - from_time) % 3600) % 60
-
- "".tap do |diff_in_words|
- diff_in_words << "%2dh,&nbsp;" % hours if hours.floor > 0
- diff_in_words << "%2dmin,&nbsp;" % minutes if minutes.floor > 0
- diff_in_words << "%2dsec" % seconds
- end
- end
- end
-
def self.registered(app)
- app.helpers Playground::Helpers
app.get "/" do
+ redirect "/playground"
+ end
+
+ app.get "/playground" do
File.read(public_file("index.html"))
end
#API
- app.get "/bunnies" do
- # return list of bunnies
+ app.get "/playground/commands" do # return list of commands
+ Message::Api.constants.sort.inject({}) do |hash, constant|
+ if constant.to_s.length > 2
+ hash[constant] = Message::Api.const_get(constant)
+ end
+ hash
+ end.to_json
+ end
+
+ app.get "/playground/bunnies" do # return list of bunnies
Bunny.all.to_json
end
- app.post "/bunnies/:bunnyid/command" do
+ app.post "/playground/bunnies/:bunnyid" do # {"command"=>["40"], "command_values"=>[["1,2,3,4"],[]]}
if bunny = Bunny.find(params[:bunnyid])
- bunny.queue_command(params[:command], params[:values])
+ bunny.queue_commands(Array(params[:command]).zip(params[:command_values]).map do |command, values|
+ [command, *values.split(",")]
+ end)
+ bunny.to_json
end
end
- app.post "/commands" do
- # return list of available commands
- Message::Api.constants.sort.inject({}) do |hash, constant|
- if constant.to_s.length > 2
- hash[constant] = Message::Api.const_get(constant)
+ app.post "/playground/bunnies" do # {"bunny"=>["0019db9c2daf"], "command"=>["40"], "command_values"=>[["1,2,3,4"],[]]}
+ Array(params[:bunny]).uniq.each do |bunnyid|
+ if bunny = Bunny.find(bunnyid)
+ bunny.queue_commands(Array(params[:command]).zip(params[:command_values]).map do |command, values|
+ [command, *values.split(",")]
+ end)
end
- hash
end
+
+ redirect "/playground"
end
##################################################################
- app.on "ping" do |data, request, run|
- if (bunny = Bunny.find(data[:bunnyid])) && bunny.queued_commands.any?
- send_nabaztag bunny.queued_commands.pop
+ app.on "ping" do |bunny|
+ if bunny
+ bunny.next_message!
end
end
- app.on 'request' do |data, request, run|
+ app.on 'request' do |bunny, data|
if bunny = Bunny.find_or_initialize_by_id(data[:bunnyid])
- bunny.seen
+ bunny.seen!
end
- nil #pass it own
+ nil # pass it on
end
end
@@ -5,12 +5,12 @@ module Streaming
REC_FILE = "rec.wav"
def self.registered(app)
- app.on "recording-finished" do |data, request, run|
+ app.on "recording-finished" do |bunny, data, request, run|
file_name = REC_FILE # TODO add timestamp??
File.open(file_name, "w+") do |f|
f.write request.body.read
end
- callback('recording-finished', file_name, request, run+1)
+ callback('recording-finished', bunny, file_name, request, run+1)
end
end
Oops, something went wrong.

0 comments on commit 24e31b5

Please sign in to comment.