Staring work on getting Electron working from Svelte app. Wrapping up for the day.

This commit is contained in:
Scott Idem
2024-08-14 19:23:29 -04:00
parent cc4588c8a6
commit 146bea0a67
12 changed files with 1957 additions and 508 deletions

View File

@@ -21,10 +21,12 @@ import { events_loc, events_sess, events_slct, events_trigger, events_trig } fro
import { events_func } from '$lib/ae_events_functions';
import { kill_processes, open_local_file_v2, run_cmd, run_cmd_sync, run_osascript, get_device_info } from './electron.js';
import { kill_processes, open_local_file_v2, run_cmd, run_cmd_sync, run_osascript, get_device_info } from '$lib/electron_relay.js';
import Event_launcher_menu from './launcher_menu.svelte';
import Event_launcher_session_view from './launcher_session_view.svelte';
// import native_app from '$lib/electron_native.js';
// Exports
@@ -247,8 +249,92 @@ if (browser) {
onMount(() => {
console.log('Pres Mgmt Launcher [slug]: +page@(launcher).svelte');
// if ($ae_event_launcher.app_mode == 'native') {
// handle_get_device_info($slct.event_device_id);
// }
// const native_app = require('$lib./script');
// let native_app_config = native_app.load_init_config();
// console.log('Native App Initial Config:', native_app_config);
});
async function handle_get_device_info(event_device_id) {
console.log('*** handle_get_device_info() ***');
// console.log(event_device_id);
// console.log('GET DEVICE INFO!!! GET DEVICE INFO!!! GET DEVICE INFO!!!');
let get_device_info_promise = get_device_info({event_device_id: event_device_id});
get_device_info_promise.then(function (result) {
// console.log('GOT DEVICE INFO!!! GOT DEVICE INFO!!! GOT DEVICE INFO!!!');
console.log(get_device_info_promise);
let event_device_data = {};
event_device_data['other_json'] = JSON.stringify(result);
event_device_data['info_hostname'] = result.hostname;
// console.log(event_device_data['other_json']);
// console.log(result.networkInterfaces);
let ni_data_li = [];
let ip_address_li = [];
for (let network_interface in result.networkInterfaces) {
// console.log(network_interface);
let ni_data = {};
if (network_interface.startsWith('en')) {
console.log('Wired or wireless network interface found');
ni_data['name'] = network_interface;
let ni_details = result.networkInterfaces[network_interface];
// console.log(ni_details);
for (let i=0; i < ni_details.length; i++) {
ni_data['address'] = ni_details[i].address;
ni_data['mac'] = ni_details[i].mac;
ip_address_li.push(ni_details[i].address);
}
ni_data_li.push(ni_data);
}
}
event_device_data['meta_json'] = JSON.stringify(ni_data_li);
console.log(event_device_data['meta_json']);
event_device_data['info_ip_list'] = ip_address_li.join('; ');
console.log(event_device_data['info_ip_list']);
event_device_obj_up_promise = update_event_device_obj({event_device_id: event_device_id, data: event_device_data, log_lvl: 0})
.then(function (up_event_device_result) {
// console.log('UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!!');
if (up_event_device_result) {
console.log(up_event_device_result);
}
})
.catch(function (error) {
console.log('No results returned or failed.');
console.log(error);
});
});
return get_device_info_promise;
}
</script>

View File

@@ -1,318 +0,0 @@
/* ### Electron Specific JavaScript ### */
// import crypto from 'crypto';
// import {fs} from 'fs';
// import path from 'path';
// const crypto = require('crypto');
// const fs = require('fs');
// const fs_promises = require('node:fs/promises');
// const path = require('path');
// const { ipcRenderer } = require('electron');
// function sleep(milliseconds) {
// const date = Date.now();
// let currentDate = null;
// do {
// currentDate = Date.now();
// } while (currentDate - date < milliseconds);
// }
// // exports.check_hash_file_cache should no longer be needed with this.
// // Updated 2022-10-11
// export let check_hash_file_cache_v2 = async function check_hash_file_cache_v2({local_file_cache_path, hash, check_hash=false}) {
// console.log('*** check_hash_file_cache_v2() ***');
// console.log(`Host File Cache Path: ${local_file_cache_path}; Hash: ${hash}`);
// let hash_filename = `${hash}.file`;
// let subdirectory = hash_filename.substring(0,2);
// let subdirectory_path = path.join(local_file_cache_path, subdirectory);
// if (fs.existsSync(subdirectory_path)) {
// } else {
// console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`);
// return null;
// }
// let hash_file_cache_path = path.join(subdirectory_path, hash_filename);
// if (fs.existsSync(hash_file_cache_path)) {
// console.log(`Hashed file exists in cache: ${hash_file_cache_path}`);
// if (check_hash) {
// const file_buffer = fs.readFileSync(hash_file_cache_path);
// const file_hash_sha256 = crypto.createHash('sha256');
// file_hash_sha256.update(file_buffer);
// const file_hash_sha256_check = file_hash_sha256.digest('hex');
// if (file_hash_sha256_check == hash) {
// console.log('File hash match', file_hash_sha256_check);
// } else {
// console.log('File hash does not match', file_hash_sha256_check);
// return false;
// }
// }
// return true;
// } else {
// console.log(`Hashed file not found in cache: ${hash_file_cache_path}`);
// return null;
// }
// }
// Updated 2022-05-07
export let kill_processes = async function kill_processes({process_name_li=[]}) {
console.log('*** kill_processes() ***');
console.log(`Process Name List: ${process_name_li}`);
let fail_flag = null;
if (process_name_li) {
for (let i = 0; i < process_name_li.length; i++) {
// separate the keys and the values
let process_name = process_name_li[i];
let signal = null;
if (process_name == 'osit_aperture_wrapper') {
signal = 'INT'; // INT (interrupt) correctly stops the wrapper
}
let kill_processes_result = await native_app.kill_processes({process_name: process_name, signal: signal});
console.log(kill_processes_result);
if (kill_processes_result) {
console.log('Killed process.');
// return kill_processes_result;
} else {
console.log('Did not kill process. Something went wrong.');
fail_flag = true;
// return false;
}
}
}
return fail_flag;
// let kill_processes_result = await native_app.kill_processes({process_name: process_name});
// console.log(kill_processes_result);
// if (kill_processes_result) {
// console.log('Killed process.');
// return kill_processes_result;
// } else {
// console.log('Did not kill process. Something went wrong.');
// return false;
// }
}
// Updated 2022-05-06
export let open_local_file = async function open_local_file({file_path, filename}) {
console.log('*** open_local_file() ***');
console.log(`File Path: ${file_path}; Filename: ${filename}`);
console.log('Process: Check local hash file cache, Download hash file to cache, and Open cached hash file after copying to temp directory');
// let check_local_file_result = await native_app.check_local_file({local_file_path: file_path, filename: filename});
// console.log(check_local_file_result);
// if (check_local_file_result) {
// console.log('Local file found.');
// } else {
// console.log('Local file not found. Will not attempt to open.');
// return false;
// }
// console.log('Local file file found and ready to be opened.');
let open_local_file_result = await native_app.open_local_file({local_file_path: file_path, filename: filename});
console.log(open_local_file_result);
if (open_local_file_result) {
console.log('Local file was opened.');
return open_local_file_result;
} else {
console.log('Local file was not opened. Something went wrong.');
return false;
}
}
// exports.open_local_file should no longer be needed with this.
// Updated 2022-10-11
export let open_local_file_v2 = async function open_local_file_v2({file_path, filename}) {
console.log('*** open_local_file_v2() ***');
console.log(`Local File Path: ${file_path}; Filename: ${filename}`);
console.log('Process: Check local hash file cache, Download hash file to cache, and Open cached hash file after copying to temp directory');
let open_local_file_result = await ipcRenderer.invoke('open_local_file', file_path, filename).then((result) => {
console.log('IPC open local file finished');
if (result) {
console.log('Local file was opened.');
return result;
} else {
console.log('Local file was not opened. Something went wrong.');
console.log(result);
return false;
}
console.log(result);
return true;
})
return open_local_file_result;
}
// // Updated 2022-05-06
// export let open_hash_file_to_temp = async function open_hash_file_to_temp({local_file_cache_path, hash, host_file_temp_path, filename}) {
// console.log('*** open_hash_file_to_temp() ***');
// console.log(`Host File Cache Path: ${local_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`);
// console.log('Process: Check local hash file cache, Download hash file to cache, and Open cached hash file after copying to temp directory');
// let open_hash_file_to_temp_result = await native_app.open_hash_file_to_temp({local_file_cache_path: local_file_cache_path, hash: hash, host_file_temp_path: host_file_temp_path, filename: filename});
// console.log(open_hash_file_to_temp_result);
// if (open_hash_file_to_temp_result) {
// console.log('Local hash file was opened from temp directory.');
// return open_hash_file_to_temp_result;
// } else {
// console.log('Local hash file was not opened from the temp directory. Something went wrong.');
// return false;
// }
// }
// // exports.open_hash_file_to_temp should no longer be needed with this.
// // Updated 2022-10-11
// export let open_hash_file_to_temp_v2 = async function open_hash_file_to_temp_v2({local_file_cache_path, hash, host_file_temp_path, filename}) {
// console.log('*** open_hash_file_to_temp_v2() ***');
// console.log(`Host File Cache Path: ${local_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`);
// console.log('Process: Check local hash file cache, Download hash file to cache, and Open cached hash file after copying to temp directory');
// let subdirectory = hash.substring(0,2);
// let subdirectory_path = path.join(local_file_cache_path, subdirectory);
// if (fs.existsSync(subdirectory_path)) {
// } else {
// console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`);
// return null;
// }
// let hash_filename = hash+'.file';
// let full_cache_file_path = path.join(subdirectory_path, hash_filename);
// console.log(full_cache_file_path);
// const file_buffer = fs.readFileSync(full_cache_file_path);
// const file_hash_sha256 = crypto.createHash('sha256');
// file_hash_sha256.update(file_buffer);
// const file_hash_sha256_check = file_hash_sha256.digest('hex');
// if (file_hash_sha256_check == hash) {
// console.log('File hash match', file_hash_sha256_check);
// } else {
// console.log('File hash does not match', file_hash_sha256_check);
// // await setTimeout(async () => {console.log('Done waiting????'); open_file_clicked = false;}, 5000);
// // sleep(6000);
// // console.log('???????? WAITED X SECONDS ????????');
// return false;
// }
// let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', subdirectory_path, hash, host_file_temp_path, filename).then((result) => {
// console.log('IPC open hash file to temp finished');
// if (result) {
// console.log('Local hash file was opened from temp directory.');
// return result;
// } else {
// console.log('Local hash file was not opened from the temp directory. Something went wrong.');
// console.log(result);
// return false;
// }
// })
// return open_hash_file_to_temp_result;
// }
// Updated 2022-05-07
export let run_cmd = async function run_cmd({cmd=null, return_stdout=null}) {
console.log('*** run_cmd() ***');
let run_cmd_result = await native_app.run_cmd({cmd: cmd, return_stdout: return_stdout})
.then(function (result) {
if (result) {
console.log('Command ran');
} else {
console.log('Command did not run. Something went wrong.');
return false;
}
if (return_stdout) {
return result;
} else {
return true;
}
});
console.log('Run Command Result:', run_cmd_result);
return run_cmd_result;
}
// Updated 2022-10-27
export let run_cmd_sync = function run_cmd_sync({cmd=null, return_stdout=null}) {
console.log('*** run_cmd_sync() ***');
let run_cmd_result = native_app.run_cmd_sync({cmd: cmd, return_stdout: return_stdout});
// if (run_cmd_result) {
// console.log('Command ran');
// } else {
// console.log('Command did not run. Something went wrong.');
// // return false;
// }
// if (return_stdout) {
// return run_cmd_result;
// } else {
// return true;
// }
console.log('Run Command Result:', run_cmd_result);
return run_cmd_result;
}
// Updated 2022-05-07
export let run_osascript = async function run_osascript({cmd=null, interactive=false, language=null, flags='h', program_file=null}) {
console.log('*** run_osascript() ***');
let run_osascript_result = await native_app.run_osascript({cmd: cmd, interactive: interactive, language: language, flags: flags, program_file: program_file});
console.log(run_osascript_result);
if (run_osascript_result) {
console.log('Apple Script ran');
} else {
console.log('Apple Script did not run. Something went wrong.');
}
return run_osascript_result;
}
// Updated 2022-05-07
export let get_device_info = async function get_device_info({event_device_id}) {
console.log('*** get_device_info() ***');
console.log(event_device_id);
let get_device_info_result = await native_app.get_device_info();
console.log(get_device_info_result);
if (get_device_info_result) {
console.log('Success');
} else {
console.log('Failed? Something went wrong.');
}
return get_device_info_result;
}

View File

@@ -66,7 +66,7 @@ let hover_timer: any = null;
$events_slct.event_session_id = event_session_obj?.id;
$events_slct.event_session_obj = event_session_obj;
ae_promises.slct_event_session_id = ({
ae_promises.slct_event_session_id = events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: event_session_obj?.id,
inc_file_li: true,
@@ -96,7 +96,7 @@ let hover_timer: any = null;
$events_slct.event_session_id = event_session_obj?.id;
$events_slct.event_session_obj = event_session_obj;
ae_promises.slct_event_session_id = ({
ae_promises.slct_event_session_id = events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: event_session_obj?.id,
inc_file_li: true,

View File

@@ -194,9 +194,7 @@ function process_search_string(search_str: string) {
// params['qry__limit'] = 35;
// }
search__event_session({
api_cfg: $ae_api,
event_id: $events_slct.event_id,
handle_search__event_session({
ft_search_str: ft_search_str_new,
lk_search_str: lk_search_str_new,
// fulltext_search_qry_str: ft_search_str_new,
@@ -213,8 +211,6 @@ function process_search_string(search_str: string) {
async function handle_search__event_session(
{
api_cfg,
event_id,
ft_search_str = '',
lk_search_str = '',
search_delay = 0,
@@ -227,8 +223,6 @@ async function handle_search__event_session(
try_cache=false,
log_lvl=1,
}: {
api_cfg: any,
event_id: string,
ft_search_str?: string,
lk_search_str?: string,
search_delay?: number, // In milliseconds
@@ -269,7 +263,7 @@ async function handle_search__event_session(
$events_sess.pres_mgmt.status_qry__search = 'loading';
search_submit_results = events_func.handle_search__event_session({
search_submit_results = events_func.search__event_session({
api_cfg: $ae_api,
event_id: $events_slct.event_id,
// type_code: type_code,

View File

@@ -228,14 +228,16 @@ async function handle_qry__event_session(
enabled = $events_loc.pres_mgmt.qry_enabled ?? 'enabled',
hidden = $events_loc.pres_mgmt.qry_hidden ?? 'not_hidden',
limit = $events_loc.pres_mgmt.qry_limit__session ?? 150,
log_lvl=2,
log_lvl=0,
}: {
qry_files?: boolean,
// file_count?: boolean,
// file_count_all?: null|number,
// ft_search_str?: string,
// lk_search_str?: string,
params?: key_val,
enabled?: string,
hidden?: string,
limit?: number,
log_lvl?: number,
}
) {
@@ -263,12 +265,14 @@ async function handle_qry__event_session(
$events_sess.pres_mgmt.status_qry__search = 'processing';
$events_sess.pres_mgmt.status_rpt[$events_sess.pres_mgmt.show_report] = 'processing';
$events_slct.event_session_obj_li = search_results;
console.log(search_results);
if (log_lvl) {
console.log(`Search results:`, search_results);
}
// $events_sess.pres_mgmt.status_qry__search = 'done';
})
.finally(() => {
if (log_lvl) {
console.log('TEST SEARCH - Search done. Pulling out the event_session_id_randoms.');
console.log('API query done. Pulling out the event_session_id_random values.');
}
// console.log(`TEST search: ${$lq_kv__event_session_obj_li}`);

View File

@@ -150,7 +150,7 @@ let ae_triggers: key_val = {};
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: log_lvl
@@ -204,7 +204,7 @@ let ae_triggers: key_val = {};
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: log_lvl
@@ -257,7 +257,7 @@ let ae_triggers: key_val = {};
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: log_lvl

View File

@@ -148,7 +148,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: 1
@@ -177,7 +177,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: 1
@@ -213,7 +213,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random
});
@@ -241,7 +241,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random
});
@@ -287,7 +287,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: 1
@@ -421,7 +421,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: 1
@@ -625,7 +625,7 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
({
events_func.load_ae_obj_id__event_session({
api_cfg: $ae_api,
event_session_id: $lq__event_session_obj?.event_session_id_random,
log_lvl: 1
@@ -875,7 +875,9 @@ $: if ($lq__event_session_obj) {
on:ae_crud_updated={e => {
console.log(`ae_crud_updated:`, e.detail);
events_func.load_ae_obj_id__event_presentation({api_cfg: $ae_api, event_presentation_id: event_presentation_obj.event_presentation_id_random, log_lvl: 1})
events_func.load_ae_obj_id__event_presentation({
api_cfg: $ae_api, event_presentation_id: event_presentation_obj.event_presentation_id_random, log_lvl: 1
})
.then(function (load_results) {
})
.then(function (load_results) {