512 lines
17 KiB
TypeScript
512 lines
17 KiB
TypeScript
import type { key_val } from '$lib/ae_stores';
|
|
import { api } from '$lib/api';
|
|
|
|
import { db_events } from "$lib/db_events";
|
|
|
|
import { load_ae_obj_id__event_location } from './ae_events__event_location';
|
|
|
|
let ae_promises: key_val = {};
|
|
|
|
|
|
// Updated 2024-10-16
|
|
export async function load_ae_obj_id__event_device(
|
|
{
|
|
api_cfg,
|
|
event_device_id,
|
|
inc_location_id = false,
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any,
|
|
event_device_id: string,
|
|
inc_location_id?: boolean,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
if (log_lvl) {
|
|
console.log(`*** load_ae_obj_id__event_device() *** event_device_id=${event_device_id}`);
|
|
}
|
|
|
|
let params = {};
|
|
|
|
ae_promises.load__event_device_obj = await api.get_ae_obj_id_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'event_device',
|
|
obj_id: event_device_id, // NOTE: This is the FQDN, not normally the ID.
|
|
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
|
|
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value in the API config.
|
|
params: params,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(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]
|
|
});
|
|
}
|
|
return event_device_obj_get_result;
|
|
} else {
|
|
console.log('No results returned.');
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.load__event_device_obj:', ae_promises.load__event_device_obj);
|
|
}
|
|
|
|
if (inc_location_id) {
|
|
// Load the location linked to this device
|
|
if (log_lvl) {
|
|
console.log(`Need to load the location id for the device now`);
|
|
}
|
|
let load_event_location_obj_id = load_ae_obj_id__event_location({
|
|
api_cfg: api_cfg,
|
|
event_location_id: ae_promises.load__event_device_obj.event_location_id,
|
|
try_cache: try_cache,
|
|
log_lvl: log_lvl
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log(`load_event_location_obj_id = `, load_event_location_obj_id);
|
|
}
|
|
ae_promises.load__event_device_obj.event_location_obj = load_event_location_obj_id;
|
|
}
|
|
|
|
return ae_promises.load__event_device_obj;
|
|
}
|
|
|
|
|
|
// Updated 2024-10-16
|
|
export async function load_ae_obj_li__event_device(
|
|
{
|
|
api_cfg,
|
|
for_obj_type,
|
|
for_obj_id,
|
|
inc_location_id = false,
|
|
order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'name': 'ASC', 'code': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'},
|
|
params = {},
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any,
|
|
for_obj_type: string,
|
|
for_obj_id: string,
|
|
inc_location_id?: boolean,
|
|
order_by_li?: key_val,
|
|
params?: key_val,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
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
|
|
let limit: number = (params.qry__limit ?? 99); // 99
|
|
let offset: number = (params.qry__offset ?? 0); // 0
|
|
|
|
let params_json: key_val = {};
|
|
|
|
// console.log('params_json:', params_json);
|
|
|
|
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: for_obj_type,
|
|
for_obj_id: for_obj_id,
|
|
use_alt_tbl: true,
|
|
use_alt_mdl: false,
|
|
use_alt_exp: false,
|
|
enabled: enabled,
|
|
hidden: hidden,
|
|
order_by_li: order_by_li,
|
|
limit: limit,
|
|
offset: offset,
|
|
params_json: params_json,
|
|
params: params,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(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
|
|
});
|
|
}
|
|
return event_device_obj_li_get_result;
|
|
} else {
|
|
return [];
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.load__event_device_obj_li:', ae_promises.load__event_device_obj_li);
|
|
}
|
|
|
|
if (inc_location_id) {
|
|
// Load the location for the devices
|
|
if (log_lvl) {
|
|
console.log(`Need to load the location list for each device now`);
|
|
}
|
|
for (let i = 0; i < ae_promises.load__event_device_obj_li.length; i++) {
|
|
let event_device_obj = ae_promises.load__event_device_obj_li[i];
|
|
|
|
let load_event_location_obj_li = load_ae_obj_id__event_location({
|
|
api_cfg: api_cfg,
|
|
event_location_id: event_device_obj.event_location_id,
|
|
try_cache: try_cache,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then((event_location_obj_li) => {
|
|
if (log_lvl) {
|
|
console.log(`event_location_obj_li = `, event_location_obj_li);
|
|
}
|
|
return event_location_obj_li;
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log(`load_event_location_obj_li = `, load_event_location_obj_li);
|
|
}
|
|
}
|
|
}
|
|
|
|
return ae_promises.load__event_device_obj_li;
|
|
}
|
|
|
|
|
|
// Updated 2024-10-16
|
|
export async function create_ae_obj__event_device(
|
|
{
|
|
api_cfg,
|
|
event_id,
|
|
data_kv,
|
|
params={},
|
|
log_lvl=0
|
|
}: {
|
|
api_cfg: any,
|
|
event_id: string,
|
|
data_kv: key_val,
|
|
params?: key_val,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
console.log(`*** create_ae_obj__event_device() *** event_id=${event_id}`);
|
|
|
|
ae_promises.create__event_device = await api.create_ae_obj_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'event_device',
|
|
fields: {
|
|
event_id_random: event_id,
|
|
...data_kv
|
|
},
|
|
key: api_cfg.api_crud_super_key,
|
|
params: params,
|
|
return_obj: true,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (event_device_obj_create_result) {
|
|
if (event_device_obj_create_result) {
|
|
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 {
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
})
|
|
.finally(function () {
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.create__event_device:', ae_promises.create__event_device);
|
|
}
|
|
return ae_promises.create__event_device;
|
|
}
|
|
|
|
|
|
// Updated 2024-10-16
|
|
export async function update_ae_obj__event_device(
|
|
{
|
|
api_cfg,
|
|
event_device_id,
|
|
data_kv,
|
|
params = {},
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any,
|
|
event_device_id: string,
|
|
data_kv: key_val,
|
|
params?: key_val,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
if (log_lvl) {
|
|
console.log(`*** update_ae_obj__event_device() *** event_device_id=${event_device_id}`, data_kv);
|
|
}
|
|
ae_promises.update__event_device_obj = await api.update_ae_obj_id_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'event_device',
|
|
obj_id: event_device_id,
|
|
fields: data_kv,
|
|
key: api_cfg.api_crud_super_key,
|
|
params: params,
|
|
return_obj: true,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(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]
|
|
});
|
|
}
|
|
return event_device_obj_update_result;
|
|
} else {
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
})
|
|
.finally(function () {
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.update__event_device_obj:', ae_promises.update__event_device_obj);
|
|
}
|
|
return ae_promises.update__event_device_obj;
|
|
}
|
|
|
|
|
|
// Updated 2024-10-16
|
|
export async function search__event_device(
|
|
{
|
|
api_cfg,
|
|
event_id,
|
|
fulltext_search_qry_str,
|
|
ft_presenter_search_qry_str,
|
|
like_search_qry_str = null,
|
|
like_presentation_search_qry_str = null,
|
|
like_presenter_search_qry_str = null,
|
|
params = {},
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any,
|
|
event_id: any,
|
|
fulltext_search_qry_str?: null|string,
|
|
ft_presenter_search_qry_str?: null|string,
|
|
like_search_qry_str?: null|string,
|
|
like_presentation_search_qry_str?: null|string,
|
|
like_presenter_search_qry_str?: null|string,
|
|
params?: any,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
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
|
|
let limit: number = (params.qry__limit ?? 25); // 99
|
|
let offset: number = (params.qry__offset ?? 0); // 0
|
|
|
|
let params_json: key_val = {};
|
|
|
|
if (!fulltext_search_qry_str && !like_search_qry_str) {
|
|
console.log('No search string provided!!!');
|
|
return false; // Returning false instead of [] because no search was performed.
|
|
}
|
|
|
|
if (fulltext_search_qry_str || ft_presenter_search_qry_str) {
|
|
params_json['ft_qry'] = {};
|
|
if (fulltext_search_qry_str && fulltext_search_qry_str.length > 2) {
|
|
params_json['ft_qry']['default_qry_str'] = fulltext_search_qry_str;
|
|
}
|
|
|
|
if (ft_presenter_search_qry_str && ft_presenter_search_qry_str.length > 2) {
|
|
params_json['ft_qry']['event_presenter_li_qry_str'] = ft_presenter_search_qry_str;
|
|
}
|
|
}
|
|
|
|
// Use the AND (AND LIKE) query
|
|
// if (like_search_qry_str || like_presenter_search_qry_str) {
|
|
// params_json['and_like'] = {};
|
|
// if (like_search_qry_str && like_search_qry_str.length > 2) {
|
|
// params_json['and_like']['default_qry_str'] = like_search_qry_str;
|
|
// }
|
|
// if (like_presenter_search_qry_str && like_presenter_search_qry_str.length > 2) {
|
|
// params_json['and_like']['event_presenter_li_qry_str'] = like_presenter_search_qry_str;
|
|
// }
|
|
// }
|
|
|
|
// Use the AND (OR LIKE) query
|
|
if (like_search_qry_str || like_presentation_search_qry_str || like_presenter_search_qry_str) {
|
|
params_json['or_like'] = {};
|
|
if (like_search_qry_str && like_search_qry_str.length > 2) {
|
|
params_json['or_like']['default_qry_str'] = like_search_qry_str;
|
|
}
|
|
if (like_presentation_search_qry_str && like_presentation_search_qry_str.length > 2) {
|
|
params_json['or_like']['event_presentation_li_qry_str'] = like_presentation_search_qry_str;
|
|
}
|
|
if (like_presenter_search_qry_str && like_presenter_search_qry_str.length > 2) {
|
|
params_json['or_like']['event_presenter_li_qry_str'] = like_presenter_search_qry_str;
|
|
}
|
|
}
|
|
|
|
params_json['and_qry'] = {};
|
|
|
|
// if (device_type_code) {
|
|
// params_json['and_qry']['device_type_code'] = device_type_code;
|
|
// }
|
|
|
|
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({
|
|
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,
|
|
enabled: enabled,
|
|
hidden: hidden,
|
|
order_by_li: order_by_li,
|
|
limit: limit,
|
|
offset: offset,
|
|
params_json: params_json,
|
|
params: params,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (event_device_obj_li_get_result) {
|
|
if (event_device_obj_li_get_result) {
|
|
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 {
|
|
return [];
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
})
|
|
.finally(function () {
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.load__event_device_obj_li:', ae_promises.load__event_device_obj_li);
|
|
}
|
|
return ae_promises.load__event_device_obj_li;
|
|
}
|
|
|
|
|
|
// This function will loop through the event_device_obj_li and save each one to the DB.
|
|
// Updated 2024-10-16
|
|
export function db_save_ae_obj_li__event_device(
|
|
{
|
|
obj_type,
|
|
obj_li,
|
|
log_lvl = 0
|
|
}: {
|
|
obj_type: string,
|
|
obj_li: any,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
if (log_lvl) {
|
|
console.log(`*** db_save_ae_obj_li__event_device() ***`);
|
|
}
|
|
|
|
if (obj_li && obj_li.length) {
|
|
obj_li.forEach(async function (obj: any) {
|
|
if (log_lvl) {
|
|
console.log(`ae_obj ${obj_type}:`, obj);
|
|
}
|
|
|
|
try {
|
|
const id_random = await db_events.devices.put({
|
|
id: obj.event_device_id_random,
|
|
event_device_id: obj.event_device_id_random,
|
|
// event_device_id_random: obj.event_device_id_random,
|
|
|
|
event_id: obj.event_id_random,
|
|
// event_id_random: obj.event_id_random,
|
|
event_location_id: obj.event_location_id_random,
|
|
// 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,
|
|
|
|
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,
|
|
});
|
|
// console.log(`Put obj with ID: ${obj.event_device_id_random} or ${id_random}`);
|
|
} catch (error) {
|
|
let status = `Failed to put ${obj.event_device_id_random}: ${error}`;
|
|
console.log(status);
|
|
}
|
|
|
|
// const id_random = await db_events.devices.put(obj);
|
|
// console.log(`Put obj with ID: ${obj.event_device_id_random}`);
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|