Compare commits
38
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e1a7d0eca | ||
|
|
1ac5ddbf33 | ||
|
|
8dffdc9bf9 | ||
|
|
23116f6a19 | ||
|
|
2badcb92e2 | ||
|
|
8abae7b9ba | ||
|
|
1bce98e083 | ||
|
|
92b3c72e3e | ||
|
|
41cf0ad7ca | ||
|
|
80ff81b1e0 | ||
|
|
46016d966d | ||
|
|
c2b947a197 | ||
|
|
66ab3fa6ca | ||
|
|
c8da529ce3 | ||
|
|
93c65a948b | ||
|
|
124ef542b0 | ||
|
|
31f8dc2f98 | ||
|
|
77f53435fe | ||
|
|
565900c9d4 | ||
|
|
a3d28e8cd5 | ||
|
|
9d024de5a6 | ||
|
|
18932a3f4d | ||
|
|
e4868f6fdc | ||
|
|
d4cf7f5b48 | ||
|
|
c8f07e22d9 | ||
|
|
22f4d777a4 | ||
|
|
3977e8d17b | ||
|
|
a50c48a92d | ||
|
|
96233d2ae5 | ||
|
|
86cc46ea9b | ||
|
|
d5738d460c | ||
|
|
80f7c89852 | ||
|
|
744d145ec2 | ||
|
|
a775109b36 | ||
|
|
97712364ca | ||
|
|
e349ef92b7 | ||
|
|
bcc3e97c80 | ||
|
|
095da18f73 |
+50
-9
@@ -1,21 +1,62 @@
|
||||
# System Architecture
|
||||
|
||||
## Overview
|
||||
Describe the project at a high level.
|
||||
OfficeSense is a Smart Office Presence system that detects user presence and identifies the room where each user is located using Bluetooth Low Energy (BLE) technology. Users carry BLE tags that periodically broadcast pseudonymized identifiers, while BLE scanners installed in different rooms detect these signals and forward the data to a Raspberry Pi for processing.
|
||||
|
||||
The system follows a privacy-by-design approach by using identifiers instead of personal data during communication. In addition to BLE-based presence detection, a camera connected to the Raspberry Pi performs face-recognition-based authentication to verify that the detected BLE tag belongs to the actual user.
|
||||
|
||||
The architecture combines edge computing, real-time processing, and lightweight storage technologies to provide low-latency monitoring, room occupancy tracking, and secure user authentication through real-time dashboards.
|
||||
|
||||
## Components
|
||||
- sensors / input devices
|
||||
- processing node
|
||||
- communication
|
||||
- storage
|
||||
- dashboard / app
|
||||
- actuators / notifications
|
||||
- BLE Tags (Seeed Studio XIAO ESP32-C6)
|
||||
- BLE Scanners (Waveshare ESP32-S3 Zero)
|
||||
- USB Camera
|
||||
- Processing Node (Raspberry Pi)
|
||||
- Communication (BLE, WiFi, MQTT, HTTP, NGSI-LD)
|
||||
- Storage (SQLite, Redis, Optional InfluxDB)
|
||||
- Dashboard (Node-RED, AdminJS, Optional Grafana)
|
||||
|
||||
## Data Flow
|
||||
Describe how data moves through the system.
|
||||
1. A BLE tag periodically broadcasts a pseudonymized UUID.
|
||||
2. Nearby BLE scanners receive the signal and measure RSSI values.
|
||||
3. The scanners:
|
||||
- validate UUIDs,
|
||||
- apply EMA filtering to RSSI values,
|
||||
- cache validation responses,
|
||||
- package the data and publish to the MQTT broker over WiFi.
|
||||
4. The Raspberry Pi receives MQTT messages and processes them.
|
||||
5. The system estimates the user’s room based on strongest RSSI values.
|
||||
6. When a new presence event occurs:
|
||||
- the camera is triggered,
|
||||
- image frames are captured,
|
||||
- the face recognition service verifies the user identity.
|
||||
7. The system compares:
|
||||
- BLE tag identity
|
||||
- face recognition identity
|
||||
8. The authentication result and room location are stored in Redis.
|
||||
9. Persistent information is stored in SQLite.
|
||||
10. Backend APIs provide data to dashboards for real-time visualization.
|
||||
11. If no BLE signal is received for a predefined timeout period, the user is automatically marked as absent.
|
||||
|
||||
## Decisions and Tradeoffs
|
||||
Explain key design choices.
|
||||
- Edge Computing with Raspberry Pi
|
||||
- Reduces latency
|
||||
- Minimizes cloud dependency
|
||||
- Improves privacy and reliability
|
||||
- Hybrid Storage Architecture
|
||||
- SQLite for reliable persistent storage
|
||||
- Redis for fast real-time operations
|
||||
- Event-Driven Processing
|
||||
- BLE detections trigger processing only when needed
|
||||
- Reduces unnecessary computation
|
||||
- Privacy-by-Design
|
||||
- Uses pseudonymized UUIDs instead of direct user identities
|
||||
- MQTT Communication
|
||||
- Lightweight and suitable for IoT environments
|
||||
- Supports asynchronous communication
|
||||
|
||||
- RSSI-based localization is less accurate than more advanced positioning systems
|
||||
- Face recognition increases security but also adds computational overhead
|
||||
|
||||
## Validation Plan
|
||||
Describe how you will test the system.
|
||||
|
||||
+33
-9
@@ -1,7 +1,5 @@
|
||||
# Weekly Progress Log
|
||||
|
||||
Update this file every week.
|
||||
|
||||
## Week 1
|
||||
### Completed
|
||||
- Repository created
|
||||
@@ -18,19 +16,45 @@ Update this file every week.
|
||||
- Finalize architecture
|
||||
- Create first Issues
|
||||
|
||||
### Team Contribution
|
||||
- Student 1:
|
||||
- Student 2:
|
||||
|
||||
## Week 2
|
||||
### Completed
|
||||
- Full BLE Tag code
|
||||
|
||||
### In Progress
|
||||
|
||||
### Problems / Risks
|
||||
|
||||
### Next Steps
|
||||
- UUID modification during Runtime
|
||||
|
||||
### Team Contribution
|
||||
- Student 1:
|
||||
- Student 2:
|
||||
## Week 3
|
||||
### Completed
|
||||
- Full BLE Scanner code
|
||||
- SQLite schema with Prisma ORM
|
||||
- UUID lookup for public API
|
||||
- Connected and tested functionality of all equipment
|
||||
|
||||
### In Progress
|
||||
- Core script to receive MQTT data, process and store in Redis
|
||||
- NGSI-LD data for public API
|
||||
- Admin API with CRUD functions
|
||||
|
||||
### Problems / Risks
|
||||
- WiFi won't reconnect
|
||||
- no auth to use public API
|
||||
|
||||
### Next Steps
|
||||
- WiFi self-healing and MQTT reconnection & testing
|
||||
|
||||
## Week 4
|
||||
### Completed
|
||||
- Tag & Scanner CLI for configuration & persistent config
|
||||
- Core script (MQTT, location estimation, Store in Redis)
|
||||
- NGSI-LD public api
|
||||
- public dashboard (Node-RED)
|
||||
|
||||
### In Progress
|
||||
- Admin API with CRUD functions
|
||||
|
||||
### Next Steps
|
||||
- Add verification with camera in core script
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
[
|
||||
{
|
||||
"id": "f1398f7c64b1d0ea",
|
||||
"type": "tab",
|
||||
"label": "OfficeSense Flow",
|
||||
"disabled": false,
|
||||
"info": "",
|
||||
"env": []
|
||||
},
|
||||
{
|
||||
"id": "20bb112ec6e856de",
|
||||
"type": "ui-table",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"group": "05cf402c93866d4e",
|
||||
"name": "",
|
||||
"label": "",
|
||||
"order": 1,
|
||||
"width": 0,
|
||||
"height": 0,
|
||||
"maxrows": 0,
|
||||
"passthru": false,
|
||||
"autocols": true,
|
||||
"showSearch": true,
|
||||
"deselect": true,
|
||||
"selectionType": "none",
|
||||
"columns": [],
|
||||
"mobileBreakpoint": "sm",
|
||||
"mobileBreakpointType": "defaults",
|
||||
"action": "replace",
|
||||
"className": "",
|
||||
"x": 690,
|
||||
"y": 360,
|
||||
"wires": [
|
||||
[]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "0a5259b54388cdfc",
|
||||
"type": "inject",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"name": "",
|
||||
"props": [
|
||||
{
|
||||
"p": "payload"
|
||||
}
|
||||
],
|
||||
"repeat": "3",
|
||||
"crontab": "",
|
||||
"once": false,
|
||||
"onceDelay": 0.1,
|
||||
"topic": "",
|
||||
"payload": "",
|
||||
"payloadType": "date",
|
||||
"x": 70,
|
||||
"y": 400,
|
||||
"wires": [
|
||||
[
|
||||
"4030b9e7bcab5435",
|
||||
"f37401387ca7a595"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "4030b9e7bcab5435",
|
||||
"type": "http request",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"name": "",
|
||||
"method": "GET",
|
||||
"ret": "obj",
|
||||
"paytoqs": "ignore",
|
||||
"url": "http://192.168.1.2/livedata/entities",
|
||||
"tls": "",
|
||||
"persist": false,
|
||||
"proxy": "",
|
||||
"insecureHTTPParser": false,
|
||||
"authType": "",
|
||||
"senderr": false,
|
||||
"headers": [],
|
||||
"x": 270,
|
||||
"y": 360,
|
||||
"wires": [
|
||||
[
|
||||
"4aee360198b34403"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "f37401387ca7a595",
|
||||
"type": "http request",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"name": "",
|
||||
"method": "GET",
|
||||
"ret": "obj",
|
||||
"paytoqs": "ignore",
|
||||
"url": "http://192.168.1.2/livedata/entities?type=room",
|
||||
"tls": "",
|
||||
"persist": false,
|
||||
"proxy": "",
|
||||
"insecureHTTPParser": false,
|
||||
"authType": "",
|
||||
"senderr": false,
|
||||
"headers": [],
|
||||
"x": 270,
|
||||
"y": 440,
|
||||
"wires": [
|
||||
[
|
||||
"448ba65b518d534d"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "448ba65b518d534d",
|
||||
"type": "function",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"name": "ParseNGSI",
|
||||
"func": "msg.payload = msg.payload.map(r => {\n return {\n Room: r.name?.value,\n Occupancy: r.occupancy?.value\n };\n});\n\nreturn msg;",
|
||||
"outputs": 1,
|
||||
"timeout": 0,
|
||||
"noerr": 0,
|
||||
"initialize": "",
|
||||
"finalize": "",
|
||||
"libs": [],
|
||||
"x": 490,
|
||||
"y": 440,
|
||||
"wires": [
|
||||
[
|
||||
"586c57be87bf8bc6",
|
||||
"49042a0c5086d4b8"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "586c57be87bf8bc6",
|
||||
"type": "ui-table",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"group": "a98ec11a60f808be",
|
||||
"name": "",
|
||||
"label": "",
|
||||
"order": 1,
|
||||
"width": 0,
|
||||
"height": 0,
|
||||
"maxrows": 0,
|
||||
"passthru": false,
|
||||
"autocols": true,
|
||||
"showSearch": true,
|
||||
"deselect": true,
|
||||
"selectionType": "none",
|
||||
"columns": [],
|
||||
"mobileBreakpoint": "sm",
|
||||
"mobileBreakpointType": "defaults",
|
||||
"action": "replace",
|
||||
"className": "",
|
||||
"x": 690,
|
||||
"y": 440,
|
||||
"wires": [
|
||||
[]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "4aee360198b34403",
|
||||
"type": "function",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"name": "ParseNGSI",
|
||||
"func": "const entities = msg.payload;\n\nconst users = entities.filter(e => e.type === \"User\");\nconst rooms = entities.filter(e => e.type === \"Room\");\n\nconst roomMap = {};\nfor (const r of rooms) {\n roomMap[r.id.replace(\"urn:ngsi-ld:Room:\", \"\")] = r;\n}\n\nmsg.payload = users.map(u => {\n\n const roomId = u.locatedIn?.object;\n const room = roomMap[roomId];\n\n return {\n Name: u.name?.value,\n Room: room ? room.name?.value : \"Unknown\",\n RSSI: u.rssi?.value,\n Auth: u.authenticationStatus?.value,\n Time: new Date(u.observedAt?.value)\n .toLocaleString(\"el-GR\", { timeZone: \"Europe/Athens\" })\n };\n});\n\nreturn msg;",
|
||||
"outputs": 1,
|
||||
"timeout": 0,
|
||||
"noerr": 0,
|
||||
"initialize": "",
|
||||
"finalize": "",
|
||||
"libs": [],
|
||||
"x": 490,
|
||||
"y": 360,
|
||||
"wires": [
|
||||
[
|
||||
"20bb112ec6e856de"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "49042a0c5086d4b8",
|
||||
"type": "ui-chart",
|
||||
"z": "f1398f7c64b1d0ea",
|
||||
"group": "a98ec11a60f808be",
|
||||
"name": "",
|
||||
"label": "Room Occupancy Chart",
|
||||
"order": 2,
|
||||
"chartType": "bar",
|
||||
"category": "",
|
||||
"categoryType": "none",
|
||||
"xAxisLabel": "Room",
|
||||
"xAxisProperty": "Room",
|
||||
"xAxisPropertyType": "property",
|
||||
"xAxisType": "category",
|
||||
"xAxisFormat": "",
|
||||
"xAxisFormatType": "auto",
|
||||
"xmin": "",
|
||||
"xmax": "",
|
||||
"yAxisLabel": "Occupancy",
|
||||
"yAxisProperty": "Occupancy",
|
||||
"yAxisPropertyType": "property",
|
||||
"ymin": "0",
|
||||
"ymax": "10",
|
||||
"bins": 10,
|
||||
"action": "replace",
|
||||
"stackSeries": false,
|
||||
"pointShape": "circle",
|
||||
"pointRadius": 4,
|
||||
"showLegend": true,
|
||||
"removeOlder": 1,
|
||||
"removeOlderUnit": "3600",
|
||||
"removeOlderPoints": "",
|
||||
"colors": [
|
||||
"#0095ff",
|
||||
"#ff0000",
|
||||
"#ff7f0e",
|
||||
"#2ca02c",
|
||||
"#a347e1",
|
||||
"#d62728",
|
||||
"#ff9896",
|
||||
"#9467bd",
|
||||
"#c5b0d5"
|
||||
],
|
||||
"textColor": [
|
||||
"#666666"
|
||||
],
|
||||
"textColorDefault": true,
|
||||
"gridColor": [
|
||||
"#e5e5e5"
|
||||
],
|
||||
"gridColorDefault": true,
|
||||
"width": 6,
|
||||
"height": 8,
|
||||
"className": "",
|
||||
"interpolation": "linear",
|
||||
"x": 750,
|
||||
"y": 520,
|
||||
"wires": [
|
||||
[]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "05cf402c93866d4e",
|
||||
"type": "ui-group",
|
||||
"name": "Users",
|
||||
"page": "fc203945d260d5ec",
|
||||
"width": 6,
|
||||
"height": 1,
|
||||
"order": 1,
|
||||
"showTitle": true,
|
||||
"className": "",
|
||||
"visible": "true",
|
||||
"disabled": "false",
|
||||
"groupType": "default"
|
||||
},
|
||||
{
|
||||
"id": "a98ec11a60f808be",
|
||||
"type": "ui-group",
|
||||
"name": "Rooms",
|
||||
"page": "fc203945d260d5ec",
|
||||
"width": 6,
|
||||
"height": 1,
|
||||
"order": 2,
|
||||
"showTitle": true,
|
||||
"className": "",
|
||||
"visible": "true",
|
||||
"disabled": "false",
|
||||
"groupType": "default"
|
||||
},
|
||||
{
|
||||
"id": "fc203945d260d5ec",
|
||||
"type": "ui-page",
|
||||
"name": "OfficeSense",
|
||||
"ui": "7d0f4d8165214cca",
|
||||
"path": "/officesense",
|
||||
"icon": "home",
|
||||
"layout": "grid",
|
||||
"theme": "f49a05b173b6cac3",
|
||||
"breakpoints": [
|
||||
{
|
||||
"name": "Default",
|
||||
"px": "0",
|
||||
"cols": "3"
|
||||
},
|
||||
{
|
||||
"name": "Tablet",
|
||||
"px": "576",
|
||||
"cols": "6"
|
||||
},
|
||||
{
|
||||
"name": "Small Desktop",
|
||||
"px": "768",
|
||||
"cols": "9"
|
||||
},
|
||||
{
|
||||
"name": "Desktop",
|
||||
"px": "1024",
|
||||
"cols": "12"
|
||||
}
|
||||
],
|
||||
"order": 1,
|
||||
"className": "",
|
||||
"visible": "true",
|
||||
"disabled": "false"
|
||||
},
|
||||
{
|
||||
"id": "7d0f4d8165214cca",
|
||||
"type": "ui-base",
|
||||
"name": "My Dashboard",
|
||||
"path": "/dashboard",
|
||||
"appIcon": "",
|
||||
"includeClientData": true,
|
||||
"acceptsClientConfig": [
|
||||
"ui-notification",
|
||||
"ui-control"
|
||||
],
|
||||
"showPathInSidebar": false,
|
||||
"headerContent": "page",
|
||||
"navigationStyle": "default",
|
||||
"titleBarStyle": "default",
|
||||
"showReconnectNotification": true,
|
||||
"notificationDisplayTime": 1,
|
||||
"showDisconnectNotification": true,
|
||||
"allowInstall": false
|
||||
},
|
||||
{
|
||||
"id": "f49a05b173b6cac3",
|
||||
"type": "ui-theme",
|
||||
"name": "Default Theme",
|
||||
"colors": {
|
||||
"surface": "#ffffff",
|
||||
"primary": "#0094ce",
|
||||
"bgPage": "#eeeeee",
|
||||
"groupBg": "#ffffff",
|
||||
"groupOutline": "#cccccc"
|
||||
},
|
||||
"sizes": {
|
||||
"density": "default",
|
||||
"pagePadding": "12px",
|
||||
"groupGap": "12px",
|
||||
"groupBorderRadius": "4px",
|
||||
"widgetGap": "12px"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "2372b8eb214a55ee",
|
||||
"type": "global-config",
|
||||
"env": [],
|
||||
"modules": {
|
||||
"@flowfuse/node-red-dashboard": "1.30.2"
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
.git
|
||||
npm-debug.log
|
||||
@@ -0,0 +1,5 @@
|
||||
node_modules
|
||||
# Keep environment variables out of version control
|
||||
.env
|
||||
|
||||
**/generated/prisma
|
||||
@@ -0,0 +1,19 @@
|
||||
FROM node:24-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update -y && apt-get install -y openssl
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm ci
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npx prisma generate
|
||||
|
||||
RUN npm run build
|
||||
|
||||
EXPOSE 80
|
||||
|
||||
CMD ["node", "dist/index.js"]
|
||||
Generated
+3638
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "officesense",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"license": "ISC",
|
||||
"author": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "tsx src/index.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/better-sqlite3": "^7.6.13",
|
||||
"@types/express": "^5.0.6",
|
||||
"@types/node": "^25.7.0",
|
||||
"prisma": "^7.8.0",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "^6.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/adapter-better-sqlite3": "^7.8.0",
|
||||
"@prisma/client": "^7.8.0",
|
||||
"dotenv": "^17.4.2",
|
||||
"express": "^5.2.1",
|
||||
"mqtt": "^5.15.1",
|
||||
"redis": "^5.12.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// This file was generated by Prisma, and assumes you have installed the following:
|
||||
// npm install --save-dev prisma dotenv
|
||||
import "dotenv/config";
|
||||
import { defineConfig } from "prisma/config";
|
||||
|
||||
export default defineConfig({
|
||||
schema: "prisma/schema.prisma",
|
||||
migrations: {
|
||||
path: "prisma/migrations",
|
||||
},
|
||||
datasource: {
|
||||
url: process.env["DATABASE_URL"],
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"firstName" TEXT NOT NULL,
|
||||
"lastName" TEXT NOT NULL,
|
||||
"faceEmbedding" BLOB NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Tag" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"userId" TEXT NOT NULL,
|
||||
CONSTRAINT "Tag_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Tag_userId_key" ON "Tag"("userId");
|
||||
@@ -0,0 +1,20 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Room" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"nameame" TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_User" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"firstName" TEXT NOT NULL,
|
||||
"lastName" TEXT NOT NULL,
|
||||
"faceEmbedding" BLOB
|
||||
);
|
||||
INSERT INTO "new_User" ("faceEmbedding", "firstName", "id", "lastName") SELECT "faceEmbedding", "firstName", "id", "lastName" FROM "User";
|
||||
DROP TABLE "User";
|
||||
ALTER TABLE "new_User" RENAME TO "User";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `nameame` on the `Room` table. All the data in the column will be lost.
|
||||
- Added the required column `name` to the `Room` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Room" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
INSERT INTO "new_Room" ("id") SELECT "id" FROM "Room";
|
||||
DROP TABLE "Room";
|
||||
ALTER TABLE "new_Room" RENAME TO "Room";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "sqlite"
|
||||
@@ -0,0 +1,27 @@
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../src/generated/prisma"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(uuid())
|
||||
firstName String
|
||||
lastName String
|
||||
faceEmbedding Bytes?
|
||||
tag Tag?
|
||||
}
|
||||
|
||||
model Tag {
|
||||
id String @id @default(uuid())
|
||||
userId String @unique
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
}
|
||||
|
||||
model Room {
|
||||
id String @id @default(uuid())
|
||||
name String
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import express from "express";
|
||||
import { getEntities, getEntitiesOfType, getEntity } from "./livedata.controller.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/entities", getEntities);
|
||||
router.get("/entities", getEntitiesOfType);
|
||||
router.get("/entities/:urn", getEntity);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,172 @@
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
import { getActiveUserData, getRedisRoomData, type RedisRoomData, type RedisUserData } from "./livedata.repository.js";
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
interface Params {
|
||||
urn: string;
|
||||
}
|
||||
|
||||
type Relationship = {
|
||||
type: "Relationship";
|
||||
object: string;
|
||||
};
|
||||
|
||||
type Property<T = any> = {
|
||||
type: "Property";
|
||||
value: T;
|
||||
};
|
||||
|
||||
interface Entity {
|
||||
id: string;
|
||||
type: string;
|
||||
name: {
|
||||
type: "Property";
|
||||
value: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface User extends Entity {
|
||||
type: "User";
|
||||
locatedIn: Relationship;
|
||||
rssi: Property<number>;
|
||||
authenticationStatus: Property<string>;
|
||||
observedAt: Property<string>;
|
||||
}
|
||||
|
||||
interface Room extends Entity {
|
||||
type: "Room";
|
||||
occupancy: Property<number>;
|
||||
}
|
||||
|
||||
function convertToNGSIUser(user: RedisUserData): User {
|
||||
return {
|
||||
id: `urn:ngsi-ld:User:${user.userID}`,
|
||||
type: "User",
|
||||
name: {
|
||||
type: "Property",
|
||||
value: user.name
|
||||
},
|
||||
locatedIn: {
|
||||
type: "Relationship",
|
||||
object: user.room
|
||||
},
|
||||
rssi: {
|
||||
type: "Property",
|
||||
value: user.rssi,
|
||||
},
|
||||
authenticationStatus: {
|
||||
type: "Property",
|
||||
value: user.verified ? "verified" : "unverified"
|
||||
},
|
||||
observedAt: {
|
||||
type: "Property",
|
||||
value: (new Date(user.timestamp)).toISOString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function convertToNGSIRoom(room: RedisRoomData): Promise<Room | null> {
|
||||
const roomName = await prisma.room.findUnique({
|
||||
where: { id: room.roomID },
|
||||
select: { name: true }
|
||||
})
|
||||
|
||||
if (!roomName?.name) return null;
|
||||
|
||||
return {
|
||||
id: `urn:ngsi-ld:Room:${room.roomID}`,
|
||||
type: "Room",
|
||||
name: {
|
||||
type: "Property",
|
||||
value: roomName.name
|
||||
},
|
||||
occupancy: {
|
||||
type: "Property",
|
||||
value: room.occupancy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getNGSIUsers(): Promise<User[]> {
|
||||
let users: User[] = [];
|
||||
|
||||
const result: RedisUserData[] = await getActiveUserData();
|
||||
|
||||
for (const r of result) {
|
||||
const user = convertToNGSIUser(r);
|
||||
|
||||
users.push(user);
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
async function getNGSIRooms(): Promise<Room[]> {
|
||||
let rooms: Room[] = [];
|
||||
|
||||
const result: RedisRoomData[] = await getRedisRoomData();
|
||||
|
||||
for (const r of result) {
|
||||
const room = await convertToNGSIRoom(r);
|
||||
if (room == null) continue;
|
||||
|
||||
rooms.push(room);
|
||||
}
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
export async function getEntities(req: Request, res: Response, next: NextFunction) {
|
||||
if (Object.keys(req.query).length > 0) return next();
|
||||
|
||||
let entities: Entity[] = [];
|
||||
|
||||
entities.push(...await getNGSIUsers());
|
||||
entities.push(...await getNGSIRooms());
|
||||
|
||||
return res.status(200).send(entities);
|
||||
}
|
||||
|
||||
export async function getEntitiesOfType(req: Request, res: Response) {
|
||||
const { type } = req.query;
|
||||
|
||||
switch (type) {
|
||||
case "user": return res.status(200).send(await getNGSIUsers());
|
||||
case "room": return res.status(200).send(await getNGSIRooms());
|
||||
default:
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getEntity(req: Request<Params>, res: Response) {
|
||||
const { urn } = req.params;
|
||||
|
||||
if (!urn || !urn.startsWith("urn:ngsi-ld:")) return res.sendStatus(400);
|
||||
|
||||
const entity = urn.slice(12);
|
||||
|
||||
if (!entity) return res.sendStatus(400);
|
||||
|
||||
if (entity.toLowerCase().startsWith("room:")) {
|
||||
const roomID = entity.slice(5);
|
||||
|
||||
if (!roomID) return res.sendStatus(400);
|
||||
|
||||
const data = await getRedisRoomData(roomID);
|
||||
|
||||
if (!data.length) return res.sendStatus(404);
|
||||
|
||||
return res.status(200).send(await convertToNGSIRoom(data[0]!));
|
||||
} else if (entity.toLowerCase().startsWith("user:")) {
|
||||
const userID = entity.slice(5);
|
||||
|
||||
if (!userID) return res.sendStatus(400);
|
||||
|
||||
const data = await getActiveUserData(userID);
|
||||
|
||||
if (!data.length) return res.sendStatus(404);
|
||||
|
||||
return res.status(200).send(convertToNGSIUser(data[0]!));
|
||||
} else
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
import { getRedis } from "../../redis/redis.js";
|
||||
|
||||
export interface RedisUserData {
|
||||
userID: string;
|
||||
name: string;
|
||||
rssi: number;
|
||||
room: string;
|
||||
verified: boolean;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface RedisRoomData {
|
||||
roomID: string;
|
||||
occupancy: number;
|
||||
}
|
||||
|
||||
export async function getActiveUserData(userID?: string): Promise<RedisUserData[]> {
|
||||
const redis = getRedis();
|
||||
|
||||
if (!userID) {
|
||||
let cursor = "0";
|
||||
let result: RedisUserData[] = [];
|
||||
|
||||
do {
|
||||
const res = await redis.scan(cursor, {
|
||||
MATCH: "user:*",
|
||||
COUNT: 100,
|
||||
});
|
||||
|
||||
cursor = res.cursor;
|
||||
|
||||
const values = await Promise.all(
|
||||
res.keys.map((key) => redis.get(key))
|
||||
);
|
||||
|
||||
result.push(...values
|
||||
.filter((v): v is string => v !== null)
|
||||
.map((v) => JSON.parse(v))
|
||||
);
|
||||
|
||||
} while (cursor !== "0");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const _userID = await redis.get(`_user:${userID}`);
|
||||
|
||||
if (!_userID) return [];
|
||||
|
||||
const res = await redis.get(`user:${_userID}`);
|
||||
|
||||
if (!res) return [];
|
||||
|
||||
const resObj: RedisUserData = JSON.parse(res);
|
||||
|
||||
return [resObj];
|
||||
}
|
||||
|
||||
export async function getRedisRoomData(roomID?: string): Promise<RedisRoomData[]> {
|
||||
const redis = getRedis();
|
||||
|
||||
if (!roomID) {
|
||||
let cursor = "0";
|
||||
let result: RedisRoomData[] = [];
|
||||
|
||||
do {
|
||||
const res = await redis.scan(cursor, {
|
||||
MATCH: "room:*",
|
||||
COUNT: 100,
|
||||
});
|
||||
|
||||
cursor = res.cursor;
|
||||
|
||||
const values = await Promise.all(
|
||||
res.keys.map(async (key) => ({
|
||||
roomID: key,
|
||||
occupancy: await redis.get(key),
|
||||
}))
|
||||
);
|
||||
|
||||
result.push(...values
|
||||
.filter((v) => v.occupancy !== null)
|
||||
.map((v) => ({
|
||||
roomID: v.roomID.slice(5),
|
||||
occupancy: Number(v.occupancy),
|
||||
}))
|
||||
);
|
||||
|
||||
} while (cursor !== "0");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const res = await redis.get(`room:${roomID}`);
|
||||
|
||||
if (!res) return [];
|
||||
|
||||
return [{ roomID: roomID, occupancy: Number(res) }];
|
||||
}
|
||||
|
||||
export async function getRoomIDs(): Promise<{ id: string; }[]> {
|
||||
return await prisma.room.findMany({
|
||||
select: { id: true }
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { existsByUUID } from "./lookup.repository.js";
|
||||
|
||||
interface Params {
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
export async function check(req: Request<Params>, res: Response) {
|
||||
const { uuid } = req.params;
|
||||
|
||||
const exists = await existsByUUID(uuid);
|
||||
|
||||
if (!exists) return res.sendStatus(404);
|
||||
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
export async function existsByUUID(uuid: string): Promise<boolean> {
|
||||
const tag = await prisma.tag.findUnique({
|
||||
where: { id: uuid }
|
||||
});
|
||||
|
||||
return !!tag;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import express from "express";
|
||||
import { check } from "./lookup.controller.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/check/:uuid", check);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,57 @@
|
||||
interface Config {
|
||||
api: {
|
||||
address: string;
|
||||
port: number;
|
||||
};
|
||||
mqtt: {
|
||||
host: string;
|
||||
port: number;
|
||||
username: string;
|
||||
password: string;
|
||||
topic: string;
|
||||
};
|
||||
redis: {
|
||||
host: string;
|
||||
port: number;
|
||||
};
|
||||
core: {
|
||||
hysteresis: number;
|
||||
candidateHysteresis: number;
|
||||
debounceMS: number;
|
||||
minSamples: number;
|
||||
transitionTTL: number;
|
||||
transitionCleanupInterval: number;
|
||||
lossThreshold: number;
|
||||
userTTL: number;
|
||||
};
|
||||
}
|
||||
|
||||
const config: Config = {
|
||||
api: {
|
||||
address: "0.0.0.0",
|
||||
port: 80,
|
||||
},
|
||||
mqtt: {
|
||||
host: "192.168.1.2",
|
||||
port: 1883,
|
||||
username: "user",
|
||||
password: "pass",
|
||||
topic: "scanners/+",
|
||||
},
|
||||
redis: {
|
||||
host: "192.168.1.2",
|
||||
port: 6379,
|
||||
},
|
||||
core: {
|
||||
hysteresis: 6,
|
||||
candidateHysteresis: 3,
|
||||
debounceMS: 3000,
|
||||
minSamples: 4,
|
||||
transitionTTL: 5 * 60 * 1000,
|
||||
transitionCleanupInterval: 60 * 1000,
|
||||
lossThreshold: 5000,
|
||||
userTTL: 3 * 60 * 1000
|
||||
},
|
||||
}
|
||||
|
||||
export default config;
|
||||
@@ -0,0 +1,173 @@
|
||||
import { getRedis } from "../redis/redis.js";
|
||||
import { prisma } from "../lib/prisma.js";
|
||||
import { RoomTransition, transitions } from "./transition.js";
|
||||
import config from "../config/config.js";
|
||||
import { type RedisUserData } from "../api/livedata/livedata.repository.js";
|
||||
|
||||
const adjectives = [
|
||||
"Crazy",
|
||||
"Silent",
|
||||
"Dark",
|
||||
"Fast",
|
||||
"Lucky",
|
||||
"Wild",
|
||||
"Epic"
|
||||
];
|
||||
|
||||
const nouns = [
|
||||
"Tiger",
|
||||
"Wolf",
|
||||
"Falcon",
|
||||
"Shadow",
|
||||
"Ninja",
|
||||
"Dragon",
|
||||
"Phoenix"
|
||||
];
|
||||
|
||||
const running = new Set<string>();
|
||||
const userRooms = new Map<string, string>();
|
||||
const userPseudo = new Map<string, string>();
|
||||
|
||||
export async function updateRoomOccupancyListener(message: string, channel: string) {
|
||||
console.log(`[Redis] Key ${message} expired.`);
|
||||
|
||||
const id = message.startsWith("user:") ? message.slice(5) : message;
|
||||
const lastRoomID = userRooms.get(id);
|
||||
const userPseudoID = userPseudo.get(id);
|
||||
|
||||
const redis = getRedis();
|
||||
|
||||
if (!lastRoomID) {
|
||||
console.log("[!] Key not found in local mapι")
|
||||
} else {
|
||||
await redis.decr(`room:${lastRoomID}`);
|
||||
}
|
||||
|
||||
await redis.del(`_user:${userPseudoID}`);
|
||||
|
||||
userRooms.delete(id);
|
||||
userPseudo.delete(id);
|
||||
}
|
||||
|
||||
function generateNickname() {
|
||||
const adjective =
|
||||
adjectives[Math.floor(Math.random() * adjectives.length)];
|
||||
|
||||
const noun =
|
||||
nouns[Math.floor(Math.random() * nouns.length)];
|
||||
|
||||
const number = Math.floor(Math.random() * 1000);
|
||||
|
||||
return `${adjective}${noun}${number}`;
|
||||
}
|
||||
|
||||
function newUser(): { pseudoID: string, psuedoName: string } {
|
||||
return {
|
||||
pseudoID: crypto.randomUUID(),
|
||||
psuedoName: generateNickname()
|
||||
};
|
||||
}
|
||||
|
||||
export async function analyzeData(roomID: string, metrics: {
|
||||
tagID: string,
|
||||
rssi: number
|
||||
}) {
|
||||
if (running.has(metrics.tagID)) {
|
||||
console.log(`[!] Skipping TAG_ID: ${metrics.tagID} already in process.`);
|
||||
return;
|
||||
}
|
||||
|
||||
running.add(metrics.tagID);
|
||||
|
||||
try {
|
||||
console.log(`\n=======================================================\n` +
|
||||
`ROOM_ID: ${roomID}\nTAG_ID: ${metrics.tagID}\nRSSI: ${metrics.rssi}\n` +
|
||||
`=======================================================\n`);
|
||||
|
||||
const userID = (
|
||||
await prisma.tag.findUnique({
|
||||
where: { id: metrics.tagID },
|
||||
select: { userId: true },
|
||||
})
|
||||
)?.userId;
|
||||
|
||||
console.log(`[SQLite] Resolved USER_ID: ${userID}`);
|
||||
|
||||
if (!userID)
|
||||
return;
|
||||
|
||||
const redis = getRedis();
|
||||
const res = await redis.get(`user:${userID}`);
|
||||
|
||||
if (!res) {
|
||||
const user = newUser();
|
||||
console.log(`[Redis] Created USER: ${user.psuedoName} USER_ID: ${user.pseudoID}`);
|
||||
|
||||
// trigger camera
|
||||
|
||||
await redis.set(`user:${userID}`, JSON.stringify({
|
||||
userID: user.pseudoID,
|
||||
name: user.psuedoName,
|
||||
rssi: metrics.rssi,
|
||||
room: roomID,
|
||||
verified: false,
|
||||
timestamp: Date.now()
|
||||
}), { PX: config.core.userTTL });
|
||||
|
||||
await redis.set(`_user:${user.pseudoID}`, userID);
|
||||
|
||||
await redis.incr(`room:${roomID}`);
|
||||
|
||||
userRooms.set(userID, roomID);
|
||||
userPseudo.set(userID, user.pseudoID);
|
||||
|
||||
console.log("[Redis] Stored new user in redis.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let resObj: RedisUserData = JSON.parse(res);
|
||||
console.log(`[Redis] User exists in redis in ROOM_ID: ${resObj["room"]}`);
|
||||
|
||||
if (roomID == resObj["room"]) {
|
||||
console.log("[Redis] Same room, updating redis...");
|
||||
resObj["rssi"] = metrics.rssi;
|
||||
resObj["timestamp"] = Date.now()
|
||||
|
||||
await redis.set(`user:${userID}`, JSON.stringify(resObj), { PX: config.core.userTTL });
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let transition = transitions.get(userID);
|
||||
|
||||
// update room occupancy
|
||||
|
||||
if (!transition) {
|
||||
console.log("[!] Starting new transition");
|
||||
transition = new RoomTransition();
|
||||
transitions.set(userID, transition);
|
||||
}
|
||||
|
||||
if (transition.shouldTransitionTo(roomID, metrics.rssi, resObj["rssi"], resObj["timestamp"])) {
|
||||
// trigger camera
|
||||
|
||||
console.log(`[!] Transition done ${resObj["room"]} -> ${roomID}`);
|
||||
|
||||
await redis.decr(`room:${resObj["room"]}`);
|
||||
await redis.incr(`room:${roomID}`);
|
||||
|
||||
userRooms.set(userID, roomID);
|
||||
|
||||
resObj["rssi"] = metrics.rssi;
|
||||
resObj["room"] = roomID;
|
||||
resObj["timestamp"] = Date.now()
|
||||
|
||||
|
||||
await redis.set(`user:${userID}`, JSON.stringify(resObj), { PX: config.core.userTTL });
|
||||
} else
|
||||
console.log("[!] Transition declined");
|
||||
} finally {
|
||||
running.delete(metrics.tagID);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { getRoomIDs } from "../api/livedata/livedata.repository.js";
|
||||
import { initRedis, initSubRedis } from "../redis/redis.js";
|
||||
import * as mqtt from "./mqtt.js";
|
||||
import { cleanupWorker } from "./transition.js";
|
||||
|
||||
export async function bootstrap() {
|
||||
try {
|
||||
const redis = await initRedis();
|
||||
|
||||
await redis.flushDb();
|
||||
console.log(`[!] Flushed Redis.`);
|
||||
|
||||
const rooms = await getRoomIDs();
|
||||
for (const { id } of rooms)
|
||||
await redis.set(`room:${id}`, 0);
|
||||
|
||||
await initSubRedis();
|
||||
await mqtt.start();
|
||||
|
||||
cleanupWorker();
|
||||
|
||||
console.log("[!] Core started.");
|
||||
} catch (err: any) {
|
||||
console.log("[!] Bootstrap failed:", err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import mqtt, { MqttClient } from "mqtt";
|
||||
import config from "../config/config.js";
|
||||
import { analyzeData } from "./analyze.js";
|
||||
|
||||
function initMqtt(): Promise<MqttClient> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const client = mqtt.connect(
|
||||
`mqtt://${config.mqtt.host}:${config.mqtt.port}`,
|
||||
{
|
||||
username: config.mqtt.username,
|
||||
password: config.mqtt.password
|
||||
}
|
||||
);
|
||||
|
||||
client.once("connect", () => {
|
||||
console.log("[MQTT] MQTT connected.");
|
||||
|
||||
client.subscribe(config.mqtt.topic, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
console.log("[MQTT] MQTT subscribed.");
|
||||
resolve(client);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
client.once("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function start() {
|
||||
try {
|
||||
const mqttClient = await initMqtt();
|
||||
|
||||
mqttClient.on("message", (topic, message) => {
|
||||
analyzeData(topic.split("/").pop()!, JSON.parse(message.toString()));
|
||||
});
|
||||
|
||||
mqttClient.on("error", (err) => {
|
||||
console.log(err.message);
|
||||
})
|
||||
} catch (err: any) {
|
||||
console.log("[MQTT] MQTT connection failed:", err.message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import config from "../config/config.js";
|
||||
|
||||
export const transitions = new Map<string, RoomTransition>();
|
||||
|
||||
export function cleanupWorker() {
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
|
||||
for (const [userID, transition] of transitions) {
|
||||
const inactiveFor = now - transition.lastSeen;
|
||||
|
||||
if (inactiveFor > 5 * config.core.transitionTTL)
|
||||
transitions.delete(userID);
|
||||
}
|
||||
}, config.core.transitionCleanupInterval);
|
||||
}
|
||||
|
||||
type Candidate = {
|
||||
roomID: string | null;
|
||||
since: number | null;
|
||||
samples: number;
|
||||
rssi: number | null;
|
||||
};
|
||||
|
||||
export class RoomTransition {
|
||||
candidate: Candidate;
|
||||
lastSeen: number;
|
||||
|
||||
constructor() {
|
||||
this.lastSeen = Date.now();
|
||||
this.candidate = { roomID: null, since: null, samples: 0, rssi: null };
|
||||
}
|
||||
|
||||
shouldTransitionTo(roomID: string, candidateRSSI: number, currRSSI: number, lastCurrentSeen: number) {
|
||||
const now = Date.now();
|
||||
this.lastSeen = now;
|
||||
|
||||
const stronger = candidateRSSI > currRSSI + config.core.hysteresis;
|
||||
|
||||
const signalLost = now - lastCurrentSeen > config.core.lossThreshold;
|
||||
|
||||
console.log(`[TRANSITION] stronger = ${stronger}`);
|
||||
console.log(`[TRANSITION] signalLost = ${signalLost}`);
|
||||
|
||||
if (!stronger && !signalLost) {
|
||||
this.reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
const noCandidate = this.candidate.roomID == null;
|
||||
const betterCandidate = this.candidate.roomID != roomID &&
|
||||
this.candidate.rssi !== null &&
|
||||
candidateRSSI > this.candidate.rssi + config.core.candidateHysteresis;
|
||||
|
||||
console.log(`[TRANSITION] noCandidate = ${noCandidate}`);
|
||||
console.log(`[TRANSITION] betterCandidate = ${betterCandidate}`);
|
||||
|
||||
if (noCandidate || betterCandidate) {
|
||||
this.candidate.roomID = roomID;
|
||||
this.candidate.since = now;
|
||||
this.candidate.samples = 1;
|
||||
this.candidate.rssi = candidateRSSI;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
this.candidate.samples++;
|
||||
this.candidate.rssi = candidateRSSI;
|
||||
|
||||
console.log(`[TRANSITION] samples = ${this.candidate.samples}`);
|
||||
console.log(`[TRANSITION] candidateRSSI = ${this.candidate.rssi}`);
|
||||
|
||||
const enoughTime = now - (this.candidate.since ?? now) >= config.core.debounceMS;
|
||||
const enoughConfirmations = this.candidate.samples >= config.core.minSamples;
|
||||
|
||||
console.log(`[TRANSITION] enoughTime = ${enoughTime}`);
|
||||
console.log(`[TRANSITION] enoughConfirmations = ${enoughConfirmations}`);
|
||||
console.log(`[TRANSITION] signalLost = ${signalLost}`);
|
||||
|
||||
if ((enoughTime && enoughConfirmations) || signalLost) {
|
||||
this.reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.candidate.roomID = null;
|
||||
this.candidate.since = null;
|
||||
this.candidate.samples = 0;
|
||||
this.candidate.rssi = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import express from "express";
|
||||
import lookupRoutes from "./api/lookup/lookup.routes.js";
|
||||
import livedataRoutes from "./api/livedata/livadata.routes.js"
|
||||
import config from "./config/config.js";
|
||||
import { bootstrap } from "./core/bootstrap.js";
|
||||
|
||||
(async () => {
|
||||
const app = express();
|
||||
|
||||
app.use("/lookup", lookupRoutes);
|
||||
app.use("/livedata", livedataRoutes);
|
||||
|
||||
app.listen(config.api.port, config.api.address, () => {
|
||||
console.log(`Server listening on ${config.api.address}:${config.api.port}`);
|
||||
});
|
||||
|
||||
bootstrap();
|
||||
|
||||
})()
|
||||
@@ -0,0 +1,10 @@
|
||||
import "dotenv/config";
|
||||
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
||||
import { PrismaClient } from "../generated/prisma/client.js";
|
||||
|
||||
const connectionString = `${process.env.DATABASE_URL}`;
|
||||
|
||||
const adapter = new PrismaBetterSqlite3({ url: connectionString });
|
||||
const prisma = new PrismaClient({ adapter });
|
||||
|
||||
export { prisma };
|
||||
@@ -0,0 +1,48 @@
|
||||
import { createClient, type RedisClientType } from "redis";
|
||||
import config from "../config/config.js";
|
||||
import { updateRoomOccupancyListener } from "../core/analyze.js";
|
||||
|
||||
let redisClient: RedisClientType;
|
||||
let subRedisClient: RedisClientType;
|
||||
|
||||
export async function initSubRedis(): Promise<void> {
|
||||
subRedisClient = createClient({
|
||||
url: `redis://${config.redis.host}:${config.redis.port}`
|
||||
});
|
||||
|
||||
subRedisClient.on("error", (err) => {
|
||||
console.log("[Redis] SubRedis error:", err.message);
|
||||
});
|
||||
|
||||
await subRedisClient.connect();
|
||||
|
||||
console.log("[Redis] SubRedis connected.");
|
||||
|
||||
subRedisClient.pSubscribe("__keyevent@0__:expired", updateRoomOccupancyListener);
|
||||
|
||||
console.log("[Redis] SubRedis listener set.");
|
||||
}
|
||||
|
||||
export async function initRedis(): Promise<RedisClientType> {
|
||||
redisClient = createClient({
|
||||
url: `redis://${config.redis.host}:${config.redis.port}`
|
||||
});
|
||||
|
||||
redisClient.on("error", (err) => {
|
||||
console.log("[Redis] Redis error:", err.message);
|
||||
});
|
||||
|
||||
await redisClient.connect();
|
||||
|
||||
console.log("[Redis] Redis connected.");
|
||||
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
export function getRedis(): RedisClientType {
|
||||
if (!redisClient) {
|
||||
throw new Error("[Redis] Redis not initialized");
|
||||
}
|
||||
|
||||
return redisClient;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
{
|
||||
// Visit https://aka.ms/tsconfig to read more about this file
|
||||
"compilerOptions": {
|
||||
// File Layout
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
// Environment Settings
|
||||
// See also https://aka.ms/tsconfig/module
|
||||
"module": "NodeNext",
|
||||
"target": "ES2022",
|
||||
"moduleResolution": "NodeNext",
|
||||
"types": [],
|
||||
"esModuleInterop": true,
|
||||
"ignoreDeprecations": "6.0",
|
||||
// For nodejs:
|
||||
// "lib": ["esnext"],
|
||||
// "types": ["node"],
|
||||
// and npm install -D @types/node
|
||||
// Other Outputs
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
// Stricter Typechecking Options
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
// Style Options
|
||||
// "noImplicitReturns": true,
|
||||
// "noImplicitOverride": true,
|
||||
// "noUnusedLocals": true,
|
||||
// "noUnusedParameters": true,
|
||||
// "noFallthroughCasesInSwitch": true,
|
||||
// "noPropertyAccessFromIndexSignature": true,
|
||||
// Recommended Options
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"verbatimModuleSyntax": true,
|
||||
"isolatedModules": true,
|
||||
"noUncheckedSideEffectImports": true,
|
||||
"moduleDetection": "force",
|
||||
"skipLibCheck": true,
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"generated",
|
||||
"dist",
|
||||
"prisma.config.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
@@ -0,0 +1,37 @@
|
||||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the convention is to give header files names that end with `.h'.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into the executable file.
|
||||
|
||||
The source code of each library should be placed in a separate directory
|
||||
("lib/your_library_name/[Code]").
|
||||
|
||||
For example, see the structure of the following example libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
Example contents of `src/main.c` using Foo and Bar:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
The PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries by scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
||||
@@ -0,0 +1,25 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:esp32-s3-devkitc-1]
|
||||
platform = espressif32
|
||||
board = esp32-s3-devkitc-1
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
board_upload.flash_size = 4MB
|
||||
board_build.partitions = default.csv
|
||||
build_flags =
|
||||
-DARDUINO_USB_CDC_ON_BOOT=1
|
||||
-DBOARD_HAS_PSRAM
|
||||
-DCORE_DEBUG_LEVEL=0
|
||||
|
||||
lib_deps =
|
||||
h2zero/NimBLE-Arduino
|
||||
knolleary/PubSubClient
|
||||
@@ -0,0 +1,482 @@
|
||||
#include <unordered_map>
|
||||
#include "main.hpp"
|
||||
#include "tag/Tag.hpp"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include <HTTPClient.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Preferences.h>
|
||||
#include "esp_system.h"
|
||||
|
||||
Preferences prefs;
|
||||
Config config;
|
||||
CB cb;
|
||||
String MQTT_PUB_TOPIC;
|
||||
String serial_buffer = "";
|
||||
std::unordered_map<std::string, Tag> tags;
|
||||
std::unordered_map<std::string, UUIDCacheEntry> uuid_cache;
|
||||
SemaphoreHandle_t uuid_mtx;
|
||||
QueueHandle_t ble_queue;
|
||||
QueueHandle_t mqtt_queue;
|
||||
QueueHandle_t http_queue;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqtt(wifiClient);
|
||||
|
||||
void cleanupCache()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
xSemaphoreTake(uuid_mtx, portMAX_DELAY);
|
||||
|
||||
for (auto it = uuid_cache.begin(); it != uuid_cache.end();)
|
||||
{
|
||||
uint32_t age = now - it->second.last_check;
|
||||
|
||||
if (age > config.interval.uuid_cache_clean_timeout)
|
||||
{
|
||||
Serial.printf("Erasing %s from cache after being inactive for %dms.\n",
|
||||
it->first.c_str(),
|
||||
age);
|
||||
it = uuid_cache.erase(it);
|
||||
}
|
||||
else
|
||||
++it;
|
||||
}
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
}
|
||||
|
||||
void cleanupTags()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
for (auto it = tags.begin(); it != tags.end();)
|
||||
{
|
||||
if ((now - it->second.getLastSeen()) >= config.interval.tag_timeout)
|
||||
{
|
||||
Serial.printf("Erasing %s from memory after being inactive for %dms.\n",
|
||||
it->first.c_str(),
|
||||
now - it->second.getLastSeen());
|
||||
it = tags.erase(it);
|
||||
}
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void httpTask(void *)
|
||||
{
|
||||
HttpEvent ev;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (xQueueReceive(http_queue, &ev, portMAX_DELAY))
|
||||
{
|
||||
HTTPClient http;
|
||||
|
||||
String url = config.api.url + config.api.uuid_check_endpoint + String("/") + String(ev.uuid);
|
||||
|
||||
http.setTimeout(3000);
|
||||
http.setReuse(false);
|
||||
|
||||
if (!http.begin(url))
|
||||
{
|
||||
Serial.println("Failed HTTP begin.");
|
||||
continue;
|
||||
}
|
||||
|
||||
int code = http.GET();
|
||||
|
||||
if (code < 0)
|
||||
{
|
||||
xSemaphoreTake(uuid_mtx, portMAX_DELAY);
|
||||
uuid_cache[ev.uuid] = {
|
||||
UUIDState::RETRY,
|
||||
millis()};
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
|
||||
http.end();
|
||||
continue;
|
||||
}
|
||||
|
||||
bool valid = (code == 200);
|
||||
|
||||
http.end();
|
||||
|
||||
xSemaphoreTake(uuid_mtx, portMAX_DELAY);
|
||||
uuid_cache[ev.uuid] = {
|
||||
(valid) ? UUIDState::VALID : UUIDState::INVALID,
|
||||
millis()};
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[%s] is now marked as %s\n", ev.uuid, (valid) ? "valid" : "invalid");
|
||||
#endif
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
|
||||
void mqttTask(void *)
|
||||
{
|
||||
NetEvent ev;
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (!mqtt.connected())
|
||||
{
|
||||
// Serial.printf("MQTT Disconnected. Connecting to %s:%d\n", config.mqtt.host.c_str(), config.mqtt.port);
|
||||
if (mqtt.connect(config.dev_name.c_str(), config.mqtt.username.c_str(), config.mqtt.password.c_str()))
|
||||
Serial.println("MQTT connected.");
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
if (xQueueReceive(mqtt_queue, &ev, pdMS_TO_TICKS(100)))
|
||||
{
|
||||
char payload[128];
|
||||
|
||||
snprintf(payload, sizeof(payload),
|
||||
"{\"tagID\":\"%s\",\"rssi\":%.2f}",
|
||||
ev.uuid,
|
||||
ev.ema);
|
||||
|
||||
bool ok = mqtt.publish(MQTT_PUB_TOPIC.c_str(), payload);
|
||||
|
||||
if (!ok)
|
||||
Serial.println("Failed to publish on MQTT.");
|
||||
}
|
||||
else
|
||||
mqtt.loop();
|
||||
}
|
||||
}
|
||||
|
||||
void processEvent(const BleEvent &event)
|
||||
{
|
||||
xSemaphoreTake(uuid_mtx, portMAX_DELAY);
|
||||
|
||||
auto it = uuid_cache.find(event.uuid);
|
||||
|
||||
uint32_t now = millis();
|
||||
|
||||
if (it == uuid_cache.end() || (it->second.state == UUIDState::RETRY))
|
||||
{
|
||||
uuid_cache[event.uuid] = {UUIDState::PENDING, now};
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
|
||||
HttpEvent he;
|
||||
|
||||
strlcpy(he.uuid, event.uuid, sizeof(event.uuid));
|
||||
xQueueSend(http_queue, &he, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (it->second.state == UUIDState::INVALID &&
|
||||
now - it->second.last_check > config.interval.uuid_cache_retry_timeout)
|
||||
{
|
||||
it->second.state = UUIDState::PENDING;
|
||||
it->second.last_check = now;
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
|
||||
HttpEvent he;
|
||||
|
||||
strlcpy(he.uuid, event.uuid, sizeof(event.uuid));
|
||||
xQueueSend(http_queue, &he, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
UUIDState state = it->second.state;
|
||||
|
||||
xSemaphoreGive(uuid_mtx);
|
||||
|
||||
if (state != UUIDState::VALID)
|
||||
return;
|
||||
|
||||
Tag &tag = tags[event.uuid];
|
||||
|
||||
tag.addSample(event.rssi);
|
||||
|
||||
if (tag.shouldPublish())
|
||||
{
|
||||
NetEvent ne;
|
||||
|
||||
strlcpy(ne.uuid, event.uuid, sizeof(event.uuid));
|
||||
ne.ema = tag.getEMA();
|
||||
|
||||
if (!xQueueSend(mqtt_queue, &ne, 0))
|
||||
Serial.println("Failed to send MQTT data over MQTT Queue.");
|
||||
}
|
||||
}
|
||||
|
||||
void CB::onResult(const NimBLEAdvertisedDevice *d)
|
||||
{
|
||||
if (!d->haveServiceUUID() || !d->haveName())
|
||||
return;
|
||||
|
||||
NimBLEUUID uuid = d->getServiceUUID(0);
|
||||
const String name(d->getName().c_str());
|
||||
|
||||
if ((uuid.bitSize() != 128) || (name != config.tag_name))
|
||||
return;
|
||||
|
||||
BleEvent event;
|
||||
|
||||
strlcpy(event.uuid, uuid.toString().c_str(), sizeof(event.uuid));
|
||||
event.rssi = d->getRSSI();
|
||||
|
||||
xQueueSend(ble_queue, &event, pdMS_TO_TICKS(5));
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
{
|
||||
prefs.begin("config", false);
|
||||
|
||||
prefs.putUInt(CLEANUP_INTERVAL_CONFIG_KEY, config.interval.cleanup);
|
||||
prefs.putUInt(TAG_TIMEOUT_CONFIG_KEY, config.interval.tag_timeout);
|
||||
prefs.putUInt(UUID_CACHE_RETRY_TIMEOUT_CONFIG_KEY, config.interval.uuid_cache_retry_timeout);
|
||||
prefs.putUInt(UUID_CACHE_CLEAN_TIMEOUT_CONFIG_KEY, config.interval.uuid_cache_clean_timeout);
|
||||
|
||||
prefs.putString(API_URL_CONFIG_KEY, config.api.url);
|
||||
prefs.putString(UUID_CHECK_ENDPOINT_CONFIG_KEY, config.api.uuid_check_endpoint);
|
||||
|
||||
prefs.putString(MQTT_HOST_CONFIG_KEY, config.mqtt.host);
|
||||
prefs.putUShort(MQTT_PORT_CONFIG_KEY, config.mqtt.port);
|
||||
prefs.putString(MQTT_USER_CONFIG_KEY, config.mqtt.username);
|
||||
prefs.putString(MQTT_PASS_CONFIG_KEY, config.mqtt.password);
|
||||
prefs.putString(MQTT_TOPIC_CONFIG_KEY, config.mqtt.topic);
|
||||
|
||||
prefs.putString(WIFI_SSID_CONFIG_KEY, config.wifi.ssid);
|
||||
prefs.putString(WIFI_PASS_CONFIG_KEY, config.wifi.password);
|
||||
|
||||
prefs.putString(DEV_NAME_CONFIG_KEY, config.dev_name);
|
||||
prefs.putString(TAG_NAME_CONFIG_KEY, config.tag_name);
|
||||
|
||||
prefs.end();
|
||||
}
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
prefs.begin("config", true);
|
||||
|
||||
config.interval.cleanup = prefs.getUInt(CLEANUP_INTERVAL_CONFIG_KEY, CLEANUP_INTERVAL);
|
||||
config.interval.tag_timeout = prefs.getUInt(TAG_TIMEOUT_CONFIG_KEY, TAG_TIMEOUT);
|
||||
config.interval.uuid_cache_retry_timeout = prefs.getUInt(UUID_CACHE_RETRY_TIMEOUT_CONFIG_KEY, UUID_CACHE_RETRY_TIMEOUT);
|
||||
config.interval.uuid_cache_clean_timeout = prefs.getUInt(UUID_CACHE_CLEAN_TIMEOUT_CONFIG_KEY, UUID_CACHE_CLEAN_TIMEOUT);
|
||||
|
||||
config.api.url = prefs.getString(API_URL_CONFIG_KEY, API_URL);
|
||||
config.api.uuid_check_endpoint = prefs.getString(UUID_CHECK_ENDPOINT_CONFIG_KEY, UUID_CHECK_ENDPOINT);
|
||||
|
||||
config.mqtt.host = prefs.getString(MQTT_HOST_CONFIG_KEY, MQTT_HOST);
|
||||
config.mqtt.port = prefs.getUShort(MQTT_PORT_CONFIG_KEY, MQTT_PORT);
|
||||
config.mqtt.username = prefs.getString(MQTT_USER_CONFIG_KEY, MQTT_USER);
|
||||
config.mqtt.password = prefs.getString(MQTT_PASS_CONFIG_KEY, MQTT_PASS);
|
||||
config.mqtt.topic = prefs.getString(MQTT_TOPIC_CONFIG_KEY, MQTT_TOPIC);
|
||||
|
||||
config.wifi.ssid = prefs.getString(WIFI_SSID_CONFIG_KEY, WIFI_SSID);
|
||||
config.wifi.password = prefs.getString(WIFI_PASS_CONFIG_KEY, WIFI_PASS);
|
||||
|
||||
config.dev_name = prefs.getString(DEV_NAME_CONFIG_KEY, DEV_NAME);
|
||||
config.tag_name = prefs.getString(TAG_NAME_CONFIG_KEY, TAG_NAME);
|
||||
|
||||
prefs.end();
|
||||
}
|
||||
|
||||
void handleCommand(String line)
|
||||
{
|
||||
if (line.startsWith("set "))
|
||||
{
|
||||
int p1 = line.indexOf(' ', 4);
|
||||
String key = line.substring(4, p1);
|
||||
String value = line.substring(p1 + 1);
|
||||
|
||||
if (key.length() == 0 || value.length() == 0)
|
||||
return;
|
||||
|
||||
if (key == CLEANUP_INTERVAL_CONFIG_KEY)
|
||||
config.interval.cleanup = (uint32_t)value.toInt();
|
||||
else if (key == TAG_TIMEOUT_CONFIG_KEY)
|
||||
config.interval.tag_timeout = (uint32_t)value.toInt();
|
||||
else if (key == UUID_CACHE_RETRY_TIMEOUT_CONFIG_KEY)
|
||||
config.interval.uuid_cache_retry_timeout = (uint32_t)value.toInt();
|
||||
else if (key == UUID_CACHE_CLEAN_TIMEOUT_CONFIG_KEY)
|
||||
config.interval.uuid_cache_clean_timeout = (uint32_t)value.toInt();
|
||||
else if (key == API_URL_CONFIG_KEY)
|
||||
config.api.url = value;
|
||||
else if (key == UUID_CHECK_ENDPOINT_CONFIG_KEY)
|
||||
config.api.uuid_check_endpoint = value;
|
||||
else if (key == MQTT_HOST_CONFIG_KEY)
|
||||
config.mqtt.host = value;
|
||||
else if (key == MQTT_PORT_CONFIG_KEY)
|
||||
config.mqtt.port = value.toInt();
|
||||
else if (key == MQTT_USER_CONFIG_KEY)
|
||||
config.mqtt.username = value;
|
||||
else if (key == MQTT_PASS_CONFIG_KEY)
|
||||
config.mqtt.password = value;
|
||||
else if (key == MQTT_TOPIC_CONFIG_KEY)
|
||||
config.mqtt.topic = value;
|
||||
else if (key == WIFI_SSID_CONFIG_KEY)
|
||||
config.wifi.ssid = value;
|
||||
else if (key == WIFI_PASS_CONFIG_KEY)
|
||||
config.wifi.password = value;
|
||||
else if (key == DEV_NAME_CONFIG_KEY)
|
||||
config.dev_name = value;
|
||||
else if (key == TAG_NAME_CONFIG_KEY)
|
||||
config.tag_name = value;
|
||||
}
|
||||
else if (line == "save")
|
||||
saveConfig();
|
||||
else if (line.startsWith("show "))
|
||||
{
|
||||
String key = line.substring(5);
|
||||
|
||||
if (key.length() == 0)
|
||||
return;
|
||||
|
||||
if (key == CLEANUP_INTERVAL_CONFIG_KEY)
|
||||
Serial.println(config.interval.cleanup);
|
||||
else if (key == TAG_TIMEOUT_CONFIG_KEY)
|
||||
Serial.println(config.interval.tag_timeout);
|
||||
else if (key == UUID_CACHE_RETRY_TIMEOUT_CONFIG_KEY)
|
||||
Serial.println(config.interval.uuid_cache_retry_timeout);
|
||||
else if (key == UUID_CACHE_CLEAN_TIMEOUT_CONFIG_KEY)
|
||||
Serial.println(config.interval.uuid_cache_clean_timeout);
|
||||
else if (key == API_URL_CONFIG_KEY)
|
||||
Serial.println(config.api.url);
|
||||
else if (key == UUID_CHECK_ENDPOINT_CONFIG_KEY)
|
||||
Serial.println(config.api.uuid_check_endpoint);
|
||||
else if (key == MQTT_HOST_CONFIG_KEY)
|
||||
Serial.println(config.mqtt.host);
|
||||
else if (key == MQTT_PORT_CONFIG_KEY)
|
||||
Serial.println(config.mqtt.port);
|
||||
else if (key == MQTT_USER_CONFIG_KEY)
|
||||
Serial.println(config.mqtt.username);
|
||||
else if (key == MQTT_PASS_CONFIG_KEY)
|
||||
Serial.println(config.mqtt.password);
|
||||
else if (key == MQTT_TOPIC_CONFIG_KEY)
|
||||
Serial.println(config.mqtt.topic);
|
||||
else if (key == WIFI_SSID_CONFIG_KEY)
|
||||
Serial.println(config.wifi.ssid);
|
||||
else if (key == WIFI_PASS_CONFIG_KEY)
|
||||
Serial.println(config.wifi.password);
|
||||
else if (key == DEV_NAME_CONFIG_KEY)
|
||||
Serial.println(config.dev_name);
|
||||
else if (key == TAG_NAME_CONFIG_KEY)
|
||||
Serial.println(config.tag_name);
|
||||
}
|
||||
else if (line == "reset")
|
||||
{
|
||||
prefs.begin("config", false);
|
||||
prefs.clear();
|
||||
prefs.end();
|
||||
|
||||
esp_restart();
|
||||
}
|
||||
else if (line == "reboot")
|
||||
{
|
||||
Serial.println("Rebooting...");
|
||||
Serial.flush();
|
||||
esp_restart();
|
||||
}
|
||||
else if (line == "help")
|
||||
{
|
||||
Serial.println("Commands:");
|
||||
Serial.println(" set <key> <value> - Set a configuration value");
|
||||
Serial.println(" show <key> - Show a configuration value");
|
||||
Serial.println(" save - Save configuration to non-volatile storage");
|
||||
Serial.println(" reset - Reset configuration to defaults");
|
||||
Serial.println(" reboot - Reboot the device");
|
||||
Serial.println(" help - Show this help message");
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
loadConfig();
|
||||
|
||||
WiFi.begin(config.wifi.ssid.c_str(), config.wifi.password.c_str());
|
||||
|
||||
MQTT_PUB_TOPIC = config.mqtt.topic + config.dev_name;
|
||||
|
||||
mqtt.setServer(config.mqtt.host.c_str(), config.mqtt.port);
|
||||
mqtt.setKeepAlive(60);
|
||||
mqtt.setSocketTimeout(5);
|
||||
|
||||
ble_queue = xQueueCreate(100, sizeof(BleEvent));
|
||||
mqtt_queue = xQueueCreate(100, sizeof(NetEvent));
|
||||
http_queue = xQueueCreate(100, sizeof(HttpEvent));
|
||||
|
||||
uuid_mtx = xSemaphoreCreateMutex();
|
||||
|
||||
xTaskCreatePinnedToCore(mqttTask, "mqtt", 4096, NULL, 1, NULL, 1);
|
||||
xTaskCreatePinnedToCore(httpTask, "http", 4096, NULL, 1, NULL, 1);
|
||||
|
||||
NimBLEDevice::init(config.dev_name.c_str());
|
||||
|
||||
NimBLEScan *scan = NimBLEDevice::getScan();
|
||||
|
||||
scan->setScanCallbacks(&cb);
|
||||
scan->setActiveScan(false);
|
||||
scan->setDuplicateFilter(false);
|
||||
|
||||
scan->start(0, false, true);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
BleEvent ev;
|
||||
|
||||
while (xQueueReceive(ble_queue, &ev, 0))
|
||||
processEvent(ev);
|
||||
|
||||
static uint32_t last_cleanup = 0;
|
||||
|
||||
if (millis() - last_cleanup >= config.interval.cleanup)
|
||||
{
|
||||
cleanupTags();
|
||||
cleanupCache();
|
||||
last_cleanup = millis();
|
||||
}
|
||||
|
||||
while (Serial.available())
|
||||
{
|
||||
char c = Serial.read();
|
||||
|
||||
if (c == '\r')
|
||||
continue;
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
Serial.print(c);
|
||||
serial_buffer.trim();
|
||||
|
||||
if (serial_buffer.length() > 0)
|
||||
handleCommand(serial_buffer);
|
||||
|
||||
serial_buffer = "";
|
||||
|
||||
Serial.printf("%s> ", config.dev_name.c_str());
|
||||
}
|
||||
else if (c == '\b')
|
||||
{
|
||||
if (serial_buffer.length() > 0)
|
||||
{
|
||||
serial_buffer.remove(serial_buffer.length() - 1);
|
||||
|
||||
Serial.print("\b \b");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(c);
|
||||
serial_buffer += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef MAIN_HPP
|
||||
#define MAIN_HPP
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <NimBLEDevice.h>
|
||||
#include <string>
|
||||
|
||||
#define CLEANUP_INTERVAL_CONFIG_KEY "int.cln"
|
||||
#define TAG_TIMEOUT_CONFIG_KEY "int.tagto"
|
||||
#define UUID_CACHE_RETRY_TIMEOUT_CONFIG_KEY "int.urtry"
|
||||
#define UUID_CACHE_CLEAN_TIMEOUT_CONFIG_KEY "int.ucclean"
|
||||
#define API_URL_CONFIG_KEY "api.url"
|
||||
#define UUID_CHECK_ENDPOINT_CONFIG_KEY "api.uuidchk"
|
||||
#define MQTT_HOST_CONFIG_KEY "mq.host"
|
||||
#define MQTT_PORT_CONFIG_KEY "mq.port"
|
||||
#define MQTT_USER_CONFIG_KEY "mq.user"
|
||||
#define MQTT_PASS_CONFIG_KEY "mq.pass"
|
||||
#define MQTT_TOPIC_CONFIG_KEY "mq.topic"
|
||||
#define WIFI_SSID_CONFIG_KEY "wf.ssid"
|
||||
#define WIFI_PASS_CONFIG_KEY "wf.pass"
|
||||
#define DEV_NAME_CONFIG_KEY "dev.name"
|
||||
#define TAG_NAME_CONFIG_KEY "tag.name"
|
||||
|
||||
#define CLEANUP_INTERVAL 5 * 1000 // ms
|
||||
#define TAG_TIMEOUT 60 * 1000 // ms
|
||||
#define UUID_CACHE_RETRY_TIMEOUT 5 * 60 * 1000 // ms
|
||||
#define UUID_CACHE_CLEAN_TIMEOUT 60 * 60 * 1000 // ms
|
||||
#define API_URL "http://192.168.1.2"
|
||||
#define UUID_CHECK_ENDPOINT "/check"
|
||||
#define TAG_NAME "X6TAG"
|
||||
#define MQTT_HOST "192.168.1.2"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "user"
|
||||
#define MQTT_PASS "pass"
|
||||
#define MQTT_TOPIC "scanners/"
|
||||
#define WIFI_SSID "COSMOTE-489882"
|
||||
#define WIFI_PASS "x32hbh54673ngccdsfa9"
|
||||
#define DEV_NAME "572b29cb-6a93-480e-adf0-c5c44e9a58df"
|
||||
|
||||
struct Config
|
||||
{
|
||||
struct Interval
|
||||
{
|
||||
uint32_t cleanup;
|
||||
uint32_t tag_timeout;
|
||||
uint32_t uuid_cache_retry_timeout;
|
||||
uint32_t uuid_cache_clean_timeout;
|
||||
} interval;
|
||||
|
||||
struct API
|
||||
{
|
||||
String url;
|
||||
String uuid_check_endpoint;
|
||||
} api;
|
||||
|
||||
struct MQTT
|
||||
{
|
||||
String host;
|
||||
uint16_t port;
|
||||
String username;
|
||||
String password;
|
||||
String topic;
|
||||
} mqtt;
|
||||
|
||||
struct WiFi
|
||||
{
|
||||
String ssid;
|
||||
String password;
|
||||
} wifi;
|
||||
|
||||
String dev_name;
|
||||
String tag_name;
|
||||
};
|
||||
|
||||
struct BleEvent
|
||||
{
|
||||
char uuid[37];
|
||||
int8_t rssi;
|
||||
};
|
||||
|
||||
struct NetEvent
|
||||
{
|
||||
char uuid[37];
|
||||
float ema;
|
||||
};
|
||||
|
||||
struct HttpEvent
|
||||
{
|
||||
char uuid[37];
|
||||
};
|
||||
|
||||
enum class UUIDState
|
||||
{
|
||||
PENDING,
|
||||
RETRY,
|
||||
VALID,
|
||||
INVALID
|
||||
};
|
||||
|
||||
struct UUIDCacheEntry
|
||||
{
|
||||
UUIDState state;
|
||||
uint32_t last_check;
|
||||
};
|
||||
|
||||
class CB : public NimBLEScanCallbacks
|
||||
{
|
||||
public:
|
||||
void onResult(const NimBLEAdvertisedDevice *d) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,177 @@
|
||||
#include <iostream>
|
||||
#include "Tag.hpp"
|
||||
|
||||
uint32_t Tag::getLastSeen() const { return this->last_seen; }
|
||||
|
||||
void Tag::addSample(int8_t rssi)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: rssi_window[%d] = %d\n", this->win_idx, rssi);
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
this->rssi_window[this->win_idx] = rssi;
|
||||
this->win_idx = (this->win_idx + 1) % 5;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: count = %d", this->count);
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
if (this->count < 5)
|
||||
{
|
||||
this->count++;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf(" -> %d", this->count);
|
||||
Serial.flush();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println();
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
int8_t median = getMedian();
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: median = %d\n", median);
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
// EMA update
|
||||
if (!this->init)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: init = false\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
this->ema = median;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: ema = %f\n", this->ema);
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
this->init = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->ema = 0.8f * this->ema + 0.2f * median;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[addSample()]: ema = %f\n", this->ema);
|
||||
Serial.flush();
|
||||
#endif
|
||||
}
|
||||
|
||||
last_seen = millis();
|
||||
}
|
||||
|
||||
int8_t Tag::getMedian() const
|
||||
{
|
||||
if (this->count == 0)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[getMedian()]: count = 0\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int8_t sorted[5];
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[getMedian()]: rssi_window[] = {");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
for (uint8_t i = 0; i < this->count; i++)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf(" %d", rssi_window[i]);
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
sorted[i] = this->rssi_window[i];
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf(" }\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
std::sort(sorted, sorted + this->count);
|
||||
|
||||
return sorted[this->count / 2];
|
||||
}
|
||||
|
||||
float Tag::getEMA() const { return this->ema; }
|
||||
|
||||
bool Tag::isReady() const { return count == 5; }
|
||||
|
||||
bool Tag::shouldPublish()
|
||||
{
|
||||
if (!this->isReady())
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[shouldPublish()]: isReady = false\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t now = millis();
|
||||
|
||||
uint32_t time_delta = (this->prev_time == 0 ? INTERVAL : now - this->prev_time);
|
||||
float ema_delta = std::abs(this->ema - this->prev_ema);
|
||||
|
||||
bool time_trigger = time_delta >= INTERVAL;
|
||||
bool ema_trigger = ema_delta >= EMA_THRESHOLD;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[shouldPublish()]: time_delta = %u\n"
|
||||
"[shouldPublish()]: ema_delta = %f\n"
|
||||
"[shouldPublish()]: time_trigger = %s\n"
|
||||
"[shouldPublish()]: ema_trigger = %s\n",
|
||||
time_delta,
|
||||
ema_delta,
|
||||
(time_trigger ? "true" : "false"),
|
||||
(ema_trigger ? "true" : "false"));
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
if (time_trigger || ema_trigger)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[shouldPublish()]: return true\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
this->prev_time = now;
|
||||
this->prev_ema = this->ema;
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.printf("[shouldPublish()]: return false\n");
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Tag::reset()
|
||||
{
|
||||
this->win_idx = 0;
|
||||
this->count = 0;
|
||||
this->ema = 0;
|
||||
this->init = false;
|
||||
|
||||
for (uint8_t i = 0; i < 5; i++)
|
||||
this->rssi_window[i] = 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef TAG_HPP
|
||||
#define TAG_HPP
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/*
|
||||
* EMA_THRESHOLD Options:
|
||||
* 1 - High Sensitivity / Fine Tracking
|
||||
* 2 - Static Object / Presence Detection
|
||||
* 3 - General Tracking (Default)
|
||||
* 4-5 - Noisy Environment / Interference
|
||||
*/
|
||||
#define EMA_THRESHOLD 3
|
||||
#define INTERVAL 5000 // ms
|
||||
#define WINDOW_SIZE 5
|
||||
// #define DEBUG 1
|
||||
|
||||
class Tag
|
||||
{
|
||||
private:
|
||||
int8_t rssi_window[WINDOW_SIZE];
|
||||
uint8_t win_idx = 0;
|
||||
uint8_t count = 0;
|
||||
uint32_t prev_time = 0;
|
||||
uint32_t last_seen = 0;
|
||||
float ema = 0;
|
||||
float prev_ema = 0;
|
||||
bool init = false;
|
||||
|
||||
public:
|
||||
Tag() = default;
|
||||
uint32_t getLastSeen() const;
|
||||
void addSample(int8_t rssi);
|
||||
int8_t getMedian() const;
|
||||
float getEMA() const;
|
||||
bool isReady() const;
|
||||
bool shouldPublish();
|
||||
void reset();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Test Runner and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
|
||||
+249
-22
@@ -1,21 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
|
||||
#include "host/ble_hs.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "driver/usb_serial_jtag.h"
|
||||
#include "main.h"
|
||||
|
||||
static const char *TAG = "BLE";
|
||||
|
||||
static const ble_uuid128_t service_uuid =
|
||||
BLE_UUID128_INIT(
|
||||
0xF0, 0xDE, 0xBC, 0x9A,
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
0xF0, 0xDE, 0xBC, 0x9A,
|
||||
0x78, 0x56, 0x34, 0x12);
|
||||
static config_t config;
|
||||
|
||||
static void start_adv(void)
|
||||
{
|
||||
@@ -24,19 +16,18 @@ static void start_adv(void)
|
||||
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
|
||||
fields.uuids128 = (ble_uuid128_t *)&service_uuid;
|
||||
fields.name = (uint8_t *)config.name;
|
||||
fields.name_len = strlen(config.name);
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
fields.uuids128 = (ble_uuid128_t *)&config.uuid;
|
||||
fields.num_uuids128 = 1;
|
||||
fields.uuids128_is_complete = 1;
|
||||
|
||||
uint8_t mfg_data[5] = {0x34, 0x34, 0x34, 0x34, 0x34};
|
||||
|
||||
fields.mfg_data = mfg_data;
|
||||
fields.mfg_data_len = sizeof(mfg_data);
|
||||
|
||||
int rc = ble_gap_adv_set_fields(&fields);
|
||||
if (rc != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "adv set fields failed: %d", rc);
|
||||
ESP_LOGE(config.name, "adv set fields failed: %d", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,17 +49,17 @@ static void start_adv(void)
|
||||
|
||||
if (rc != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "adv start failed: %d", rc);
|
||||
ESP_LOGE(config.name, "adv start failed: %d", rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "advertising started");
|
||||
ESP_LOGI(config.name, "advertising started");
|
||||
}
|
||||
}
|
||||
|
||||
static void on_sync(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "BLE synced");
|
||||
ESP_LOGI(config.name, "BLE synced");
|
||||
|
||||
start_adv();
|
||||
}
|
||||
@@ -78,15 +69,251 @@ static void host_task(void *param)
|
||||
nimble_port_run();
|
||||
}
|
||||
|
||||
static bool uuid_from_string(const char *str, ble_uuid128_t *uuid)
|
||||
{
|
||||
unsigned int b[16];
|
||||
|
||||
int rc = sscanf(str,
|
||||
"%02x%02x%02x%02x-"
|
||||
"%02x%02x-"
|
||||
"%02x%02x-"
|
||||
"%02x%02x-"
|
||||
"%02x%02x%02x%02x%02x%02x",
|
||||
|
||||
&b[15], &b[14], &b[13], &b[12],
|
||||
&b[11], &b[10],
|
||||
&b[9], &b[8],
|
||||
&b[7], &b[6],
|
||||
&b[5], &b[4], &b[3],
|
||||
&b[2], &b[1], &b[0]);
|
||||
|
||||
if (rc != 16)
|
||||
return false;
|
||||
|
||||
uuid->u.type = BLE_UUID_TYPE_128;
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
uuid->value[i] = (uint8_t)b[i];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static esp_err_t reset_config()
|
||||
{
|
||||
nvs_handle_t nvs;
|
||||
|
||||
esp_err_t err = nvs_open("config", NVS_READWRITE, &nvs);
|
||||
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
|
||||
err = nvs_erase_all(nvs);
|
||||
|
||||
if (err != ESP_OK)
|
||||
goto exit;
|
||||
|
||||
err = nvs_commit(nvs);
|
||||
|
||||
exit:
|
||||
nvs_close(nvs);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t save_config()
|
||||
{
|
||||
nvs_handle_t nvs;
|
||||
|
||||
esp_err_t err = nvs_open("config", NVS_READWRITE, &nvs);
|
||||
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
|
||||
err = nvs_set_str(nvs, "uuid", config.uuid_str);
|
||||
|
||||
if (err != ESP_OK)
|
||||
goto exit;
|
||||
|
||||
err = nvs_set_str(nvs, "name", config.name);
|
||||
|
||||
if (err != ESP_OK)
|
||||
goto exit;
|
||||
|
||||
err = nvs_set_u32(nvs, "adv_int", config.adv_interval);
|
||||
|
||||
if (err != ESP_OK)
|
||||
goto exit;
|
||||
|
||||
err = nvs_commit(nvs);
|
||||
|
||||
exit:
|
||||
nvs_close(nvs);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t load_config()
|
||||
{
|
||||
nvs_handle_t nvs;
|
||||
|
||||
esp_err_t err = nvs_open("config", NVS_READONLY, &nvs);
|
||||
|
||||
strcpy(config.uuid_str, DEFAULT_UUID);
|
||||
strcpy(config.name, DEFAULT_NAME);
|
||||
config.adv_interval = DEFAULT_ADV_INTERVAL;
|
||||
|
||||
uuid_from_string(config.uuid_str, &config.uuid);
|
||||
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
|
||||
size_t len;
|
||||
|
||||
len = sizeof(config.uuid_str);
|
||||
|
||||
nvs_get_str(nvs, "uuid", config.uuid_str, &len);
|
||||
|
||||
len = sizeof(config.name);
|
||||
|
||||
nvs_get_str(nvs, "name", config.name, &len);
|
||||
|
||||
nvs_get_u32(nvs, "adv_int", &config.adv_interval);
|
||||
|
||||
nvs_close(nvs);
|
||||
|
||||
uuid_from_string(config.uuid_str, &config.uuid);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void serial_task(void *arg)
|
||||
{
|
||||
char line[128];
|
||||
char buf[64];
|
||||
int pos = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
uint8_t ch;
|
||||
|
||||
int len = usb_serial_jtag_read_bytes(&ch, 1, portMAX_DELAY);
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
if (ch == '\n' || ch == '\r')
|
||||
{
|
||||
usb_serial_jtag_write_bytes("\n", 1, 20 / portTICK_PERIOD_MS);
|
||||
line[pos] = '\0';
|
||||
|
||||
if (pos > 0)
|
||||
{
|
||||
if (strncmp(line, "set uuid ", 9) == 0)
|
||||
{
|
||||
if (uuid_from_string(line + 9, &config.uuid))
|
||||
strcpy(config.uuid_str, line + 9);
|
||||
}
|
||||
else if (strncmp(line, "set name ", 9) == 0)
|
||||
{
|
||||
strncpy(config.name, line + 9, sizeof(config.name) - 1);
|
||||
config.name[sizeof(config.name) - 1] = '\0';
|
||||
}
|
||||
else if (strncmp(line, "set adv_interval ", 17) == 0)
|
||||
{
|
||||
uint32_t interval = atoi(line + 17);
|
||||
if (interval > 0)
|
||||
config.adv_interval = interval;
|
||||
}
|
||||
else if (strcmp(line, "show uuid") == 0)
|
||||
{
|
||||
usb_serial_jtag_write_bytes(config.uuid_str, strlen(config.uuid_str), 20 / portTICK_PERIOD_MS);
|
||||
usb_serial_jtag_write_bytes("\n", 1, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
else if (strcmp(line, "show name") == 0)
|
||||
{
|
||||
usb_serial_jtag_write_bytes(config.name, strlen(config.name), 20 / portTICK_PERIOD_MS);
|
||||
usb_serial_jtag_write_bytes("\n", 1, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
else if (strcmp(line, "show adv_interval") == 0)
|
||||
{
|
||||
int len = snprintf(buf, sizeof(buf), "%lu\n", (unsigned long)config.adv_interval);
|
||||
usb_serial_jtag_write_bytes(buf, len, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
else if (strcmp(line, "save") == 0)
|
||||
save_config();
|
||||
else if (strcmp(line, "reset") == 0)
|
||||
{
|
||||
reset_config();
|
||||
esp_restart();
|
||||
}
|
||||
else if (strcmp(line, "reboot") == 0)
|
||||
{
|
||||
usb_serial_jtag_write_bytes("Rebooting...\n", 13, 20 / portTICK_PERIOD_MS);
|
||||
esp_restart();
|
||||
}
|
||||
else if (strcmp(line, "help") == 0)
|
||||
{
|
||||
usb_serial_jtag_write_bytes("Commands:\n"
|
||||
" set uuid <uuid>\n"
|
||||
" set name <name>\n"
|
||||
" set adv_interval <ms>\n"
|
||||
" show uuid\n"
|
||||
" show name\n"
|
||||
" show adv_interval\n"
|
||||
" save\n"
|
||||
" reset\n"
|
||||
" reboot\n"
|
||||
" help\n",
|
||||
145, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
int len = snprintf(buf, sizeof(buf), "%s> ", config.name);
|
||||
usb_serial_jtag_write_bytes(buf, len, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
else if (ch == 0x08)
|
||||
{
|
||||
if (pos > 0)
|
||||
{
|
||||
pos--;
|
||||
usb_serial_jtag_write_bytes("\b \b", 3, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pos < sizeof(line) - 1)
|
||||
{
|
||||
line[pos++] = ch;
|
||||
usb_serial_jtag_write_bytes((char *)&ch, 1, 20 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
usb_serial_jtag_driver_config_t cfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
|
||||
usb_serial_jtag_driver_install(&cfg);
|
||||
|
||||
load_config();
|
||||
|
||||
nimble_port_init();
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gap_device_name_set("XIAO_C6");
|
||||
ble_svc_gap_device_name_set("X6TAG");
|
||||
|
||||
ble_hs_cfg.sync_cb = on_sync;
|
||||
|
||||
xTaskCreate(
|
||||
serial_task,
|
||||
"serial_task",
|
||||
4096,
|
||||
NULL,
|
||||
5,
|
||||
NULL);
|
||||
|
||||
nimble_port_freertos_init(host_task);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include "host/ble_hs.h"
|
||||
|
||||
#define DEFAULT_UUID "12345678-9abc-def0-1234-56789abcdef0"
|
||||
#define DEFAULT_NAME "X6TAG"
|
||||
#define DEFAULT_ADV_INTERVAL 0x80
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ble_uuid128_t uuid;
|
||||
char uuid_str[37];
|
||||
char name[32];
|
||||
uint32_t adv_interval;
|
||||
} config_t;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
# dlib compile cache
|
||||
.docker-cache/
|
||||
|
||||
# face recognition models
|
||||
models/
|
||||
|
||||
# executable
|
||||
output/
|
||||
@@ -0,0 +1,55 @@
|
||||
ARG BUILDPLATFORM=linux/amd64
|
||||
FROM --platform=$BUILDPLATFORM golang:1.26-bookworm AS builder
|
||||
|
||||
# Install dlib deps
|
||||
RUN dpkg --add-architecture arm64 && \
|
||||
apt-get update && apt-get install -y \
|
||||
g++-aarch64-linux-gnu \
|
||||
libdlib-dev:arm64 \
|
||||
libjpeg-dev:arm64 \
|
||||
libblas-dev:arm64 \
|
||||
libopenblas-dev:arm64 \
|
||||
libatlas-base-dev:arm64 \
|
||||
liblapack-dev:arm64 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Compiler wrapper for arm64
|
||||
RUN printf '#!/bin/bash\nexec aarch64-linux-gnu-gcc "${@//-march=native/-march=armv8-a}"\n' \
|
||||
> /usr/local/bin/arm64-gcc && \
|
||||
printf '#!/bin/bash\nexec aarch64-linux-gnu-g++ "${@//-march=native/-march=armv8-a}"\n' \
|
||||
> /usr/local/bin/arm64-g++ && \
|
||||
chmod +x /usr/local/bin/arm64-gcc /usr/local/bin/arm64-g++
|
||||
|
||||
# Compiler variables for cross-compile to arm64
|
||||
ENV CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOARCH=arm64 \
|
||||
CC=/usr/local/bin/arm64-gcc \
|
||||
CXX=/usr/local/bin/arm64-g++ \
|
||||
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# Pre-compile all CGO deps into cache
|
||||
RUN go build -v ./... 2>/dev/null || \
|
||||
go list -m all && \
|
||||
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 \
|
||||
go build -gcflags="-trimpath" -v \
|
||||
$(go list -f '{{if .CgoFiles}}{{.ImportPath}}{{end}}' ./...) 2>/dev/null; exit 0
|
||||
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
--mount=type=cache,target=/go/pkg \
|
||||
go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Build - reuse CGO cache
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
--mount=type=cache,target=/go/pkg \
|
||||
go build -o cameraSession
|
||||
|
||||
FROM scratch AS export
|
||||
COPY --from=builder /app/cameraSession /cameraSession
|
||||
@@ -0,0 +1,10 @@
|
||||
all:
|
||||
docker buildx build --platform linux/amd64 --cache-from type=local,src=./.docker-cache --cache-to type=local,dest=./.docker-cache --target export --output type=local,dest=./output .
|
||||
|
||||
models:
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/shape_predictor_5_face_landmarks.dat
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/dlib_face_recognition_resnet_model_v1.dat
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/mmod_human_face_detector.dat
|
||||
|
||||
clean:
|
||||
rm -r output/
|
||||
@@ -0,0 +1,163 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/Kagami/go-face"
|
||||
"github.com/blackjack/webcam"
|
||||
)
|
||||
|
||||
type InputPacket struct {
|
||||
UUID string `json:"uuid"`
|
||||
Descriptor face.Descriptor `json:"descriptor"`
|
||||
}
|
||||
|
||||
type ResultPacket struct {
|
||||
UUID string `json:"uuid"`
|
||||
Verified bool `json:"verified"`
|
||||
}
|
||||
|
||||
var (
|
||||
rec *face.Recognizer
|
||||
mtx sync.Mutex
|
||||
results = make([]ResultPacket, 0)
|
||||
)
|
||||
|
||||
func captureCameraFrame() ([]byte, error) {
|
||||
cam, err := webcam.Open("/dev/video0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cam.Close()
|
||||
|
||||
format := webcam.PixelFormat(uint32(0x47504a4d)) // MJPG
|
||||
|
||||
_, _, _, err = cam.SetImageFormat(format, 1280, 720)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("camera format error: %v", err)
|
||||
}
|
||||
|
||||
err = cam.StartStreaming()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cam.StopStreaming()
|
||||
|
||||
for {
|
||||
err = cam.WaitForFrame(5)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
frame, err := cam.ReadFrame()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(frame) != 0 {
|
||||
result := make([]byte, len(frame))
|
||||
copy(result, frame)
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verfication thread
|
||||
func verifyFace(packet InputPacket, descriptors []face.Descriptor, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
const threshold = 0.35
|
||||
|
||||
verified := false
|
||||
|
||||
for _, descriptor := range descriptors {
|
||||
distance := face.SquaredEuclideanDistance(packet.Descriptor, descriptor)
|
||||
|
||||
if distance < threshold {
|
||||
verified = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
mtx.Lock()
|
||||
results = append(results, ResultPacket{
|
||||
UUID: packet.UUID,
|
||||
Verified: verified,
|
||||
})
|
||||
mtx.Unlock()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
|
||||
// initialize recognizer
|
||||
rec, err = face.NewRecognizer("./models")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialize recognizer: %v", err)
|
||||
}
|
||||
defer rec.Close()
|
||||
|
||||
// capture image
|
||||
cameraImage, err := captureCameraFrame()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read camera: %v", err)
|
||||
}
|
||||
|
||||
// recognize faces from camera input
|
||||
faces, err := rec.RecognizeCNN(cameraImage)
|
||||
if err != nil {
|
||||
log.Fatalf("recognition error: %v", err)
|
||||
}
|
||||
|
||||
if len(faces) == 0 {
|
||||
fmt.Println("no face found.")
|
||||
return
|
||||
}
|
||||
|
||||
var descriptors []face.Descriptor
|
||||
|
||||
// extract descriptors to array
|
||||
for _, f := range faces {
|
||||
descriptors = append(descriptors, f.Descriptor)
|
||||
}
|
||||
|
||||
// read stdin
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
|
||||
var packet InputPacket
|
||||
|
||||
err := json.Unmarshal(line, &packet)
|
||||
if err != nil {
|
||||
log.Printf("invalid json packet: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
go verifyFace(packet, descriptors, &wg)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("stdin error: %v", err)
|
||||
}
|
||||
|
||||
// wait threads to finish
|
||||
wg.Wait()
|
||||
|
||||
// print results to stdout
|
||||
output, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to marshal results: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
module OfficeSense/cameraSession
|
||||
|
||||
go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e
|
||||
github.com/blackjack/webcam v0.6.1
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.14.0 // indirect
|
||||
@@ -0,0 +1,6 @@
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e h1:lqIUFzxaqyYqUn4MhzAvSAh4wIte/iLNcIEWxpT/qbc=
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e/go.mod h1:9wdDJkRgo3SGTcFwbQ7elVIQhIr2bbBjecuY7VoqmPU=
|
||||
github.com/blackjack/webcam v0.6.1 h1:K0T6Q0zto23U99gNAa5q/hFoye6uGcKr2aE6hFoxVoE=
|
||||
github.com/blackjack/webcam v0.6.1/go.mod h1:zs+RkUZzqpFPHPiwBZ6U5B34ZXXe9i+SiHLKnnukJuI=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
@@ -0,0 +1,55 @@
|
||||
ARG BUILDPLATFORM=linux/amd64
|
||||
FROM --platform=$BUILDPLATFORM golang:1.26-bookworm AS builder
|
||||
|
||||
# Install dlib deps
|
||||
RUN dpkg --add-architecture arm64 && \
|
||||
apt-get update && apt-get install -y \
|
||||
g++-aarch64-linux-gnu \
|
||||
libdlib-dev:arm64 \
|
||||
libjpeg-dev:arm64 \
|
||||
libblas-dev:arm64 \
|
||||
libopenblas-dev:arm64 \
|
||||
libatlas-base-dev:arm64 \
|
||||
liblapack-dev:arm64 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Compiler wrapper for arm64
|
||||
RUN printf '#!/bin/bash\nexec aarch64-linux-gnu-gcc "${@//-march=native/-march=armv8-a}"\n' \
|
||||
> /usr/local/bin/arm64-gcc && \
|
||||
printf '#!/bin/bash\nexec aarch64-linux-gnu-g++ "${@//-march=native/-march=armv8-a}"\n' \
|
||||
> /usr/local/bin/arm64-g++ && \
|
||||
chmod +x /usr/local/bin/arm64-gcc /usr/local/bin/arm64-g++
|
||||
|
||||
# Compiler variables for cross-compile to arm64
|
||||
ENV CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOARCH=arm64 \
|
||||
CC=/usr/local/bin/arm64-gcc \
|
||||
CXX=/usr/local/bin/arm64-g++ \
|
||||
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# Pre-compile all CGO deps into cache
|
||||
RUN go build -v ./... 2>/dev/null || \
|
||||
go list -m all && \
|
||||
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 \
|
||||
go build -gcflags="-trimpath" -v \
|
||||
$(go list -f '{{if .CgoFiles}}{{.ImportPath}}{{end}}' ./...) 2>/dev/null; exit 0
|
||||
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
--mount=type=cache,target=/go/pkg \
|
||||
go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Build - reuse CGO cache
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
--mount=type=cache,target=/go/pkg \
|
||||
go build -o extractEmbeddings
|
||||
|
||||
FROM scratch AS export
|
||||
COPY --from=builder /app/extractEmbeddings /extractEmbeddings
|
||||
@@ -0,0 +1,10 @@
|
||||
all:
|
||||
docker buildx build --platform linux/amd64 --cache-from type=local,src=./.docker-cache --cache-to type=local,dest=./.docker-cache --target export --output type=local,dest=./output .
|
||||
|
||||
models:
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/shape_predictor_5_face_landmarks.dat
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/dlib_face_recognition_resnet_model_v1.dat
|
||||
wget https://github.com/Kagami/go-face-testdata/raw/master/models/mmod_human_face_detector.dat
|
||||
|
||||
clean:
|
||||
rm -r output/
|
||||
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
|
||||
"os"
|
||||
|
||||
"github.com/Kagami/go-face"
|
||||
"golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
Success bool `json:"success"`
|
||||
Descriptor face.Descriptor `json:"descriptor"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
var res Result
|
||||
|
||||
func atExit() {
|
||||
resString, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(string(resString))
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func resizeIfNeeded(img image.Image, max int) image.Image {
|
||||
b := img.Bounds()
|
||||
w := b.Dx()
|
||||
h := b.Dy()
|
||||
|
||||
if w <= max && h <= max {
|
||||
return img
|
||||
}
|
||||
|
||||
var scale float64
|
||||
if w > h {
|
||||
scale = float64(max) / float64(w)
|
||||
} else {
|
||||
scale = float64(max) / float64(h)
|
||||
}
|
||||
|
||||
newW := int(float64(w) * scale)
|
||||
newH := int(float64(h) * scale)
|
||||
|
||||
dst := image.NewRGBA(image.Rect(0, 0, newW, newH))
|
||||
draw.BiLinear.Scale(dst, dst.Bounds(), img, b, draw.Over, nil)
|
||||
|
||||
return dst
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer atExit()
|
||||
|
||||
// Read input image from stdin in Base64 encoding
|
||||
input, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[STDIN]: " + err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
// Decode Base64
|
||||
image, err := base64.StdEncoding.DecodeString(string(input))
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[BASE64]: " + err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
imageJPEG, err := jpeg.Decode(bytes.NewReader(image))
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[JPEG][DECODE]: " + err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
imageJPEG = resizeIfNeeded(imageJPEG, 600)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = jpeg.Encode(&buf, imageJPEG, &jpeg.Options{Quality: 90})
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[JPEG][ENCODE]: " + err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
image = buf.Bytes()
|
||||
|
||||
// Create recognizer - provide ./models dir
|
||||
rec, err := face.NewRecognizer("models")
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[DLIB][INIT]: " + err.Error()
|
||||
return
|
||||
}
|
||||
defer rec.Close()
|
||||
|
||||
faces, err := rec.RecognizeCNN(image)
|
||||
if err != nil {
|
||||
res.Success = false
|
||||
res.Error = "[DLIB][CNN]: " + err.Error()
|
||||
return
|
||||
} else if len(faces) == 0 {
|
||||
res.Success = false
|
||||
res.Error = "[DLIB][CNN]: No face found in image."
|
||||
return
|
||||
} else if len(faces) != 1 {
|
||||
res.Success = false
|
||||
res.Error = "[DLIB][CNN]: Multiple faces found in image."
|
||||
return
|
||||
}
|
||||
|
||||
res.Success = true
|
||||
res.Descriptor = faces[0].Descriptor
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
module OfficeSense/extractEmbeddings
|
||||
|
||||
go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e
|
||||
golang.org/x/image v0.41.0
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e h1:lqIUFzxaqyYqUn4MhzAvSAh4wIte/iLNcIEWxpT/qbc=
|
||||
github.com/Kagami/go-face v0.0.0-20210630145111-0c14797b4d0e/go.mod h1:9wdDJkRgo3SGTcFwbQ7elVIQhIr2bbBjecuY7VoqmPU=
|
||||
golang.org/x/image v0.41.0 h1:8wS72eGJMJaBxK6okTzd4WaXumUlTVlb753MlsSvTCo=
|
||||
golang.org/x/image v0.41.0/go.mod h1:uIc348UZMSvS5Z65CVZ7iDPaNobNFEPeJ4kbqTOszmA=
|
||||
Reference in New Issue
Block a user