Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
say is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
chendo committed Nov 24, 2011
1 parent 7c04d93 commit 57ec437
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 169 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -19,7 +19,7 @@ if config.plugins
if plugin.is_a? String
gem "siriproxy-#{plugin.downcase}"
else
gem "siriproxy-#{plugin['name'].downcase}", :path => plugin['path'], :git => plugin['git'], :require => plugin['require']
gem "siriproxy-#{plugin['gem'] || plugin['name'].downcase}", :path => plugin['path'], :git => plugin['git'], :require => plugin['require']
end
end
end
54 changes: 2 additions & 52 deletions plugins/siriproxy-example-redux/lib/siriproxy-example-redux.rb
Expand Up @@ -10,58 +10,8 @@

class SiriProxy::Plugin::ExampleRedux < SiriProxy::Plugin

####
# This gets called every time an object is received from the Guzzoni server
def object_from_guzzoni(object, connection)
puts "Setup test proxy with #{object.inspect}"
object
end

####
# This gets called every time an object is received from an iPhone
def object_from_client(object, connection)
puts "Received object from client: #{object.inspect}"
object
end


####
# When the server reports an "unkown command", this gets called. It's useful for implementing commands that aren't otherwise covered
def unknown_command(object, connection, command)
if(command.match(/test siri proxy/i))
plugin_manager.block_rest_of_session_from_server

return generate_siri_utterance(connection.last_ref_id, "Siri Proxy is up and running!")
end

object
end

####
# This is called whenever the server recognizes speech. It's useful for overriding commands that Siri would otherwise recognize
def speech_recognized(object, connection, phrase)
if(phrase.match(/siri proxy map/i))
plugin_manager.block_rest_of_session_from_server

connection.inject_object_to_output_stream(object)

addViews = SiriAddViews.new
addViews.make_root(connection.last_ref_id)
mapItemSnippet = SiriMapItemSnippet.new
mapItemSnippet.items << SiriMapItem.new
utterance = SiriAssistantUtteranceView.new("Testing map injection!")
addViews.views << utterance
addViews.views << mapItemSnippet

connection.inject_object_to_output_stream(addViews.to_hash)

request_complete = SiriRequestCompleted.new
request_complete.make_root(connection.last_ref_id)

return request_complete.to_hash
end

object
listen_for /proxy/i do
say "Siri proxy up and running!"
end

end
78 changes: 53 additions & 25 deletions siri_proxy/plugin.rb
@@ -1,31 +1,59 @@
class SiriProxy::Plugin
attr_accessor :plugin_manager
attr_accessor :plugin_manager, :connection

class << self

def listen_for(regex, &block)
default_listeners[regex] = block
end

def default_listeners
@default_listeners ||= {}
end

end

def default_listeners
self.class.default_listeners
end

def say(text)
log "Say: #{text}"
connection.inject_object_to_output_stream(generate_siri_utterance(connection.last_ref_id, text))
end

# Old plugin stuff
def initialize(pluginConfig)

end

def object_from_guzzoni(object, connection)

object
end


#Don't forget to return the object!
def object_from_client(object, connection)

object
end


def unknown_command(object, connection, command)

object
end

def speech_recognized(object, connection, phrase)

object
end

end
def object_from_guzzoni(object, connection)

object
end


#Don't forget to return the object!
def object_from_client(object, connection)

object
end


def unknown_command(object, connection, command)

object
end

def speech_recognized(object, connection, phrase)

object
end

private

def log(text)
$stderr.puts(text)
end

end
214 changes: 123 additions & 91 deletions siri_proxy/plugin_manager.rb
@@ -1,13 +1,13 @@
class SiriProxy::PluginManager
attr_accessor :plugins
attr_accessor :plugins

def initialize()
load_plugins()
def initialize()
load_plugins()

@blockNextObjectsFromServer = 0
@blockNextObjectsFromClient = 0
@blockRestOfSessionFromServer = false
end
@blockNextObjectsFromServer = 0
@blockNextObjectsFromClient = 0
@blockRestOfSessionFromServer = false
end

def load_plugins()
@plugins = []
Expand All @@ -26,90 +26,122 @@ def load_plugins()
@plugins << plugin
end
end
puts "Plugins laoded: #{@plugins}"
puts "Plugins laoded: #{@plugins}"
end

def object_from_guzzoni(object, connection)
if(@blockRestOfSessionFromServer)
if(connection.last_ref_id == object["refId"])
puts "[Info - Dropping Object from Guzzoni] #{object["class"]}"
return nil
else
@blockRestOfSessionFromServer = false
end
end

