commit
98a5a63cd8
|
@ -1,5 +1,5 @@
|
||||||
# FarmBot Controller
|
# FarmBot Controller
|
||||||
|
require 'pry'
|
||||||
require_relative 'settings.rb'
|
require_relative 'settings.rb'
|
||||||
|
|
||||||
system('clear')
|
system('clear')
|
||||||
|
@ -10,7 +10,7 @@ puts '---------'
|
||||||
puts ' FarmBot '
|
puts ' FarmBot '
|
||||||
puts '---------'
|
puts '---------'
|
||||||
puts ' \/ '
|
puts ' \/ '
|
||||||
puts ''
|
puts '========='
|
||||||
|
|
||||||
require_relative 'lib/status'
|
require_relative 'lib/status'
|
||||||
$status = Status.new
|
$status = Status.new
|
||||||
|
@ -41,7 +41,6 @@ puts 'OK'
|
||||||
|
|
||||||
puts "uuid #{$info_uuid}"
|
puts "uuid #{$info_uuid}"
|
||||||
puts "token #{$info_token}"
|
puts "token #{$info_token}"
|
||||||
|
|
||||||
if $controller_disable == 0
|
if $controller_disable == 0
|
||||||
print 'controller '
|
print 'controller '
|
||||||
require_relative 'lib/controller'
|
require_relative 'lib/controller'
|
||||||
|
|
|
@ -2,7 +2,6 @@ require 'active_record'
|
||||||
require 'date'
|
require 'date'
|
||||||
require_relative 'database/dbaccess'
|
require_relative 'database/dbaccess'
|
||||||
require_relative 'controller_command_proc'
|
require_relative 'controller_command_proc'
|
||||||
|
|
||||||
# FarmBot Controller: This module executes the schedule. It reads the next
|
# FarmBot Controller: This module executes the schedule. It reads the next
|
||||||
# command and sends it to the hardware implementation
|
# command and sends it to the hardware implementation
|
||||||
class Controller
|
class Controller
|
||||||
|
@ -30,7 +29,7 @@ class Controller
|
||||||
get_next_command
|
get_next_command
|
||||||
check_and_execute_command
|
check_and_execute_command
|
||||||
|
|
||||||
rescue Exception => e
|
rescue => e
|
||||||
puts("Error in controller\n#{e.message}\n#{e.backtrace.inspect}")
|
puts("Error in controller\n#{e.message}\n#{e.backtrace.inspect}")
|
||||||
@bot_dbaccess.write_to_log(1,"Error in controller\n#{e.message}\n#{e.backtrace.inspect}")
|
@bot_dbaccess.write_to_log(1,"Error in controller\n#{e.message}\n#{e.backtrace.inspect}")
|
||||||
end
|
end
|
||||||
|
@ -159,9 +158,7 @@ class Controller
|
||||||
# new data has arrived
|
# new data has arrived
|
||||||
|
|
||||||
while Time.now < wait_start_time + 60 and refresh_received == false
|
while Time.now < wait_start_time + 60 and refresh_received == false
|
||||||
|
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
|
|
||||||
check_hardware()
|
check_hardware()
|
||||||
|
|
||||||
refresh_received = true if @bot_dbaccess.check_refresh
|
refresh_received = true if @bot_dbaccess.check_refresh
|
||||||
|
|
|
@ -7,12 +7,13 @@ class ControllerCommandProc
|
||||||
@bot_dbaccess = $bot_dbaccess
|
@bot_dbaccess = $bot_dbaccess
|
||||||
end
|
end
|
||||||
|
|
||||||
def whitelist
|
WHITELIST = ['move_absolute','move_relative','home_x','home_y','home_z',
|
||||||
['move_absolute','move_relative','home_x','home_y','home_z','calibrate_x','calibrate_y','calibrate_z','dose_water','set_speed','pin_write','pin_read','pin_mode','pin_pulse','servo_move']
|
'calibrate_x','calibrate_y','calibrate_z','dose_water',
|
||||||
end
|
'set_speed','pin_write','pin_read','pin_mode','pin_pulse',
|
||||||
|
'servo_move']
|
||||||
|
|
||||||
def check_whitelist(function)
|
def check_whitelist(function)
|
||||||
raise "UNAUTHORIZED" unless whitelist.include?(function.downcase)
|
raise "UNAUTHORIZED" unless WHITELIST.include?(function.downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_command( cmd )
|
def process_command( cmd )
|
||||||
|
|
|
@ -9,7 +9,13 @@ require_relative 'messagehandler_message.rb'
|
||||||
require_relative 'messagehandler_parameters.rb'
|
require_relative 'messagehandler_parameters.rb'
|
||||||
require_relative 'messagehandler_schedule.rb'
|
require_relative 'messagehandler_schedule.rb'
|
||||||
require_relative 'messagehandler_status.rb'
|
require_relative 'messagehandler_status.rb'
|
||||||
|
require_relative 'messagehandler_message'
|
||||||
|
|
||||||
|
class NullDatabase
|
||||||
|
def method_missing(*)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Get the JSON command, received through skynet, and send it to the farmbot
|
# Get the JSON command, received through skynet, and send it to the farmbot
|
||||||
# command queue Parses JSON messages received through SkyNet.
|
# command queue Parses JSON messages received through SkyNet.
|
||||||
|
@ -22,7 +28,7 @@ class MessageHandler
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
#@dbaccess = DbAccess.new
|
#@dbaccess = DbAccess.new
|
||||||
@dbaccess = $dbaccess
|
@dbaccess = $dbaccess || NullDatabase.new
|
||||||
@last_time_stamp = ''
|
@last_time_stamp = ''
|
||||||
|
|
||||||
@message_handlers = Array.new
|
@message_handlers = Array.new
|
||||||
|
@ -32,13 +38,14 @@ class MessageHandler
|
||||||
@message_handlers << MessageHandlerParameter.new
|
@message_handlers << MessageHandlerParameter.new
|
||||||
@message_handlers << MessageHandlerSchedule.new
|
@message_handlers << MessageHandlerSchedule.new
|
||||||
@message_handlers << MessageHandlerStatus.new
|
@message_handlers << MessageHandlerStatus.new
|
||||||
|
@message_handlers << MessageHandlerMessage.new
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Handle the message received from skynet
|
# Handle the message received from skynet
|
||||||
#
|
#
|
||||||
def handle_message(message)
|
def handle_message(message)
|
||||||
|
puts "WebSocket Message"
|
||||||
sender = ""
|
sender = ""
|
||||||
time_stamp = nil
|
time_stamp = nil
|
||||||
|
|
||||||
|
@ -61,6 +68,7 @@ class MessageHandler
|
||||||
check_if_message_handled(message_obj)
|
check_if_message_handled(message_obj)
|
||||||
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
puts e.message, e.backtrace.first
|
||||||
err_snd = true
|
err_snd = true
|
||||||
err_msg = e.message
|
err_msg = e.message
|
||||||
err_trc = e.backtrace.inspect
|
err_trc = e.backtrace.inspect
|
||||||
|
@ -70,6 +78,7 @@ class MessageHandler
|
||||||
begin
|
begin
|
||||||
handle_message_error(err_snd, sender, time_stamp, err_msg, err_trc)
|
handle_message_error(err_snd, sender, time_stamp, err_msg, err_trc)
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
puts e.message, e.backtrace.first
|
||||||
puts "Error while sending error message: #{e.message}"
|
puts "Error while sending error message: #{e.message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -150,7 +159,7 @@ class MessageHandler
|
||||||
def check_if_message_handled(message_obj)
|
def check_if_message_handled(message_obj)
|
||||||
if message_obj.handled == false
|
if message_obj.handled == false
|
||||||
@dbaccess.write_to_log(2,'message could not be handled')
|
@dbaccess.write_to_log(2,'message could not be handled')
|
||||||
send_error(sender, '', 'message could not be handled')
|
send_error(message_obj.sender, '', 'message could not be handled')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,4 +10,109 @@ class MessageHandlerMessage
|
||||||
handled = false
|
handled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Handle the message received from skynet
|
||||||
|
#
|
||||||
|
def handle_message(message)
|
||||||
|
if authorized?(message)
|
||||||
|
self.send(translate_action_name(message), message)
|
||||||
|
message.handled = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Ensure that the attempted command is allowed and also implemented.
|
||||||
|
def authorized?(message)
|
||||||
|
cmd = translate_action_name(message)
|
||||||
|
is_command = ControllerCommandProc::WHITELIST.include?(cmd)
|
||||||
|
is_method = self.respond_to?(cmd)
|
||||||
|
is_command && is_method
|
||||||
|
end
|
||||||
|
|
||||||
|
# PROBLEM: There are numerous spelling of commands and names for commands
|
||||||
|
# throughout the application. Sometimes it is move_rel, other times it is
|
||||||
|
# "MOVE RELATIVE". Sometimes we call it a message, other times an action.
|
||||||
|
# This method is a quick fix. Maybe we should make a uniform naming convention
|
||||||
|
def translate_action_name(message)
|
||||||
|
(message.payload["command"]["action"] || '').downcase.gsub(' ', '_')
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_absolute(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_relative(message)
|
||||||
|
inputs = message.payload["command"] || {}
|
||||||
|
# {"action"=>"MOVE RELATIVE", "x"=>0, "y"=>0, "z"=>0, "speed"=>100, "delay"=>0}
|
||||||
|
args = [
|
||||||
|
'MOVE RELATIVE', # action
|
||||||
|
inputs['x'] || 0, # x
|
||||||
|
inputs['y'] || 0, # y
|
||||||
|
inputs['z'] || 0, # z
|
||||||
|
inputs['speed'] || 100, # speed
|
||||||
|
0, # amount
|
||||||
|
0, # pin_nr
|
||||||
|
0, # value1
|
||||||
|
0, # value2
|
||||||
|
0, # mode
|
||||||
|
0, # time
|
||||||
|
0, # external_info
|
||||||
|
]
|
||||||
|
$bot_dbaccess.create_new_command(Time.now,'menu')
|
||||||
|
$bot_dbaccess.add_command_line(*args)
|
||||||
|
$bot_dbaccess.save_new_command
|
||||||
|
$bot_dbaccess.increment_refresh
|
||||||
|
rescue => e
|
||||||
|
puts e.message, e.backtrace.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def home_x(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def home_y(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def home_z(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def calibrate_x(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def calibrate_y(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def calibrate_z(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def dose_water(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_speed(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def pin_write(*)
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def pin_read
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def pin_mode
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def pin_pulse
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def servo_move
|
||||||
|
raise 'Not implemented yet.'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue