Skip to content

Commit

Permalink
Kdyz soubor neexistuje/je prazdny tak ho vytvori nebo nevyhodi chybu
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamBenes3 committed Dec 1, 2023
1 parent 70a38c7 commit b24a79a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 48 deletions.
155 changes: 108 additions & 47 deletions GAPP.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function appendToJsonFile(filePath, newData) {
let jsonData = JSON.parse(existingData);
jsonData.history.push(newData);
const updatedJsonData = JSON.stringify(jsonData, null, 2);
if (fs.existsSync("data/" + filePath)) {

}
else {
fs.writeFileSync("data/" + filePath, JSON.stringify({}));
}
fs.writeFileSync("data/" + filePath, updatedJsonData);
console.log('Data appended successfully.');
} catch (error) {
Expand All @@ -24,6 +30,12 @@ function appendToJsonFile(filePath, newData) {
function saveDataToFile(filename, data) {
try {
console.log("Saved to file: " + filename);
if (fs.existsSync("data/" + filename)) {

}
else {
fs.writeFileSync("data/" + filename, JSON.stringify({}));
}
fs.writeFileSync("data/" + filename, JSON.stringify(data, null, 2));
} catch (error) {
console.error('Error saving data to file:', error);
Expand Down Expand Up @@ -65,12 +77,17 @@ app.post('/post/ldp/hb', (req, res) => {
});

app.get('/get/ldp/hb', async (req, res) => {
const filePath = "data/ldp_hb.json";
try {
const rawData = fs.readFileSync("data/" + 'ldp_hb.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

Expand Down Expand Up @@ -101,34 +118,49 @@ app.post('/post/car/hb', (req, res) => {
});

app.get('/get/car/hb/1', async (req, res) => {
const filePath = "data/car1_hb.json";
try {
const rawData = fs.readFileSync("data/" + 'car1_hb.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});


app.get('/get/car/hb/2', async (req, res) => {
const filePath = "data/car2_hb.json";
try {
const rawData = fs.readFileSync("data/" + 'car2_hb.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});


app.get('/get/car/hb/3', async (req, res) => {
const filePath = "data/car3_hb.json";
try {
const rawData = fs.readFileSync("data/" + 'car3_hb.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

Expand Down Expand Up @@ -169,32 +201,47 @@ app.post('/post/car/data', async (req, res) => {
});

app.get('/get/car/data/1', async (req, res) => {
const filePath = "data/car1_data.json";
try {
const rawData = fs.readFileSync("data/" + 'car1_data.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

app.get('/get/car/data/2', async (req, res) => {
const filePath = "data/car2_data.json";
try {
const rawData = fs.readFileSync("data/" + 'car2_data.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

app.get('/get/car/data/3', async (req, res) => {
const filePath = "data/car3_data.json";
try {
const rawData = fs.readFileSync("data/" + 'car3_data.json', 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

Expand All @@ -209,12 +256,17 @@ app.post('/post/cdp/hb', (req, res) => {
});

app.get('/get/cdp/hb', async (req, res) => {
const filePath = "data/cdp_hb.json";
try {
const rawData = fs.readFileSync("data/" + "cdp_hb.json", 'utf8');
const decodedData = JSON.parse(rawData);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).send('Error rendering the page');
res.status(500).json({ error: 'Error loading data' });
}
});

Expand Down Expand Up @@ -249,36 +301,45 @@ app.post('/post/data', (req, res) => {
});

app.get('/get/fik8b', (req, res) => {
const filePath = "data/fik8b.json";
try {
const rawData = fs.readFileSync("data/" + 'fik8b.json', 'utf8');
const decodedData = JSON.parse(rawData);
// const rawData2 = fs.readFileSync("data/" + 'dataLocation.json', 'utf8');
// const decodedData2 = JSON.parse(rawData2);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).json({ error: 'Error loading data' });
}
});

app.get('/get/px4', (req, res) => {
const filePath = "data/px4.json";
try {
const rawData = fs.readFileSync("data/" + 'px4.json', 'utf8');
const decodedData = JSON.parse(rawData);
// const rawData2 = fs.readFileSync("data/" + 'dataLocation.json', 'utf8');
// const decodedData2 = JSON.parse(rawData2);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).json({ error: 'Error loading data' });
}
});

app.get('/get/car/data', (req, res) => {
const filePath = "data/car_data.json";
try {
const rawData = fs.readFileSync("data/" + 'car_data.json', 'utf8');
const decodedData = JSON.parse(rawData);
// const rawData2 = fs.readFileSync("data/" + 'dataLocation.json', 'utf8');
// const decodedData2 = JSON.parse(rawData2);
res.json({ data: decodedData });
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath, 'utf8');
const decodedData = rawData ? JSON.parse(rawData) : {};
res.json({ data: decodedData });
} else {
fs.writeFileSync(filePath, JSON.stringify({}));
}
} catch (error) {
res.status(500).json({ error: 'Error loading data' });
}
Expand Down
1 change: 0 additions & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@
});
if (load) {
responses[3].json().then(data => {
updateStaticTableWithldpHb(data.data.ldp_heartbeat_value);
var now = new Date();
var lastReloadTimeElement = document.getElementById('lastReloadTime');
lastReloadTimeElement.textContent = 'Last Reload Time: ' + now.toLocaleString();
Expand Down

0 comments on commit b24a79a

Please sign in to comment.