u
This commit is contained in:
57
server.js
57
server.js
@@ -16,6 +16,18 @@ const PORT = process.env.PICUPPER_PORT;
|
||||
const UPLOAD_DIR = process.env.PICUPPER_UPLOAD_DIR || path.join(__dirname, 'uploads');
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB - typical webcam snapshot size
|
||||
|
||||
// WSS Client Configuration
|
||||
import { TischlerClient } from './wss-client.js';
|
||||
const WSS_URL = process.env.WSS_SERVER_URL || 'wss://dash.bosewolf.de/agentapi/';
|
||||
const WSS_KEY = process.env.WSS_API_KEY;
|
||||
|
||||
const wssClient = new TischlerClient(WSS_URL, WSS_KEY);
|
||||
if (WSS_KEY) {
|
||||
wssClient.connect().catch(err => console.error('WSS Client connection failed:', err.message));
|
||||
} else {
|
||||
console.log('WSS_API_KEY not set. WSS Client disabled.');
|
||||
}
|
||||
|
||||
if (!PORT) {
|
||||
console.error('ERROR: PICUPPER_PORT environment variable is required');
|
||||
process.exit(1);
|
||||
@@ -387,6 +399,8 @@ app.post('/upload', authenticate, upload.single('image'), async (req, res) => {
|
||||
const user = req.authenticatedUser;
|
||||
const storageKey = `${user}:${cameraId}`;
|
||||
const settings = loadCameraSettings()[storageKey] || {};
|
||||
const factor = settings.factor !== undefined ? settings.factor : 1.0;
|
||||
const adjustedBrightness = brightness * factor;
|
||||
|
||||
if (settings.insertBrightnessToDb) {
|
||||
const dbPath = '/home/seb/src/actest/ac_data.db';
|
||||
@@ -396,15 +410,49 @@ app.post('/upload', authenticate, upload.single('image'), async (req, res) => {
|
||||
INSERT INTO readings (dev_name, port, port_name, fan_speed)
|
||||
VALUES ('Wall', 3, 'Light', ?)
|
||||
`);
|
||||
stmt.run(brightness);
|
||||
stmt.run(adjustedBrightness);
|
||||
db.close();
|
||||
console.log(`Brightness ${brightness} inserted into ac_data.db as Light`);
|
||||
console.log(`Brightness ${brightness} (factored to ${adjustedBrightness}) inserted into ac_data.db as Light`);
|
||||
}
|
||||
}
|
||||
} catch (dbErr) {
|
||||
console.error('Failed to insert brightness into ac_data.db:', dbErr);
|
||||
}
|
||||
|
||||
// Send data to WSS API
|
||||
if (WSS_KEY && wssClient.authenticated) {
|
||||
try {
|
||||
const user = req.authenticatedUser;
|
||||
const storageKey = `${user}:${cameraId}`;
|
||||
const settings = loadCameraSettings()[storageKey] || {};
|
||||
const readings = [];
|
||||
const factor = settings.factor !== undefined ? settings.factor : 1.0;
|
||||
const adjustedBrightness = brightness * factor;
|
||||
|
||||
if (settings.ocr && settings.ocr.enabled && ocr_val !== null) {
|
||||
readings.push({
|
||||
device: cameraId,
|
||||
channel: settings.chartLabel || 'CO2',
|
||||
value: ocr_val
|
||||
});
|
||||
}
|
||||
|
||||
if (settings.insertBrightnessToDb) {
|
||||
readings.push({
|
||||
device: cameraId,
|
||||
channel: settings.chartLabel || 'Light',
|
||||
value: adjustedBrightness
|
||||
});
|
||||
}
|
||||
|
||||
if (readings.length > 0) {
|
||||
wssClient.sendReadings(readings);
|
||||
}
|
||||
} catch (wssErr) {
|
||||
console.error('Failed to send WSS readings:', wssErr);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
cameraId,
|
||||
@@ -612,7 +660,7 @@ app.put('/settings/:cameraId', authenticate, express.json(), (req, res) => {
|
||||
const existing = allSettings[storageKey] || {};
|
||||
|
||||
// Separate v4l2 control values from config settings
|
||||
const configKeys = ['rotation', 'crop', 'ocr', 'chartLabel', 'insertBrightnessToDb'];
|
||||
const configKeys = ['rotation', 'crop', 'ocr', 'chartLabel', 'insertBrightnessToDb', 'factor'];
|
||||
const newValues = {};
|
||||
const newConfig = {};
|
||||
|
||||
@@ -646,7 +694,8 @@ app.put('/settings/:cameraId', authenticate, express.json(), (req, res) => {
|
||||
crop: allSettings[storageKey].crop,
|
||||
ocr: allSettings[storageKey].ocr,
|
||||
chartLabel: allSettings[storageKey].chartLabel,
|
||||
insertBrightnessToDb: allSettings[storageKey].insertBrightnessToDb
|
||||
insertBrightnessToDb: allSettings[storageKey].insertBrightnessToDb,
|
||||
factor: allSettings[storageKey].factor
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user