Eingabe und Listing funktionieren erst mal
This commit is contained in:
19
db/mongo.js
19
db/mongo.js
@@ -16,9 +16,9 @@ let MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017';
|
||||
}
|
||||
*/
|
||||
|
||||
const DB_NAME = process.env.DB_NAME || 'espdb';
|
||||
const DB_NAME = process.env.DB_NAME || 'sensor_data';
|
||||
|
||||
let db, entriesCollection, usersCollection, prop_fluxCollection;
|
||||
let db, entriesCollection, usersCollection, prop_fluxCollection, propertiesCollection;
|
||||
|
||||
export async function initMongo() {
|
||||
const client = new MongoClient(MONGO_URI);
|
||||
@@ -27,11 +27,12 @@ export async function initMongo() {
|
||||
entriesCollection = db.collection('entries');
|
||||
usersCollection = db.collection('users');
|
||||
prop_fluxCollection = db.collection('prop_flux');
|
||||
propertiesCollection = db.collection('properties')
|
||||
return { db, entriesCollection, usersCollection };
|
||||
}
|
||||
|
||||
export function getCollections() {
|
||||
return { db, entriesCollection, usersCollection };
|
||||
return { db, entriesCollection, usersCollection, prop_fluxCollection };
|
||||
}
|
||||
|
||||
export const update_pflux = async(sn, doc) => {
|
||||
@@ -42,4 +43,16 @@ export const update_pflux = async(sn, doc) => {
|
||||
} catch (e) {
|
||||
return { "error": true, "what": e}
|
||||
}
|
||||
}
|
||||
|
||||
export const get_pflux = async(sn) => {
|
||||
try {
|
||||
let r = await prop_fluxCollection.findOne({_id: sn})
|
||||
if (r == null) {
|
||||
return { "error": true, "what": "Not found", "erg": r}
|
||||
}
|
||||
return {"error": null, "what": null, "erg": r}
|
||||
} catch (e) {
|
||||
return { "error": true, "what": e, "erg": null}
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,16 @@ async function fetchAddressIfValid() {
|
||||
const data = await res.json();
|
||||
if (!data.error && data.address) {
|
||||
addressInput.value = data.address;
|
||||
// Felder automatisch füllen, wenn props vorhanden
|
||||
if (!data.props.error) {
|
||||
if(data.props.erg.chip !== undefined) {
|
||||
let pp = data.props.erg.chip
|
||||
espIdInput.value = pp.id || ''
|
||||
nameInput.value = pp.name || ''
|
||||
descriptionInput.value = pp.description || ''
|
||||
// Weitere Felder nach Bedarf
|
||||
}
|
||||
}
|
||||
} else {
|
||||
addressInput.value = '';
|
||||
sensorNumberInput.disabled = true;
|
||||
@@ -145,19 +155,14 @@ sensorNumberInput.addEventListener('blur', fetchAddressIfValid);
|
||||
|
||||
tableBody.innerHTML = '';
|
||||
items.forEach(item => {
|
||||
const date = new Date(item.createdAt).toISOString().split('T')[0];
|
||||
const date = new Date(item.chip.createdAt).toISOString().split('T')[0];
|
||||
const tr = document.createElement('tr');
|
||||
tr.innerHTML = `
|
||||
<td>${item.espId}</td>
|
||||
<td>${item.sensorNumber}</td>
|
||||
<td>${item.name || ''}</td>
|
||||
<td>${item.description || ''}</td>
|
||||
<td>${item.address || ''}</td>
|
||||
<td>${date}</td>
|
||||
<td>
|
||||
<button data-id="${item._id}" class="editBtn">Bearbeiten</button>
|
||||
<button data-id="${item._id}" class="deleteBtn">Löschen</button>
|
||||
</td>
|
||||
<td>${item._id}</td>
|
||||
<td>${item.chip.id}</td>
|
||||
<td>${item.chip.name || ''}</td>
|
||||
<td id="tdBeschreibung">${item.chip.description || ''}</td>
|
||||
<td id="tdDate">${date}</td>
|
||||
`;
|
||||
tableBody.appendChild(tr);
|
||||
});
|
||||
|
||||
@@ -103,6 +103,18 @@ th, td {
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
#tdDate {
|
||||
width: 8em;
|
||||
}
|
||||
|
||||
#tdBeschreibung {
|
||||
width: 10em;
|
||||
}
|
||||
|
||||
#tdAktionen {
|
||||
width: 20em;
|
||||
}
|
||||
|
||||
.controls input#page,
|
||||
.controls input#limit {
|
||||
width: 50px;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { update_pflux } from '../db/mongo.js';
|
||||
import { get_pflux } from '../db/mongo.js';
|
||||
|
||||
export function registerAddressRoute(app, requireLogin) {
|
||||
const ADDRESS_SERVICE_URL = process.env.ADDRESS_SERVICE_URL || 'https://noise.fuerst-stuttgart.de/srv/getaddress';
|
||||
@@ -25,16 +25,20 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
return res.status(404).json({ error: 'Sensor nicht gefunden' });
|
||||
}
|
||||
|
||||
// Kopiere nach prop_flux mit sensorNumber als _id
|
||||
try {
|
||||
await propFlux.replaceOne(
|
||||
{ _id: sensorNumber },
|
||||
{ ...propEntry, _id: sensorNumber },
|
||||
{ upsert: true }
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Fehler beim Kopieren nach prop_flux:', err);
|
||||
// Kein Abbruch, nur Logging
|
||||
// Sensor bekannt -> Wurde der schon rüber kopiert?
|
||||
const propsF = await get_pflux(sensorNumber)
|
||||
if(!propsF.erg) {
|
||||
// nein, also kopieren nach prop_flux mit sensorNumber als _id
|
||||
try {
|
||||
await propFlux.replaceOne(
|
||||
{ _id: sensorNumber },
|
||||
{ ...propEntry, _id: sensorNumber },
|
||||
{ upsert: true }
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Fehler beim Kopieren nach prop_flux:', err);
|
||||
// Kein Abbruch, nur Logging
|
||||
}
|
||||
}
|
||||
|
||||
// Adresse wie bisher holen (über die Sensornummer via nominative)
|
||||
@@ -58,10 +62,11 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
}
|
||||
|
||||
await client.close();
|
||||
|
||||
return res.json({
|
||||
address: addressString,
|
||||
parts: addrParts,
|
||||
propEntry
|
||||
props: propsF
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import bcrypt from 'bcrypt';
|
||||
import { getCollections, update_pflux } from '../db/mongo.js';
|
||||
|
||||
export function registerApiRoutes(app, requireLogin) {
|
||||
const { entriesCollection, usersCollection } = getCollections();
|
||||
const { entriesCollection, usersCollection, prop_fluxCollection } = getCollections();
|
||||
|
||||
app.get('/api/check-email', async (req, res) => {
|
||||
const email = (req.query.email || '').toLowerCase().trim();
|
||||
@@ -46,7 +46,7 @@ export function registerApiRoutes(app, requireLogin) {
|
||||
}
|
||||
sensorNumber = parseInt(sensorNumber, 10);
|
||||
try {
|
||||
await entriesCollection.updateOne(
|
||||
await prop_fluxCollection.updateOne(
|
||||
{ _id: new ObjectId(id) },
|
||||
{ $set: { espId, sensorNumber, name, description, address } }
|
||||
);
|
||||
@@ -61,7 +61,7 @@ export function registerApiRoutes(app, requireLogin) {
|
||||
const { id } = req.query;
|
||||
if (id) {
|
||||
try {
|
||||
const item = await entriesCollection.findOne({ _id: new ObjectId(id) });
|
||||
const item = await prop_fluxCollection.findOne({ _id: new ObjectId(id) });
|
||||
if (item) return res.json([item]);
|
||||
return res.json([]);
|
||||
} catch (err) {
|
||||
@@ -73,8 +73,8 @@ export function registerApiRoutes(app, requireLogin) {
|
||||
const limit = parseInt(req.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
try {
|
||||
const items = await entriesCollection.find({})
|
||||
.sort({ createdAt: -1 })
|
||||
const items = await prop_fluxCollection.find({chip: {$exists: true}})
|
||||
.sort({ "chip.createdAt": -1 })
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.toArray();
|
||||
@@ -86,7 +86,7 @@ export function registerApiRoutes(app, requireLogin) {
|
||||
});
|
||||
|
||||
app.delete('/api/delete/:id', requireLogin, async (req, res) => {
|
||||
await entriesCollection.deleteOne({ _id: new ObjectId(req.params.id) });
|
||||
await prop_fluxCollection.deleteOne({ _id: new ObjectId(req.params.id) });
|
||||
res.json({ success: true });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ html(lang="de")
|
||||
textarea#description
|
||||
|
||||
label(for="address") Anschrift:
|
||||
input#address(type="text" placeholder="Wird automatisch ausgefüllt" readonly)
|
||||
input#address(type="text" placeholder="Wird automatisch ausgefüllt" readonly disabled)
|
||||
|
||||
button#saveBtn(type="button") Speichern
|
||||
div#result
|
||||
@@ -50,9 +50,7 @@ html(lang="de")
|
||||
th ESP-ID
|
||||
th Bezeichnung
|
||||
th Beschreibung
|
||||
th Anschrift
|
||||
th Datum
|
||||
th Aktionen
|
||||
tbody
|
||||
script(type="module" src="/global.js")
|
||||
script.
|
||||
|
||||
Reference in New Issue
Block a user