<commit_before>-module(commands).
-export([ stop/3, forward/3, backward/3, rotate_ccw/3, rotate_cw/3, speed_up/3, slow_down/3 ]).
-export([path1/3]).

path1(SessionID, Env, _Input) ->
  io:format("Environment: ~p~n", [ Env ]),
  % { _, QueryString } = lists:keyfind(query_string, 1, Env),
  % { content_length, ContentLength } = lists:keyfind(content_length, 1, Env),
  mod_esi:deliver(SessionID, "Hello from path1").
  % mod_esi:deliver(SessionID, "Query string: "),
  % mod_esi:deliver(SessionID, QueryString),
  % mod_esi:deliver(SessionID, "Content length: "),
  % mod_esi:deliver(SessionID, ContentLength).
  %

stop(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { stop, RobotName }),
  mod_esi:deliver(SessionID, "command|stop").

forward(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { forward, RobotName }),
  mod_esi:deliver(SessionID, "command|forward").

backward(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { backward, RobotName }),
  mod_esi:deliver(SessionID, "command|backward").

rotate_ccw(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { rotate_ccw, RobotName }),
  mod_esi:deliver(SessionID, "command|rotate_ccw").

rotate_cw(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { rotate_cw, RobotName }),
  mod_esi:deliver(SessionID, "command|rotate_cw").

speed_up(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { speed_up, RobotName }),
  mod_esi:deliver(SessionID, "command|speed_up").

slow_down(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { slow_down, RobotName }),
  mod_esi:deliver(SessionID, "command|slow_down").


%% Private methods

parse_robot_name(Env) ->
  case lists:keyfind(query_string, 1, Env) of
    { query_string, QString } ->
      Params = q_params:parse_query_parameters(QString);
    _ ->
      Params = []
  end,
  case lists:keyfind("robot", 1, Params) of
    { "robot", Name } ->
      Name;
    _ ->
      "anonymous"
  end.<commit_msg>Properly formatting the tube name for commands.<commit_after>-module(commands).
-export([ stop/3, forward/3, backward/3, rotate_ccw/3, rotate_cw/3, speed_up/3, slow_down/3 ]).
-export([path1/3]).

path1(SessionID, Env, _Input) ->
  io:format("Environment: ~p~n", [ Env ]),
  % { _, QueryString } = lists:keyfind(query_string, 1, Env),
  % { content_length, ContentLength } = lists:keyfind(content_length, 1, Env),
  mod_esi:deliver(SessionID, "Hello from path1").
  % mod_esi:deliver(SessionID, "Query string: "),
  % mod_esi:deliver(SessionID, QueryString),
  % mod_esi:deliver(SessionID, "Content length: "),
  % mod_esi:deliver(SessionID, ContentLength).
  %

stop(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { stop, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|stop").

forward(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { forward, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|forward").

backward(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { backward, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|backward").

rotate_ccw(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { rotate_ccw, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|rotate_ccw").

rotate_cw(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { rotate_cw, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|rotate_cw").

speed_up(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { speed_up, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|speed_up").

slow_down(SessionID, Env, _Input) ->
  RobotName = parse_robot_name(Env),
  gen_server:cast(whereis(command_queuer), { slow_down, format_tube_name(RobotName) }),
  mod_esi:deliver(SessionID, "command|slow_down").


%% Private methods

format_tube_name(RobotName) ->
  io_lib:format("~s_commands", [ RobotName ]).

parse_robot_name(Env) ->
  case lists:keyfind(query_string, 1, Env) of
    { query_string, QString } ->
      Params = q_params:parse_query_parameters(QString);
    _ ->
      Params = []
  end,
  case lists:keyfind("robot", 1, Params) of
    { "robot", Name } ->
      Name;
    _ ->
      "anonymous"
  end.
{'fraction_non_alphanumeric': 0.15186482588089842, 'fraction_numerical': 0.006181743251596951, 'mean_word_length': 5.732316227461858, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '329940', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1822, 'n_words': 274}