From 02ee099a59882e5a50e53160dc60d12beeb823be Mon Sep 17 00:00:00 2001 From: TimEvWw Date: Thu, 27 Feb 2014 21:13:31 -0100 Subject: [PATCH] read skynet return address --- lib/hardware/ramps.rb | 2 +- lib/skynet/messagehandler.rb | 19 +++++++++++-------- lib/skynet/skynet.rb | 10 +++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/hardware/ramps.rb b/lib/hardware/ramps.rb index cde1cf16..fe22dda6 100644 --- a/lib/hardware/ramps.rb +++ b/lib/hardware/ramps.rb @@ -8,7 +8,7 @@ class HardwareInterface @pos_z = 0.0 # should come from configuration: - @move_home_timeout = 3 # seconds after which home command is aborted + @move_home_timeout = 30 # seconds after which home command is aborted @sleep_after_pin_set = 0.005 @sleep_after_enable = 0.001 diff --git a/lib/skynet/messagehandler.rb b/lib/skynet/messagehandler.rb index 38fbb19c..a4db2bf0 100644 --- a/lib/skynet/messagehandler.rb +++ b/lib/skynet/messagehandler.rb @@ -19,7 +19,7 @@ class MessageHandler end # Main entry point for (Hash) commands coming in over SkyNet. - #{ + # { # "message_type" : "single_command", # "time_stamp" : 2001-01-01 01:01:01.001 # "command" : { @@ -31,8 +31,8 @@ class MessageHandler # "amount" : 5, # "delay" : 6 # } - #} - def handle_message(skynet, channel, message) + # } + def handle_message(channel, message) @message = message requested_command = message["message_type"].to_s.downcase if whitelist.include?(requested_command) @@ -50,10 +50,12 @@ class MessageHandler def single_command(message) time_stamp = message['time_stamp'] + sender = message['fromUuid'] if time_stamp != @last_time_stamp @last_time_stamp = time_stamp + # send the command to the queue delay = message['command']['delay'] action = message['command']['action'] @@ -64,7 +66,7 @@ class MessageHandler amount = message['command']['amount'] delay = message['command']['delay'] - puts "[new command] received at #{Time.now}" + puts "[new command] received at #{Time.now} from #{sender}" puts "[#{action}] x: #{x}, y: #{y}, z: #{z}, speed: #{speed}, amount: "\ "#{amount} delay: #{delay}" @@ -73,16 +75,17 @@ class MessageHandler speed.to_s, amount.to_i) @command_queue.save_new_command - skynet.confirmed = false + $skynet.confirmed = false + command = { :message_type => 'confirmation', :time_stamp => Time.now.to_f.to_s, :confirm_id => time_stamp - } + } - skynet.send_message("44128811-8c59-11e3-b99a-11476114e05f", command) + $skynet.send_message(sender, command) end end -end \ No newline at end of file +end diff --git a/lib/skynet/skynet.rb b/lib/skynet/skynet.rb index 2f2dbfa4..aab74255 100644 --- a/lib/skynet/skynet.rb +++ b/lib/skynet/skynet.rb @@ -21,8 +21,12 @@ class Skynet @uuid = creds[:uuid] @token = creds[:token] @socket = SocketIO::Client::Simple.connect 'http://skynet.im:80' + @confirmed = false + create_socket_events + puts "uuid: #{@uuid}" + @message_handler = MessageHandler.new end @@ -36,16 +40,16 @@ class Skynet def handle_message(channel, message) if message.class.to_s == 'Hash' - @message_handler.handle_message(self, channel, message) + @message_handler.handle_message(channel, message) end if message.class.to_s == 'String' message_hash = JSON.parse(message) - @message_handler.handle_message(self, channel, message_hash) + @message_handler.handle_message(channel, message_hash) end rescue raise "Runtime error while attempting to parse message: #{message}." end -end \ No newline at end of file +end