221 lines
5.5 KiB
JavaScript
221 lines
5.5 KiB
JavaScript
// Global variables
|
|
let global_project_list;
|
|
let current_project = "";
|
|
|
|
function formatDate(isoString) {
|
|
const date = new Date(isoString);
|
|
|
|
const options = {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
hour: "numeric",
|
|
minute: "numeric",
|
|
second: "numeric",
|
|
timeZoneName: "short",
|
|
};
|
|
|
|
return date.toLocaleString("en-US", options);
|
|
}
|
|
// Function to get data from API
|
|
|
|
function getAllProject() {
|
|
return $.ajax({
|
|
url: api_url.concat("/projects"),
|
|
method: "GET",
|
|
dataType: "json"
|
|
}).then((data) => {
|
|
return data;
|
|
});
|
|
}
|
|
|
|
function getSingleProject(id) {
|
|
return $.ajax({
|
|
url: api_url.concat("/projects/"+id),
|
|
method: "GET",
|
|
dataType: "json"
|
|
}).then((data) => {
|
|
return data;
|
|
});
|
|
}
|
|
|
|
function getDataMetrics(projectId){
|
|
return $.ajax({
|
|
url: api_url.concat(`/projects/${projectId}/measurements`),
|
|
method: "GET",
|
|
dataType: "json",
|
|
}).then((data) => {
|
|
// If the request is successful, store the data in the cache and return it
|
|
localStorage.setItem("project_list", JSON.stringify(data));
|
|
return data;
|
|
});
|
|
}
|
|
|
|
|
|
async function getDataProjectVideosFromApi(id) {
|
|
try {
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/projects/${id}/videos`),
|
|
method: "GET",
|
|
dataType: "json",
|
|
});
|
|
// If the request is successful, store the data in the cache and return it
|
|
return response;
|
|
} catch (error) {
|
|
console.error("Error fetching data:", error);
|
|
throw error; // Re-throw the error to handle it outside the function if needed
|
|
}
|
|
}
|
|
|
|
async function getDataVideoFromApi(id) {
|
|
try {
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/videos/${id}`),
|
|
method: "GET",
|
|
dataType: "json",
|
|
});
|
|
// If the request is successful, store the data in the cache and return it
|
|
return response;
|
|
} catch (error) {
|
|
console.error("Error fetching data:", error);
|
|
throw error; // Re-throw the error to handle it outside the function if needed
|
|
}
|
|
}
|
|
|
|
async function postNewVideo(project_id, measurements_id, name_video, resolution, duration) {
|
|
try {
|
|
const measures = JSON.stringify(measurements_id);
|
|
const mydata = JSON.stringify({
|
|
project_id: project_id,
|
|
measurement_ids: measures, // Ensure this matches the expected field name
|
|
name: name_video,
|
|
resolution: resolution,
|
|
duration: duration
|
|
});
|
|
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/videos/`),
|
|
method: "POST",
|
|
dataType: "json",
|
|
contentType: "application/json",
|
|
data: mydata
|
|
});
|
|
|
|
console.log("Video posted successfully:", response);
|
|
} catch (error) {
|
|
console.error("Error posting video:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
async function PostNewProject(nameProject, description){
|
|
try {
|
|
if(description.length==0){
|
|
description="Non renseignée"
|
|
}
|
|
const mydata = JSON.stringify({
|
|
name: nameProject,
|
|
description: description
|
|
});
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/projects`),
|
|
method: "POST",
|
|
dataType: "json",
|
|
contentType: "application/json",
|
|
data: mydata
|
|
});
|
|
|
|
console.log("Video posted successfully:", response);
|
|
} catch (error) {
|
|
console.error("Error posting video:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function deleteProject(id){
|
|
try {
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/projects/${id}`),
|
|
method: "DELETE",
|
|
}).then(()=>{
|
|
alert("Projet supprimé avec succès")
|
|
})
|
|
} catch (error) {
|
|
console.error("Error deleting project, project id :"+ id+"\n error :"+error);
|
|
throw error;
|
|
}
|
|
}
|
|
async function deleteVideo(id){
|
|
try {
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/videos/${id}`),
|
|
method: "DELETE",
|
|
}).then(()=>{
|
|
alert("Video supprimé avec succès")
|
|
})
|
|
} catch (error) {
|
|
console.error("Error deleting video, video id :"+ id+"\n error :"+error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function renderVideo(id){
|
|
try {
|
|
const mydata = JSON.stringify({
|
|
info:"none"
|
|
});
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/videos/render/`+id),
|
|
method: "POST",
|
|
dataType: "json",
|
|
contentType: "application/json",
|
|
data: mydata
|
|
});
|
|
|
|
console.log("Video rendered successfully:", response);
|
|
} catch (error) {
|
|
console.error("Error rendering video:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function start_timelapse(id,frequency){
|
|
try {
|
|
const mydata = JSON.stringify({
|
|
projectId: id,
|
|
interval: frequency
|
|
|
|
});
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/procedure/start/`),
|
|
method: "POST",
|
|
dataType: "json",
|
|
contentType: "application/json",
|
|
data: mydata
|
|
});
|
|
|
|
console.log("data retrieval started:", response);
|
|
} catch (error) {
|
|
console.error("Error starting the retrieval:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function stopCamera(id){
|
|
try {
|
|
const mydata = JSON.stringify({
|
|
info:"None"
|
|
});
|
|
const response = await $.ajax({
|
|
url: api_url.concat(`/procedure/stop`),
|
|
method: "POST",
|
|
dataType: "json",
|
|
contentType: "application/json",
|
|
data: mydata
|
|
});
|
|
|
|
console.log("Camera stopped succesfully :", response);
|
|
} catch (error) {
|
|
console.error("Error stopping the camera :", error);
|
|
throw error;
|
|
}
|
|
} |