Module: Commands

Defined in:
lib/bolt_train_runner/commands/exit.rb,
lib/bolt_train_runner/commands/move.rb,
lib/bolt_train_runner/commands/stop.rb,
lib/bolt_train_runner/commands/debug.rb,
lib/bolt_train_runner/commands/power.rb,
lib/bolt_train_runner/commands/connect.rb,
lib/bolt_train_runner/commands/sessions.rb,
lib/bolt_train_runner/commands/throttle.rb,
lib/bolt_train_runner/commands/disconnect.rb

Class Method Summary collapse

Class Method Details

.connect(args, log) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bolt_train_runner/commands/connect.rb', line 6

def self.connect(args, log)
  conf = Conf.load_conf
  if !args.empty? && args[0] =~ /help/i
    log.help('Command: connect')
    log.help('Syntax: connect [server:port]')
    log.help('If called with no argument, it will attempt to use the last specified server:port')
    log.help('If this is the first time connect was called with no arguments, it will prompt you for the server and port')
    return
  end
  if args.empty?
    server = conf['server']
    unless server
      prompt = 'Please enter server:port [10.0.7.82:12080] > '
      print prompt
      server = gets.chomp
      server = '10.0.7.82:12080' if server.empty?
      log.info_file("#{prompt} #{server}")
    end
  else
    server = args[0]
  end
  conf['server'] = server
  Conf.save_conf(conf)

  log.info("Connecting to ws://#{server}/json/")
  comms = Comms.new(server, log)
  log.info("Connected")
  return comms
end

.debug(args, log) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bolt_train_runner/commands/debug.rb', line 5

def self.debug(args, log)
  if args.empty? || args[0] =~ /help/i
    log.help('Command: debug')
    log.help('Syntax: debug <on|off>')
    #Should fix this at some point 
    log.help('Turns debug logging on or off. Choice will be persistent across program invocations.')
    return
  end
  state = args[0]
  if !['on','off'].include?(state)
    log.error('Debug must be called with either "on" or "off"')
    return
  end
  conf = Conf.load_conf
  conf['debug'] = state == 'on'
  Conf.save_conf(conf)
  state == 'on' ? log.set_console_level('DEBUG') : log.set_console_level('INFO')
  log.info("Debug mode #{state}")
end

.disconnect(comms, log) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/bolt_train_runner/commands/disconnect.rb', line 5

def self.disconnect(comms, log)
  if comms.nil?
    log.error('Not currently connected')
    return
  end
  comms.disconnect
  log.info('Disconnected')
end

.exit_program(comms, session_runner, log) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/bolt_train_runner/commands/exit.rb', line 5

def self.exit_program(comms, session_runner, log)
  session_runner.stop if session_runner
  comms.disconnect if comms
  log.info('Seeya later!')
  log.close
  exit 0
end

.move(args, comms, log) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bolt_train_runner/commands/move.rb', line 7

def self.move(args, comms, log)
  unless comms
    log.error('Please connect first')
    return
  end
  if args.empty? || args[0] =~ /help/i
    log.help('Command: move')
    log.help('Syntax: move <forward|reverse> <speed 0-10> <time 1-60>')
    log.help('Move the train the given direction at the given speed for the given number of seconds')
    log.help('Note that the power must be on first')
    return
  end


  if args.is_a?(Hash)
    direction = args['direction']
    speed = args['speed']
    time = args['time']
  else
    direction, speed, time = args
  end

  unless direction && speed && time
    log.error('Please provide direction, speed, and time')
    return
  end
  speed = speed.to_i
  time = time.to_i
  direction = direction == 'f' ? 'forward' : direction == 'r' ? 'reverse' : direction
  unless ['forward','reverse'].include?(direction)
    log.error('Please provide "forward" or "reverse" for direction')
    return
  end
  if speed < 0 or speed > 10
    log.error('Please select a speed between 0 and 10')
    return
  end
  if time < 1 or time > 60
    log.error('Please select a time between 1 and 60 seconds')
    return
  end

  log.info("Moving train #{direction} direction at speed #{speed} for #{time} seconds...")
  Commands.throttle([speed, direction], comms, log)
  sleep(time)
  Commands.stop(nil, comms, log)
  log.info('Move complete')
end

.power(args, comms, log) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bolt_train_runner/commands/power.rb', line 5

def self.power(args, comms, log)
  unless comms
    log.error('Please connect first')
    return
  end
  if args.empty? || args[0] =~ /help/i
    log.help('Command: power')
    log.help('Syntax: power <on|off>')
    log.help('Turns power to the train on or off. Must first be connected.')
    return
  end

  if args.is_a?(Hash)
    state = args['state']
  else
    state = args[0]
  end
  
  unless ['on','off'].include?(state)
    log.error('Please provide either "on" or "off"')
    return
  end

  vals = {'on' => '2', 'off' => '4'}
  value = vals[state]
  message = {
    'type'   => 'power',
    'method' => 'put',
    'data'   => {
      'name'  => 'LocoNet',
      'state' => value
    }
  }
  comms.send_message(message)
  log.info("Power #{state}")
end

.sessions(args, comms, session_runner, log) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bolt_train_runner/commands/sessions.rb', line 6

def self.sessions(args, comms, session_runner, log)
  if args.empty? || args[0] =~ /help/i
    log.help('Command: sessions')
    log.help('Syntax: sessions <start|stop>')
    log.help('Start or stop the thread that monitors the sessions folder for sessions files. When running, this thread')
    log.help('will automatically execute the commands contained in sessions files generated by the bolt-train-api server')
    log.help('as they appear in the folder.')
    return
  end
  
  state = args[0]
  starting = state == 'start'
  if !['start','stop'].include?(state)
    log.error('Please provide either "start" or "stop"')
    return
  end

  unless comms
    log.error('Please connect first')
    return
  end

  if starting and session_runner
    log.warn('Session runner thread already started')
    return session_runner
  end

  if !starting and !session_runner
    log.warn('No session runner thread currently running')
    return nil
  end

  if starting
    conf = Conf.load_conf
    if args[1]
      session_dir = args[1]
      conf['session_dir'] = session_dir
      Conf.save_conf(conf)
    else
      session_dir = conf['session_dir']
      unless session_dir
        # This should be changed to pick up BOLT_TRAIN_QUEUE_DIR automatically
        prompt = 'Please enter directory for session files [/tmp/bolt-train-queue] > '
        print prompt
        session_dir = gets.chomp
        session_dir = '/tmp/bolt-train-queue' if session_dir.empty?
        conf['session_dir'] = session_dir
        Conf.save_conf(conf)
        log.info_file("#{prompt} #{session_dir}")
      end
    end
    
    runner = SessionRunner.new(comms, session_dir, log)
    runner.start
    return runner
  else
    session_runner.stop
    return nil
  end
end

.stop(_args, comms, log) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/bolt_train_runner/commands/stop.rb', line 5

def self.stop(_args, comms, log)
  unless comms
    log.error('Please connect first')
    return
  end
  Commands.throttle([0], comms, log)
end

.throttle(args, comms, log) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bolt_train_runner/commands/throttle.rb', line 5

def self.throttle(args, comms, log)
  unless comms
    log.error('Please connect first')
    return
  end
  if args.empty? || args[0] =~ /help/i
    log.help('Command: throttle')
    log.help('Syntax: throttle <0-10> [forward|reverse]')
    log.help('Sets the throttle to the given level, between 0 and 10. May optionally provide a direction.')
    log.help('Note that power must be on first for this to take effect.')
    return
  end

  if args.is_a?(Hash)
    speed = args['speed']
    direction = args['direction']
  else
    speed, direction = args
  end
  
  speed = speed.to_i
  if speed < 0 or speed > 10
    log.error('Please select a speed between 0 and 10')
    return
  end

  unless direction.nil? || ['forward','reverse'].include?(direction)
    log.error('Direction must be either "forward" or "reverse"')
    return
  end

  message = {
    'type'   => 'throttle',
    'method' => 'post',
    'data'   => {
      'throttle' => 'bolttrain',
      'address'  => '6871',
      'speed'    => "#{speed/10.0}",
    }
  }
  message['data']['forward'] = direction == 'forward' if direction
  comms.send_message(message)
  direction_string = direction.nil? ? '' : " with direction #{direction}"
  log.info("Throttle set to #{speed}#{direction_string}")
end