More robust loading
This commit is contained in:
@@ -35,8 +35,8 @@ console.log('Temporary: '+tmp_directory);
|
||||
|
||||
let config = null;
|
||||
|
||||
exports.load_config = function () {
|
||||
console.log('*** Aether App Native export: load_config() ***');
|
||||
exports.load_init_config = function () {
|
||||
console.log('*** Aether App Native export: load_init_config() ***');
|
||||
|
||||
let cwd = process.cwd();
|
||||
console.log(`CWD: ${cwd}`);
|
||||
@@ -59,8 +59,8 @@ exports.load_config = function () {
|
||||
|
||||
// let config = null;
|
||||
let config_directory = null;
|
||||
// let default_config_path = path.join(process.cwd(),'aether_native_app_config.current.json');
|
||||
let default_config_path = 'aether_native_app_config.current.json';
|
||||
// let default_config_path = path.join(process.cwd(),'ae_native_app_config.current.json');
|
||||
let default_config_path = 'ae_native_app_config.current.json';
|
||||
console.log(`Default Config File (path): ${default_config_path}`);
|
||||
let config_path = null;
|
||||
|
||||
@@ -80,38 +80,38 @@ exports.load_config = function () {
|
||||
fs.mkdirSync(config_directory);
|
||||
console.log('Config directory created: '+config_directory);
|
||||
|
||||
//default_config_path = path.join(process.cwd(),'aether_native_app_config.current.json');
|
||||
// config_path = path.join(config_directory, 'aether_native_app_config.json');
|
||||
//default_config_path = path.join(process.cwd(),'ae_native_app_config.current.json');
|
||||
// config_path = path.join(config_directory, 'ae_native_app_config.json');
|
||||
// fs.copyFileSync(default_config_path, config_path);
|
||||
// console.log('Default config file copied: '+config_directory);
|
||||
}
|
||||
|
||||
config_path = path.join(config_directory, 'aether_native_app_config.json');
|
||||
config_path = path.join(config_directory, 'ae_native_app_config.json');
|
||||
|
||||
// Attempt to open the config file. The preferred location is based on the OS's config directory.
|
||||
if (fs.existsSync(config_path)) {
|
||||
console.log(`Config file (aether_native_app_config.json) found under ${config_directory}`);
|
||||
console.log(`Config file (ae_native_app_config.json) found under ${config_directory}`);
|
||||
} else if (!fs.existsSync(config_path) && fs.existsSync(default_config_path)) {
|
||||
fs.copyFileSync(default_config_path, config_path);
|
||||
console.log('Default config file copied: '+config_directory);
|
||||
|
||||
// config = JSON.parse(fs.readFileSync(config_path));
|
||||
// console.log('Config file read.');
|
||||
} else if (fs.existsSync(path.join(cwd, 'aether_native_app_config.json'))) {
|
||||
} else if (fs.existsSync(path.join(cwd, 'ae_native_app_config.json'))) {
|
||||
//fs.copyFileSync(default_config_path, config_path);
|
||||
//console.log('Default config file copied: '+config_directory);
|
||||
|
||||
console.log(`Config file (config.json) not found under ${config_directory}. Using config in CWD. ${cwd}`);
|
||||
config_path = path.join(cwd, 'aether_native_app_config.json');
|
||||
config_path = path.join(cwd, 'ae_native_app_config.json');
|
||||
|
||||
console.log(`Config file (config.json) not found under ${config_directory}. Using config in CWD. ${cwd}`);
|
||||
let found_config_path = path.join(cwd, 'aether_native_app_config.json');
|
||||
let found_config_path = path.join(cwd, 'ae_native_app_config.json');
|
||||
|
||||
fs.copyFileSync(found_config_path, config_path);
|
||||
console.log(`Found config file copied: ${config_directory}`);
|
||||
} else if (fs.existsSync(path.join(cwd, 'aether_native_app_config.current.json'))) {
|
||||
} else if (fs.existsSync(path.join(cwd, 'ae_native_app_config.current.json'))) {
|
||||
console.log(`Config file (config.json) not found under ${config_directory} or CWD. Using default config in CWD. ${cwd}`);
|
||||
default_config_path = path.join(cwd, 'aether_native_app_config.current.json');
|
||||
default_config_path = path.join(cwd, 'ae_native_app_config.current.json');
|
||||
|
||||
fs.copyFileSync(default_config_path, config_path);
|
||||
console.log(`Default config file copied: ${config_directory}`);
|
||||
@@ -121,8 +121,11 @@ exports.load_config = function () {
|
||||
return false;
|
||||
}
|
||||
|
||||
config = JSON.parse(fs.readFileSync(config_path));
|
||||
console.log('Config file read.', config);
|
||||
let local_config = JSON.parse(fs.readFileSync(config_path));
|
||||
console.log('Config file read.', local_config);
|
||||
config = local_config;
|
||||
|
||||
// Do some quick clean up.
|
||||
|
||||
config.home_directory = home_directory; // From the OS platform
|
||||
config.tmp_directory = tmp_directory; // From the OS platform
|
||||
@@ -149,6 +152,10 @@ exports.load_config = function () {
|
||||
console.log(`Host file temp directory created: ${config.host_file_temp_path}`);
|
||||
}
|
||||
|
||||
if (!config.account_id) {
|
||||
config.account_id = '_XY7DXtc9MY'; // Not ideal...?
|
||||
}
|
||||
|
||||
let import_config_to_ipc_result = ipcRenderer.invoke('import_config', config).then((result) => {
|
||||
console.log('IPC import config finished');
|
||||
// console.log(result);
|
||||
@@ -160,6 +167,146 @@ exports.load_config = function () {
|
||||
}
|
||||
|
||||
|
||||
exports.load_full_config = function (init_config) {
|
||||
console.log('*** Aether App Native export: load_config() ***');
|
||||
|
||||
console.log('Init config.', init_config);
|
||||
// config = init_config;
|
||||
|
||||
config = get_url_cfg(init_config).then((cfg) => {
|
||||
console.log('URL config file downloaded.', cfg);
|
||||
|
||||
let new_config = init_config;
|
||||
|
||||
// Update with remote device config settings from API.
|
||||
new_config.account_id = cfg.account_id_random;
|
||||
new_config.event_id = cfg.event_id_random;
|
||||
new_config.event_device_id = cfg.event_device_id;
|
||||
new_config.event_location_id = cfg.event_location_id;
|
||||
new_config.event_session_id = cfg.event_session_id;
|
||||
|
||||
new_config.check_event_device_loop_period = cfg.check_event_device_loop_period;
|
||||
new_config.check_event_location_loop_period = cfg.check_event_location_loop_period;
|
||||
new_config.check_event_loop_period = cfg.check_event_loop_period;
|
||||
new_config.check_event_session_loop_period = cfg.check_event_session_loop_period;
|
||||
|
||||
new_config.record_audio = cfg.record_audio;
|
||||
new_config.record_video = cfg.record_video;
|
||||
new_config.recording_path = cfg.recording_path;
|
||||
new_config.recording_path = new_config.recording_path.replace('[home]', home_directory);
|
||||
new_config.recording_path = new_config.recording_path.replace('[tmp]', home_directory);
|
||||
|
||||
new_config.home_directory = home_directory; // From the OS platform
|
||||
new_config.tmp_directory = tmp_directory; // From the OS platform
|
||||
|
||||
new_config.app_root_path = new_config.app_root_path.replace('[home]', home_directory);
|
||||
new_config.app_root_path = new_config.app_root_path.replace('[tmp]', tmp_directory);
|
||||
console.log(`App Root Path: ${new_config.app_root_path}`);
|
||||
|
||||
new_config.local_file_cache_path = new_config.local_file_cache_path.replace('[home]', home_directory);
|
||||
new_config.local_file_cache_path = new_config.local_file_cache_path.replace('[tmp]', tmp_directory);
|
||||
console.log(`Local File Cache Path: ${new_config.local_file_cache_path}`);
|
||||
if (fs.existsSync(new_config.local_file_cache_path)) {
|
||||
} else {
|
||||
fs.mkdirSync(new_config.local_file_cache_path);
|
||||
console.log(`Host file cache directory created: ${new_config.local_file_cache_path}`);
|
||||
}
|
||||
|
||||
new_config.host_file_temp_path = new_config.host_file_temp_path.replace('[home]', home_directory);
|
||||
new_config.host_file_temp_path = new_config.host_file_temp_path.replace('[tmp]', tmp_directory);
|
||||
console.log(`Host file temp path: ${new_config.host_file_temp_path}`);
|
||||
if (fs.existsSync(new_config.host_file_temp_path)) {
|
||||
} else {
|
||||
fs.mkdirSync(new_config.host_file_temp_path);
|
||||
console.log(`Host file temp directory created: ${new_config.host_file_temp_path}`);
|
||||
}
|
||||
|
||||
let import_config_to_ipc_result = ipcRenderer.invoke('import_config', new_config).then((result) => {
|
||||
console.log('IPC import config finished');
|
||||
// console.log(result);
|
||||
return result; // Result should be "true"
|
||||
})
|
||||
|
||||
return new_config
|
||||
})
|
||||
|
||||
//console.log(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
async function get_url_cfg(cfg) {
|
||||
let base_url = `${cfg.api_protocol}://${cfg.api_server}:${cfg.api_port}/${cfg.api_path}`;
|
||||
|
||||
let axios_api = axios.create({
|
||||
baseURL: base_url,
|
||||
timeout: 60000, // in milliseconds; 60000 = 60 seconds
|
||||
/* other custom settings */
|
||||
});
|
||||
// axios_api.defaults.headers = cfg['headers'];
|
||||
|
||||
// axios.defaults.baseURL = `${cfg.api_protocol}://${cfg.api_server}:${cfg.api_port}/${cfg.api_path}`;
|
||||
axios_api.defaults.headers.common['Access-Control-Allow-Origin'] = cfg.access_control_allow_origin; // '*'; // app_cfg.access_control_allow_origin;
|
||||
axios_api.defaults.headers.common['content-type'] = 'application/json';
|
||||
axios_api.defaults.headers.common['x-aether-api-key'] = cfg.api_secret_key;
|
||||
// axios_api.defaults.headers.common['x-account-id'] = cfg.account_id;
|
||||
|
||||
let event_device_id = 'dbgMWS3KEHE';
|
||||
let endpoint = `/event/device/${event_device_id}`;
|
||||
|
||||
let params = {'event_device_code': 'asdf'};
|
||||
|
||||
let response_data_promise = await axios_api.get(
|
||||
endpoint,
|
||||
{
|
||||
params: params,
|
||||
onDownloadProgress: (progressEvent) => {
|
||||
let percent_completed = Math.round(
|
||||
(progressEvent.loaded * 100) / progressEvent.total
|
||||
);
|
||||
console.log('GET Data Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed);
|
||||
|
||||
// temp_get_object_percent_completed = percent_completed;
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(function (response) {
|
||||
console.log(`Response: ${response}`);
|
||||
|
||||
let return_data = response.data['data'];
|
||||
if (Array.isArray(return_data)) {
|
||||
console.log(`Data result is an array/list. Array length: ${return_data.length}`);
|
||||
} else {
|
||||
console.log(`Data result is a dictionary/object, not an array/list.`);
|
||||
}
|
||||
return return_data;
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(`Base URL: ${base_url} | Endpoint: ${endpoint}`);
|
||||
|
||||
console.log('Error Message:', error.message); // Is this needed here or below in the in the else portion???
|
||||
if (error.response) {
|
||||
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
|
||||
} else {
|
||||
console.log('Error:', error);
|
||||
}
|
||||
|
||||
if (error.response && error.response.status === 404) {
|
||||
return null; // Returning null since there were no results
|
||||
}
|
||||
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
|
||||
});
|
||||
|
||||
return response_data_promise;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Check for local file
|
||||
// Updated 2022-05-06
|
||||
exports.check_local_file = async function ({local_file_path, filename}) {
|
||||
|
||||
Reference in New Issue
Block a user