App-EventStreamr

 view release on metacpan or  search on metacpan

lib/App/EventStreamr/Config.pm  view on Meta::CPAN

    $self->_create_config_path();
    my $config = Config::JSON->create($self->config_file);

    $config->{config} = {
      nickname => $self->nickname,
      room => $self->room,
      record_path => $self->record_path,
      run => $self->run,
      backend => $self->backend,
    };
    $config->write;
    return $config;
  }
}

method _load_config() {
  # TODO: There has to be a better way..
  foreach my $key (keys %{$self->localconfig->{config}}) {
    $self->{$key} = $self->localconfig->{config}{$key};
  }
}

method reload_config() {
  $self->_build_localconfig;
}

method _clean_config() {
  return {
    nickname => $self->nickname,
    room => $self->room,
    record_path => $self->record_path,
    run => $self->run,
    backend => $self->backend,
    roles => $self->roles,
    stream => $self->stream,
    youtube => $self->youtube,
    sync => $self->sync,
    mixer => $self->mixer,
    control => $self->control,
    devices => $self->devices,
    backend => $self->backend,
  }
}

method _post_data() {
  my $config = $self->_clean_config();
  $config->{manager}{pid} = $$;
  $config->{macaddress} = $self->macaddress();
  return $config;
}


method write_config() {
  $self->localconfig->{config} = $self->_clean_config();
  $self->localconfig->write;
}



method configure() {
  say "Welcome to the EventStreamr config utility\n";
  
  my $answer = $self->prompt(
    "It will clear the current config, is this ok? y/n",
    "n",
  );
  exit 1 if ($answer =~ /n/i);
 
  # TODO: Write config clear method
  $self->roles([]);
  # Config Questions
  $self->configure_room();
  $self->configure_backend();
  $self->configure_mixer();
  $self->configure_ingest();
  $self->write_config();
  
  say;

  # Confirm written.
  if (-e $self->config_file) {
    say "Config written successfully";
  } else {
    say "Config failed to write";
  }
  # TODO: Stream Configuration  
  #$answer = undef;
  #$answer = $self->prompt(
  #  "Stream - stream to configured service y/n: ",
  #  "y",
  #);

  #push(@{$self->{roles}}, "stream") if ($answer =~ /y/i);
}

method configure_mixer() {
  my $answer = $self->prompt(
    "Mixer - Video mixer interface y/n",
    "y",
  );

  if ($answer =~ /y/i) {
    push(@{$self->roles}, "mixer");
    $self->configure_remote_mixer();

    # We could run gst-switch srv or dvsink
    # on separate hosts, but this will do
    # for now.
    push(@{$self->roles}, "record");
    $self->configure_recordpath();
  }
}

method configure_ingest() {
  my $answer = $self->prompt(
    "Ingest - audio/video ingest y/n",
    "y",
  );

  if ($answer =~ /y/i) {
    push(@{$self->roles}, "ingest");
    
    $self->update_devices();
    $self->devices([]);
    
    foreach my $device (@{$self->{available_devices}{array}}) {
      my $ingest = $self->prompt(
        "Enable '".$device->{name}."' for ingest",
        "y",
      );
      push(@{$self->devices}, $device) if ($ingest =~ /y/i);
    }
    $self->configure_remote_mixer() if (! $self->{mixer}{host});
  }
}

method configure_remote_mixer() {
  # TODO: Setting config like this is terrible, 
  # figure out how to do it better
  my $answer = $self->prompt(
    "host - switching host",
    "127.0.0.1",
  );
  $self->{mixer}{host} = $answer ;
  $answer = $self->prompt(
    "port - switching port",
    "1234",
  );
  $self->{mixer}{port} = $answer;
}

method configure_backend() {
  my $answer = $self->prompt(
    "backend - DVswitch|GSTswitch",
    $self->backend,
  );
  if ($answer =~ /gst/i) {
    $self->backend('GSTswitch');
    return;
  } elsif ($answer =~ /dv/i) {
    $self->backend('DVswitch');
    return;
  } else {
    say "Invalid backend";
    $self->configure_backend;
    return
  }
}

method configure_recordpath() {
  say '$room + $date can be used as variables in the path and';
  say 'will correctly be set and created at run time';
  my $answer = $self->prompt(
    "recordpath - ",
    '/tmp/$room/$date',
  );
  $self->record_path($answer);
}

method configure_room() {
  my $answer = $self->prompt(
    "Room - For record path and controller",
    $self->room,
  );
  $self->room($answer);
}

method prompt($question,$default?) { # inspired from here: http://alvinalexander.com/perl/edu/articles/pl010005
  if ($default) {
    print $question, " [", $default, "]: ";
  } else {
    print $question, ": ";
    $default = "";
  }

  $| = 1;               # flush
  $_ = <STDIN>;         # get input

  chomp;
  if ("$default") {
    return $_ ? $_ : $default;    # return $_ if it has a value
  } else {
    return $_;
  }
}

with('App::EventStreamr::Roles::Logger','App::EventStreamr::Roles::ConfigAPI');

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::EventStreamr::Config - A config object

=head1 VERSION

version 0.5

=head1 SYNOPSIS

This package provides core configuration methods. Class
will attempt to load the config via a number of different
methods and fall back to a default state.

=head1 DESCRIPTION



( run in 0.499 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )