commit
98a5a63cd8
|
@ -1,5 +1,5 @@
|
|||
# FarmBot Controller
|
||||
|
||||
require 'pry'
|
||||
require_relative 'settings.rb'
|
||||
|
||||
system('clear')
|
||||
|
@ -10,7 +10,7 @@ puts '---------'
|
|||
puts ' FarmBot '
|
||||
puts '---------'
|
||||
puts ' \/ '
|
||||
puts ''
|
||||
puts '========='
|
||||
|
||||
require_relative 'lib/status'
|
||||
$status = Status.new
|
||||
|
@ -41,7 +41,6 @@ puts 'OK'
|
|||
|
||||
puts "uuid #{$info_uuid}"
|
||||
puts "token #{$info_token}"
|
||||
|
||||
if $controller_disable == 0
|
||||
print 'controller '
|
||||
require_relative 'lib/controller'
|
||||
|
|
|
@ -2,7 +2,6 @@ require 'active_record'
|
|||
require 'date'
|
||||
require_relative 'database/dbaccess'
|
||||
require_relative 'controller_command_proc'
|
||||
|
||||
# FarmBot Controller: This module executes the schedule. It reads the next
|
||||
# command and sends it to the hardware implementation
|
||||
class Controller
|
||||
|
@ -30,7 +29,7 @@ class Controller
|
|||
get_next_command
|
||||
check_and_execute_command
|
||||
|
||||
rescue Exception => e
|
||||
rescue => e
|
||||
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}")
|
||||
end
|
||||
|
@ -60,7 +59,7 @@ class Controller
|
|||
|
||||
$status.info_status = 'starting'
|
||||
puts 'OK'
|
||||
|
||||
|
||||
print 'arduino '
|
||||
sleep 1
|
||||
$bot_hardware.read_device_version() if $hardware_sim == 0
|
||||
|
@ -126,7 +125,7 @@ class Controller
|
|||
|
||||
wait_start_time = Time.now
|
||||
|
||||
# wait until the scheduled time has arrived, or wait for a minute or
|
||||
# wait until the scheduled time has arrived, or wait for a minute or
|
||||
# until a refresh it set in the database as a sign new data has arrived
|
||||
|
||||
while Time.now < wait_start_time + 60 and @command.scheduled_time > Time.now - 1 and refresh_received == false
|
||||
|
@ -159,9 +158,7 @@ class Controller
|
|||
# new data has arrived
|
||||
|
||||
while Time.now < wait_start_time + 60 and refresh_received == false
|
||||
|
||||
sleep 0.1
|
||||
|
||||
check_hardware()
|
||||
|
||||
refresh_received = true if @bot_dbaccess.check_refresh
|
||||
|
|
|
@ -7,12 +7,13 @@ class ControllerCommandProc
|
|||
@bot_dbaccess = $bot_dbaccess
|
||||
end
|
||||
|
||||
def whitelist
|
||||
['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']
|
||||
end
|
||||
WHITELIST = ['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']
|
||||
|
||||
def check_whitelist(function)
|
||||
raise "UNAUTHORIZED" unless whitelist.include?(function.downcase)
|
||||
raise "UNAUTHORIZED" unless WHITELIST.include?(function.downcase)
|
||||
end
|
||||
|
||||
def process_command( cmd )
|
||||
|
@ -36,7 +37,7 @@ class ControllerCommandProc
|
|||
if $hardware_sim == 0
|
||||
send(function, command_line)
|
||||
else
|
||||
#$status.info_movement =
|
||||
#$status.info_movement =
|
||||
@info_movement = "#{command_line.action.downcase} xyz=#{command_line.coord_x} #{command_line.coord_y} #{command_line.coord_z} amt=#{command_line.amount} spd=#{command_line.speed} pin=#{command_line.pin_nr}"
|
||||
puts "simulating: #{@info_movement}"
|
||||
#@bot_dbaccess.write_to_log(1,@info_movement)
|
||||
|
@ -98,7 +99,7 @@ class ControllerCommandProc
|
|||
end
|
||||
|
||||
def pin_pulse(command_line)
|
||||
$bot_hardware.pin_std_pulse(command_line.pin_nr, command_line.pin_value_1,
|
||||
$bot_hardware.pin_std_pulse(command_line.pin_nr, command_line.pin_value_1,
|
||||
command_line.pin_value_2, command_line.pin_time, command_line.pin_mode)
|
||||
end
|
||||
|
||||
|
|
|
@ -9,7 +9,13 @@ require_relative 'messagehandler_message.rb'
|
|||
require_relative 'messagehandler_parameters.rb'
|
||||
require_relative 'messagehandler_schedule.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
|
||||
# command queue Parses JSON messages received through SkyNet.
|
||||
|
@ -22,7 +28,7 @@ class MessageHandler
|
|||
|
||||
def initialize
|
||||
#@dbaccess = DbAccess.new
|
||||
@dbaccess = $dbaccess
|
||||
@dbaccess = $dbaccess || NullDatabase.new
|
||||
@last_time_stamp = ''
|
||||
|
||||
@message_handlers = Array.new
|
||||
|
@ -32,13 +38,14 @@ class MessageHandler
|
|||
@message_handlers << MessageHandlerParameter.new
|
||||
@message_handlers << MessageHandlerSchedule.new
|
||||
@message_handlers << MessageHandlerStatus.new
|
||||
@message_handlers << MessageHandlerMessage.new
|
||||
|
||||
end
|
||||
|
||||
# Handle the message received from skynet
|
||||
#
|
||||
def handle_message(message)
|
||||
|
||||
puts "WebSocket Message"
|
||||
sender = ""
|
||||
time_stamp = nil
|
||||
|
||||
|
@ -61,6 +68,7 @@ class MessageHandler
|
|||
check_if_message_handled(message_obj)
|
||||
|
||||
rescue Exception => e
|
||||
puts e.message, e.backtrace.first
|
||||
err_snd = true
|
||||
err_msg = e.message
|
||||
err_trc = e.backtrace.inspect
|
||||
|
@ -70,6 +78,7 @@ class MessageHandler
|
|||
begin
|
||||
handle_message_error(err_snd, sender, time_stamp, err_msg, err_trc)
|
||||
rescue Exception => e
|
||||
puts e.message, e.backtrace.first
|
||||
puts "Error while sending error message: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
@ -144,13 +153,13 @@ class MessageHandler
|
|||
if message_obj.handled == false
|
||||
handler.handle_message( message_obj )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def check_if_message_handled(message_obj)
|
||||
if message_obj.handled == false
|
||||
@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
|
||||
|
||||
|
|
|
@ -10,4 +10,109 @@ class MessageHandlerMessage
|
|||
handled = false
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue