Saving work after rounds of updates to the event object type functions.

This commit is contained in:
Scott Idem
2025-05-23 16:17:42 -04:00
parent 589320a850
commit 7c70d93a68
6 changed files with 918 additions and 92 deletions

View File

@@ -1,6 +1,7 @@
import type { key_val } from '$lib/ae_stores';
import { api } from '$lib/api';
import { db_save_ae_obj_li__ae_obj } from "$lib/ae_core/core__idb_dexie";
import { db_events } from "$lib/ae_events/db_events";
import { load_ae_obj_id__event_location } from './ae_events__event_location';
@@ -8,7 +9,7 @@ import { load_ae_obj_id__event_location } from './ae_events__event_location';
let ae_promises: key_val = {};
// Updated 2024-10-16
// Updated 2025-05-23
export async function load_ae_obj_id__event_device(
{
api_cfg,
@@ -39,14 +40,33 @@ export async function load_ae_obj_id__event_device(
params: params,
log_lvl: log_lvl
})
.then(function (event_device_obj_get_result) {
.then(async function (event_device_obj_get_result) {
if (event_device_obj_get_result) {
if (try_cache) {
// This is expecting a list
db_save_ae_obj_li__event_device({
obj_type: 'event_device',
obj_li: [event_device_obj_get_result]
let processed_obj_li = await process_ae_obj__event_device_props({
obj_li: [event_device_obj_get_result],
log_lvl
});
// Save the updated results list to the database
if (log_lvl) {
console.log('Saving to DB...');
}
await db_save_ae_obj_li__ae_obj({
db_instance: db_events,
table_name: 'devices',
obj_li: processed_obj_li,
properties_to_save,
log_lvl,
});
if (log_lvl) {
console.log('DB save completed.');
}
// // This is expecting a list
// db_save_ae_obj_li__event_device({
// obj_type: 'event_device',
// obj_li: [event_device_obj_get_result]
// });
}
return event_device_obj_get_result;
} else {
@@ -84,7 +104,7 @@ export async function load_ae_obj_id__event_device(
}
// Updated 2024-10-16
// Updated 2025-05-23
export async function load_ae_obj_li__event_device(
{
api_cfg,
@@ -106,7 +126,9 @@ export async function load_ae_obj_li__event_device(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__event_device() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__event_device() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
let hidden: string = (params.qry__hidden ?? 'all'); // all, hidden, not_hidden
@@ -134,13 +156,36 @@ export async function load_ae_obj_li__event_device(
params: params,
log_lvl: log_lvl
})
.then(function (event_device_obj_li_get_result) {
.then(async function (event_device_obj_li_get_result) {
if (event_device_obj_li_get_result) {
if (try_cache) {
db_save_ae_obj_li__event_device({
obj_type: 'event_device',
obj_li: event_device_obj_li_get_result
// Process the results first
let processed_obj_li = await process_ae_obj__event_device_props({
obj_li: event_device_obj_li_get_result,
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('Processed object list:', processed_obj_li);
}
// Save the updated results list to the database
if (log_lvl) {
console.log('Saving to DB...');
}
await db_save_ae_obj_li__ae_obj({
db_instance: db_events,
table_name: 'devices',
obj_li: processed_obj_li,
properties_to_save: properties_to_save,
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('DB save completed.');
}
// db_save_ae_obj_li__event_device({
// obj_type: 'event_device',
// obj_li: event_device_obj_li_get_result
// });
}
return event_device_obj_li_get_result;
} else {
@@ -186,7 +231,7 @@ export async function load_ae_obj_li__event_device(
}
// Updated 2024-10-16
// Updated 2025-05-23
export async function create_ae_obj__event_device(
{
api_cfg,
@@ -220,14 +265,37 @@ export async function create_ae_obj__event_device(
return_obj: true,
log_lvl: log_lvl
})
.then(function (event_device_obj_create_result) {
.then(async function (event_device_obj_create_result) {
if (event_device_obj_create_result) {
if (try_cache) {
db_save_ae_obj_li__event_device(
{
obj_type: 'event_device',
obj_li: [event_device_obj_create_result]
// Process the results first
let processed_obj_li = await process_ae_obj__event_device_props({
obj_li: [event_device_obj_create_result],
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('Processed object list:', processed_obj_li);
}
// Save the updated results list to the database
if (log_lvl) {
console.log('Saving to DB...');
}
db_save_ae_obj_li__ae_obj({
db_instance: db_events,
table_name: 'devices',
obj_li: processed_obj_li,
properties_to_save: properties_to_save,
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('DB save completed.');
}
// db_save_ae_obj_li__event_device(
// {
// obj_type: 'event_device',
// obj_li: [event_device_obj_create_result]
// });
}
return event_device_obj_create_result;
} else {
@@ -247,7 +315,7 @@ export async function create_ae_obj__event_device(
}
// Updated 2024-11-08
// Updated 2025-05-23
export async function delete_ae_obj_id__event_device(
{
api_cfg,
@@ -278,14 +346,6 @@ export async function delete_ae_obj_id__event_device(
method: method,
log_lvl: log_lvl
})
.then(function (event_device_obj_delete_result) {
// if (event_device_obj_delete_result) {
// // db_save_ae_obj_li__event_device({obj_type: 'event_device', obj_li: [event_device_obj_delete_result]});
// return event_device_obj_delete_result;
// } else {
// return null;
// }
})
.catch(function (error) {
console.log('No results returned or failed.', error);
})
@@ -337,12 +397,35 @@ export async function update_ae_obj__event_device(
return_obj: true,
log_lvl: log_lvl
})
.then(function (event_device_obj_update_result) {
.then(async function (event_device_obj_update_result) {
if (event_device_obj_update_result) {
if (try_cache) {
db_save_ae_obj_li__event_device({
obj_type: 'event_device', obj_li: [event_device_obj_update_result]
// Process the results first
let processed_obj_li = await process_ae_obj__event_device_props({
obj_li: [event_device_obj_update_result],
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('Processed object list:', processed_obj_li);
}
// Save the updated results list to the database
if (log_lvl) {
console.log('Saving to DB...');
}
db_save_ae_obj_li__ae_obj({
db_instance: db_events,
table_name: 'devices',
obj_li: processed_obj_li,
properties_to_save: properties_to_save,
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('DB save completed.');
}
// db_save_ae_obj_li__event_device({
// obj_type: 'event_device', obj_li: [event_device_obj_update_result]
// });
}
return event_device_obj_update_result;
} else {
@@ -363,7 +446,7 @@ export async function update_ae_obj__event_device(
// Not yet used or tested fully!
// Updated 2024-10-16
// Updated 2025-05-23
export async function search__event_device(
{
api_cfg,
@@ -389,7 +472,9 @@ export async function search__event_device(
log_lvl?: number
}
) {
console.log(`*** search__event_device() *** event_id=${event_id}`);
if (log_lvl) {
console.log(`*** search__event_device() *** event_id=${event_id}`);
}
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden
@@ -447,13 +532,14 @@ export async function search__event_device(
let order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'start_datetime': 'ASC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'};
ae_promises.load__event_device_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
// ae_promises.load__event_device_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
ae_promises.load__event_device_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'event_device',
for_obj_type: 'event',
for_obj_id: event_id,
use_alt_table: true, // NOTE: We want to use the alt table for device searching
use_alt_base: false,
use_alt_tbl: true, // NOTE: We want to use the alt table for device searching
// use_alt_mdl: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,
@@ -584,3 +670,161 @@ export function db_save_ae_obj_li__event_device(
return true;
}
}
// Updated 2025-05-23
export const properties_to_save = [
'id',
'event_device_id',
'event_device_id_random',
'event_id',
'event_id_random',
'event_location_id',
'event_location_id_random',
'code',
'name',
'description',
'passcode',
'local_file_cache_path',
'host_file_temp_path',
'recording_path',
'record_audio',
'record_video',
'trigger_open_file_id',
'trigger_open_session_id',
'trigger_recording_start',
'trigger_recording_stop',
'trigger_reset',
'trigger_show_admin',
'trigger_show_hidden',
'alert',
'alert_msg',
'alert_on',
'status',
'status_msg',
'status_on',
'record_status',
'record_status_msg',
'record_status_on',
'heartbeat',
'info_hostname',
'info_ip_list',
'meta_json',
'other_json',
'enable',
'hide',
'priority',
'sort',
'group',
'notes',
'created_on',
'updated_on',
// From SQL view
'event_name',
'event_location_code',
'event_location_name',
];
// Updated 2025-05-23
export async function process_ae_obj__event_device_props({
obj_li,
log_lvl = 0,
}: {
obj_li: any[];
log_lvl?: number;
}) {
if (log_lvl) {
console.log(`*** process_ae_obj__event_location_props() ***`, obj_li);
}
if (!obj_li || obj_li.length === 0) {
if (log_lvl) console.log('No objects to process.');
return [];
}
const processed_obj_li = [];
for (const obj of obj_li) {
if (log_lvl) console.log(`Processing ae_obj event_device:`, obj);
let processed_obj = {
id: obj.id,
event_device_id: obj.event_device_id,
event_device_id_random: obj.event_device_id_random,
event_id: obj.event_id,
event_id_random: obj.event_id_random,
event_location_id: obj.event_location_id,
event_location_id_random: obj.event_location_id_random,
code: obj.code,
name: obj.name,
description: obj.description,
passcode: obj.passcode,
local_file_cache_path: obj.local_file_cache_path,
host_file_temp_path: obj.host_file_temp_path,
recording_path: obj.recording_path,
record_audio: obj.record_audio,
record_video: obj.record_video,
trigger_open_file_id: obj.trigger_open_file_id,
trigger_open_session_id: obj.trigger_open_session_id,
trigger_recording_start: obj.trigger_recording_start,
trigger_recording_stop: obj.trigger_recording_stop,
trigger_reset: obj.trigger_reset,
trigger_show_admin: obj.trigger_show_admin,
trigger_show_hidden: obj.trigger_show_hidden,
alert: obj.alert,
alert_msg: obj.alert_msg,
alert_on: obj.alert_on,
status: obj.status,
status_msg: obj.status_msg,
status_on: obj.status_on,
record_status: obj.record_status,
record_status_msg: obj.record_status_msg,
record_status_on: obj.record_status_on,
// These are timestamps that are in UTC but missing the 'Z' at the end
heartbeat: obj.heartbeat ? obj.heartbeat+'Z' : null,
info_hostname: obj.info_hostname,
info_ip_list: obj.info_ip_list,
meta_json: obj.meta_json,
other_json: obj.other_json,
enable: obj.enable,
hide: obj.hide,
priority: obj.priority,
sort: obj.sort,
group: obj.group,
notes: obj.notes,
created_on: obj.created_on,
updated_on: obj.updated_on,
// From SQL view
event_name: obj.event_name,
event_location_code: obj.event_location_code,
event_location_name: obj.event_location_name,
};
processed_obj_li.push(processed_obj);
}
return processed_obj_li;
}