diff --git a/.gitignore b/.gitignore index e3200e0..2273c67 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* +bin/config.yml diff --git a/bin/NETRAVE b/bin/NETRAVE index 370fa9b..3597643 100644 --- a/bin/NETRAVE +++ b/bin/NETRAVE @@ -1,10 +1,17 @@ #!/usr/bin/env ruby -# Require necessary files +require 'yaml' require_relative '../lib/utils/system_information_gather.rb' require_relative '../lib/utils/database_manager.rb' require_relative '../lib/utils/first_run_init.rb' +# Check if config.yml exists, if not, create it +unless File.exist?('config.yml') + first_run_init = FirstRunInit.new + db_details = first_run_init.ask_for_db_details + first_run_init.write_db_details_to_config_file(db_details) +end + # Load database configuration db_details = YAML.load_file('config.yml') @@ -18,7 +25,7 @@ else end # Initialize the first run setup -first_run_setup = FirstRunSetup.new(db_manager) +first_run_setup = FirstRunInit.new(db_manager) first_run_setup.run # TODO: Add the rest of your application logic here diff --git a/lib/utils/database_manager.rb b/lib/utils/database_manager.rb index c18eb35..c99562f 100644 --- a/lib/utils/database_manager.rb +++ b/lib/utils/database_manager.rb @@ -1,10 +1,11 @@ +require 'sequel' +require 'mysql2' require_relative 'system_information_gather.rb' require_relative '../utils/utilities' class DatabaseManager def initialize @db = nil - @info_gatherer = SystemInformationGather.new end def test_db_connection(db_details) @@ -30,11 +31,21 @@ class DatabaseManager end end - def store_system_info - system_info = @info_gatherer.gather_system_info - system_info[:uplink_speed] = Utilities.convert_speed_to_mbps(system_info[:uplink_speed]) - system_info[:downlink_speed] = Utilities.convert_speed_to_mbps(system_info[:downlink_speed]) - system_info[:services] = Utilities.services_to_hash(system_info[:services]) + def store_system_info(system_info) @db[:system_info].insert(system_info) + end + + def create_services_table + @db.create_table :services do + primary_key :id + String :service_name + Boolean :status + end + end + + def store_services(services) + services.each do |service, status| + @db[:services].insert(service_name: service, status: status) + end end end \ No newline at end of file diff --git a/lib/utils/first_run_init.rb b/lib/utils/first_run_init.rb index 36b7fb7..9c92070 100644 --- a/lib/utils/first_run_init.rb +++ b/lib/utils/first_run_init.rb @@ -6,20 +6,22 @@ require_relative 'utilities.rb' class FirstRunInit include Utilities - def initialize - @db_manager = DatabaseManager.new - @info_gatherer = SystemInformationGather.new + def initialize(db_manager) + @db_manager = db_manager + @info_gatherer = SystemInformationGather.new(@db_manager) + end + + def run + first_run_setup end def first_run_setup - color = @info_gatherer.ask_for_color - # TODO: Use the color for something - db_details = @info_gatherer.ask_for_db_details + db_details = ask_for_db_details while !@db_manager.test_db_connection(db_details) Curses.setpos(4, 0) Curses.addstr("Whoops! We couldn't connect to the database with the details you provided. Please try again!") Curses.refresh - db_details = @info_gatherer.ask_for_db_details + db_details = ask_for_db_details end @db_manager.create_system_info_table uplink_speed = @info_gatherer.ask_for_uplink_speed @@ -32,6 +34,34 @@ class FirstRunInit @db_manager.store_system_info(services) end + def ask_for_db_details + Curses.clear + Curses.setpos(1, 0) + Curses.addstr("Please enter your database username: ") + Curses.refresh + username = Curses.getstr.strip + + Curses.setpos(2, 0) + Curses.addstr("Please enter your database password: ") + Curses.refresh + Curses.noecho + password = Curses.getstr.strip + Curses.echo + + Curses.setpos(3, 0) + Curses.addstr("Please enter your database name: ") + Curses.refresh + database = Curses.getstr.strip + + { username: username, password: password, database: database } + end + + def write_db_details_to_config_file(db_details) + File.open("config.yml", "w") do |file| + file.write(db_details.to_yaml) + end + end + def ask_for_default_mode while true Curses.setpos(8, 0) diff --git a/lib/utils/system_information_gather.rb b/lib/utils/system_information_gather.rb index eab72f1..c24cc56 100644 --- a/lib/utils/system_information_gather.rb +++ b/lib/utils/system_information_gather.rb @@ -1,10 +1,41 @@ require 'curses' require 'yaml' require_relative 'utilities.rb' +require_relative 'database_manager.rb' class SystemInformationGather include Utilities + def initialize(db_manager) + @db_manager = db_manager + end + + def gather_system_info + uplink_speed = ask_for_uplink_speed + downlink_speed = ask_for_downlink_speed + services = ask_for_services + + total_bandwidth = uplink_speed + downlink_speed + + system_info = { + uplink_speed: uplink_speed, + downlink_speed: downlink_speed, + total_bandwidth: total_bandwidth + } + + # Check if the system_info table exists, if not, create it + @db_manager.create_system_info_table unless @db_manager.table_exists?(:system_info) + + # Store the gathered system info in the database + @db_manager.store_system_info(system_info) + + # Check if the services table exists, if not, create it + @db_manager.create_services_table unless @db_manager.table_exists?(:services) + + # Store the services in the services table + @db_manager.store_services(services) + end + def ask_for_uplink_speed while true Curses.clear @@ -12,29 +43,13 @@ class SystemInformationGather Curses.addstr("Please enter your uplink speed (upload speed, e.g., 1000Mbps or 1Gbps). ") Curses.addstr("This is typically the maximum upload speed provided by your ISP. ") Curses.addstr("You can check your ISP bill, use an online speed test, or contact your ISP if you're unsure. ") + Curses.addstr(" ") + Curses.setpos(5, 0) + Curses.addstr("Uplink Speed: ") Curses.refresh speed = Curses.getstr.strip.downcase if valid_speed?(speed) - return convert_speed_to_mbps(speed) - else - Curses.setpos(3, 0) - Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!") - Curses.refresh - end - end - end - - def ask_for_downlink_speed - while true - Curses.clear - Curses.setpos(4, 0) - Curses.addstr("Please enter your downlink speed (download speed, e.g., 1000Mbps or 1Gbps). ") - Curses.addstr("This is typically the maximum download speed provided by your ISP. ") - Curses.addstr("You can check your ISP bill, use an online speed test, or contact your ISP if you're unsure. ") - Curses.refresh - speed = Curses.getstr.strip.downcase - if valid_speed?(speed) - return convert_speed_to_mbps(speed) + return speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i else Curses.setpos(5, 0) Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!") @@ -42,6 +57,27 @@ class SystemInformationGather end end end + + def ask_for_downlink_speed + while true + Curses.clear + Curses.setpos(2, 0) + Curses.addstr("Please enter your downlink speed (download speed, e.g., 1000Mbps or 1Gbps). ") + Curses.addstr("This is typically the maximum download speed provided by your ISP. ") + Curses.addstr("You can check your ISP bill, use an online speed test, or contact your ISP if you're unsure. ") + Curses.setpos(5, 0) + Curses.addstr("Downlink Speed: ") + Curses.refresh + speed = Curses.getstr.strip.downcase + if valid_speed?(speed) + return speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i + else + Curses.setpos(5, 0) + Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!") + Curses.refresh + end + end + end def valid_speed?(speed) speed.to_i > 0 @@ -75,31 +111,4 @@ class SystemInformationGather services.each { |service| services_hash[service] = true } services_hash end - - def ask_for_db_details - Curses.clear - Curses.setpos(1, 0) - Curses.addstr("Please enter your database username: ") - Curses.refresh - username = Curses.getstr.strip - - Curses.setpos(2, 0) - Curses.addstr("Please enter your database password: ") - Curses.refresh - Curses.echo = false - password = Curses.getstr.strip - Curses.echo - Curses.setpos(3, 0) - Curses.addstr("Please enter your database name: ") - Curses.refresh - database = Curses.getstr.strip - - { username: username, password: password, database: database } - end - - def write_db_details_to_config_file(db_details) - File.open("config.yml", "w") do |file| - file.write(db_details.to_yaml) - end - end end \ No newline at end of file