Skip to content

Commit

Permalink
Creating single-shot onboarding process
Browse files Browse the repository at this point in the history
  • Loading branch information
nexxai committed Jul 24, 2023
1 parent 1d7687e commit 2b7aaa8
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ APP_NAME='LG TV Messenger'
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
TV_KEY=e2e7b9a56e31dc60782be1485ac822ab
APP_KEY=
1 change: 1 addition & 0 deletions .github/workflows/RunTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ jobs:
run: |
cp .env.example .env
php -v
php artisan key:generate
./vendor/bin/pest --parallel --coverage-clover=coverage.xml
bash <(curl -s https://codecov.io/bash) || echo 'Codecov failed to upload'
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ yarn-error.log
/.fleet
/.idea
/.vscode
/config/nexxaitvs.php
/config/lgtvs.php
/public/aiowebostv/__pycache__
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## What in the...

Have you ever needed to send a message to someone in another room of your house who was watching TV with headphones on but wasn't great at checking their phone? Then this app might be for you. Run this app on a server in your house, connect to it from your phone, and have fun spamming your friends and family!
Have you ever needed to send a message to someone in another room of your house who was watching TV with headphones on but wasn't great at checking their phone? Or maybe you just want to annoy the everliving crap out of your spouse? Then this app might be for you. Run it on a server in your house, connect to it from your phone, and have fun spamming your friends and family for years to come!

### Notes

Expand All @@ -22,6 +22,8 @@ This app requires PHP, NodeJS *and* Python. You must have the following version
## Instructions

1. Run `composer install`, `npm install`, and `npm run build`
2. Copy the `.env.example` file in the root directory to a file called `.env`, and update the `APP_URL` fields to appropriate values
3. Get the client key from your TV by running `php artisan lg:add` and following the instructions
2. Run `php artisan lg:first-time`
3. Open the newly created `.env` file, and update the `APP_URL` fields to appropriate values
4. Point your web server's root directory to the `/public` folder in this distribution.

If you ever need to add another TV to the list, you can run `php artisan lg:add` and follow the prompts
18 changes: 8 additions & 10 deletions app/Console/Commands/AddTVKeyToConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,33 @@ public function handle()
{
$name = $this->ask('Friendly name for the TV');
$ip = $this->ask('IP address of TV');
$exec = PythonExec::get();

$this->components->info('Attempting to pair with TV');

$exec = PythonExec::handle();
$key = Process::timeout(120)->start("{$exec} public/key.py -t {$ip}");

$counter = 0;
while ($key->running()) {
if ($counter % 5 == 0) {
$this->warn('You must accept the prompt on screen to pair this app with the TV.');
$this->components->warn('You must accept the prompt on screen to pair this app with the TV.');
}

sleep(1);
$counter++;
}

$key_name = str($name)->snake()->upper().'_TV_KEY';
$client_key = $key->output();

$env_line = $key_name.'='.$client_key;
$client_key = trim($key->output());

$tvInstance = [
'name' => $name,
'key' => "env({$key_name})",
'key' => $client_key,
'ip' => $ip,
];

$config = new ConfigMaintain();
$config->add($tvInstance, $env_line);
$config->add($tvInstance);

$this->newLine();
$this->info("Added TV {$name} to the configuration");
$this->components->info("Added TV '{$name}' to the configuration");
}
}
34 changes: 34 additions & 0 deletions app/Console/Commands/FirstTimeSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Console\Commands;

use App\Services\ConfigMaintain;
use App\Services\CpExec;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

class FirstTimeSetup extends Command
{
protected $signature = 'lg:first-time';

protected $description = 'Prepare the environment for the first time';

public function handle()
{
if (! file_exists(base_path().'/.env')) {
$copyCommand = CpExec::handle();
Process::run("{$copyCommand} .env.example .env");
$this->components->info('.env file created');
}

if (! file_exists(base_path().'/config/lgtvs.php')) {
$config = new ConfigMaintain();
$config->create_lgtvs_file();
$this->components->info('Config file created');
}

$this->call('lg:add');

$this->call('key:generate');
}
}
7 changes: 1 addition & 6 deletions app/Http/Livewire/MessagePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ class MessagePage extends Component

public function mount()
{
if (! empty(config('nexxaitvs'))) {
$tvs = array_merge(config('lgtvs'), config('nexxaitvs'));
} else {
$tvs = config('lgtvs');
}
$this->tvList = $tvs;
$this->tvList = config('lgtvs');
}

public function sendMessage(LGTVMessenger $messenger)
Expand Down
34 changes: 23 additions & 11 deletions app/Services/ConfigMaintain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@

class ConfigMaintain
{
public function add($instance, $env_line)
public string $config_base_path;

public function __construct()
{
$config_path = base_path().'/config/lgtvs.php';
$env_path = base_path().'/.env';
$this->config_base_path = base_path().'/config/lgtvs.php';
}

$config = include $config_path;
$env = file_get_contents($env_path);
public function create_lgtvs_file(): void
{
$empty_config_file = <<< 'PHP'
<?php
$config[] = $instance;
$newConfig = "<?php\n\nreturn ".$this->varexport($config).';';
file_put_contents($config_path, $newConfig);
return [
];
PHP;

$env = $env."\n".$env_line;
file_put_contents($env_path, $env);
file_put_contents($this->config_base_path, $empty_config_file);
}

public function add($instance)
{
$config = include $this->config_base_path;

$config[] = $instance;
$newConfig = "<?php\n\nreturn ".$this->better_var_export($config).';';
file_put_contents($this->config_base_path, $newConfig);
}

protected function varexport($expression)
protected function better_var_export($expression)
{
$export = var_export($expression, true);
$export = preg_replace('/^([ ]*)(.*)/m', '$1$1$2', $export);
Expand Down
17 changes: 17 additions & 0 deletions app/Services/CpExec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Services;

class CpExec
{
public static function handle(): string
{
$os = OS::detect();

if ($os == OS::WINDOWS) {
return 'copy';
} else {
return 'cp';
}
}
}
2 changes: 1 addition & 1 deletion app/Services/LGTVMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function send(string $message)
throw new Exception('No message provided');
}

$exec = PythonExec::get();
$exec = PythonExec::handle();

Process::run("{$exec} -m pip install -r requirements.txt");

Expand Down
18 changes: 18 additions & 0 deletions app/Services/OS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Services;

class OS
{
const WINDOWS = 1;
const NONWINDOWS = 2;

public static function detect(): int
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return self::WINDOWS;
} else {
return self::NONWINDOWS;
}
}
}
6 changes: 4 additions & 2 deletions app/Services/PythonExec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

class PythonExec
{
public static function get()
public static function handle(): string
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$os = OS::detect();

if($os == OS::WINDOWS) {
return 'python';
} else {
return 'python3';
Expand Down

0 comments on commit 2b7aaa8

Please sign in to comment.