if(@blockNextObjectsFromServer > 0)
puts "[Info - Dropping Object from Guzzoni] #{object["class"]}"
@blockNextObjectsFromServer -= 1
return nil
end

plugins.each { |plugin|
object = plugin.object_from_guzzoni(object, connection)
}

object
end

def object_from_client(object, connection)
if(@blockNextObjectsFromClient > 0)
puts "[Info - Dropping Object from iPhone] #{object["class"]}"
@blockNextObjectsFromClient -= 1
return nil
end

##Often this indicates a bug in OUR code. So let's not send it to Apple. :-)
if(object["class"] == "CommandIgnored")
pp object
return nil
end

plugins.each { |plugin|
object = plugin.object_from_client(object, connection)
}

# This is used for commands from phone that should not be sent back to Apple,
# and will be interpreted by plugins directly
if(object && object["properties"] && object['properties']['proxyOnly'])
puts "[Info - Not forwarding Object from iPhone because proxyOnly flag was set] #{object["class"]}"
return nil
end

object
end

def unknown_command(object, connection, command)
puts "[UnknownCommand] #{command}"

plugins.each { |plugin|
puts "plugin: #{plugin.inspect}"
if plugin.respond_to?(:default_listeners)
plugin.default_listeners.each do |regex, block|
if command =~ regex
block_rest_of_session_from_server
plugin.connection = connection
plugin.instance_exec(&block)
end
end
else
object = plugin.unknown_command(object, connection, command)
end
}

object
end

def speech_recognized(object, connection, phrase)
puts "[Recognized Speech] #{phrase}"

plugins.each { |plugin|
puts "Checking plugin #{plugin.class}"
if plugin.respond_to?(:default_listeners)
plugin.default_listeners.each do |regex, block|
if phrase =~ regex
puts "Phrase #{phrase} matches #{regex}"
block_rest_of_session_from_server
plugin.connection = connection
plugin.instance_exec(&block)

return request_complete(connection.last_ref_id)
end
end
else
object = plugin.speech_recognized(object, connection, phrase)
end
}

object
end


def block_next_objects_from_server(count=1)
@blockNextObjectsFromServer += count
end

def block_next_objects_from_client(count=1)
@blockNextObjectsFromClient += count
end

#Blocks everything from server until a new refId is seen
def block_rest_of_session_from_server
@blockRestOfSessionFromServer = true
end

def request_complete(ref_id)
SiriRequestCompleted.new.tap do |obj|
obj.make_root(ref_id)
end.to_hash
end

def object_from_guzzoni(object, connection)
if(@blockRestOfSessionFromServer)
if(connection.last_ref_id == object["refId"])
puts "[Info - Dropping Object from Guzzoni] #{object["class"]}"
return nil
else
@blockRestOfSessionFromServer = false
end
end

if(@blockNextObjectsFromServer > 0)
puts "[Info - Dropping Object from Guzzoni] #{object["class"]}"
@blockNextObjectsFromServer -= 1
return nil
end

plugins.each { |plugin|
object = plugin.object_from_guzzoni(object, connection)
}

object
end

def object_from_client(object, connection)
if(@blockNextObjectsFromClient > 0)
puts "[Info - Dropping Object from iPhone] #{object["class"]}"
@blockNextObjectsFromClient -= 1
return nil
end

##Often this indicates a bug in OUR code. So let's not send it to Apple. :-)
if(object["class"] == "CommandIgnored")
pp object
return nil
end

plugins.each { |plugin|
object = plugin.object_from_client(object, connection)
}

# This is used for commands from phone that should not be sent back to Apple,
# and will be interpreted by plugins directly
if(object && object["properties"] && object['properties']['proxyOnly'])
puts "[Info - Not forwarding Object from iPhone because proxyOnly flag was set] #{object["class"]}"
return nil
end

object
end

def unknown_command(object, connection, command)
puts "[UnknownCommand] #{command}"

plugins.each { |plugin|
object = plugin.unknown_command(object, connection, command)
}

object
end

def speech_recognized(object, connection, phrase)
puts "[Recognized Speech] #{phrase}"

plugins.each { |plugin|
object = plugin.speech_recognized(object, connection, phrase)
}

object
end


def block_next_objects_from_server(count=1)
@blockNextObjectsFromServer += count
end

def block_next_objects_from_client(count=1)
@blockNextObjectsFromClient += count
end

#Blocks everything from server until a new refId is seen
def block_rest_of_session_from_server
@blockRestOfSessionFromServer = true
end
end

0 comments on commit 57ec437

Please sign in to comment.