Files
yaris-obd/static/js/dash.js
T
Mathieu MoranandCopilot 6d61b70a06 Add SIM868 cellular fallback for tracking uploads (untested, no SIM card)
modules/app_cellular.py: uses the SIM868's built-in AT-command HTTP(S)
client (AT+HTTPINIT/HTTPPARA/HTTPACTION/HTTPREAD) instead of a real
ppp0 interface, since the module's GNSS (polled continuously by
app_gps.py) and its GSM/GPRS radio share the same physical UART
(/dev/serial0) - a pppd/chat dial-up would take over that port
entirely and starve GNSS polling. Staying in AT-command mode lets
cellular HTTP calls interleave safely through the same serial lock.

Only carries small JSON tracking batches, never dashcam video -
cellular is far too slow/limited for that, and the modem's HTTP AT
interface has too small an internal buffer anyway. Dashcam uploads
remain WiFi/Ethernet-only, unchanged.

modules/app_upload.py: tracking-batch upload always tries WiFi/
Ethernet first; only on a connection-level failure (not an HTTP error
status, which means the server was reached) does it retry over
cellular. Exposes last_network_path/cellular_fallback_configured in
/api/upload/status; UI shows which path the last batch used.

Feature-flagged off by default via new YARIS_CELLULAR_ENABLED/
YARIS_CELLULAR_APN/_APN_USER/_APN_PASS keys in db/upload_creds.env
(all blank by default) - has zero effect on the existing WiFi upload
path unless explicitly configured. Regression-tested against the real
deployed server that normal WiFi uploads are unaffected while
cellular stays unconfigured.

*** UNTESTED against real cellular hardware - no SIM card was
*** available. See docstrings in app_cellular.py and PLAN.md for
*** exactly what needs validation once a SIM card is available.

Co-authored-by: Copilot <[email protected]>
2026-08-08 20:34:53 -04:00

124 lines
4.6 KiB
JavaScript

// Dashcam page: scan for a USB dashcam, then trigger importing its videos.
// Uses plain fetch() (not socket.io / yaris.js) since this is a one-shot
// request/response flow, not a live data stream, and yaris.js assumes DOM
// elements (#map, #speedGraph) that don't exist on this page.
const deviceStatus = document.getElementById('dash-device-status');
const importStatus = document.getElementById('dash-import-status');
const scanBtn = document.getElementById('dash-scan-btn');
const importBtn = document.getElementById('dash-import-btn');
function describeDevice(device) {
const label = device.label ? ` "${device.label}"` : '';
return `${device.path}${label} (${device.size}, ${device.fstype || 'unknown fs'})`;
}
function scanForDashcam() {
deviceStatus.textContent = 'Scanning...';
importBtn.disabled = true;
fetch('/api/dash/devices')
.then(response => response.json())
.then(data => {
const devices = data.devices || [];
if (devices.length === 0) {
deviceStatus.textContent = 'No USB dashcam/storage device detected. Plug it in and scan again.';
return;
}
deviceStatus.textContent = 'Detected: ' + devices.map(describeDevice).join(', ');
importBtn.disabled = false;
})
.catch(err => {
deviceStatus.textContent = 'Error while scanning: ' + err;
});
}
function importDashcam() {
importStatus.textContent = 'Importing... this can take a while for large videos.';
importBtn.disabled = true;
fetch('/api/dash/import', { method: 'POST' })
.then(response => response.json())
.then(summary => {
if (summary.status !== 'ok') {
importStatus.textContent = summary.message || ('Import failed: ' + summary.status);
return;
}
let text = `Found ${summary.found} video(s), copied ${summary.copied}, skipped ${summary.skipped} (already imported).`;
if (summary.errors && summary.errors.length > 0) {
text += ` Errors: ${summary.errors.join('; ')}`;
}
importStatus.textContent = text;
})
.catch(err => {
importStatus.textContent = 'Error while importing: ' + err;
})
.finally(() => {
importBtn.disabled = false;
});
}
scanBtn.addEventListener('click', scanForDashcam);
importBtn.addEventListener('click', importDashcam);
// Auto-scan once when the page loads.
scanForDashcam();
// --- Remote server upload status/toggle ---
const uploadStatusEl = document.getElementById('upload-status');
const dashcamToggle = document.getElementById('dashcam-upload-toggle');
function describeUploadStatus(status) {
if (!status.configured) {
return 'Remote server not configured on this device (see db/upload_creds.env).';
}
let text = `Pending trip-log rows to upload: ${status.pending_tracking_rows}.`;
if (status.last_success_at) {
text += ` Last successful upload: ${status.last_success_at}`;
if (status.last_network_path) {
text += ` (via ${status.last_network_path === 'cellular' ? 'cellular fallback' : 'WiFi/Ethernet'})`;
}
text += '.';
}
if (status.cellular_fallback_configured) {
text += ' Cellular (SIM868) fallback is enabled for trip logs if WiFi/Ethernet is unreachable.';
}
if (status.last_error) {
text += ` Last error: ${status.last_error}.`;
}
return text;
}
function refreshUploadStatus() {
fetch('/api/upload/status')
.then(response => response.json())
.then(status => {
uploadStatusEl.textContent = describeUploadStatus(status);
dashcamToggle.checked = status.dashcam_upload_enabled;
dashcamToggle.disabled = !status.configured;
})
.catch(err => {
uploadStatusEl.textContent = 'Error while checking upload status: ' + err;
});
}
dashcamToggle.addEventListener('change', () => {
fetch('/api/upload/dashcam_toggle', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled: dashcamToggle.checked }),
})
.then(response => response.json())
.then(status => {
uploadStatusEl.textContent = describeUploadStatus(status);
dashcamToggle.checked = status.dashcam_upload_enabled;
})
.catch(err => {
uploadStatusEl.textContent = 'Error while updating toggle: ' + err;
});
});
refreshUploadStatus();
setInterval(refreshUploadStatus, 15000);