|
| 1 | +# Device Sensor Data Logger (C++) |
| 2 | + |
| 3 | +A C++17 sensor logging application for simulated or serial temperature/light sources. It supports CSV or SQLite persistence, threaded sampling, runtime summaries, threshold alerts, and CSV export. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Sensor input modes: |
| 8 | + - Simulated sensor mode (Linux/Windows, no hardware needed) |
| 9 | + - Serial sensor mode (Linux UART/USB serial device) |
| 10 | +- Logging outputs: |
| 11 | + - CSV file |
| 12 | + - SQLite database (when SQLite3 is available) |
| 13 | +- Runtime controls: |
| 14 | + - Start logging |
| 15 | + - Stop logging |
| 16 | + - Show live stats (min/max/avg) |
| 17 | + - Show runtime status (accepted/rejected samples, failures, last reading) |
| 18 | + - Reset session statistics without restarting the app |
| 19 | + - Export logs to CSV |
| 20 | +- Safety and observability: |
| 21 | + - Input validation rejects invalid readings such as `NaN` and negative lux values |
| 22 | + - Configurable alert thresholds for temperature and light |
| 23 | + - Optional `--max-samples` auto-stop for bounded runs and tests |
| 24 | +- Demonstrates: |
| 25 | + - OOP interfaces (`ISensor`, `ILogSink`) |
| 26 | + - File I/O and optional SQLite storage |
| 27 | + - Threaded periodic logging loop |
| 28 | + - Basic serial communication parsing |
| 29 | + |
| 30 | +## Architecture |
| 31 | + |
| 32 | +- `ISensor` abstracts data sources. `SimulatedSensor` generates drifting readings. `SerialSensor` reads `temp,light` lines from a serial device on non-Windows platforms. |
| 33 | +- `ILogSink` abstracts persistence. `CsvLogSink` appends CSV rows. `SqliteLogSink` stores rows in a `sensor_log` table and can export them back to CSV. |
| 34 | +- `DataLogger` owns one sensor and one sink, runs the sampling loop on a worker thread, tracks aggregated stats, validates samples, counts failures, and raises threshold alerts. |
| 35 | +- `StatsAggregator` maintains min/max/average for temperature and light. |
| 36 | + |
| 37 | +## Build |
| 38 | + |
| 39 | +```bash |
| 40 | +cmake -S . -B build |
| 41 | +cmake --build build |
| 42 | +``` |
| 43 | + |
| 44 | +## Test |
| 45 | + |
| 46 | +```bash |
| 47 | +ctest --test-dir build --output-on-failure |
| 48 | +``` |
| 49 | + |
| 50 | +The test target covers stats reset logic plus the logger lifecycle, invalid sample rejection, alert tracking, and bounded auto-stop behavior. |
| 51 | + |
| 52 | +## Run |
| 53 | + |
| 54 | +### Interactive mode (simulated sensor + CSV) |
| 55 | + |
| 56 | +```bash |
| 57 | +./build/device_sensor_logger --mode sim --output csv --file sensor_log.csv --interval-ms 1000 --temp-alert-max 28 --light-alert-min 100 |
| 58 | +``` |
| 59 | + |
| 60 | +Then use commands: |
| 61 | + |
| 62 | +- `start` |
| 63 | +- `stop` |
| 64 | +- `stats` |
| 65 | +- `status` |
| 66 | +- `reset-stats` |
| 67 | +- `export exported.csv` |
| 68 | +- `quit` |
| 69 | + |
| 70 | +### Timed run (auto start/stop) |
| 71 | + |
| 72 | +```bash |
| 73 | +./build/device_sensor_logger --mode sim --output sqlite --file sensor_log.db --duration-sec 20 --max-samples 50 --export out.csv |
| 74 | +``` |
| 75 | + |
| 76 | +### Serial mode example (Linux) |
| 77 | + |
| 78 | +```bash |
| 79 | +./build/device_sensor_logger --mode serial --serial-port /dev/ttyUSB0 --baud 115200 --output csv --file serial_log.csv |
| 80 | +``` |
| 81 | + |
| 82 | +Expected incoming serial line format: |
| 83 | + |
| 84 | +```text |
| 85 | +24.1,315.5 |
| 86 | +``` |
| 87 | + |
| 88 | +## Command Line Options |
| 89 | + |
| 90 | +- `--mode sim|serial` |
| 91 | +- `--serial-port <path>` |
| 92 | +- `--baud <rate>` |
| 93 | +- `--interval-ms <milliseconds>` |
| 94 | +- `--output csv|sqlite` |
| 95 | +- `--file <output-path>` |
| 96 | +- `--duration-sec <seconds>` |
| 97 | +- `--max-samples <count>` |
| 98 | +- `--temp-alert-min <value>` |
| 99 | +- `--temp-alert-max <value>` |
| 100 | +- `--light-alert-min <value>` |
| 101 | +- `--light-alert-max <value>` |
| 102 | +- `--export <csv-path>` |
| 103 | + |
| 104 | +## Example Output |
| 105 | + |
| 106 | +```text |
| 107 | +Sensor Data Logger |
| 108 | +Commands: |
| 109 | + start Start logging |
| 110 | + stop Stop logging |
| 111 | + stats Show min/max/avg |
| 112 | + status Show runtime counters and last reading |
| 113 | + reset-stats Clear collected session statistics |
| 114 | + export <file.csv> Export log data to CSV |
| 115 | + help Show commands |
| 116 | + quit Stop and exit |
| 117 | +> start |
| 118 | +Logging started. |
| 119 | +> status |
| 120 | +running=yes accepted=5 rejected=0 read_failures=0 write_failures=0 alerts=1 |
| 121 | +last=2026-03-12T12:00:05Z temp=28.214C light=92.113 lux |
| 122 | +``` |
| 123 | + |
| 124 | +## Notes |
| 125 | + |
| 126 | +- If SQLite3 is not found during CMake configure, SQLite output is disabled automatically. |
| 127 | +- In serial mode on Linux, ensure your user has permission to access the serial device (for example, group `dialout`). |
| 128 | +- On Windows, serial mode is stubbed out in the current implementation and simulated mode should be used unless Windows serial support is added. |
0 commit comments