Compare commits
52 Commits
v0.4.2
...
v0.5.8-dem
| Author | SHA1 | Date | |
|---|---|---|---|
| b296e8a74a | |||
| c9be9b5807 | |||
| 0558344dc2 | |||
| 8f07dee048 | |||
| 3cddd9c2e1 | |||
| 248025cdf9 | |||
| ff4f95c3b0 | |||
| 48a30d97db | |||
| ecc8bbba6f | |||
| 97a8ecc03a | |||
| 50d122e5f0 | |||
| dd3331ee9d | |||
| 68a50d10b1 | |||
| 8137480877 | |||
| f5eb2bb124 | |||
| 0b29dd4a50 | |||
| e33761d6d0 | |||
| 02b2c5bc39 | |||
| e5a63cc412 | |||
| e27eb8e927 | |||
| 7c564aca3c | |||
| 7dda6ab508 | |||
| e0bd764e44 | |||
| 6068b2c3c7 | |||
| dae18d651b | |||
| c7312d31b4 | |||
| f4102443ea | |||
| 359d48e1c4 | |||
| 830c1c1573 | |||
| 4db71e9381 | |||
| 6fafecd6e4 | |||
| 0e4cb1274e | |||
| abcf6aa012 | |||
| f61da2eeed | |||
| 2e60fbb57a | |||
| f97ffc913a | |||
| 62c2cc8a7b | |||
| 16f0e69cec | |||
| 9062d2fc51 | |||
| 177584e812 | |||
| 81e2c605b9 | |||
| 1607f4ce74 | |||
| f88d441bbf | |||
| 4ac08e5a85 | |||
| 7c74431fcf | |||
| 01f1c17c7f | |||
| 448fba8770 | |||
| 4bd1718491 | |||
| 51b9cb9743 | |||
| 2835252bb3 | |||
| 7021d5d2f5 | |||
| 4c62fc0ef3 |
1
.gitignore
vendored
@@ -66,3 +66,4 @@ node_modules/
|
||||
# Left behind by `pip install .` of the engine (setuptools metadata), not source.
|
||||
/behavision.egg-info/
|
||||
/.prod/
|
||||
/.demo/
|
||||
|
||||
197
API.md
@@ -43,9 +43,12 @@ user; the tenant is always taken from the session and never from the request.
|
||||
|---|---|
|
||||
| `POST /api/auth/login` `refresh` · `GET /api/auth/invitation` · `POST /api/auth/register` | **no auth** |
|
||||
| `POST /api/auth/logout` · `GET /api/auth/me` · `/api/auth/sessions*` | authed |
|
||||
| `POST /api/auth/password` — change your OWN | authed (platform admins too) |
|
||||
| `GET /api/visits` · `GET /api/visits/stream` | authed |
|
||||
| `GET /api/visitors` · `GET /api/visitors/{id}/history` · `GET /api/visitors/{id}/image` · `GET /api/faces/{id}` | authed |
|
||||
| `PUT /api/visitors/{id}/profile` · `POST /api/purchases` | staff |
|
||||
| `POST /api/customers` | staff |
|
||||
| `POST /api/visitors/{id}/merge` — **irreversible** | manager |
|
||||
| `DELETE /api/visitors/{id}` — erasure | manager |
|
||||
| `GET /api/sites` · `GET /api/sites/{site}/check` · `GET /api/cameras` · `GET /api/cameras/{id}/snapshot.jpg` · `GET /api/cameras/{id}/live` | authed |
|
||||
| `POST /api/sites/{site}/cameras` · `PATCH` / `DELETE /api/cameras/{id}` · `POST /api/cameras/{id}/check` | manager |
|
||||
@@ -53,15 +56,23 @@ user; the tenant is always taken from the session and never from the request.
|
||||
| `POST /api/sites/{site}/enrolment-code` | manager |
|
||||
| `GET /api/team` | authed (tenant users only) |
|
||||
| `POST /api/team/members` · `POST /api/team/{id}/password` · `PATCH /api/team/{id}` · `/api/team/invitations*` | manager |
|
||||
| `GET /api/reports/footfall` · `GET /api/reports/conversion` | authed |
|
||||
| `GET /api/reports/footfall` · `GET /api/reports/conversion` · `GET /api/sales` · `GET /api/sales/{id}` · `GET /api/dashboard/summary` | authed |
|
||||
| `POST /api/assistant` | authed |
|
||||
| `GET` / `POST /api/admin/clients` · `PATCH /api/admin/clients/{id}` · `POST /api/admin/clients/{id}/owner-password` · `DELETE /api/admin/clients/{id}` | **platform admin** |
|
||||
| `GET /api/admin/clients/{id}` · `…/{id}/sites` · `…/sites/{site}` · `…/sites/{site}/cameras` · `…/cameras/{camera}` · `GET /api/admin/monitoring/summary` | **platform admin** |
|
||||
| `/api/agent/*` | **shop PC token** — never a user |
|
||||
|
||||
A role that may not call something gets **403 `forbidden`** with a message
|
||||
saying who can. Another tenant's data returns **404**, never 403: a tenant user
|
||||
has no business learning that a resource exists.
|
||||
|
||||
`authed` above means any signed-in user **of a company**. A platform admin has
|
||||
no company — that absence is what defines one — so a company's own routes
|
||||
answer them **403 `not_a_tenant_account`**, naming the `/api/admin/clients/{id}/…`
|
||||
route that reads the same data. Their own `/api/auth/*` keeps working: a session
|
||||
is not a company's data, and revoking a lost device must not depend on having a
|
||||
tenant.
|
||||
|
||||
---
|
||||
|
||||
## The onboarding chain — who creates whom
|
||||
@@ -469,6 +480,34 @@ deactivate them (§4); that revokes every session they hold.
|
||||
|
||||
---
|
||||
|
||||
### `POST /api/auth/password` — any signed-in account
|
||||
|
||||
Change your own password. Works for **every** account including a platform
|
||||
admin, who has no company and therefore cannot be reached by the team routes.
|
||||
|
||||
```json
|
||||
{ "current_password": "...", "new_password": "..." }
|
||||
```
|
||||
|
||||
```json
|
||||
{ "changed": true, "sessions_revoked": 3 }
|
||||
```
|
||||
|
||||
- **The current password is required.** An access token lives twelve hours and
|
||||
travels on shop-floor PCs and staff phones; without this a stolen one would
|
||||
own the account permanently rather than until it expires. Wrong current
|
||||
password is **403 `wrong_password`** and changes nothing.
|
||||
- **Every other session is revoked; the caller's is kept.** Somebody changing
|
||||
their password because they think it is known must not wonder whether the
|
||||
device that already had it is still signed in — and must not be signed out of
|
||||
the one in their hand while dealing with it.
|
||||
- A new password under the floor is **400**, and so is reusing the current one.
|
||||
|
||||
This is the route to use rather than asking an administrator. `POST
|
||||
/api/team/{id}/password` remains what a manager uses on somebody *else*.
|
||||
|
||||
---
|
||||
|
||||
## 4. The team
|
||||
|
||||
### `GET /api/team` — anyone in the company
|
||||
@@ -687,6 +726,105 @@ record of what you were permitted to do is what an auditor asks for.
|
||||
Links a sale to a customer so the conversion report can say who bought. **One
|
||||
currency per report** — see §9.
|
||||
|
||||
### `GET /api/sales` — anyone in the company
|
||||
|
||||
The purchases behind the conversion report. That report has always summed this
|
||||
table; until 28 Sep nothing could read a row of it, so "revenue was 41,000"
|
||||
could not be checked against a till.
|
||||
|
||||
Takes the same window as a report: `from`, `to` (`YYYY-MM-DD`), `site` or
|
||||
`site_id` (slug or uuid), plus `customer` (`V-42` or a uuid) and `limit`
|
||||
(default 50, max 200). An unknown shop or customer is a **400**, not a silently
|
||||
ignored filter.
|
||||
|
||||
```json
|
||||
[{ "id": "8525ef18-…", "occurred_at": "2026-09-19T10:48:44Z",
|
||||
"site_id": "93d0565f-…", "site": "TeNext Coimbatore", "site_slug": "chennai",
|
||||
"amount": 1000, "currency": "INR",
|
||||
"visitor_id": "ba5e5d2e-…", "visitor_ref": "V-1", "visitor_label": "Visitor 1",
|
||||
"visit_id": "3bcd53ca-…", "items": ["Headphones"], "source": "manual" }]
|
||||
```
|
||||
|
||||
- **A sale with no `visitor_id` is listed**, not joined away. An unidentified
|
||||
walk-in is still revenue, and hiding it would make this disagree with the
|
||||
conversion report computed over the same rows.
|
||||
- `items` is always an array, never `null`.
|
||||
- **No cursor, deliberately.** A keyset cursor needs a monotonic
|
||||
server-assigned column and `purchases` has none; ordering by
|
||||
`(occurred_at, id)` with a random uuid tie-break is the shape that silently
|
||||
dropped visits from the arrivals feed before `visits.seq` existed. Narrow by
|
||||
date and `limit` instead.
|
||||
|
||||
### `GET /api/sales/{id}`
|
||||
|
||||
One sale, same shape. Another company's sale is **404**.
|
||||
|
||||
### `GET /api/dashboard/summary` — anyone in the company
|
||||
|
||||
The home screen in one call, so a client does not combine four.
|
||||
|
||||
Takes `site`/`site_id` and `tz` (IANA, default the company's). "Today" is cut
|
||||
in **that timezone** — a dashboard that says today and means UTC is five and a
|
||||
half hours wrong in India.
|
||||
|
||||
```json
|
||||
{ "date": "2026-09-28", "visitors": 0, "visits": 0,
|
||||
"sites_total": 4, "sites_online": 0, "cameras_total": 1, "cameras_up": 1,
|
||||
"fraction_below_gate": 0.59, "worst_site": "TeNext Coimbatore",
|
||||
"timezone": "Asia/Kolkata" }
|
||||
```
|
||||
|
||||
`visitors` is unique people and `visits` is arrivals — **do not add the daily
|
||||
bars of a footfall report to get either.** `fraction_below_gate` travels with
|
||||
them because it is what says whether the count is a number or a floor.
|
||||
|
||||
### `POST /api/customers` — staff and above
|
||||
|
||||
Register a customer before any camera has seen them — somebody standing at the
|
||||
counter. Body is a profile; **at least a name or a phone** is required, since a
|
||||
record with neither is a number nobody can search for.
|
||||
|
||||
```json
|
||||
{ "id": "…", "ref": "V-7", "label": "Asha Menon", "visit_count": 0, "has_profile": true }
|
||||
```
|
||||
|
||||
They get a `V-` reference from the same counter an enrolled customer does, so a
|
||||
hand-created record is indistinguishable from one the engine made.
|
||||
|
||||
**Know what this implies.** They have no face template, so when a camera sees
|
||||
that person later the matcher has nothing to compare against and enrols them
|
||||
again — by design, not by failure. Join the two with the merge below.
|
||||
|
||||
### `POST /api/visitors/{id}/merge` — manager and above
|
||||
|
||||
Fold the customer in the path **into** the one named in the body, and delete
|
||||
the source. `into` takes a uuid or a `V-` reference.
|
||||
|
||||
```json
|
||||
{ "into": "V-12" }
|
||||
```
|
||||
|
||||
```json
|
||||
{ "visitor_id": "…", "ref": "V-12", "label": "Asha Menon",
|
||||
"visits": 2, "purchases": 1, "embeddings": 1, "consents": 1,
|
||||
"retired_ref": "V-7",
|
||||
"discarded": ["phone: 9000000001"] }
|
||||
```
|
||||
|
||||
- **Irreversible**, which is why it is manager-and-above. Two different people
|
||||
welded together cannot be separated: nothing records which visit came from
|
||||
whom. It logs at WARNING and writes an audit row.
|
||||
- **`retired_ref` is the reference that has stopped resolving.** Staff write
|
||||
these on cards; asking for it afterwards returns 404.
|
||||
- **Nothing is discarded silently.** The survivor keeps its own profile values
|
||||
and its blanks are filled from the source; anything that loses is listed in
|
||||
`discarded` **and** appended to the survivor's notes — and `GET /api/visitors?q=`
|
||||
searches notes, so a customer reached by their old phone number is still found.
|
||||
- Visits, purchases, templates and consents all move. `visit_count` is
|
||||
recomputed by counting rows, and `first_seen_at` takes the earlier of the two.
|
||||
- Merging a customer into themselves is **400**; an unknown or another tenant's
|
||||
customer is **404**.
|
||||
|
||||
### `DELETE /api/visitors/{id}` — **erasure**, manager and above
|
||||
|
||||
Destroys the face template and the photo outright. Keeps the visit rows,
|
||||
@@ -1091,6 +1229,57 @@ first — a failure there is `502 storage_error` and nothing else is touched —
|
||||
then the shop PCs' broker logins, then every row (templates, visits, users,
|
||||
sessions, cameras) by cascade.
|
||||
|
||||
### The drill-down: `GET /api/admin/clients/{id}` and below
|
||||
|
||||
A platform admin has **no company**, so the tenant routes cannot serve this —
|
||||
they scope by the signed-in account's client, and an admin has none. These take
|
||||
the merchant in the path instead. `{site}` accepts a slug or a uuid; `{camera}`
|
||||
accepts the engine's camera id or a uuid.
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| `GET /api/admin/clients/{id}` | the list row plus `owner_email`, `owner_name` |
|
||||
| `GET …/{id}/sites` | same shape as a tenant's `GET /api/sites` |
|
||||
| `GET …/{id}/sites/{site}` | one of them |
|
||||
| `GET …/{id}/sites/{site}/cameras` | **redacted** — see below |
|
||||
| `GET …/{id}/sites/{site}/cameras/{camera}` | one of them |
|
||||
| `GET /api/admin/monitoring/summary` | `cameras_total`, `cameras_online`, `merchants_active`, `sites_total`, `events_today`, `as_of` |
|
||||
|
||||
**Camera rows here are a different shape from `GET /api/cameras`** and carry no
|
||||
`host`, `port`, `path`, `username` or `has_password`. A company seeing those
|
||||
for its own camera is correct; a platform admin browsing somebody else's estate
|
||||
is a different question, and an RTSP host with a username beside it is most of
|
||||
a live path into that customer's camera.
|
||||
|
||||
```json
|
||||
[{ "id": "3a96a742-…", "site_id": "93d0565f-…", "site": "TeNext Coimbatore",
|
||||
"camera_id": "cam2", "label": "Open office", "enabled": true,
|
||||
"connected": true, "last_seen_at": "2026-09-24T08:33:10Z",
|
||||
"snapshot_at": "2026-09-24T08:33:10Z", "check": { … } }]
|
||||
```
|
||||
|
||||
`connected` is still three states: `null` = no shop PC has reported yet,
|
||||
`false` = not connecting, `true` = up.
|
||||
|
||||
A shop or camera belonging to a **different** merchant is **404**, never an
|
||||
empty list — `[]` would say "this shop has no cameras" when the truth is "not
|
||||
your shop". A suspended merchant stays readable; that is what an admin opens
|
||||
the console to see. Every read below the merchant list is written to
|
||||
`audit_log`; the counts-only summary is not.
|
||||
|
||||
**Not built, and each is a decision rather than a missing handler:**
|
||||
|
||||
- `…/events` and `…/alerts` — there is no events table and the shop PC
|
||||
deliberately does not send diagnostics (`camera.up`, `person.missed`) to the
|
||||
server. This needs that decision, a table and a retention policy first; an
|
||||
endpoint now would return `[]` forever.
|
||||
- `POST /api/admin/assistant` — the assistant's tools take no client id by
|
||||
design, which is what makes cross-tenant access impossible rather than merely
|
||||
disallowed. An admin one needs a principal scoped to the merchant being
|
||||
viewed, which weakens that. Deliberately not done quietly.
|
||||
|
||||
---
|
||||
|
||||
## 12. Errors
|
||||
|
||||
```json
|
||||
@@ -1105,7 +1294,7 @@ rewritten freely.
|
||||
|---|---|
|
||||
| 400 | the request was wrong; `message` says how — including an unknown reference in a query filter |
|
||||
| 401 | not signed in, or `token_expired` → refresh once and retry |
|
||||
| 403 | `forbidden` — signed in, but this role may not; `message` says who can |
|
||||
| 403 | `forbidden` — signed in, but this role may not; `message` says who can. Also `not_a_tenant_account`: a **platform admin** calling a company's own route, who reads that data through `/api/admin/clients/{id}/…` instead |
|
||||
| 404 | not found — **also** what another tenant's data returns, always; and what admin routes return to non-admins |
|
||||
| 409 | a conflict `message` explains (`last_owner`, duplicate address) |
|
||||
| 429 | `too_many_attempts` |
|
||||
@@ -1126,3 +1315,7 @@ a user session is refused.
|
||||
|
||||
A web or mobile client never calls them. They are listed here so nobody wonders
|
||||
what they are.
|
||||
|
||||
One field name, because it is the only one in the product that is easy to guess
|
||||
wrong: `POST /api/agent/enrol` takes **`site_token`** (the installation code),
|
||||
not `code`, plus an optional `device`. Verified against production.
|
||||
|
||||
@@ -19,25 +19,34 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := flag.String("cameras", "", "JSON array of cameras (id, host, port, path, username, password)")
|
||||
in := flag.String("cameras", "", "JSON array of cameras (id, host, port, path, username, password) - the PC then runs on its own")
|
||||
enrolCode := flag.String("enrol-code", "", "an installation code from head office - the PC then claims that shop and gets its cameras from there")
|
||||
cloud := flag.String("cloud", "https://mcp.loyaly.ai", "head office, with -enrol-code")
|
||||
out := flag.String("out", "demo-cameras.enc", "sealed bundle to write")
|
||||
flag.Parse()
|
||||
if *in == "" {
|
||||
fmt.Fprintln(os.Stderr, "usage: behavision-demo-pack -cameras cameras.json [-out demo-cameras.enc]")
|
||||
if *in == "" && *enrolCode == "" {
|
||||
fmt.Fprintln(os.Stderr, "usage: behavision-demo-pack (-cameras cameras.json | -enrol-code CODE [-cloud URL]) [-out demo-cameras.enc]")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
raw, err := os.ReadFile(*in)
|
||||
if err != nil {
|
||||
die("read cameras: %v", err)
|
||||
var payload demo.Payload
|
||||
if *in != "" {
|
||||
raw, err := os.ReadFile(*in)
|
||||
if err != nil {
|
||||
die("read cameras: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(raw, &payload.Cameras); err != nil {
|
||||
die("cameras.json: %v", err)
|
||||
}
|
||||
if len(payload.Cameras) == 0 {
|
||||
die("no cameras in %s", *in)
|
||||
}
|
||||
}
|
||||
var cams []demo.Camera
|
||||
if err := json.Unmarshal(raw, &cams); err != nil {
|
||||
die("cameras.json: %v", err)
|
||||
}
|
||||
if len(cams) == 0 {
|
||||
die("no cameras in %s", *in)
|
||||
payload.EnrolCode = *enrolCode
|
||||
if *enrolCode != "" {
|
||||
payload.CloudBase = *cloud
|
||||
}
|
||||
cams := payload.Cameras
|
||||
for i, c := range cams {
|
||||
switch {
|
||||
case c.ID == "":
|
||||
@@ -50,7 +59,7 @@ func main() {
|
||||
}
|
||||
// Re-marshal so only the fields the engine accepts travel, in a stable
|
||||
// shape, whatever extra keys the input happened to carry.
|
||||
plain, err := json.Marshal(cams)
|
||||
plain, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
die("marshal: %v", err)
|
||||
}
|
||||
@@ -67,7 +76,11 @@ func main() {
|
||||
die("write: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("\n sealed %d camera(s) into %s (%d bytes)\n\n", len(cams), *out, len(sealed))
|
||||
if payload.EnrolCode != "" {
|
||||
fmt.Printf("\n sealed an installation code for %s into %s (%d bytes)\n\n", payload.CloudBase, *out, len(sealed))
|
||||
} else {
|
||||
fmt.Printf("\n sealed %d camera(s) into %s (%d bytes)\n\n", len(cams), *out, len(sealed))
|
||||
}
|
||||
fmt.Printf(" unlock code: %s\n\n", code)
|
||||
fmt.Println(" Shown once. Give it to whoever runs behavision-setup, by voice")
|
||||
fmt.Println(" or message - not in the same place as the zip.")
|
||||
|
||||
@@ -38,6 +38,7 @@ import (
|
||||
"github.com/loyaly/behavision-agent/pkg/config"
|
||||
"github.com/loyaly/behavision-agent/pkg/demo"
|
||||
"github.com/loyaly/behavision-agent/pkg/engine"
|
||||
"github.com/loyaly/behavision-agent/pkg/enrol"
|
||||
"github.com/loyaly/behavision-agent/pkg/paths"
|
||||
)
|
||||
|
||||
@@ -45,6 +46,61 @@ import (
|
||||
// otherwise arrive as a syntax error deep inside a dependency.
|
||||
const minMinor = 10
|
||||
|
||||
// maxMinor is a WHEEL-availability ceiling, not a language one, and it is the
|
||||
// reason this constant exists at all.
|
||||
//
|
||||
// findPython used to take the newest interpreter it could find, with a floor
|
||||
// and no ceiling - which is precisely backwards, because the newest Python is
|
||||
// the one least likely to have binary wheels for anything. Measured on a
|
||||
// second Mac: it chose Python 3.14, pip found no numpy wheel for cp314, fell
|
||||
// back to building numpy from source, and produced
|
||||
//
|
||||
// ERROR: Unknown compiler(s): [['cc'], ['gcc'], ['clang'], ...]
|
||||
//
|
||||
// then, once the operator installed Xcode's command line tools to get past
|
||||
// that, ten minutes of compiling ending in
|
||||
//
|
||||
// arm_neon.h:28:2: error: "<arm_neon.h> is intended only for ARM and
|
||||
// AArch64 targets"
|
||||
//
|
||||
// Two screens of C compiler output, on a shop counter, for a version choice
|
||||
// made silently by this program. Refusing in one line, before anything is
|
||||
// downloaded, is the whole of the fix.
|
||||
//
|
||||
// Raise it when the dependency set has wheels for the next version. Today
|
||||
// onnxruntime is the binding one (cp314 is its newest); numpy publishes
|
||||
// further ahead, and opencv-python ships a stable-ABI wheel that covers
|
||||
// everything. `pip download --only-binary=:all: -r requirements.txt` against
|
||||
// a candidate interpreter is the check.
|
||||
const maxMinor = 14
|
||||
|
||||
// The three answers a candidate interpreter can get. Three, not two: a
|
||||
// version that is too new and one that is too old need opposite actions from
|
||||
// the operator, and collapsing them tells somebody holding Python 3.14 to go
|
||||
// and install a newer Python.
|
||||
const (
|
||||
verdictOK = "ok"
|
||||
verdictTooOld = "old"
|
||||
verdictTooNew = "new"
|
||||
verdictUnknown = "unparseable"
|
||||
)
|
||||
|
||||
func pythonVerdict(major, minor int, parsed bool) string {
|
||||
switch {
|
||||
case !parsed:
|
||||
return verdictUnknown
|
||||
case major != 3:
|
||||
// Python 4 is not a version this has been tried against, and 2 is
|
||||
// long gone. Neither is a thing to guess about.
|
||||
return verdictTooNew
|
||||
case minor < minMinor:
|
||||
return verdictTooOld
|
||||
case minor > maxMinor:
|
||||
return verdictTooNew
|
||||
}
|
||||
return verdictOK
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "\n Setup did not finish: %v\n\n", err)
|
||||
@@ -76,12 +132,31 @@ func run() error {
|
||||
// A demo release ships its cameras sealed. Ask for the code NOW, before
|
||||
// the ten-minute download, so a mistyped one costs seconds; the cameras
|
||||
// are actually added at the end, through the running engine.
|
||||
demoCams, err := unlockDemo(src)
|
||||
bundle, err := unlockDemo(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if demoCams != nil {
|
||||
step("Demo cameras", fmt.Sprintf("%d unlocked", len(demoCams)))
|
||||
var demoCams []demo.Camera
|
||||
if bundle != nil {
|
||||
demoCams = bundle.Cameras
|
||||
switch {
|
||||
case bundle.EnrolCode != "":
|
||||
step("Demo", "unlocked - this PC will join a shop at head office")
|
||||
default:
|
||||
step("Demo cameras", fmt.Sprintf("%d unlocked", len(demoCams)))
|
||||
}
|
||||
}
|
||||
|
||||
if running := behavisionRunning(); running != "" {
|
||||
// lint:ignore ST1005 — this is not a wrapped error, it is the whole
|
||||
// message an operator reads at a shop counter. ST1005 forbids
|
||||
// trailing punctuation because errors get concatenated mid-sentence;
|
||||
// nothing wraps this one, and stripping the full stops would make
|
||||
// three sentences run together.
|
||||
//lint:ignore ST1005 operator-facing prose, never wrapped
|
||||
return fmt.Errorf("%s is running. Quit Behavision from the tray icon first, then run setup again.\n\n"+
|
||||
"Setting up underneath a running copy starts a second engine on the same port and, in a demo,\n"+
|
||||
"re-claims the shop while the open app still holds the old credentials.", running)
|
||||
}
|
||||
|
||||
py, ver, err := findPython()
|
||||
@@ -128,13 +203,35 @@ func run() error {
|
||||
// Proving it starts is the point. An installer that reports success and
|
||||
// leaves a shop with an engine that will not run has done worse than
|
||||
// failing: the failure surfaces later, to someone who did not install it.
|
||||
// Joining a shop: head office supplies the cameras, so any left on this PC
|
||||
// from an earlier install go first. Otherwise the reconciler offers them UP
|
||||
// to head office - without their passwords, which the engine never returns
|
||||
// - and the shop ends up with the same lens listed twice, one copy of which
|
||||
// can never be pushed to another PC. Measured on the first claimed demo.
|
||||
if bundle != nil && bundle.EnrolCode != "" {
|
||||
if err := os.Remove(paths.CamerasFile()); err == nil {
|
||||
step("Earlier cameras", "removed - head office supplies them now")
|
||||
}
|
||||
}
|
||||
if err := smokeTest(vpy, demoCams); err != nil {
|
||||
return fmt.Errorf("the engine installed but would not start: %w", err)
|
||||
}
|
||||
step("Engine starts and answers", "verified")
|
||||
if demoCams != nil {
|
||||
if len(demoCams) > 0 {
|
||||
step("Demo cameras", "added to the engine")
|
||||
// No head office in a demo. Without this the app opens on "type an
|
||||
}
|
||||
switch {
|
||||
case bundle != nil && bundle.EnrolCode != "":
|
||||
// The demo that IS the product: this PC claims a real shop, exactly
|
||||
// as a customer install does, and its cameras arrive from head office
|
||||
// on the first sync. The app then opens on Login.
|
||||
siteName, err := claimShop(bundle.EnrolCode, bundle.CloudBase)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not join the shop at head office: %w", err)
|
||||
}
|
||||
step("Head office", "linked to "+siteName)
|
||||
case bundle != nil:
|
||||
// No head office in this demo. Without this the app opens on "type an
|
||||
// installation code" and sits there; with it, it opens on Live.
|
||||
if err := markStandalone(); err != nil {
|
||||
return err
|
||||
@@ -143,8 +240,19 @@ func run() error {
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(" Done. Start Behavision from the Start menu or the desktop icon.")
|
||||
fmt.Println(" It appears in the system tray; right-click there to stop it.")
|
||||
// The last thing setup says is the first thing the operator does, so it
|
||||
// has to describe THEIR machine. On macOS there is no Start menu and,
|
||||
// deliberately, no tray at all - telling somebody to right-click a tray
|
||||
// icon that does not exist is how software loses their trust on the step
|
||||
// where it was otherwise finished.
|
||||
if runtime.GOOS == "windows" {
|
||||
fmt.Println(" Done. Start Behavision from the Start menu or the desktop icon.")
|
||||
fmt.Println(" It appears in the system tray; right-click there to stop it.")
|
||||
} else {
|
||||
fmt.Println(" Done. Open Behavision.app - right-click it and choose Open the")
|
||||
fmt.Println(" first time, because this build is not notarised.")
|
||||
fmt.Println(" There is no tray on macOS: closing the window stops recognition.")
|
||||
}
|
||||
fmt.Println()
|
||||
return nil
|
||||
}
|
||||
@@ -180,6 +288,22 @@ func engineSource() (string, error) {
|
||||
// `py -3` first on Windows: the launcher is what the official installer puts
|
||||
// on PATH, and `python` there is often the Microsoft Store stub that prints an
|
||||
// advert and exits 9009 instead of running anything.
|
||||
// behavisionRunning names a Behavision process if one is up. Windows only -
|
||||
// that is the platform setup ships on - and by image name via tasklist, which
|
||||
// needs no extra privilege.
|
||||
func behavisionRunning() string {
|
||||
if runtime.GOOS != "windows" {
|
||||
return ""
|
||||
}
|
||||
for _, name := range []string{"Behavision.exe", "behavision-agent.exe"} {
|
||||
out, err := exec.Command("tasklist", "/FI", "IMAGENAME eq "+name, "/NH").Output()
|
||||
if err == nil && strings.Contains(strings.ToLower(string(out)), strings.ToLower(name)) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func findPython() (string, string, error) {
|
||||
type cand struct {
|
||||
exe string
|
||||
@@ -189,13 +313,59 @@ func findPython() (string, string, error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
cands = append(cands, cand{"py", []string{"-3"}})
|
||||
}
|
||||
|
||||
// Versioned names FIRST, newest first, and this is not belt-and-braces on
|
||||
// macOS - it is the only thing that works. `/usr/bin/python3` there is
|
||||
// always the Command Line Tools build, 3.9 on current macOS, which is
|
||||
// below the 3.10 floor. Anything newer installs as `python3.12` or into a
|
||||
// directory that is not on a GUI application's PATH. Searching only
|
||||
// `python3` therefore told a Mac with Python 3.12 sitting on it to go and
|
||||
// install Python - measured on this machine, which has 3.12 under
|
||||
// ~/.local/opt and reported "Found, but too old: python3 3.9".
|
||||
// Newest first WITHIN the supported range. Newest overall is what broke
|
||||
// this; a version nobody has built wheels for is not a better choice than
|
||||
// one that works.
|
||||
var versions []string
|
||||
for v := maxMinor; v >= minMinor; v-- {
|
||||
versions = append(versions, fmt.Sprintf("3.%d", v))
|
||||
}
|
||||
for _, v := range versions {
|
||||
cands = append(cands, cand{"python" + v, nil})
|
||||
}
|
||||
cands = append(cands, cand{"python3", nil}, cand{"python", nil})
|
||||
|
||||
var tried []string
|
||||
// And the places a Mac puts an interpreter that LookPath will not find,
|
||||
// because a double-clicked app inherits a minimal PATH rather than the
|
||||
// one a shell profile builds.
|
||||
if runtime.GOOS != "windows" {
|
||||
home, _ := os.UserHomeDir()
|
||||
for _, v := range versions {
|
||||
for _, dir := range []string{
|
||||
"/opt/homebrew/bin",
|
||||
"/usr/local/bin",
|
||||
"/Library/Frameworks/Python.framework/Versions/" + v + "/bin",
|
||||
filepath.Join(home, ".local", "opt", "python"+v, "bin"),
|
||||
} {
|
||||
cands = append(cands, cand{filepath.Join(dir, "python"+v), nil})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var tried, tooNew []string
|
||||
for _, c := range cands {
|
||||
exe, err := exec.LookPath(c.exe)
|
||||
if err != nil {
|
||||
continue
|
||||
exe := c.exe
|
||||
if filepath.IsAbs(exe) {
|
||||
// An absolute candidate is a guess about where an interpreter
|
||||
// might be; most will not exist, and that is not an error.
|
||||
if fi, err := os.Stat(exe); err != nil || fi.IsDir() {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
found, err := exec.LookPath(exe)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
exe = found
|
||||
}
|
||||
args := append(append([]string{}, c.args...), "-c",
|
||||
"import sys;print('%d.%d'%sys.version_info[:2])")
|
||||
@@ -205,7 +375,18 @@ func findPython() (string, string, error) {
|
||||
}
|
||||
ver := strings.TrimSpace(string(out))
|
||||
tried = append(tried, c.exe+" "+ver)
|
||||
if major, minor, ok := parseVer(ver); ok && (major > 3 || (major == 3 && minor >= minMinor)) {
|
||||
major, minor, parsed := parseVer(ver)
|
||||
switch verdict := pythonVerdict(major, minor, parsed); verdict {
|
||||
case verdictTooNew:
|
||||
// Recorded separately: "too new" and "too old" need opposite
|
||||
// actions, and a single "found, but unsuitable" list sends
|
||||
// somebody to upgrade a Python that is already past the problem.
|
||||
tooNew = append(tooNew, c.exe+" "+ver)
|
||||
continue
|
||||
case verdictTooOld, verdictUnknown:
|
||||
continue
|
||||
}
|
||||
{
|
||||
full := exe
|
||||
if len(c.args) > 0 {
|
||||
full = exe + " " + strings.Join(c.args, " ")
|
||||
@@ -214,13 +395,48 @@ func findPython() (string, string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
msg := "no Python 3.10 or newer was found on this PC.\n\n" +
|
||||
" Install it from https://www.python.org/downloads/windows/\n" +
|
||||
" and tick \"Add python.exe to PATH\" on the first screen,\n" +
|
||||
" then run this again."
|
||||
// The advice has to match the machine. Telling a Mac user to tick "Add
|
||||
// python.exe to PATH" on a Windows installer page reads as software that
|
||||
// does not know where it is running, which is exactly the moment somebody
|
||||
// stops trusting the rest of what it says.
|
||||
// Only a too-new Python is a different problem with a different fix, and
|
||||
// saying "no Python was found" to somebody looking at Python 3.14 is the
|
||||
// kind of message that makes people stop believing the next one.
|
||||
if len(tooNew) > 0 && len(tried) == 0 {
|
||||
// Built as a value and wrapped, not written as an fmt.Errorf literal:
|
||||
// this is a paragraph shown to an operator, and a linter that wants
|
||||
// error strings to be lower-case fragments is right about errors
|
||||
// programs read and wrong about the ones people do.
|
||||
tooNewMsg := fmt.Sprintf(
|
||||
"this computer has %s, which is newer than Behavision supports.\n\n"+
|
||||
" Some of the libraries the engine needs have no build for it\n"+
|
||||
" yet, so installing would fail part-way through.\n\n"+
|
||||
" Install Python 3.%d and run this again:\n"+
|
||||
" macOS: brew install python@3.%d\n"+
|
||||
" or https://www.python.org/downloads/macos/\n"+
|
||||
" Windows: https://www.python.org/downloads/windows/\n\n"+
|
||||
" Both versions can sit on the machine together; this picks\n"+
|
||||
" the one it can use.",
|
||||
strings.Join(tooNew, ", "), maxMinor, maxMinor)
|
||||
return "", "", errors.New(tooNewMsg)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("no Python between 3.%d and 3.%d was found on this computer.\n\n",
|
||||
minMinor, maxMinor)
|
||||
if runtime.GOOS == "windows" {
|
||||
msg += " Install it from https://www.python.org/downloads/windows/\n" +
|
||||
" and tick \"Add python.exe to PATH\" on the first screen,\n" +
|
||||
" then run this again."
|
||||
} else {
|
||||
msg += " Install it with `brew install python@3.12`, or from\n" +
|
||||
" https://www.python.org/downloads/macos/, then run this again."
|
||||
}
|
||||
if len(tried) > 0 {
|
||||
msg += "\n\n Found, but too old: " + strings.Join(tried, ", ")
|
||||
}
|
||||
if len(tooNew) > 0 {
|
||||
msg += "\n\n Found, but too new: " + strings.Join(tooNew, ", ")
|
||||
}
|
||||
return "", "", errors.New(msg)
|
||||
}
|
||||
|
||||
@@ -257,14 +473,52 @@ func venvPython(venv string) string {
|
||||
// engine requires - inside a shared interpreter is how you break the other
|
||||
// thing months later, silently.
|
||||
func makeVenv(py, venv string) error {
|
||||
// An existing environment is reused - but only if the Python inside it is
|
||||
// one this build supports.
|
||||
//
|
||||
// It used to be reused unconditionally, and that would have made the
|
||||
// version ceiling above look like it did not work. The machine this was
|
||||
// all found on already had a runtime built by Python 3.14, from the run
|
||||
// that failed: with the ceiling in place setup would choose a good
|
||||
// interpreter, reach here, find the 3.14 environment, keep it, and die in
|
||||
// the same clang error as before. A fix that is defeated by the wreckage
|
||||
// of the bug it fixes is not one.
|
||||
//
|
||||
// Rebuilding costs a re-download of the libraries and nothing else. The
|
||||
// models are in the state root, not in here, so they survive.
|
||||
if _, err := os.Stat(venvPython(venv)); err == nil {
|
||||
return nil // already built; pip below brings it up to date
|
||||
ok, ver := venvUsable(venv)
|
||||
if ok {
|
||||
return nil // pip below brings it up to date
|
||||
}
|
||||
fmt.Printf(" [..] %-24s %s\n", "Rebuilding environment",
|
||||
"the existing one uses "+ver+", which is not supported")
|
||||
if err := os.RemoveAll(venv); err != nil {
|
||||
return fmt.Errorf("removing the old environment at %s: %w", venv, err)
|
||||
}
|
||||
}
|
||||
exe, args := splitLauncher(py)
|
||||
args = append(args, "-m", "venv", venv)
|
||||
return stream(exec.Command(exe, args...), "creating the virtual environment")
|
||||
}
|
||||
|
||||
// venvUsable reports whether the interpreter already inside an environment is
|
||||
// one this build supports, and what it is when it is not.
|
||||
//
|
||||
// An environment that cannot be asked counts as unusable: a half-created or
|
||||
// truncated one answers nothing, and reusing it fails later in pip with an
|
||||
// error about a package rather than about the environment.
|
||||
func venvUsable(venv string) (bool, string) {
|
||||
out, err := exec.Command(venvPython(venv), "-c",
|
||||
"import sys;print('%d.%d'%sys.version_info[:2])").Output()
|
||||
if err != nil {
|
||||
return false, "an interpreter that will not run"
|
||||
}
|
||||
ver := strings.TrimSpace(string(out))
|
||||
major, minor, parsed := parseVer(ver)
|
||||
return pythonVerdict(major, minor, parsed) == verdictOK, "Python " + ver
|
||||
}
|
||||
|
||||
func pipInstall(vpy, src string) error {
|
||||
fmt.Println(" Installing the engine and its libraries. This downloads a few")
|
||||
fmt.Println(" hundred megabytes and takes a while on a slow connection.")
|
||||
@@ -285,7 +539,30 @@ func pipInstall(vpy, src string) error {
|
||||
// 'behavision.egg-info': Read-only file system". Falling back to source
|
||||
// copies it somewhere writable first, for the same reason.
|
||||
if wheels, _ := filepath.Glob(filepath.Join(src, "behavision-*.whl")); len(wheels) > 0 {
|
||||
return stream(exec.Command(vpy, "-m", "pip", "install", "--upgrade", wheels[0]),
|
||||
// TWO calls, and the second is the one that matters.
|
||||
//
|
||||
// `--upgrade` alone is not an upgrade when the version has not moved:
|
||||
// pip skips the wheel and says so, one line above this program
|
||||
// printing "[ok] Engine and dependencies installed". The wheel version
|
||||
// was a frozen literal for several releases, so every engine fix in
|
||||
// them silently failed to reach any machine that had run setup once -
|
||||
// while the Go binaries beside it, rebuilt every release, updated
|
||||
// normally. Half the product current, half of it months old, and
|
||||
// nothing saying which.
|
||||
//
|
||||
// release.sh stamps the tag into the version now, so the versions do
|
||||
// differ. This does not rely on that: a rebuild at the same version is
|
||||
// the ordinary case while developing, and "installed" has to mean the
|
||||
// code in this folder either way.
|
||||
if err := stream(exec.Command(vpy, "-m", "pip", "install", "--upgrade", wheels[0]),
|
||||
"installing the engine"); err != nil {
|
||||
return err
|
||||
}
|
||||
// --no-deps so this is our own package only: the call above has
|
||||
// already settled the dependencies, and forcing those too would
|
||||
// re-download ~300 MB of numpy, OpenCV and onnxruntime every run.
|
||||
return stream(exec.Command(vpy, "-m", "pip", "install",
|
||||
"--force-reinstall", "--no-deps", wheels[0]),
|
||||
"installing the engine")
|
||||
}
|
||||
tmp, err := os.MkdirTemp("", "behavision-src-")
|
||||
@@ -446,7 +723,7 @@ func pause() {
|
||||
// unlockDemo returns the sealed cameras a demo release ships, or nil when this
|
||||
// is not a demo release. Asks for the unlock code on the console; three tries,
|
||||
// because a code is read down a phone and typed by hand.
|
||||
func unlockDemo(src string) ([]demo.Camera, error) {
|
||||
func unlockDemo(src string) (*demo.Payload, error) {
|
||||
sealed, err := os.ReadFile(filepath.Join(src, "demo-cameras.enc"))
|
||||
if err != nil {
|
||||
return nil, nil // not a demo release
|
||||
@@ -461,12 +738,12 @@ func unlockDemo(src string) ([]demo.Camera, error) {
|
||||
line, _ := in.ReadString('\n')
|
||||
plain, err := demo.Open(line, sealed)
|
||||
if err == nil {
|
||||
var cams []demo.Camera
|
||||
if err := json.Unmarshal(plain, &cams); err != nil {
|
||||
payload, err := demo.Decode(plain)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the bundle unlocked but did not parse: %w", err)
|
||||
}
|
||||
fmt.Println()
|
||||
return cams, nil
|
||||
return &payload, nil
|
||||
}
|
||||
fmt.Printf(" %v\n", err)
|
||||
}
|
||||
@@ -474,6 +751,47 @@ func unlockDemo(src string) ([]demo.Camera, error) {
|
||||
"with whoever gave you this release and run setup again")
|
||||
}
|
||||
|
||||
// claimShop redeems the installation code sealed in the bundle: the same call
|
||||
// the app's Setup screen and `behavision-agent claim` make, so the PC ends up
|
||||
// in exactly the state a customer's would - broker login, API token, the
|
||||
// broker's CA on disk - and head office pushes its cameras down on the first
|
||||
// sync.
|
||||
func claimShop(code, base string) (string, error) {
|
||||
if base == "" {
|
||||
base = "https://mcp.loyaly.ai"
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
b, err := enrol.Claim(ctx, base, code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := paths.AgentConfig()
|
||||
cfg, err := config.Load(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cfg.ClientID = b.ClientSlug
|
||||
cfg.SiteID = b.SiteSlug
|
||||
cfg.SiteName = b.SiteName
|
||||
cfg.BrokerURL = b.MQTTURL
|
||||
cfg.BrokerUsername = b.MQTTUser
|
||||
cfg.BrokerPassword = b.MQTTPass
|
||||
cfg.AgentToken = b.AgentToken
|
||||
cfg.CloudBase = base
|
||||
cfg.Standalone = false
|
||||
cfg.SessionToken, cfg.SessionRefresh, cfg.SessionEmail = "", "", ""
|
||||
caPath, err := enrol.SaveCA(b.CACert, paths.BrokerCA())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cfg.BrokerCAFile = caPath
|
||||
if err := cfg.Save(path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.SiteName, nil
|
||||
}
|
||||
|
||||
// addCameras posts each demo camera to the running engine, with the credential
|
||||
// the engine generated for itself on first start.
|
||||
func addCameras(cams []demo.Camera) error {
|
||||
|
||||
112
agent/cmd/behavision-setup/python_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The choice this program makes silently, and got wrong.
|
||||
//
|
||||
// findPython took the newest interpreter on the machine, with a floor and no
|
||||
// ceiling - backwards, because the newest Python is the one least likely to
|
||||
// have binary wheels. On a Mac holding Python 3.14 it chose 3.14, pip found
|
||||
// no numpy wheel for cp314, fell back to a source build and produced two
|
||||
// screens of clang errors ending in "<arm_neon.h> is intended only for ARM
|
||||
// and AArch64 targets". The operator's machine was fine; the version was not.
|
||||
func TestTooNewIsRefusedRatherThanCompiled(t *testing.T) {
|
||||
if got := pythonVerdict(3, maxMinor+1, true); got != verdictTooNew {
|
||||
t.Errorf("3.%d = %q, want %q - picking it means a source build",
|
||||
maxMinor+1, got, verdictTooNew)
|
||||
}
|
||||
if got := pythonVerdict(3, maxMinor, true); got != verdictOK {
|
||||
t.Errorf("3.%d = %q, want %q - the ceiling is inclusive", maxMinor, got, verdictOK)
|
||||
}
|
||||
}
|
||||
|
||||
// Too old and too new must stay different answers. Telling somebody holding
|
||||
// Python 3.14 that no Python was found, or that theirs is too old, sends them
|
||||
// to install a newer one - which is the direction that already failed.
|
||||
func TestOldAndNewAreDifferentAnswers(t *testing.T) {
|
||||
old := pythonVerdict(3, minMinor-1, true)
|
||||
fresh := pythonVerdict(3, maxMinor+1, true)
|
||||
if old == fresh {
|
||||
t.Fatalf("3.%d and 3.%d both reported %q", minMinor-1, maxMinor+1, old)
|
||||
}
|
||||
if old != verdictTooOld {
|
||||
t.Errorf("3.%d = %q, want %q", minMinor-1, old, verdictTooOld)
|
||||
}
|
||||
}
|
||||
|
||||
// Every version in the range is accepted, so the window this program claims
|
||||
// to support is the one it actually uses.
|
||||
func TestTheWholeSupportedRangeIsAccepted(t *testing.T) {
|
||||
for m := minMinor; m <= maxMinor; m++ {
|
||||
if got := pythonVerdict(3, m, true); got != verdictOK {
|
||||
t.Errorf("3.%d = %q, want %q", m, got, verdictOK)
|
||||
}
|
||||
}
|
||||
if minMinor > maxMinor {
|
||||
t.Fatal("the supported range is empty; nothing would ever be chosen")
|
||||
}
|
||||
}
|
||||
|
||||
// A major version nobody has tested against is not something to guess at, and
|
||||
// an unreadable version string is not a working interpreter.
|
||||
func TestUnknownVersionsAreNotAccepted(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
major, minor int
|
||||
parsed bool
|
||||
}{
|
||||
{"python 4", 4, 0, true},
|
||||
{"python 2", 2, 7, true},
|
||||
{"unparseable", 0, 0, false},
|
||||
} {
|
||||
if got := pythonVerdict(c.major, c.minor, c.parsed); got == verdictOK {
|
||||
t.Errorf("%s was accepted", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An environment already on disk is reused, and that is right until the Python
|
||||
// inside it is one this build cannot use.
|
||||
//
|
||||
// It was reused unconditionally, which would have defeated the ceiling above
|
||||
// on the exact machine that found the bug: that Mac already had a runtime
|
||||
// built by Python 3.14, left behind by the run that failed. Setup would pick a
|
||||
// good interpreter, find the 3.14 environment, keep it, and die in the same
|
||||
// clang error as before - a fix defeated by the wreckage of the bug it fixes.
|
||||
//
|
||||
// Real environments, not a fake: the thing under test is what an interpreter
|
||||
// on disk reports about itself.
|
||||
func TestAnUnsupportedEnvironmentIsNotReused(t *testing.T) {
|
||||
py, _, err := findPython()
|
||||
if err != nil {
|
||||
t.Skipf("no supported Python on this machine: %v", err)
|
||||
}
|
||||
venv := filepath.Join(t.TempDir(), "runtime")
|
||||
if err := makeVenv(py, venv); err != nil {
|
||||
t.Fatalf("makeVenv: %v", err)
|
||||
}
|
||||
if ok, ver := venvUsable(venv); !ok {
|
||||
t.Fatalf("an environment built from the interpreter setup just chose "+
|
||||
"reported itself unusable (%s)", ver)
|
||||
}
|
||||
|
||||
// The two states that must not be confused with a working one.
|
||||
empty := filepath.Join(t.TempDir(), "gone")
|
||||
if ok, _ := venvUsable(empty); ok {
|
||||
t.Error("a missing environment was reported usable")
|
||||
}
|
||||
broken := filepath.Join(t.TempDir(), "broken")
|
||||
if err := os.MkdirAll(filepath.Dir(venvPython(broken)), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(venvPython(broken), []byte("not an interpreter"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, ver := venvUsable(broken); ok {
|
||||
t.Errorf("a half-created environment was reported usable (%s)", ver)
|
||||
}
|
||||
}
|
||||
BIN
agent/cmd/behavision-setup/rsrc_windows_amd64.syso
Normal file
@@ -3,9 +3,12 @@ module github.com/loyaly/behavision-agent
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.3 // indirect
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.3
|
||||
golang.org/x/sys v0.20.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
golang.org/x/net v0.8.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
)
|
||||
|
||||
@@ -81,6 +81,7 @@ usage: %s <command>
|
||||
// agent.json - which is the state that screen was built to end.
|
||||
func cmdClaim(args []string) error {
|
||||
if len(args) == 0 {
|
||||
//lint:ignore ST1005 usage text read by a person, never wrapped
|
||||
return fmt.Errorf("usage: behavision-agent claim <installation code>\n" +
|
||||
"Ask whoever manages your shops for one - they can create it from\n" +
|
||||
"the Behavision platform, under the shop.")
|
||||
@@ -119,6 +120,7 @@ func cmdClaim(args []string) error {
|
||||
cfg.BrokerPassword = b.MQTTPass
|
||||
cfg.AgentToken = b.AgentToken
|
||||
cfg.CloudBase = base
|
||||
cfg.SessionToken, cfg.SessionRefresh, cfg.SessionEmail = "", "", ""
|
||||
caPath, err := enrol.SaveCA(b.CACert, paths.BrokerCA())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -160,6 +162,7 @@ func cmdStatus() error {
|
||||
// which is the default. Reading it here is what stops every call the agent
|
||||
// makes to the engine coming back 401 on a stock install.
|
||||
cfg = cfg.WithEngineCredentials(paths.APICredentials())
|
||||
creds := config.NewCreds(paths.APICredentials(), cfg.APIUser, cfg.APIPassword)
|
||||
q, err := spool.Open(paths.SpoolDir(), cfg.SpoolMax)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -168,7 +171,7 @@ func cmdStatus() error {
|
||||
Command: func(context.Context) *exec.Cmd { return nil },
|
||||
HealthURL: strings.TrimRight(cfg.APIBase, "/") + "/api/health",
|
||||
StatsURL: strings.TrimRight(cfg.APIBase, "/") + "/api/stats",
|
||||
User: cfg.APIUser, Password: cfg.APIPassword,
|
||||
User: cfg.APIUser, Password: cfg.APIPassword, Creds: creds,
|
||||
})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
@@ -203,6 +206,7 @@ func cmdRun() error {
|
||||
// which is the default. Reading it here is what stops every call the agent
|
||||
// makes to the engine coming back 401 on a stock install.
|
||||
cfg = cfg.WithEngineCredentials(paths.APICredentials())
|
||||
creds := config.NewCreds(paths.APICredentials(), cfg.APIUser, cfg.APIPassword)
|
||||
// Opened before the engine starts: detections arriving in the first second
|
||||
// must have somewhere to land.
|
||||
q, err := spool.Open(paths.SpoolDir(), cfg.SpoolMax)
|
||||
@@ -228,6 +232,14 @@ func cmdRun() error {
|
||||
sup := engine.New(engine.Options{
|
||||
Command: func(ctx context.Context) *exec.Cmd {
|
||||
cmd := exec.CommandContext(ctx, exe, cfg.EngineArgs...)
|
||||
// Run the engine FROM a known directory rather than from whatever
|
||||
// happened to launch us. A double-clicked bundle hands its child
|
||||
// "/", and an engine invoked as `-m behavision` then cannot find
|
||||
// itself - measured on macOS, where it retried forever.
|
||||
cmd.Dir = cfg.EngineDir
|
||||
if cmd.Dir == "" {
|
||||
cmd.Dir = paths.InstallRoot()
|
||||
}
|
||||
// How the engine learns where to send detections. The engine's
|
||||
// config already reads `events.webhook_url: ${BEHAVISION_WEBHOOK_URL}`
|
||||
// and python-dotenv does not override a variable the process
|
||||
@@ -243,7 +255,7 @@ func cmdRun() error {
|
||||
LogWriter: logFile,
|
||||
HealthURL: strings.TrimRight(cfg.APIBase, "/") + "/api/health",
|
||||
StatsURL: strings.TrimRight(cfg.APIBase, "/") + "/api/stats",
|
||||
User: cfg.APIUser, Password: cfg.APIPassword,
|
||||
User: cfg.APIUser, Password: cfg.APIPassword, Creds: creds,
|
||||
})
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(),
|
||||
@@ -278,6 +290,7 @@ func cmdRun() error {
|
||||
cloud := cameras.NewCloudClient(cfg.CloudBase, cfg.AgentToken)
|
||||
cloud.Upload = uploader.UploadBytes
|
||||
eng := cameras.NewEngineClient(cfg.APIBase, cfg.APIUser, cfg.APIPassword)
|
||||
eng.Creds = creds
|
||||
go cameras.New(eng, cloud, logger).Run(ctx)
|
||||
// The live relay, which uploads nothing until somebody at head office is
|
||||
// actually watching a camera.
|
||||
@@ -312,7 +325,7 @@ func cmdRun() error {
|
||||
cfg.ClientID, cfg.SiteID, cfg.BrokerURL)
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
BrokerURL: cfg.BrokerURL,
|
||||
ClientID: "behavision-" + cfg.ClientID + "-" + cfg.SiteID,
|
||||
ClientID: cfg.MQTTClientID(),
|
||||
Username: cfg.BrokerUsername, Password: cfg.BrokerPassword,
|
||||
CAFile: cfg.BrokerCAFile, Log: logger,
|
||||
})
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/loyaly/behavision-agent/pkg/bridge"
|
||||
"github.com/loyaly/behavision-agent/pkg/config"
|
||||
)
|
||||
|
||||
// EngineClient talks to the recognition engine on this PC's loopback.
|
||||
@@ -21,7 +22,12 @@ type EngineClient struct {
|
||||
Base string
|
||||
User string
|
||||
Password string
|
||||
Client *http.Client
|
||||
// Creds re-reads the engine's generated credential when one is rejected.
|
||||
// Without it a fresh install is 401 for the life of the process: the agent
|
||||
// starts the engine, and the engine writes its credential file seconds
|
||||
// after the agent has already read (and failed to find) it.
|
||||
Creds *config.Creds
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
func NewEngineClient(base, user, password string) *EngineClient {
|
||||
@@ -50,14 +56,24 @@ func (e *EngineClient) do(ctx context.Context, method, path string, body, out an
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if e.User != "" {
|
||||
req.SetBasicAuth(e.User, e.Password)
|
||||
user, pass := e.User, e.Password
|
||||
if e.Creds != nil {
|
||||
user, pass = e.Creds.Get()
|
||||
}
|
||||
if user != "" {
|
||||
req.SetBasicAuth(user, pass)
|
||||
}
|
||||
resp, err := e.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusUnauthorized && e.Creds != nil && e.Creds.Refresh() {
|
||||
// The engine generated its credential after we last looked. Read it
|
||||
// and try once more rather than failing for the life of the process.
|
||||
resp.Body.Close()
|
||||
return e.do(ctx, method, path, body, out)
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
// The engine's message, not just a status. "camera stored but failed to
|
||||
// start: connection refused" is something an operator can act on;
|
||||
|
||||
@@ -30,13 +30,12 @@ import (
|
||||
// argument, and it is why the wanted-check comes first and the push stops the
|
||||
// moment the server says the last viewer has gone.
|
||||
type Live struct {
|
||||
Engine *EngineClient
|
||||
Cloud *CloudClient
|
||||
Log *log.Logger
|
||||
FPS float64
|
||||
Width int
|
||||
Quality int
|
||||
pollDelay time.Duration
|
||||
Engine *EngineClient
|
||||
Cloud *CloudClient
|
||||
Log *log.Logger
|
||||
FPS float64
|
||||
Width int
|
||||
Quality int
|
||||
}
|
||||
|
||||
// Defaults, measured against the office camera rather than guessed.
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -37,7 +39,18 @@ type Config struct {
|
||||
BrokerCAFile string `json:"broker_ca_file"`
|
||||
|
||||
// Engine process.
|
||||
EngineExe string `json:"engine_exe"`
|
||||
EngineExe string `json:"engine_exe"`
|
||||
// EngineDir is the working directory the engine is launched IN. Empty
|
||||
// means the install root.
|
||||
//
|
||||
// It exists because nothing set it, so the engine inherited whatever
|
||||
// launched the app - and for an app started by double-clicking its
|
||||
// bundle that is "/", not anywhere useful. The symptom on macOS was
|
||||
// `python: No module named behavision` repeating forever: the dev engine
|
||||
// is `-m behavision`, which resolves against the working directory. The
|
||||
// same app started from a terminal in the repo worked, which is exactly
|
||||
// the shape of a bug that survives every test run by a developer.
|
||||
EngineDir string `json:"engine_dir,omitempty"`
|
||||
EngineArgs []string `json:"engine_args"`
|
||||
APIBase string `json:"api_base"`
|
||||
APIUser string `json:"api_user"`
|
||||
@@ -72,6 +85,10 @@ type Config struct {
|
||||
// Queue.
|
||||
SpoolMax int `json:"spool_max"`
|
||||
|
||||
// InstallID distinguishes THIS installation from every other one claimed
|
||||
// to the same site. See MQTTClientID.
|
||||
InstallID string `json:"install_id,omitempty"`
|
||||
|
||||
path string
|
||||
}
|
||||
|
||||
@@ -113,6 +130,14 @@ func Load(path string) (Config, error) {
|
||||
return cfg, fmt.Errorf("config %s: %w", path, err)
|
||||
}
|
||||
cfg.path = path
|
||||
// Minted on first load and written back, so an installation that predates
|
||||
// this field gets one without anybody doing anything. Best effort: a
|
||||
// read-only config still yields a working id for this run, it is simply
|
||||
// not the same one next time.
|
||||
if cfg.InstallID == "" {
|
||||
cfg.InstallID = newInstallID()
|
||||
_ = cfg.Save(path)
|
||||
}
|
||||
for _, field := range []*string{&cfg.BrokerPassword, &cfg.APIPassword,
|
||||
&cfg.SessionToken, &cfg.SessionRefresh, &cfg.AgentToken} {
|
||||
plain, err := reveal(*field)
|
||||
@@ -199,3 +224,41 @@ func reveal(stored string) (string, error) {
|
||||
}
|
||||
return string(plain), nil
|
||||
}
|
||||
|
||||
// MQTTClientID names this INSTALLATION, not this site.
|
||||
//
|
||||
// It was `behavision-<client>-<site>`, which is the same string on every
|
||||
// computer claimed to one shop. MQTT requires client ids to be unique and a
|
||||
// broker enforces it by disconnecting the older session when a new one
|
||||
// arrives with the same id - so two machines on one site take turns kicking
|
||||
// each other off, forever. Measured on a second Mac claimed to a live shop:
|
||||
//
|
||||
// broker connected / broker connection lost: EOF / broker connected / ...
|
||||
//
|
||||
// The damage is not confined to the new machine. The shop's own till is the
|
||||
// other half of that loop, so somebody signing in on a laptop to look at the
|
||||
// product stops the shop delivering visits - and nothing at either end says
|
||||
// why, because from each side it reads as an unstable network.
|
||||
//
|
||||
// The site stays in the id because it is what a broker log is read by, and
|
||||
// the random half is short for the same reason. `CleanSession(true)` means
|
||||
// there is no session state for a changed id to strand.
|
||||
func (c Config) MQTTClientID() string {
|
||||
id := c.InstallID
|
||||
if id == "" {
|
||||
// A config that could not be written still has to produce a UNIQUE
|
||||
// id, or this falls straight back into the collision it exists to
|
||||
// prevent. Per-run is the right failure: the connection works and the
|
||||
// only cost is a new name in the broker's log after a restart.
|
||||
id = newInstallID()
|
||||
}
|
||||
return "behavision-" + c.ClientID + "-" + c.SiteID + "-" + id
|
||||
}
|
||||
|
||||
func newInstallID() string {
|
||||
b := make([]byte, 4)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "x"
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// EngineCredentials reads the Basic credentials the engine generated for
|
||||
@@ -60,3 +61,60 @@ func (c Config) WithEngineCredentials(path string) Config {
|
||||
c.APIUser, c.APIPassword = EngineCredentials(path)
|
||||
return c
|
||||
}
|
||||
|
||||
// Creds resolves the engine's Basic credentials, re-reading the file when it
|
||||
// has none.
|
||||
//
|
||||
// Reading once at startup is wrong on a fresh install, and that is the case
|
||||
// that matters: the agent starts the engine, the engine generates its
|
||||
// credential and writes the file a few seconds later, and an agent that read
|
||||
// the file before that holds "" forever. Every call it makes - health, stats,
|
||||
// camera sync, the embedding for a visit - then comes back 401 for the life of
|
||||
// the process, on a brand new shop PC, with the tray showing a red engine that
|
||||
// is running perfectly. Measured on a fresh state directory: three 401s and no
|
||||
// camera ever reconciled.
|
||||
//
|
||||
// A configured credential is never re-read: an operator who set
|
||||
// BEHAVISION_API_USER means it.
|
||||
type Creds struct {
|
||||
path string
|
||||
mu sync.Mutex
|
||||
user string
|
||||
pass string
|
||||
fixed bool
|
||||
}
|
||||
|
||||
// NewCreds takes whatever the config already has. Non-empty means configured,
|
||||
// and is used unchanged.
|
||||
func NewCreds(path, user, password string) *Creds {
|
||||
c := &Creds{path: path, user: user, pass: password}
|
||||
c.fixed = user != "" || password != ""
|
||||
return c
|
||||
}
|
||||
|
||||
// Get returns the current pair, reading the file if it has nothing yet.
|
||||
func (c *Creds) Get() (string, string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.user == "" && !c.fixed {
|
||||
c.user, c.pass = EngineCredentials(c.path)
|
||||
}
|
||||
return c.user, c.pass
|
||||
}
|
||||
|
||||
// Refresh re-reads the file after a rejection and reports whether the pair
|
||||
// changed. Callers retry once when it did - which covers both the fresh-install
|
||||
// race and a credential the engine regenerated under a running agent.
|
||||
func (c *Creds) Refresh() bool {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.fixed {
|
||||
return false
|
||||
}
|
||||
u, p := EngineCredentials(c.path)
|
||||
if u == c.user && p == c.pass {
|
||||
return false
|
||||
}
|
||||
c.user, c.pass = u, p
|
||||
return u != ""
|
||||
}
|
||||
|
||||
77
agent/pkg/config/credentials_firstrun_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The sequence on a brand new shop PC, in order:
|
||||
//
|
||||
// agent starts -> file does not exist yet
|
||||
// agent starts the engine
|
||||
// engine generates its credential and writes the file
|
||||
// agent calls the engine -> must now succeed
|
||||
//
|
||||
// Read once at startup, the agent holds "" for the life of the process and
|
||||
// every engine call is 401: health, stats, camera sync, the embedding for a
|
||||
// visit. The tray shows a red engine that is running perfectly, and nothing
|
||||
// says why. Measured on a fresh state directory before this existed.
|
||||
func TestCredentialsArriveAfterTheAgentHasAlreadyLooked(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "api_credentials.txt")
|
||||
|
||||
creds := NewCreds(path, "", "") // nothing configured, file not there yet
|
||||
if u, _ := creds.Get(); u != "" {
|
||||
t.Fatalf("expected no credential before the engine has written one, got %q", u)
|
||||
}
|
||||
|
||||
// the engine starts and writes its credential
|
||||
if err := os.WriteFile(path, []byte("username=behavision\npassword=s3cret\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// a 401 makes the agent look again
|
||||
if !creds.Refresh() {
|
||||
t.Fatal("Refresh did not pick up the credential the engine just wrote")
|
||||
}
|
||||
u, p := creds.Get()
|
||||
if u != "behavision" || p != "s3cret" {
|
||||
t.Fatalf("got %q/%q", u, p)
|
||||
}
|
||||
}
|
||||
|
||||
// An operator who set BEHAVISION_API_USER means it, and a file must never
|
||||
// override them.
|
||||
func TestAConfiguredCredentialIsNeverReplacedByTheFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "api_credentials.txt")
|
||||
if err := os.WriteFile(path, []byte("username=generated\npassword=nope\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
creds := NewCreds(path, "chosen", "byhand")
|
||||
if u, p := creds.Get(); u != "chosen" || p != "byhand" {
|
||||
t.Fatalf("configured credential was replaced: %q/%q", u, p)
|
||||
}
|
||||
if creds.Refresh() {
|
||||
t.Fatal("Refresh overrode a configured credential")
|
||||
}
|
||||
}
|
||||
|
||||
// A credential the engine regenerates under a running agent is picked up too -
|
||||
// the same mechanism, and the reason paths.APICredentials says the agent reads
|
||||
// the file "rather than storing a second copy".
|
||||
func TestARegeneratedCredentialIsPickedUp(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "api_credentials.txt")
|
||||
os.WriteFile(path, []byte("username=behavision\npassword=old\n"), 0o600)
|
||||
creds := NewCreds(path, "", "")
|
||||
creds.Get()
|
||||
os.WriteFile(path, []byte("username=behavision\npassword=new\n"), 0o600)
|
||||
if !creds.Refresh() {
|
||||
t.Fatal("a regenerated password was not picked up")
|
||||
}
|
||||
if _, p := creds.Get(); p != "new" {
|
||||
t.Fatalf("still holding %q", p)
|
||||
}
|
||||
}
|
||||
88
agent/pkg/config/installid_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The bug this exists to prevent, measured on a second Mac claimed to a live
|
||||
// shop: MQTT requires client ids to be unique, and a broker enforces it by
|
||||
// disconnecting the older session when a new one arrives with the same id. The
|
||||
// id was `behavision-<client>-<site>` - identical on every computer claimed to
|
||||
// one shop - so the two took turns kicking each other off:
|
||||
//
|
||||
// broker connected / broker connection lost: EOF / broker connected / ...
|
||||
//
|
||||
// The damage is not confined to the new machine. The shop's own till is the
|
||||
// other half of that loop, so somebody signing in on a laptop to look at the
|
||||
// product stops the shop delivering visits.
|
||||
func TestTwoInstallsOnOneSiteGetDifferentClientIDs(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
one := writeClaimed(t, filepath.Join(dir, "a.json"))
|
||||
two := writeClaimed(t, filepath.Join(dir, "b.json"))
|
||||
|
||||
if one.MQTTClientID() == two.MQTTClientID() {
|
||||
t.Fatalf("both installs answered to %q; the broker will disconnect one "+
|
||||
"whenever the other connects", one.MQTTClientID())
|
||||
}
|
||||
}
|
||||
|
||||
// And the same install keeps its name across restarts, or a broker log is a
|
||||
// list of strangers and nobody can tell one till from a stream of new ones.
|
||||
func TestOneInstallKeepsItsClientIDAcrossRestarts(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "agent.json")
|
||||
first := writeClaimed(t, path)
|
||||
|
||||
again, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
if got, want := again.MQTTClientID(), first.MQTTClientID(); got != want {
|
||||
t.Errorf("after a restart the id was %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// The site stays in the id: it is what somebody reading a broker log is
|
||||
// reading FOR, and an opaque random string would make every connection
|
||||
// anonymous.
|
||||
func TestTheClientIDStillNamesTheShop(t *testing.T) {
|
||||
c := Config{ClientID: "tenext-retail", SiteID: "chennai", InstallID: "abcd1234"}
|
||||
id := c.MQTTClientID()
|
||||
for _, want := range []string{"tenext-retail", "chennai", "abcd1234"} {
|
||||
if !strings.Contains(id, want) {
|
||||
t.Errorf("client id %q does not contain %q", id, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A config that could not be written still has to produce a UNIQUE id, or a
|
||||
// read-only install falls straight back into the collision. Per-run is the
|
||||
// right failure: the connection works, and the only cost is a new name in the
|
||||
// broker's log after a restart.
|
||||
func TestAnUnsavedConfigStillGetsAUniqueID(t *testing.T) {
|
||||
a := Config{ClientID: "c", SiteID: "s"}
|
||||
b := Config{ClientID: "c", SiteID: "s"}
|
||||
if a.MQTTClientID() == b.MQTTClientID() {
|
||||
t.Fatal("two configs with no install id produced the same client id")
|
||||
}
|
||||
}
|
||||
|
||||
func writeClaimed(t *testing.T, path string) Config {
|
||||
t.Helper()
|
||||
cfg := Defaults()
|
||||
cfg.ClientID, cfg.SiteID = "tenext-retail", "chennai"
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatalf("save: %v", err)
|
||||
}
|
||||
// Loading is what mints the id, so an installation that predates the
|
||||
// field gets one without anybody doing anything.
|
||||
got, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("load: %v", err)
|
||||
}
|
||||
if got.InstallID == "" {
|
||||
t.Fatal("loading a config without an install id did not mint one")
|
||||
}
|
||||
return got
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -29,6 +30,31 @@ import (
|
||||
// told apart from corruption instead of failing as "authentication failed".
|
||||
const magic = "BVDEMO1\n"
|
||||
|
||||
// Payload is what a sealed bundle carries. Two demo shapes exist:
|
||||
//
|
||||
// - cameras only: the PC runs on its own with these cameras (the first
|
||||
// demo build);
|
||||
// - an enrolment code: the PC claims a real shop at head office and gets
|
||||
// its cameras from there, exactly as a customer install would, so the
|
||||
// demo exercises the whole product rather than a local copy of it. The
|
||||
// code is single-use, so one bundle is one install.
|
||||
//
|
||||
// A bundle from the first build is a bare JSON array; Decode accepts both.
|
||||
type Payload struct {
|
||||
Cameras []Camera `json:"cameras,omitempty"`
|
||||
EnrolCode string `json:"enrol_code,omitempty"`
|
||||
CloudBase string `json:"cloud_base,omitempty"`
|
||||
}
|
||||
|
||||
// Decode reads either payload shape.
|
||||
func Decode(plain []byte) (Payload, error) {
|
||||
var p Payload
|
||||
if len(plain) > 0 && plain[0] == '[' {
|
||||
return p, json.Unmarshal(plain, &p.Cameras)
|
||||
}
|
||||
return p, json.Unmarshal(plain, &p)
|
||||
}
|
||||
|
||||
// Camera is one entry as the engine's Add Camera endpoint accepts it.
|
||||
type Camera struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -21,7 +21,12 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/loyaly/behavision-agent/pkg/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -56,6 +61,9 @@ type Options struct {
|
||||
// no captured output is undiagnosable, which on a customer site means a
|
||||
// site visit.
|
||||
LogWriter io.Writer
|
||||
// Creds re-reads the engine's generated credential when one is rejected,
|
||||
// which is the ordinary case on a first run.
|
||||
Creds *config.Creds
|
||||
// HealthURL, StatsURL, User, Password address the engine's own API.
|
||||
HealthURL string
|
||||
StatsURL string
|
||||
@@ -76,6 +84,7 @@ type Supervisor struct {
|
||||
restarts int
|
||||
cancel context.CancelFunc
|
||||
done chan struct{}
|
||||
progress Progress
|
||||
}
|
||||
|
||||
func New(opts Options) *Supervisor {
|
||||
@@ -109,6 +118,37 @@ func (s *Supervisor) Start() {
|
||||
go s.supervise(ctx, done)
|
||||
}
|
||||
|
||||
// Progress is what the engine is busy with before it answers - on first run,
|
||||
// downloading ~275 MB of models. Empty once the engine is up.
|
||||
type Progress struct {
|
||||
What string `json:"what"`
|
||||
Percent int `json:"percent"`
|
||||
}
|
||||
|
||||
var progressRe = regexp.MustCompile(`download: (.+?) (\d{1,3})%`)
|
||||
|
||||
func (s *Supervisor) noteProgress(line string) {
|
||||
m := progressRe.FindStringSubmatch(line)
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
pct, _ := strconv.Atoi(m[2])
|
||||
s.mu.Lock()
|
||||
if pct >= 100 {
|
||||
s.progress = Progress{}
|
||||
} else {
|
||||
s.progress = Progress{What: m[1], Percent: pct}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// Progress reports the current first-run download, if any.
|
||||
func (s *Supervisor) Progress() Progress {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.progress
|
||||
}
|
||||
|
||||
// Stop asks the engine to exit and waits for it.
|
||||
func (s *Supervisor) Stop() {
|
||||
s.mu.Lock()
|
||||
@@ -204,6 +244,13 @@ func (s *Supervisor) runOnce(ctx context.Context) error {
|
||||
}
|
||||
return kill()
|
||||
}
|
||||
// Cancel sends the kill; WaitDelay bounds how long Wait() then waits for
|
||||
// the output pipes to close. Without it Wait() blocks until every writer
|
||||
// is gone - and Stop() blocks on Wait() - so one grandchild still holding
|
||||
// the engine's stdout hangs Stop FOREVER, which on the desktop app means
|
||||
// the tray's Quit never returns. stopGrace was declared for exactly this
|
||||
// and never wired to anything; staticcheck found it as an unused const.
|
||||
cmd.WaitDelay = stopGrace
|
||||
prepare(cmd)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("engine failed to start: %w", err)
|
||||
@@ -218,18 +265,35 @@ func (s *Supervisor) runOnce(ctx context.Context) error {
|
||||
kill = k
|
||||
defer release()
|
||||
|
||||
// The last few lines the engine printed travel with the failure, because
|
||||
// "engine exited: exit status 1" sends somebody to a log file on a shop
|
||||
// PC, and the one line that matters - "port 8010 is already in use" - was
|
||||
// right there.
|
||||
var tailMu sync.Mutex
|
||||
var tail []string
|
||||
pumped := make(chan struct{})
|
||||
go func() {
|
||||
defer close(pumped)
|
||||
sc := bufio.NewScanner(stdout)
|
||||
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
for sc.Scan() {
|
||||
fmt.Fprintln(s.opts.LogWriter, sc.Text())
|
||||
line := sc.Text()
|
||||
fmt.Fprintln(s.opts.LogWriter, line)
|
||||
s.noteProgress(line)
|
||||
tailMu.Lock()
|
||||
tail = append(tail, line)
|
||||
if len(tail) > 12 {
|
||||
tail = tail[1:]
|
||||
}
|
||||
tailMu.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
s.setState(Running, nil)
|
||||
waitErr := cmd.Wait()
|
||||
s.mu.Lock()
|
||||
s.progress = Progress{}
|
||||
s.mu.Unlock()
|
||||
<-pumped
|
||||
|
||||
// A context cancel terminates the child through exec's own handling; the
|
||||
@@ -238,6 +302,12 @@ func (s *Supervisor) runOnce(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
if waitErr != nil {
|
||||
tailMu.Lock()
|
||||
reason := explain(tail)
|
||||
tailMu.Unlock()
|
||||
if reason != "" {
|
||||
return fmt.Errorf("%s (%v)", reason, waitErr)
|
||||
}
|
||||
return fmt.Errorf("engine exited: %w", waitErr)
|
||||
}
|
||||
return errors.New("engine exited unexpectedly with status 0")
|
||||
@@ -351,14 +421,24 @@ func (s *Supervisor) getJSON(ctx context.Context, url string, out any) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.opts.User != "" {
|
||||
req.SetBasicAuth(s.opts.User, s.opts.Password)
|
||||
user, pass := s.opts.User, s.opts.Password
|
||||
if s.opts.Creds != nil {
|
||||
user, pass = s.opts.Creds.Get()
|
||||
}
|
||||
if user != "" {
|
||||
req.SetBasicAuth(user, pass)
|
||||
}
|
||||
resp, err := (&http.Client{Timeout: 5 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusUnauthorized && s.opts.Creds != nil && s.opts.Creds.Refresh() {
|
||||
// See cameras.EngineClient: on a fresh install the engine writes its
|
||||
// credential after the agent has already read for one.
|
||||
resp.Body.Close()
|
||||
return s.getJSON(ctx, url, out)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s returned %s", url, resp.Status)
|
||||
}
|
||||
@@ -373,3 +453,28 @@ func LogFile(path string) (*os.File, error) {
|
||||
}
|
||||
return os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
|
||||
}
|
||||
|
||||
// explain turns the engine's last output into the sentence the tray shows.
|
||||
// The cases are the ones seen on real installs; anything else shows the last
|
||||
// non-empty line verbatim.
|
||||
func explain(tail []string) string {
|
||||
last := ""
|
||||
for _, l := range tail {
|
||||
low := strings.ToLower(l)
|
||||
switch {
|
||||
case strings.Contains(low, "address already in use") || strings.Contains(low, "only one usage of each socket address"):
|
||||
return "port 8010 is already in use - another Behavision or its engine is still running"
|
||||
case strings.Contains(low, "no module named behavision"):
|
||||
return "the engine is not installed in this Python - run behavision-setup again"
|
||||
case strings.Contains(low, "modulenotfounderror") || strings.Contains(low, "importerror"):
|
||||
return "the engine is missing a library - run behavision-setup again"
|
||||
}
|
||||
if strings.TrimSpace(l) != "" {
|
||||
last = strings.TrimSpace(l)
|
||||
}
|
||||
}
|
||||
if len(last) > 120 {
|
||||
last = last[:120] + "…"
|
||||
}
|
||||
return last
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
neturl "net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -225,7 +224,7 @@ func checkTransport(raw string) error {
|
||||
// url.Parse, not hand-rolled splitting: an IPv6 literal is bracketed and
|
||||
// full of colons, so scanning for the first ":" turns "[::1]:1883" into
|
||||
// "[" and refuses a perfectly good loopback address.
|
||||
u, err := neturl.Parse(raw)
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mqtt: cannot parse broker url %q: %w", raw, err)
|
||||
}
|
||||
|
||||
@@ -92,7 +92,11 @@ func TestPublishOnADeadClientErrorsRatherThanPanics(t *testing.T) {
|
||||
// The pump calls this on every tick; a nil-client panic would take the
|
||||
// whole agent down instead of backing off.
|
||||
c := &Client{}
|
||||
if err := c.Publish(nil, "t", []byte("{}")); err == nil { //nolint:staticcheck
|
||||
// The nil context is the POINT: the pump must not panic on a client that
|
||||
// never connected. //nolint is golangci-lint's directive and staticcheck
|
||||
// ignores it, which is why this kept being reported.
|
||||
//lint:ignore SA1012 passing nil is what is under test
|
||||
if err := c.Publish(nil, "t", []byte("{}")); err == nil {
|
||||
t.Fatal("publish on an unconnected client reported success")
|
||||
}
|
||||
if c.Connected() {
|
||||
|
||||
@@ -205,14 +205,3 @@ func (p *Pump) logf(format string, args ...any) {
|
||||
p.Log.Printf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func sleep(ctx context.Context, d time.Duration) bool {
|
||||
t := time.NewTimer(d)
|
||||
defer t.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-t.C:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,12 @@ func APICredentials() string {
|
||||
return filepath.Join(StateRoot(), "data", "api_credentials.txt")
|
||||
}
|
||||
|
||||
// CamerasFile is the engine's own camera store. The agent never edits it -
|
||||
// cameras go through the engine's API so passwords are sealed - but setup
|
||||
// removes it when a PC joins a shop, because head office is the source of
|
||||
// truth from then on.
|
||||
func CamerasFile() string { return filepath.Join(StateRoot(), "data", "cameras.json") }
|
||||
|
||||
// EnsureState creates the writable tree. Called before anything opens a file
|
||||
// under it, so a first run on a fresh machine does not fail on a missing dir.
|
||||
func EnsureState() error {
|
||||
|
||||
BIN
agent/rsrc_windows_amd64.syso
Normal file
@@ -4,4 +4,15 @@ Pipeline: capture -> detect (YuNet) -> track (IoU) -> align + encode
|
||||
(ArcFace ONNX) -> match / auto-enroll (FAISS + SQLite) -> events + API.
|
||||
"""
|
||||
|
||||
__version__ = "1.0.0"
|
||||
# Stamped by release.sh from the git tag, into a copy of this line, so a
|
||||
# release always produces a wheel nobody has installed before. It was a frozen
|
||||
# "1.0.0" here and a frozen "1.1.0" in pyproject.toml - two literals that
|
||||
# disagreed with each other and tracked nothing - which is how every engine fix
|
||||
# for several releases silently failed to reach a machine that had run setup
|
||||
# once. pip skips a wheel whose version is already installed and says so, one
|
||||
# line above setup printing "[ok] Engine and dependencies installed".
|
||||
#
|
||||
# In a checkout it stays obviously a checkout: "0.0.0+dev" on /api/health is
|
||||
# the truth about a developer's machine, and a plausible-looking number there
|
||||
# would be worse than none.
|
||||
__version__ = "0.0.0+dev"
|
||||
|
||||
@@ -21,6 +21,17 @@ def cmd_run(args: argparse.Namespace) -> int:
|
||||
|
||||
cfg = load_config(args.config)
|
||||
setup_logging(cfg.app.log_level, cfg.app.data_dir)
|
||||
if cfg.app.detect_threads > 0:
|
||||
# OpenCV sizes its pool for one big job on an idle machine. This is a
|
||||
# small job repeated forever on a machine also running the recogniser,
|
||||
# the tracker and possibly three other cameras, so the default costs
|
||||
# twice the CPU for no useful latency. Measured: 31 ms CPU/frame at the
|
||||
# default against 15 ms at one thread, for 6 ms more wall time against
|
||||
# a 66 ms budget.
|
||||
import cv2
|
||||
cv2.setNumThreads(cfg.app.detect_threads)
|
||||
log.info("detection threads: %d (OpenCV default was %d)",
|
||||
cfg.app.detect_threads, cv2.getNumThreads())
|
||||
missing = setup_models(cfg.app.models_dir)
|
||||
if missing:
|
||||
log.error("required models missing: %s", ", ".join(missing))
|
||||
|
||||
@@ -17,6 +17,7 @@ from fastapi.responses import HTMLResponse, Response, StreamingResponse
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from . import __version__
|
||||
from .config import ApiSection, CameraConfig, CameraTuning
|
||||
from .commission import CommissionRun
|
||||
from .events import Event
|
||||
@@ -142,7 +143,7 @@ def _reencode(jpeg: bytes, width: int, quality: int) -> "bytes | None":
|
||||
|
||||
|
||||
def create_app(engine: Engine) -> FastAPI:
|
||||
app = FastAPI(title="Behavision", version="1.0.0",
|
||||
app = FastAPI(title="Behavision", version=__version__,
|
||||
dependencies=_auth_dependencies(engine.cfg.api))
|
||||
|
||||
def worker_or_404(camera_id: str):
|
||||
@@ -155,11 +156,29 @@ def create_app(engine: Engine) -> FastAPI:
|
||||
def dashboard() -> str:
|
||||
return (_STATIC / "dashboard.html").read_text(encoding="utf-8")
|
||||
|
||||
@app.get("/static/favicon.png")
|
||||
def favicon() -> Response:
|
||||
# The one static asset besides the page itself. Served explicitly
|
||||
# rather than mounting the directory: nothing else in there is meant
|
||||
# to be reachable, and a mount would make that a matter of what lands
|
||||
# in the folder.
|
||||
return Response((_STATIC / "favicon.png").read_bytes(), media_type="image/png",
|
||||
headers={"cache-control": "public, max-age=86400"})
|
||||
|
||||
@app.get("/api/health")
|
||||
def health() -> dict:
|
||||
from .paths import describe
|
||||
# A gallery the running encoder cannot read is the failure most
|
||||
# worth catching from outside: the process is healthy, the cameras
|
||||
# are up, and the shop recognises nobody it already knows.
|
||||
stranded = engine.gallery.health["stranded"]
|
||||
return {"status": "ok" if engine.started_at else "starting",
|
||||
# Which build is actually running. Without it there was no way
|
||||
# to tell a shop PC three releases behind from a current one -
|
||||
# which is precisely how a silent install failure survived.
|
||||
"version": __version__,
|
||||
"recognition_model": engine.encoder.model_name,
|
||||
"gallery_unreadable_embeddings": stranded,
|
||||
# "where is my database" must be answerable from the API: the
|
||||
# tray, the installer and support all need it, and installed
|
||||
# it is not next to the code.
|
||||
@@ -323,6 +342,17 @@ def create_app(engine: Engine) -> FastAPI:
|
||||
worker.commission.cancel()
|
||||
return {"cancelled": camera_id}
|
||||
|
||||
@app.get("/api/cameras/discover")
|
||||
def discover_cameras() -> dict:
|
||||
"""Cameras on this PC's network, for the add-camera form to pick from.
|
||||
|
||||
A sync def so FastAPI runs it in the threadpool: it holds a socket
|
||||
open for a couple of seconds and sweeps a /24, and the event loop
|
||||
must keep serving the live picture meanwhile.
|
||||
"""
|
||||
from .discover import discover
|
||||
return discover()
|
||||
|
||||
@app.post("/api/cameras/test")
|
||||
def test_camera(payload: CameraPayload) -> dict:
|
||||
"""Try a camera WITHOUT saving it - the UI's Test button.
|
||||
|
||||
@@ -20,7 +20,26 @@ log = logging.getLogger(__name__)
|
||||
# Set before OpenCV loads ffmpeg, which reads this once.
|
||||
#
|
||||
# rtsp_transport=tcp: UDP is the default and silently drops frames on lossy
|
||||
# Wi-Fi. stimeout: a 5s socket timeout so a dead camera is noticed.
|
||||
# Wi-Fi.
|
||||
#
|
||||
# The timeout here is NOT what bounds a dead camera, and the comment that
|
||||
# once said it did was wrong. Measured against OpenCV 4.11 / FFmpeg 7.1 on a
|
||||
# socket that accepts the connection and then says nothing:
|
||||
#
|
||||
# stimeout;5000000 -> 30.0s timeout;5000000 -> 30.0s
|
||||
# stimeout;2000000 -> 30.5s timeout;2000000 -> 30.4s
|
||||
# no timeout option at all -> 30.3s
|
||||
#
|
||||
# Identical with the option absent, so it is not being honoured under either
|
||||
# name through this path. `stimeout` was renamed `timeout` in FFmpeg 5.0, and
|
||||
# neither reaches the RTSP protocol here. What actually bounds it is
|
||||
# OpenCV's own interrupt callback (30s for open, 30s for read), which is a
|
||||
# compile-time constant we do not control.
|
||||
#
|
||||
# Both names are still set, because on a build where they DO take effect the
|
||||
# shorter bound is what we want and an unrecognised option is ignored. But
|
||||
# nothing may depend on it: a wrong address is caught by _tcp_reachable
|
||||
# below, in code we own, in under a second.
|
||||
#
|
||||
# fflags=nobuffer and flags=low_delay: without them ffmpeg's RTSP demuxer
|
||||
# holds a comfortable queue of frames before handing over the first, which
|
||||
@@ -30,10 +49,97 @@ log = logging.getLogger(__name__)
|
||||
# reorder wait for the same reason.
|
||||
os.environ.setdefault(
|
||||
"OPENCV_FFMPEG_CAPTURE_OPTIONS",
|
||||
"rtsp_transport;tcp|stimeout;5000000|fflags;nobuffer|flags;low_delay|max_delay;200000",
|
||||
"rtsp_transport;tcp|stimeout;5000000|timeout;5000000"
|
||||
"|fflags;nobuffer|flags;low_delay|max_delay;200000",
|
||||
)
|
||||
|
||||
|
||||
# A stream can stay open and stop delivering. OpenCV breaks a blocked read
|
||||
# after 30s and we reconnect, but for those 30s `connected` is True and the
|
||||
# camera is dead — and a stream that trickles a frame every 20s never trips
|
||||
# that timeout at all, so it never reconnects and never recovers either.
|
||||
#
|
||||
# 10s is not a preference. The tracker gives up on a face after `max_misses`
|
||||
# (25 frames, ~1.7s at 15 fps), so by 10s every track is long gone and 150
|
||||
# frames are missing: whatever this is, it is not something recognition can
|
||||
# work with. Reported separately from `connected` because the two need
|
||||
# opposite actions — one says check the network, the other says the camera
|
||||
# is answering but sending nothing.
|
||||
STALL_AFTER_S = 10.0
|
||||
|
||||
|
||||
def _local_ipv4() -> str:
|
||||
"""This machine's address on the interface holding the default route.
|
||||
|
||||
A UDP socket is `connect`ed and nothing is sent - it only fixes a route so
|
||||
the kernel will name the source address. No packet leaves, and it needs no
|
||||
dependency, which matters in an engine that already ships 200 MB of models.
|
||||
"""
|
||||
import socket
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.settimeout(0.5)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
return s.getsockname()[0]
|
||||
except OSError:
|
||||
return ""
|
||||
|
||||
|
||||
def _wrong_network_hint(host: str) -> str:
|
||||
"""Why a private camera address is unreachable, when that is the reason.
|
||||
|
||||
A camera lives on the shop's LAN behind a router, and 192.168.x.x means
|
||||
"something on the network I am attached to" - nothing more. From mobile
|
||||
data, a hotel, or head office it either resolves to nobody or to a
|
||||
completely different device that happens to hold that number. There is no
|
||||
route in from the internet and there must not be: an RTSP camera reachable
|
||||
from outside is how a shop's cameras end up being watched by strangers.
|
||||
|
||||
Without this the answer was "cannot reach 192.168.1.121:554 - Operation
|
||||
timed out", which reads as a broken camera and sends somebody to re-type
|
||||
an address and a password that were always correct. Asked directly by the
|
||||
owner, about his own cameras, from his phone's connection.
|
||||
|
||||
Two states, two different actions, so they must not share a sentence: on
|
||||
the same network the camera or its address is the problem; on a different
|
||||
one the COMPUTER is in the wrong place and no setting will fix it.
|
||||
"""
|
||||
import ipaddress
|
||||
try:
|
||||
addr = ipaddress.ip_address(host)
|
||||
except ValueError:
|
||||
return "" # a DNS name; nothing can be concluded from the string
|
||||
# The RFC1918 blocks and link-local, spelled out rather than `is_private`.
|
||||
# That property is broader than "an address on somebody's LAN": it also
|
||||
# covers the carrier-grade NAT range and the documentation networks
|
||||
# (192.0.2, 198.51.100, 203.0.113), and telling somebody who typed one of
|
||||
# those that it is "on the shop's own network" would be a confident wrong
|
||||
# answer in the place people look first. Found by a test using 203.0.113.9
|
||||
# as an example of a PUBLIC address, which `is_private` calls private.
|
||||
lan = any(addr in ipaddress.ip_network(n) for n in
|
||||
("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "169.254.0.0/16")
|
||||
if addr.version == 4)
|
||||
if not lan:
|
||||
return ""
|
||||
|
||||
mine = _local_ipv4()
|
||||
if not mine:
|
||||
return (" - that is a private address, reachable only from inside "
|
||||
"the network the camera is on")
|
||||
try:
|
||||
same = ipaddress.ip_network(f"{mine}/24", strict=False).supernet_of(
|
||||
ipaddress.ip_network(f"{host}/24", strict=False))
|
||||
except (ValueError, TypeError):
|
||||
same = False
|
||||
if same:
|
||||
return (f" - this computer is on that network ({mine}), so check the "
|
||||
f"camera is powered on and that {host} is its address")
|
||||
return (f" - this computer is on {mine}, not the camera's network. A "
|
||||
f"private address like {host} is only reachable from inside the "
|
||||
f"shop's own network, never over the internet or mobile data, so "
|
||||
f"recognition has to run on a computer in the shop")
|
||||
|
||||
|
||||
def _tcp_reachable(source: "str | int", timeout: float
|
||||
) -> "tuple[bool, str]":
|
||||
"""Cheap pre-flight for an rtsp:// URL. Non-URL sources pass through."""
|
||||
@@ -51,10 +157,11 @@ def _tcp_reachable(source: "str | int", timeout: float
|
||||
return True, ""
|
||||
except socket.timeout:
|
||||
return False, (f"no response from {parsed.hostname}:{port} within "
|
||||
f"{timeout:.0f}s - check the IP address and that the "
|
||||
f"camera is on the same network")
|
||||
f"{timeout:.0f}s{_wrong_network_hint(parsed.hostname)}")
|
||||
except OSError as exc:
|
||||
return False, f"cannot reach {parsed.hostname}:{port} - {exc.strerror or exc}"
|
||||
return False, (f"cannot reach {parsed.hostname}:{port} - "
|
||||
f"{exc.strerror or exc}"
|
||||
f"{_wrong_network_hint(parsed.hostname)}")
|
||||
|
||||
|
||||
def _fourcc(cap) -> str:
|
||||
@@ -169,6 +276,10 @@ class VideoSource(threading.Thread):
|
||||
self.frames_total = 0
|
||||
self.reconnects = 0
|
||||
self._ever_connected = False
|
||||
# Why the last open failed, in the words an installer can act on.
|
||||
# Without it a camera that never connects reports only `connected:
|
||||
# false`, which cannot distinguish a wrong IP from a wrong password.
|
||||
self.last_error = ""
|
||||
|
||||
# -- public ---------------------------------------------------------
|
||||
def latest(self) -> "tuple[Optional[np.ndarray], float]":
|
||||
@@ -199,11 +310,23 @@ class VideoSource(threading.Thread):
|
||||
def stop(self) -> None:
|
||||
self._stopping.set()
|
||||
|
||||
def stalled(self) -> bool:
|
||||
"""Open, but not delivering. See STALL_AFTER_S."""
|
||||
if not self.connected or not self._frame_ts:
|
||||
return False
|
||||
return (time.time() - self._frame_ts) > STALL_AFTER_S
|
||||
|
||||
def stats(self) -> dict:
|
||||
return {
|
||||
"camera_id": self.camera_id,
|
||||
"url": self._display_url,
|
||||
"connected": self.connected,
|
||||
# Connected AND delivering. `connected` alone stays true through
|
||||
# a stall, so it is the wrong thing for a dashboard to colour a
|
||||
# camera green on.
|
||||
"streaming": self.connected and not self.stalled(),
|
||||
"stalled": self.stalled(),
|
||||
"last_error": self.last_error,
|
||||
"frames_total": self.frames_total,
|
||||
"reconnects": self.reconnects,
|
||||
"last_frame_age_s": round(time.time() - self._frame_ts, 1)
|
||||
@@ -260,6 +383,19 @@ class VideoSource(threading.Thread):
|
||||
log.info("[%s] capture stopped", self.camera_id)
|
||||
|
||||
def _open(self) -> Optional[cv2.VideoCapture]:
|
||||
# Pre-flight the socket, exactly as probe_source does. Without it a
|
||||
# camera that is off, moved or mistyped costs 30s per attempt inside
|
||||
# the VideoCapture constructor (measured; it is OpenCV's interrupt
|
||||
# timeout, not ours to shorten) — and the constructor is not
|
||||
# interruptible, so stop() cannot cut it short and a removed camera
|
||||
# leaves a daemon thread holding a socket for half a minute. A
|
||||
# refused or unroutable address answers in well under a second, which
|
||||
# is also what lets the backoff below mean what it says.
|
||||
reachable, why = _tcp_reachable(self._source, 2.0)
|
||||
if not reachable:
|
||||
log.debug("[%s] %s", self.camera_id, why)
|
||||
self.last_error = why
|
||||
return None
|
||||
try:
|
||||
if isinstance(self._source, int):
|
||||
cap = cv2.VideoCapture(self._source)
|
||||
@@ -268,8 +404,12 @@ class VideoSource(threading.Thread):
|
||||
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
|
||||
if not cap.isOpened():
|
||||
cap.release()
|
||||
self.last_error = ("reachable, but the stream would not open "
|
||||
"- check the path and credentials")
|
||||
return None
|
||||
self.last_error = ""
|
||||
return cap
|
||||
except cv2.error:
|
||||
log.exception("[%s] VideoCapture error", self.camera_id)
|
||||
self.last_error = "VideoCapture error - see the engine log"
|
||||
return None
|
||||
|
||||
@@ -132,6 +132,29 @@ class AppSection(BaseModel):
|
||||
# on changes what the system is under GDPR and India's DPDP, so it has to
|
||||
# be a decision somebody makes rather than one they inherit.
|
||||
store_faces: bool = False
|
||||
# How many threads OpenCV may use for detection. Measured on the office
|
||||
# camera (800x448 sub-stream): the default of 8 costs 31 ms of CPU per
|
||||
# frame for 8.9 ms of wall time, while ONE thread costs 15.3 ms of CPU for
|
||||
# 15.3 ms of wall - half the CPU for 6 ms more latency, against a 66 ms
|
||||
# frame budget at 15 fps. The default is wrong here because OpenCV sizes it
|
||||
# for one big job on an idle machine, and this is a small job repeated
|
||||
# forever on a machine also running the recogniser, the tracker and three
|
||||
# other cameras. 0 leaves OpenCV's own default alone.
|
||||
detect_threads: int = 1
|
||||
# Skip detection on frames where nothing has changed and nothing is being
|
||||
# tracked. A shop is empty most of the day and a frame of an empty room
|
||||
# costs exactly as much to search as a busy one. See CameraWorker.run for
|
||||
# why this cannot lose a face.
|
||||
motion_gate: bool = True
|
||||
# Mean absolute difference, 0-255, over a 160x90 greyscale thumbnail. 1.0
|
||||
# is well below the noise floor of a real camera - measured on this one,
|
||||
# an empty room varies by ~0.3 between frames - so it triggers on movement
|
||||
# rather than on sensor noise, and anything ambiguous detects.
|
||||
motion_threshold: float = 1.0
|
||||
# Detect at least this often regardless of the gate, so a change the
|
||||
# thumbnail cannot see - someone entering at the far edge, a slow lean into
|
||||
# frame - is still found within a second.
|
||||
motion_max_skip: int = 12
|
||||
|
||||
|
||||
class ApiSection(BaseModel):
|
||||
|
||||
206
behavision/discover.py
Normal file
@@ -0,0 +1,206 @@
|
||||
"""Find the cameras on the shop's network, so nobody has to type an address.
|
||||
|
||||
The add-camera form asked for an IP address, and a shop owner does not know
|
||||
their camera's IP address. It is on a sticker under the camera, if at all, or
|
||||
inside the camera's own app under a menu called something different for every
|
||||
make. That one field is where onboarding stopped for anyone who was not an
|
||||
installer.
|
||||
|
||||
Two probes, merged:
|
||||
|
||||
- **WS-Discovery** (ONVIF's discovery protocol): one multicast to
|
||||
239.255.255.250:3702 and every ONVIF camera on the LAN answers with its
|
||||
address and, usually, its make and model. Cheap, fast, and names the device
|
||||
- but only cameras that speak ONVIF answer, and some cheap ones do not.
|
||||
- **A TCP sweep of port 554** across the local /24: anything listening on the
|
||||
RTSP port is very probably a camera or a recorder. Names nothing, misses
|
||||
nothing that streams.
|
||||
|
||||
A host found by either is a candidate; one found by both is a camera with a
|
||||
name. The result is a list to pick from, not a decision: the person still
|
||||
supplies the password, and Test still proves the stream opens.
|
||||
|
||||
Stdlib only. This runs inside the engine, which ships as a small wheel, and a
|
||||
network-scanning dependency would be a large thing to add for two sockets.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import re
|
||||
import socket
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from typing import Iterable
|
||||
|
||||
# ONVIF WS-Discovery probe. The MessageID must be unique per probe; devices
|
||||
# ignore a repeat.
|
||||
_PROBE = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>'
|
||||
'<e:Envelope xmlns:e="http://www.w3.org/2003/05/soap-envelope" '
|
||||
'xmlns:w="http://schemas.xmlsoap.org/ws/2004/08/addressing" '
|
||||
'xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery" '
|
||||
'xmlns:dn="http://www.onvif.org/ver10/network/wsdl">'
|
||||
'<e:Header><w:MessageID>uuid:{mid}</w:MessageID>'
|
||||
'<w:To e:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To>'
|
||||
'<w:Action e:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>'
|
||||
'</e:Header><e:Body><d:Probe><d:Types>dn:NetworkVideoTransmitter</d:Types></d:Probe></e:Body>'
|
||||
'</e:Envelope>'
|
||||
)
|
||||
_MCAST = ("239.255.255.250", 3702)
|
||||
|
||||
# Makes we can name from an ONVIF scope or hostname, mapped to the ids the
|
||||
# camera-make picker uses so the form can preselect the stream path.
|
||||
_MAKES = (
|
||||
("hikvision", "hikvision"), ("hik", "hikvision"), ("dahua", "dahua"),
|
||||
("cp plus", "cpplus"), ("cpplus", "cpplus"), ("cp-plus", "cpplus"),
|
||||
("uniview", "uniview"), ("unv", "uniview"), ("tapo", "tplink"),
|
||||
("tp-link", "tplink"), ("reolink", "reolink"), ("amcrest", "amcrest"),
|
||||
("axis", "axis"),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Found:
|
||||
host: str
|
||||
rtsp: bool = False # port 554 answered
|
||||
onvif: bool = False # answered WS-Discovery
|
||||
name: str = "" # from ONVIF scopes, e.g. "Hikvision DS-2CD2043"
|
||||
make: str = "" # picker id, when it can be guessed
|
||||
onvif_url: str = ""
|
||||
sources: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def local_networks() -> list[ipaddress.IPv4Network]:
|
||||
"""The /24s this machine sits on, best effort and without dependencies.
|
||||
|
||||
Interface masks are not portable in the stdlib, so this assumes /24 - the
|
||||
shape of nearly every shop's router - for each local IPv4 address it can
|
||||
find. A bigger network would need a scan anyway that this should not run
|
||||
unasked.
|
||||
"""
|
||||
addrs: set[str] = set()
|
||||
try:
|
||||
# The address the OS would use to reach the internet: the LAN we care about.
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(0.5)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
addrs.add(s.getsockname()[0])
|
||||
s.close()
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
for a in socket.gethostbyname_ex(socket.gethostname())[2]:
|
||||
addrs.add(a)
|
||||
except OSError:
|
||||
pass
|
||||
nets = []
|
||||
for a in addrs:
|
||||
try:
|
||||
ip = ipaddress.IPv4Address(a)
|
||||
except ValueError:
|
||||
continue
|
||||
if ip.is_loopback or ip.is_link_local:
|
||||
continue
|
||||
nets.append(ipaddress.IPv4Network(f"{a}/24", strict=False))
|
||||
return sorted(set(nets), key=str)
|
||||
|
||||
|
||||
def ws_discover(timeout: float = 2.5) -> list[Found]:
|
||||
"""One ONVIF probe, every answer within `timeout` seconds."""
|
||||
out: dict[str, Found] = {}
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
|
||||
sock.settimeout(timeout)
|
||||
sock.sendto(_PROBE.format(mid=uuid.uuid4()).encode(), _MCAST)
|
||||
except OSError:
|
||||
return []
|
||||
import time
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
data, (host, _) = sock.recvfrom(65535)
|
||||
except socket.timeout:
|
||||
break
|
||||
except OSError:
|
||||
break
|
||||
f = parse_probe_match(data.decode("utf-8", "replace"), host)
|
||||
if f:
|
||||
out[f.host] = f
|
||||
sock.close()
|
||||
return list(out.values())
|
||||
|
||||
|
||||
_XADDR = re.compile(r"<[^>]*XAddrs[^>]*>([^<]+)<")
|
||||
_SCOPES = re.compile(r"<[^>]*Scopes[^>]*>([^<]+)<")
|
||||
|
||||
|
||||
def parse_probe_match(xml: str, host: str) -> Found | None:
|
||||
"""Pull the address and the human-readable scopes out of a ProbeMatch.
|
||||
|
||||
A regex rather than an XML parser on purpose: cameras emit every namespace
|
||||
prefix imaginable and some emit XML that is not quite well-formed, and the
|
||||
two fields wanted are flat text.
|
||||
"""
|
||||
xaddrs = _XADDR.search(xml)
|
||||
scopes = _SCOPES.search(xml)
|
||||
if not xaddrs and not scopes:
|
||||
return None
|
||||
url = xaddrs.group(1).split()[0] if xaddrs else ""
|
||||
# Prefer the host from the XAddrs URL: a device with several interfaces
|
||||
# answers from the one it heard us on, which is the one we can reach.
|
||||
m = re.match(r"https?://([^/:]+)", url)
|
||||
ip = m.group(1) if m else host
|
||||
f = Found(host=ip, onvif=True, onvif_url=url, sources=["onvif"])
|
||||
if scopes:
|
||||
words = []
|
||||
for s in scopes.group(1).split():
|
||||
if "/name/" in s or "/hardware/" in s:
|
||||
from urllib.parse import unquote
|
||||
words.append(unquote(s.rsplit("/", 1)[-1]))
|
||||
f.name = " ".join(dict.fromkeys(words)) # dedupe, keep order
|
||||
f.make = guess_make(f.name)
|
||||
return f
|
||||
|
||||
|
||||
def guess_make(text: str) -> str:
|
||||
low = text.lower()
|
||||
for needle, make in _MAKES:
|
||||
if needle in low:
|
||||
return make
|
||||
return ""
|
||||
|
||||
|
||||
def rtsp_sweep(nets: Iterable[ipaddress.IPv4Network], timeout: float = 0.5,
|
||||
workers: int = 128) -> list[str]:
|
||||
"""Every host in `nets` with port 554 open. ~254 hosts in about a second."""
|
||||
def probe(ip: str) -> str | None:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(timeout)
|
||||
try:
|
||||
return ip if s.connect_ex((ip, 554)) == 0 else None
|
||||
except OSError:
|
||||
return None
|
||||
finally:
|
||||
s.close()
|
||||
hosts = [str(h) for n in nets for h in n.hosts()]
|
||||
with ThreadPoolExecutor(max_workers=workers) as ex:
|
||||
return [ip for ip in ex.map(probe, hosts) if ip]
|
||||
|
||||
|
||||
def discover(timeout: float = 2.5) -> dict:
|
||||
"""Both probes, merged, as the API returns it."""
|
||||
nets = local_networks()
|
||||
found: dict[str, Found] = {f.host: f for f in ws_discover(timeout)}
|
||||
for ip in rtsp_sweep(nets):
|
||||
f = found.setdefault(ip, Found(host=ip))
|
||||
f.rtsp = True
|
||||
f.sources.append("rtsp")
|
||||
cams = sorted(found.values(), key=lambda f: (not (f.rtsp and f.onvif), not f.rtsp,
|
||||
ipaddress.IPv4Address(f.host)))
|
||||
return {
|
||||
"networks": [str(n) for n in nets],
|
||||
"cameras": [asdict(c) for c in cams],
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import numpy as np
|
||||
|
||||
from .attributes import AttributeEstimator, aggregate as aggregate_attrs
|
||||
from .cameras import CameraStore
|
||||
from . import capture
|
||||
from .capture import VideoSource
|
||||
from .faces import FaceOutbox
|
||||
from .commission import CommissionRun
|
||||
@@ -169,7 +170,13 @@ class CameraWorker(threading.Thread):
|
||||
self._overlay_ts = 0.0
|
||||
self._last_frame_ts = 0.0
|
||||
self._was_connected = False
|
||||
self._was_stalled = False
|
||||
self.frames_processed = 0
|
||||
# Motion gate state: a 160x90 greyscale thumbnail of the last frame we
|
||||
# actually searched, and how many frames we have skipped since.
|
||||
self._motion_prev = None
|
||||
self._motion_skipped = 0
|
||||
self.frames_skipped = 0
|
||||
self.faces_seen = 0
|
||||
self.pipeline = PipelineStats()
|
||||
# One outbox per worker, all writing into the same directory. Files are
|
||||
@@ -236,6 +243,7 @@ class CameraWorker(threading.Thread):
|
||||
return {
|
||||
**self.source.stats(),
|
||||
"frames_processed": self.frames_processed,
|
||||
"frames_skipped": self.frames_skipped,
|
||||
"faces_seen": self.faces_seen,
|
||||
"active_tracks": len(self.tracker.tracks),
|
||||
"pipeline": self.pipeline.snapshot(self.rcfg.min_enroll_quality),
|
||||
@@ -244,6 +252,43 @@ class CameraWorker(threading.Thread):
|
||||
"enroll_threshold": self.rcfg.enroll_threshold},
|
||||
}
|
||||
|
||||
def _nothing_moved(self, frame) -> bool:
|
||||
"""True when this frame is close enough to the last searched one that
|
||||
searching it again would find the same nothing.
|
||||
|
||||
It cannot lose a face, and that property is what makes it acceptable
|
||||
rather than merely cheap. Three guards, in order:
|
||||
|
||||
* the caller only asks while NO track is open, so a person already
|
||||
being followed is never affected by it;
|
||||
* `motion_max_skip` forces a real detection about once a second
|
||||
whatever the thumbnail says, which covers a change too small or too
|
||||
gradual for it - someone easing into frame at the far edge;
|
||||
* the threshold sits well above measured sensor noise and well below
|
||||
a person, and anything ambiguous falls through to detection. When
|
||||
in doubt it looks.
|
||||
|
||||
Cost is 0.1 ms against detection's 15 ms, so an empty shop stops paying
|
||||
for a search of an empty room ~90 times a second.
|
||||
"""
|
||||
import cv2 as _cv2
|
||||
small = _cv2.resize(_cv2.cvtColor(frame, _cv2.COLOR_BGR2GRAY), (160, 90),
|
||||
interpolation=_cv2.INTER_AREA)
|
||||
prev, self._motion_prev = self._motion_prev, small
|
||||
if prev is None:
|
||||
return False
|
||||
if self._motion_skipped >= self.cfg.app.motion_max_skip:
|
||||
self._motion_skipped = 0
|
||||
return False
|
||||
if float(_cv2.absdiff(small, prev).mean()) >= self.cfg.app.motion_threshold:
|
||||
self._motion_skipped = 0
|
||||
# Keep the thumbnail we just searched against, not this one, so a
|
||||
# slow drift cannot creep past the threshold one frame at a time.
|
||||
return False
|
||||
self._motion_prev = prev
|
||||
self._motion_skipped += 1
|
||||
return True
|
||||
|
||||
# -- thread ---------------------------------------------------------
|
||||
def run(self) -> None:
|
||||
tcfg = self.cfg.tracking
|
||||
@@ -256,6 +301,15 @@ class CameraWorker(threading.Thread):
|
||||
continue
|
||||
self._last_frame_ts = ts
|
||||
|
||||
# An empty room costs exactly as much to search as a busy one,
|
||||
# and a shop is empty most of the day. Only ever while nothing
|
||||
# is being tracked - see _nothing_moved.
|
||||
if (self.cfg.app.motion_gate and not self.tracker.tracks
|
||||
and self._nothing_moved(frame)):
|
||||
self.frames_skipped += 1
|
||||
self._remember_tracks([])
|
||||
continue
|
||||
|
||||
detections = self.detector.detect(frame)
|
||||
for det in detections:
|
||||
det.quality = face_quality(frame, det.box, det.kps)
|
||||
@@ -294,6 +348,20 @@ class CameraWorker(threading.Thread):
|
||||
type="camera.up" if connected else "camera.down",
|
||||
camera_id=self.cam_cfg.id))
|
||||
|
||||
# A stall is not a disconnect and must not be reported as one: the
|
||||
# socket is fine, the camera is answering, and nothing is arriving.
|
||||
# Logged on the transition only — a per-frame warning would bury the
|
||||
# one line that matters under thousands of copies of itself.
|
||||
stalled = self.source.stalled()
|
||||
if stalled != self._was_stalled:
|
||||
self._was_stalled = stalled
|
||||
if stalled:
|
||||
log.warning("[%s] connected but no frame for over %.0fs - the "
|
||||
"camera is answering and sending nothing",
|
||||
self.cam_cfg.id, capture.STALL_AFTER_S)
|
||||
else:
|
||||
log.info("[%s] frames resumed", self.cam_cfg.id)
|
||||
|
||||
def _finish_track(self, track: Track, ts: float) -> None:
|
||||
"""Record what became of a track, once, as it ends.
|
||||
|
||||
@@ -629,7 +697,10 @@ class Engine:
|
||||
"age_model": ("genderage" if self.attributes is not None
|
||||
and self.attributes.has_genderage else "caffe/none"),
|
||||
},
|
||||
"gallery": self.store.stats(),
|
||||
# Counts, plus whether the running encoder can actually SEARCH
|
||||
# them. A gallery of 21 identities that the loaded model cannot
|
||||
# read is the silent version of an empty one.
|
||||
"gallery": {**self.store.stats(), **self.gallery.health},
|
||||
"cameras": [w.stats() for w in self.snapshot_workers()],
|
||||
}
|
||||
|
||||
|
||||
@@ -53,10 +53,56 @@ class Gallery:
|
||||
# vectors from a different model are numerically incompatible.
|
||||
ids, vecs = store.all_embeddings(index.dim, model=model_name)
|
||||
index.add(ids, vecs)
|
||||
self.health = self._assess(len(ids))
|
||||
if self.health["stranded"]:
|
||||
# Not an INFO line. The encoder fallback chain exists so a
|
||||
# memory-starved box still runs, and when it fires every vector
|
||||
# written by the previous encoder becomes invisible: the shop
|
||||
# keeps its customer list and recognises nobody on it, greeting
|
||||
# every regular as new and enrolling them a second time. Footfall
|
||||
# stays right, which is exactly why nothing looks wrong. The old
|
||||
# message for that state was "gallery ready: 0 embeddings".
|
||||
log.warning(
|
||||
"gallery: %d of %d stored embeddings were written by a "
|
||||
"DIFFERENT encoder (%s) and cannot be searched - %d known "
|
||||
"%s unrecognisable under the running model '%s'. Either "
|
||||
"restore that model or accept that these identities start "
|
||||
"over.",
|
||||
self.health["stranded"], self.health["stored"],
|
||||
", ".join(sorted(self.health["other_models"])),
|
||||
self.health["identities_stranded"],
|
||||
"person is" if self.health["identities_stranded"] == 1
|
||||
else "people are",
|
||||
model_name)
|
||||
log.info("gallery ready: %d embeddings (model '%s') across %d "
|
||||
"identities", len(ids), model_name,
|
||||
store.stats()["identities"])
|
||||
|
||||
def _assess(self, usable: int) -> dict:
|
||||
"""What share of the gallery the running encoder can actually reach.
|
||||
|
||||
Reported rather than merely logged, because a log line on a shop PC
|
||||
is read by nobody: this travels to head office the same way
|
||||
`fraction_below_gate` does, beside the number it qualifies.
|
||||
"""
|
||||
counts = self.store.model_counts()
|
||||
stored = sum(counts.values())
|
||||
others = {m: n for m, n in counts.items() if m != self.model_name}
|
||||
identities = self.store.stats()["identities"]
|
||||
return {
|
||||
"model": self.model_name,
|
||||
"stored": stored,
|
||||
"usable": usable,
|
||||
"stranded": sum(others.values()),
|
||||
"other_models": sorted(others),
|
||||
"identities": identities,
|
||||
"identities_usable": self.store.identities_with_model(
|
||||
self.model_name),
|
||||
"identities_stranded": max(
|
||||
0, identities - self.store.identities_with_model(
|
||||
self.model_name)),
|
||||
}
|
||||
|
||||
def resolve(self, embedding: np.ndarray, quality: float, camera_id: str,
|
||||
ts: "float | None" = None,
|
||||
attributes: "dict | None" = None,
|
||||
|
||||
@@ -281,6 +281,31 @@ class IdentityStore:
|
||||
return None
|
||||
return np.frombuffer(row["vector"], dtype=np.float32), float(row["quality"])
|
||||
|
||||
def model_counts(self) -> "dict[str, int]":
|
||||
"""How many stored embeddings each encoder produced.
|
||||
|
||||
The gallery only ever searches vectors tagged with the *running*
|
||||
encoder, so this is what says whether the rest of the gallery is
|
||||
reachable at all. See `Gallery.health` for why that matters.
|
||||
"""
|
||||
with self._lock:
|
||||
rows = self._db.execute(
|
||||
"SELECT model, COUNT(*) AS n FROM embeddings "
|
||||
"GROUP BY model").fetchall()
|
||||
return {str(r["model"]): int(r["n"]) for r in rows}
|
||||
|
||||
def identities_with_model(self, model: str) -> int:
|
||||
"""Identities holding at least one embedding from this encoder.
|
||||
|
||||
Not the same as the identity count: an identity whose only vectors
|
||||
came from a previous encoder still exists, and is unrecognisable.
|
||||
"""
|
||||
with self._lock:
|
||||
row = self._db.execute(
|
||||
"SELECT COUNT(DISTINCT identity_id) AS n FROM embeddings "
|
||||
"WHERE model=?", (model,)).fetchone()
|
||||
return int(row["n"]) if row else 0
|
||||
|
||||
def embedding_owners(self, model: "str | None" = None) -> "dict[int, int]":
|
||||
"""embedding_id -> identity_id, for turning index hits into identity
|
||||
pairs without a round trip to SQLite per hit."""
|
||||
|
||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import shutil
|
||||
import ssl
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
@@ -35,6 +37,121 @@ _COPY_MAP = {
|
||||
}
|
||||
|
||||
|
||||
def _https_context() -> "ssl.SSLContext | None":
|
||||
"""The CA store to trust, or None to use whatever Python defaults to.
|
||||
|
||||
Returning None first is deliberate. On Windows and on a Homebrew or
|
||||
system Python, the default context reads the machine's own certificate
|
||||
store - which is what makes a corporate proxy with its own root CA work.
|
||||
Replacing that with certifi's bundle unconditionally would break every
|
||||
site that has one, in order to fix a different platform.
|
||||
|
||||
The platform this fixes is a python.org macOS build. It ships its own
|
||||
OpenSSL with NO trust store, and populates one only when somebody
|
||||
double-clicks `Install Certificates.command` in the Python folder -
|
||||
which nobody installing face-recognition software has any reason to know
|
||||
about. Every HTTPS request from that interpreter fails with:
|
||||
|
||||
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]
|
||||
certificate verify failed: unable to get local issuer certificate
|
||||
|
||||
Measured on a colleague's Mac: the engine installed perfectly and then
|
||||
could not download a 230 KB model file, ending setup in forty lines of
|
||||
traceback about `_ssl.c`.
|
||||
"""
|
||||
try:
|
||||
import certifi
|
||||
except ImportError: # pragma: no cover - certifi ships with requests
|
||||
return None
|
||||
return ssl.create_default_context(cafile=certifi.where())
|
||||
|
||||
|
||||
def _is_cert_failure(err: BaseException) -> bool:
|
||||
"""Is this a certificate-verification failure, however it is wrapped?
|
||||
|
||||
`urllib` does NOT let `ssl.SSLCertVerificationError` out. It catches it and
|
||||
re-raises `urllib.error.URLError(err)`, carrying the original on `.reason`
|
||||
- so `except ssl.SSLCertVerificationError` around `urlopen` matches
|
||||
nothing, ever.
|
||||
|
||||
That is not a subtlety this file gets to record academically: the first
|
||||
version of the fallback below was written exactly that way, shipped, and
|
||||
failed on the machine it was written for with the very traceback it was
|
||||
meant to prevent. The unit test passed throughout, because the fake it
|
||||
used raised the bare SSL error - a shape real urllib never produces. A
|
||||
stub that agrees with the author is worse than no test, and the test now
|
||||
raises what urllib raises.
|
||||
"""
|
||||
reason = getattr(err, "reason", None)
|
||||
return isinstance(err, ssl.SSLCertVerificationError) or \
|
||||
isinstance(reason, ssl.SSLCertVerificationError)
|
||||
|
||||
|
||||
def _urlopen(url: str, timeout: float = 60.0):
|
||||
"""Open a URL, falling back to certifi's CA bundle on a verify failure.
|
||||
|
||||
Default first, certifi second, so the fix is additive: a machine whose
|
||||
own store works keeps using it, and one with no store at all gets a
|
||||
bundle rather than a traceback. certifi is already here - `requests` is a
|
||||
hard dependency and brings it.
|
||||
"""
|
||||
try:
|
||||
return urllib.request.urlopen(url, timeout=timeout)
|
||||
except (urllib.error.URLError, ssl.SSLCertVerificationError) as err:
|
||||
# Only a certificate problem is worth a second attempt. "No route to
|
||||
# host" and "connection refused" arrive as URLError too, and retrying
|
||||
# those with a different CA list changes nothing except how long the
|
||||
# operator waits for the real message.
|
||||
if not _is_cert_failure(err):
|
||||
raise
|
||||
ctx = _https_context()
|
||||
if ctx is None:
|
||||
raise
|
||||
log.info("the system certificate store could not verify %s; "
|
||||
"using the bundled CA list", url.split("/")[2])
|
||||
return urllib.request.urlopen(url, timeout=timeout, context=ctx)
|
||||
|
||||
|
||||
def _fetch(url: str, dest: Path, label: str) -> None:
|
||||
"""Download with progress on stdout the supervisor can read.
|
||||
|
||||
On first run this is minutes of nothing: the API is not up yet, so the
|
||||
app cannot ask the engine what it is doing, and a shop PC that shows a
|
||||
stopped engine for five minutes after install looks broken. The
|
||||
supervisor watches for `download: <label> <n>%` and puts the number in
|
||||
the tray and the window. Logged every 5 points, not every chunk, so the
|
||||
log file does not fill with a progress bar.
|
||||
"""
|
||||
last = -5
|
||||
|
||||
def hook(blocks: int, block_size: int, total: int) -> None:
|
||||
nonlocal last
|
||||
if total <= 0:
|
||||
return
|
||||
pct = min(100, blocks * block_size * 100 // total)
|
||||
if pct >= last + 5:
|
||||
last = pct
|
||||
log.info("download: %s %d%%", label, pct)
|
||||
|
||||
# Streamed rather than urlretrieve, only because urlretrieve offers no way
|
||||
# to pass an SSL context and the whole point here is choosing one. The
|
||||
# `download: <label> <n>%` lines are a contract: the supervisor parses
|
||||
# them (`progressRe`) to put first-run progress in the tray, and without
|
||||
# them a shop PC shows a stopped engine for five minutes after install.
|
||||
with _urlopen(url) as resp:
|
||||
total = int(resp.headers.get("Content-Length") or 0)
|
||||
blocks, block_size = 0, 64 * 1024
|
||||
with open(dest, "wb") as out:
|
||||
while True:
|
||||
chunk = resp.read(block_size)
|
||||
if not chunk:
|
||||
break
|
||||
out.write(chunk)
|
||||
blocks += 1
|
||||
hook(blocks, block_size, total)
|
||||
log.info("download: %s 100%%", label)
|
||||
|
||||
|
||||
def setup_models(models_dir: Path) -> "list[str]":
|
||||
"""Ensure all model files exist in models_dir. Returns missing ones."""
|
||||
models_dir = Path(models_dir)
|
||||
@@ -44,7 +161,7 @@ def setup_models(models_dir: Path) -> "list[str]":
|
||||
if not yunet.exists():
|
||||
log.info("downloading YuNet face detector (~230 KB)...")
|
||||
tmp = yunet.with_suffix(".part")
|
||||
urllib.request.urlretrieve(YUNET_URL, tmp)
|
||||
_fetch(YUNET_URL, tmp, "face detector")
|
||||
tmp.rename(yunet)
|
||||
log.info("YuNet saved to %s", yunet)
|
||||
|
||||
@@ -66,7 +183,7 @@ def setup_models(models_dir: Path) -> "list[str]":
|
||||
import io
|
||||
import zipfile
|
||||
|
||||
with urllib.request.urlopen(BUFFALO_SC_URL) as resp:
|
||||
with _urlopen(BUFFALO_SC_URL) as resp:
|
||||
payload = io.BytesIO(resp.read())
|
||||
with zipfile.ZipFile(payload) as zf, \
|
||||
zf.open("w600k_mbf.onnx") as src, \
|
||||
@@ -88,7 +205,7 @@ def setup_models(models_dir: Path) -> "list[str]":
|
||||
import zipfile
|
||||
|
||||
tmp = models_dir / "buffalo_l.zip.part"
|
||||
urllib.request.urlretrieve(BUFFALO_L_URL, tmp)
|
||||
_fetch(BUFFALO_L_URL, tmp, "recognition models")
|
||||
with zipfile.ZipFile(tmp) as zf:
|
||||
for name, target in wanted.items():
|
||||
member = next((n for n in zf.namelist()
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Behavision</title>
|
||||
<link rel="icon" type="image/png" href="/static/favicon.png">
|
||||
<style>
|
||||
:root { color-scheme: dark; }
|
||||
* { box-sizing: border-box; margin: 0; }
|
||||
@@ -513,11 +514,24 @@ async function refresh() {
|
||||
]);
|
||||
renderFeeds(camList);
|
||||
renderCameras(camList);
|
||||
// 'stalled' is its own word on purpose: connected and offline send you
|
||||
// to the network, a camera that is answering and sending nothing does
|
||||
// not. Three states, because two of them need opposite actions.
|
||||
const cams = stats.cameras.map(c =>
|
||||
`${c.camera_id}: ${c.connected ? 'live' : 'offline'}`).join(' · ');
|
||||
`${c.camera_id}: ${c.streaming ? 'live' : c.connected ? 'stalled' : 'offline'}`
|
||||
).join(' · ');
|
||||
// The one failure that otherwise looks like perfect health: the encoder
|
||||
// that loaded cannot read the embeddings already stored, so every known
|
||||
// customer is a stranger. Counts stay right, which is why it needs saying.
|
||||
const stranded = stats.gallery.stranded || 0;
|
||||
const warn = stranded
|
||||
? ` · ⚠ ${stats.gallery.identities_stranded} people unrecognisable `
|
||||
+ `(${stranded} embeddings from ${stats.gallery.other_models.join(', ')}, `
|
||||
+ `running ${stats.gallery.model})`
|
||||
: '';
|
||||
// textContent, not innerHTML — no escaping needed here.
|
||||
document.getElementById('status').textContent =
|
||||
`${cams} · ${stats.gallery.identities} people · ${stats.gallery.sightings} sightings`;
|
||||
`${cams} · ${stats.gallery.identities} people · ${stats.gallery.sightings} sightings${warn}`;
|
||||
|
||||
document.getElementById('events').innerHTML = events.map(e => {
|
||||
const cls = e.type === 'person.new' ? 'new'
|
||||
|
||||
BIN
behavision/static/favicon.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
brand/loyaly-icon-128.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
brand/loyaly-icon-16.png
Normal file
|
After Width: | Height: | Size: 732 B |
BIN
brand/loyaly-icon-256.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
brand/loyaly-icon-32.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
brand/loyaly-icon-48.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
BIN
brand/loyaly-icon-512.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
BIN
brand/loyaly-icon-64.png
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
brand/loyaly-mark.png
Normal file
|
After Width: | Height: | Size: 30 KiB |
BIN
brand/loyaly.ico
Normal file
|
After Width: | Height: | Size: 67 KiB |
258
demo/console.html
Normal file
@@ -0,0 +1,258 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Behavision — live demo</title>
|
||||
<style>
|
||||
:root{
|
||||
--bg:#0A0E12; --s1:#11171C; --s2:#161D24; --s3:#1D262E;
|
||||
--line:#24303A; --line2:#1B242C;
|
||||
--ink:#E8EEF3; --ink2:#9FB0BD; --ink3:#6B7E8C;
|
||||
--accent:#3DD0C4; --accent-dim:#123039;
|
||||
--ok:#3FBF7F; --warn:#E0A33A; --bad:#E15B4C;
|
||||
--mono:'SF Mono',ui-monospace,Menlo,monospace;
|
||||
--font:'Inter',-apple-system,BlinkMacSystemFont,'Segoe UI',system-ui,sans-serif;
|
||||
}
|
||||
*{box-sizing:border-box;margin:0;padding:0}
|
||||
body{background:var(--bg);color:var(--ink);font-family:var(--font);font-size:14px;line-height:1.5;
|
||||
-webkit-font-smoothing:antialiased;padding:18px;min-height:100vh}
|
||||
.top{display:flex;align-items:center;gap:14px;margin-bottom:16px;flex-wrap:wrap}
|
||||
.brand{display:flex;align-items:center;gap:10px;margin-right:auto}
|
||||
.brand img{width:26px;height:26px;object-fit:contain}
|
||||
.brand b{font-size:16px;letter-spacing:-.01em}
|
||||
.brand span{color:var(--ink3);font-size:12px}
|
||||
.pill{display:inline-flex;align-items:center;gap:7px;padding:5px 11px;border-radius:99px;
|
||||
border:1px solid var(--line);background:var(--s1);font-size:12px;color:var(--ink2)}
|
||||
.dot{width:7px;height:7px;border-radius:99px;background:var(--ink3);flex:none}
|
||||
.dot.ok{background:var(--ok);box-shadow:0 0 0 3px rgba(63,191,127,.16)}
|
||||
.dot.bad{background:var(--bad);box-shadow:0 0 0 3px rgba(225,91,76,.16)}
|
||||
.dot.warn{background:var(--warn);box-shadow:0 0 0 3px rgba(224,163,58,.16)}
|
||||
|
||||
.grid{display:grid;grid-template-columns:minmax(0,1.05fr) minmax(0,1fr);gap:14px;align-items:start}
|
||||
@media(max-width:1100px){.grid{grid-template-columns:minmax(0,1fr)}}
|
||||
.card{background:var(--s1);border:1px solid var(--line);border-radius:12px;overflow:hidden}
|
||||
.card h2{font-size:11px;font-weight:600;letter-spacing:.09em;text-transform:uppercase;color:var(--ink3);
|
||||
padding:12px 16px;border-bottom:1px solid var(--line2);display:flex;align-items:center;gap:10px}
|
||||
.card h2 .grow{margin-left:auto;font-weight:500;letter-spacing:0;text-transform:none;font-size:12px;color:var(--ink3)}
|
||||
.pad{padding:16px}
|
||||
|
||||
.cam{position:relative;aspect-ratio:16/9;background:#05090C}
|
||||
.cam img{width:100%;height:100%;object-fit:cover;display:block}
|
||||
.cam .none{position:absolute;inset:0;display:grid;place-items:center;color:var(--ink3);font-size:13px;text-align:center;padding:20px}
|
||||
.cam .tag{position:absolute;top:10px;left:10px;background:rgba(10,14,18,.78);backdrop-filter:blur(8px);
|
||||
border:1px solid var(--line);border-radius:8px;padding:5px 10px;font-size:11.5px;font-family:var(--mono)}
|
||||
|
||||
/* the chain */
|
||||
.chain{display:flex;flex-direction:column;gap:0}
|
||||
.step{display:grid;grid-template-columns:26px 1fr auto;gap:12px;align-items:start;padding:11px 16px;
|
||||
border-bottom:1px solid var(--line2);opacity:.38;transition:opacity .25s}
|
||||
.step:last-child{border-bottom:0}
|
||||
.step.on{opacity:1}
|
||||
.step .n{width:22px;height:22px;border-radius:99px;display:grid;place-items:center;font-size:11px;font-weight:600;
|
||||
background:var(--s3);color:var(--ink3);border:1px solid var(--line);margin-top:1px}
|
||||
.step.on .n{background:var(--accent);color:#04161B;border-color:transparent}
|
||||
.step b{font-size:13.5px;font-weight:550;display:block}
|
||||
.step small{color:var(--ink2);font-size:12px;display:block;margin-top:1px;font-family:var(--mono)}
|
||||
.step .ms{font-family:var(--mono);font-size:11.5px;color:var(--accent);white-space:nowrap;margin-top:2px}
|
||||
|
||||
.empty{padding:34px 16px;text-align:center;color:var(--ink3);font-size:13px;line-height:1.6}
|
||||
.empty b{display:block;color:var(--ink2);font-size:14px;margin-bottom:5px}
|
||||
|
||||
/* customer */
|
||||
.who{display:flex;gap:13px;align-items:center;padding:16px;border-bottom:1px solid var(--line2)}
|
||||
.av{width:50px;height:50px;border-radius:10px;background:var(--s3);border:1px solid var(--line);
|
||||
display:grid;place-items:center;font-weight:600;font-size:17px;color:var(--ink2);flex:none;overflow:hidden}
|
||||
.av img{width:100%;height:100%;object-fit:cover}
|
||||
.who .n{font-size:16px;font-weight:600;letter-spacing:-.01em}
|
||||
.who .m{color:var(--ink3);font-size:12.5px;margin-top:2px}
|
||||
.badge{display:inline-block;padding:2px 8px;border-radius:99px;font-size:10.5px;font-weight:600;
|
||||
letter-spacing:.04em;text-transform:uppercase}
|
||||
.badge.new{background:var(--accent-dim);color:var(--accent)}
|
||||
.badge.seen{background:rgba(63,191,127,.14);color:var(--ok)}
|
||||
label{display:block;font-size:11.5px;font-weight:550;color:var(--ink2);margin-bottom:5px}
|
||||
input{width:100%;background:var(--s2);border:1px solid var(--line);border-radius:7px;padding:9px 11px;
|
||||
color:var(--ink);font:inherit;font-size:13.5px}
|
||||
input:focus{outline:none;border-color:var(--accent)}
|
||||
.row{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:12px}
|
||||
button{background:var(--accent);color:#04161B;border:0;border-radius:7px;padding:9px 16px;
|
||||
font:inherit;font-size:13px;font-weight:600;cursor:pointer}
|
||||
button:disabled{opacity:.45;cursor:default}
|
||||
button.sec{background:var(--s3);color:var(--ink);border:1px solid var(--line)}
|
||||
.saved{color:var(--ok);font-size:12.5px;margin-top:9px;display:flex;align-items:center;gap:6px}
|
||||
|
||||
/* raw json */
|
||||
pre{font-family:var(--mono);font-size:11px;line-height:1.55;color:var(--ink2);
|
||||
background:#080C10;border-top:1px solid var(--line2);padding:13px 16px;margin:0;
|
||||
max-height:230px;overflow:auto;white-space:pre-wrap;word-break:break-word}
|
||||
.req{font-family:var(--mono);font-size:11.5px;color:var(--accent);padding:10px 16px;background:var(--s2)}
|
||||
.req .st{float:right;color:var(--ink3)}
|
||||
.hint{color:var(--ink3);font-size:12px;padding:10px 16px 14px;line-height:1.55}
|
||||
.stack{display:flex;flex-direction:column;gap:14px}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="top">
|
||||
<div class="brand">
|
||||
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZD0iTTEyIDIxcy04LTQuNS04LTEwYTQuNSA0LjUgMCAwIDEgOC0yLjggNC41IDQuNSAwIDAgMSA4IDIuOGMwIDUuNS04IDEwLTggMTB6IiBmaWxsPSIjRjJDMTFGIi8+PC9zdmc+" alt="">
|
||||
<div><b>Behavision</b> <span id="site">— live demo</span></div>
|
||||
</div>
|
||||
<span class="pill"><i class="dot" id="d-eng"></i><span id="t-eng">engine…</span></span>
|
||||
<span class="pill"><i class="dot" id="d-cam"></i><span id="t-cam">camera…</span></span>
|
||||
<span class="pill"><i class="dot" id="d-cloud"></i><span id="t-cloud">cloud…</span></span>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<div class="stack">
|
||||
<div class="card">
|
||||
<h2>The camera <span class="grow" id="camname"></span></h2>
|
||||
<div class="cam">
|
||||
<img id="feed" alt="" style="display:none">
|
||||
<div class="none" id="feednone">waiting for the camera…</div>
|
||||
<div class="tag" id="camtag" style="display:none"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>The customer <span class="grow">type a name, then walk past again</span></h2>
|
||||
<div id="cust">
|
||||
<div class="empty"><b>Nobody yet</b>Walk in front of the camera.</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="stack">
|
||||
<div class="card">
|
||||
<h2>What just happened <span class="grow" id="lat"></span></h2>
|
||||
<div id="chain"><div class="empty"><b>Waiting</b>Every step below lights up as it really happens.</div></div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>What the mobile app receives</h2>
|
||||
<div class="req" id="m-req">GET /api/visits<span class="st" id="m-st"></span></div>
|
||||
<pre id="m-body">…</pre>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>What the dashboard receives</h2>
|
||||
<div class="req" id="d-req">GET /api/reports/footfall<span class="st" id="d-st"></span></div>
|
||||
<pre id="d-body">…</pre>
|
||||
<div class="hint">Both of these are the real production API at mcp.loyaly.ai, called from this
|
||||
machine with a staff login — not a mock, and not the local engine.</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const $ = s => document.querySelector(s);
|
||||
let camStarted = null, current = null, savedFor = null;
|
||||
|
||||
function setPill(dot, text, tone, label){
|
||||
$(dot).className = 'dot' + (tone ? ' ' + tone : '');
|
||||
$(text).textContent = label;
|
||||
}
|
||||
|
||||
function initials(name, ref){
|
||||
const m = /^Visitor (\d+)$/.exec((name||'').trim());
|
||||
if (m) return m[1];
|
||||
const w = (name||'').trim().split(/\s+/).filter(Boolean);
|
||||
if (!w.length) return '?';
|
||||
return (w[0][0] + (w[1]?.[0] ?? '')).toUpperCase();
|
||||
}
|
||||
|
||||
function drawChain(e){
|
||||
if (!e){ $('#chain').innerHTML = '<div class="empty"><b>Waiting</b>Every step below lights up as it really happens.</div>'; $('#lat').textContent=''; return; }
|
||||
const g = e.engine, c = e.cloud;
|
||||
const steps = [
|
||||
[true, 'Camera saw a face', g.quality != null ? `quality ${(+g.quality).toFixed(2)} · camera ${g.camera}` : `camera ${g.camera}`, ''],
|
||||
[true, e.kind === 'new' ? 'Engine: nobody it knows → enrolled' : 'Engine: matched a returning customer',
|
||||
(g.label || '') + (g.similarity != null && g.similarity >= 0 ? ` · similarity ${(+g.similarity).toFixed(2)}` : '') , ''],
|
||||
[true, 'Agent queued the visit', 'durable on this disk until the broker confirms', ''],
|
||||
[!!c, 'Broker delivered it', 'MQTT over TLS to mcp.loyaly.ai', ''],
|
||||
[!!c, 'Server recorded it', c ? `${c.site} · ${c.is_new ? 'new customer' : 'returning'}` : 'waiting…', ''],
|
||||
[!!c, 'Mobile + dashboard can see it', c ? `visit ${String(c.visit_id).slice(0,8)}` : 'waiting…',
|
||||
e.latency != null ? `+${e.latency}s` : ''],
|
||||
];
|
||||
$('#chain').innerHTML = steps.map(([on,title,sub,ms],i)=>
|
||||
`<div class="step ${on?'on':''}"><div class="n">${i+1}</div><div><b>${title}</b><small>${sub}</small></div><div class="ms">${ms}</div></div>`
|
||||
).join('');
|
||||
$('#lat').textContent = e.latency != null ? `camera → cloud in ${e.latency}s` : '';
|
||||
}
|
||||
|
||||
function drawCustomer(e){
|
||||
const c = e && e.cloud;
|
||||
if (!c){ if(!current) $('#cust').innerHTML = '<div class="empty"><b>Nobody yet</b>Walk in front of the camera.</div>'; return; }
|
||||
const changed = !current || current.visitor_id !== c.visitor_id || current.visit_id !== c.visit_id;
|
||||
if (!changed) return;
|
||||
current = c;
|
||||
const name = c.label || 'Unrecognised';
|
||||
const img = c.image && c.image.available && c.image.url;
|
||||
$('#cust').innerHTML = `
|
||||
<div class="who">
|
||||
<div class="av">${img ? `<img src="${img}">` : initials(name)}</div>
|
||||
<div style="flex:1;min-width:0">
|
||||
<div class="n">${name}</div>
|
||||
<div class="m">${c.ref ? c.ref + ' · ' : ''}${c.is_new ? 'first time here' : 'returning'}${c.similarity>0 ? ' · match ' + (+c.similarity).toFixed(2) : ''}</div>
|
||||
</div>
|
||||
<span class="badge ${c.is_new?'new':'seen'}">${c.is_new?'new':'returning'}</span>
|
||||
</div>
|
||||
<div class="pad">
|
||||
<div class="row">
|
||||
<div><label>Name</label><input id="f-name" placeholder="e.g. Suriya" value=""></div>
|
||||
<div><label>Phone</label><input id="f-phone" placeholder="+91…" value=""></div>
|
||||
</div>
|
||||
<button id="save">Save to the customer record</button>
|
||||
<div id="savedmsg"></div>
|
||||
<div class="hint" style="padding:12px 0 0">This writes to the production API. Walk past again and
|
||||
the name comes back through the cloud instead of “${name}”.</div>
|
||||
</div>`;
|
||||
$('#save').onclick = async () => {
|
||||
const b = $('#save'); b.disabled = true; b.textContent = 'Saving…';
|
||||
const r = await fetch('/api/profile', {method:'POST', headers:{'content-type':'application/json'},
|
||||
body: JSON.stringify({id: c.visitor_id, full_name: $('#f-name').value, phone: $('#f-phone').value})});
|
||||
const d = await r.json();
|
||||
b.disabled = false; b.textContent = 'Save to the customer record';
|
||||
$('#savedmsg').innerHTML = (d.status===200||d.status===204)
|
||||
? '<div class="saved">✓ Saved — PUT /api/visitors/'+String(c.visitor_id).slice(0,8)+'…/profile → '+d.status+'</div>'
|
||||
: '<div class="saved" style="color:var(--bad)">'+(d.body&&d.body.message||('HTTP '+d.status))+'</div>';
|
||||
savedFor = c.visitor_id;
|
||||
};
|
||||
}
|
||||
|
||||
async function tick(){
|
||||
let s;
|
||||
try { s = await (await fetch('/api/snapshot')).json(); } catch { return; }
|
||||
|
||||
const eng = s.engine || {};
|
||||
setPill('#d-eng','#t-eng', eng.up ? 'ok' : 'bad', eng.up ? ('engine · ' + (eng.model||'starting')) : 'engine starting…');
|
||||
const cam = (eng.cameras||[])[0];
|
||||
setPill('#d-cam','#t-cam', cam && cam.connected ? 'ok' : 'warn',
|
||||
cam ? (cam.connected ? `camera live · ${cam.frames||0} frames` : 'camera connecting…') : 'no camera yet');
|
||||
setPill('#d-cloud','#t-cloud', s.cloud_ok ? 'ok' : 'bad', s.cloud_ok ? 'cloud connected' : 'cloud unreachable');
|
||||
$('#camname').textContent = cam ? cam.id : '';
|
||||
|
||||
if (cam && cam.connected){
|
||||
if (camStarted !== cam.id){
|
||||
camStarted = cam.id;
|
||||
$('#feed').style.display = 'block'; $('#feednone').style.display = 'none';
|
||||
$('#camtag').style.display = 'block';
|
||||
// A polled still rather than an MJPEG stream: the engine re-serves its
|
||||
// latest frame anyway, and a multipart stream through a proxy is one
|
||||
// more thing to fail in front of an audience.
|
||||
setInterval(() => { $('#feed').src = '/camera.jpg?id=' +
|
||||
encodeURIComponent(camStarted) + '&t=' + Date.now(); }, 350);
|
||||
}
|
||||
$('#camtag').textContent = cam.id + (cam.tracks ? ` · ${cam.tracks} in frame` : '');
|
||||
}
|
||||
|
||||
const e = (s.chain||[])[0];
|
||||
drawChain(e); drawCustomer(e);
|
||||
|
||||
$('#m-st').textContent = s.mobile.status;
|
||||
$('#m-body').textContent = JSON.stringify(s.mobile.body, null, 1).slice(0, 2600);
|
||||
$('#d-st').textContent = s.dashboard.status;
|
||||
$('#d-body').textContent = JSON.stringify(s.dashboard.body, null, 1).slice(0, 1800);
|
||||
}
|
||||
tick(); setInterval(tick, 1500);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
330
demo/console.py
Normal file
@@ -0,0 +1,330 @@
|
||||
"""A one-screen live demo of the whole Behavision chain, for showing someone.
|
||||
|
||||
Run it on the shop PC (here, this Mac) while the engine and agent are running.
|
||||
It holds every credential itself and the browser holds none, so the page can be
|
||||
put on a projector without putting a token on it.
|
||||
|
||||
What it shows, and why each part is there:
|
||||
|
||||
- the live camera, so the person walking past sees themselves;
|
||||
- the CHAIN, measured rather than described: the engine recognised a face at
|
||||
this instant, the same visit appeared in the cloud API this many seconds
|
||||
later. That number is the product's claim, and it is computed here from two
|
||||
independent sources rather than asserted;
|
||||
- the customer, editable - type a name, walk past again, watch the name come
|
||||
back through the cloud instead of "Visitor 5";
|
||||
- the raw JSON a phone and a dashboard receive, side by side, because a
|
||||
colleague's real question is "is this actually wired up or is it a mock".
|
||||
|
||||
.venv/bin/python demo/console.py # http://127.0.0.1:8099
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
STATE = Path(os.environ.get("BEHAVISION_DATA_DIR", ROOT / ".demo"))
|
||||
CLOUD = os.environ.get("BEHAVISION_CLOUD", "https://mcp.loyaly.ai")
|
||||
ENGINE = "http://127.0.0.1:8010"
|
||||
PORT = int(os.environ.get("DEMO_PORT", "8099"))
|
||||
|
||||
# Whoever the demo signs in as. Staff on purpose: it is the weakest role that
|
||||
# can do everything the shop floor does, so nothing here is only possible
|
||||
# because we used an owner.
|
||||
EMAIL = os.environ.get("DEMO_EMAIL", "staff.demo@tenext.in")
|
||||
PASSWORD = os.environ.get("DEMO_PASSWORD", "admin@123")
|
||||
|
||||
|
||||
def engine_auth() -> str:
|
||||
"""The engine invents a Basic credential when none is configured, and
|
||||
writes it here. Read it rather than keeping a second copy."""
|
||||
f = STATE / "data" / "api_credentials.txt"
|
||||
if not f.exists():
|
||||
return ""
|
||||
user = pw = ""
|
||||
for line in f.read_text().splitlines():
|
||||
# `key=value`, and `key: value` too - the engine writes one and people
|
||||
# read the other, and which is which is not worth a support call.
|
||||
if "=" in line or ":" in line:
|
||||
k, v = line.split("=", 1) if "=" in line else line.split(":", 1)
|
||||
if k.strip().lower() == "username":
|
||||
user = v.strip()
|
||||
elif k.strip().lower() == "password":
|
||||
pw = v.strip()
|
||||
if not user:
|
||||
return ""
|
||||
return "Basic " + base64.b64encode(f"{user}:{pw}".encode()).decode()
|
||||
|
||||
|
||||
def fetch(url: str, *, headers=None, body=None, method="GET", timeout=20):
|
||||
req = urllib.request.Request(url, method=method,
|
||||
data=json.dumps(body).encode() if body is not None else None,
|
||||
headers={k: v for k, v in (headers or {}).items() if v})
|
||||
if body is not None:
|
||||
req.add_header("content-type", "application/json")
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
raw = r.read()
|
||||
return r.status, (json.loads(raw) if raw and r.headers.get("content-type", "").startswith("application/json") else raw)
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
try:
|
||||
return e.code, json.loads(raw or b"{}")
|
||||
except Exception:
|
||||
return e.code, raw[:400]
|
||||
except Exception as e:
|
||||
return 0, {"error": str(e)}
|
||||
|
||||
|
||||
class Cloud:
|
||||
"""The signed-in session, refreshed when it expires."""
|
||||
|
||||
def __init__(self):
|
||||
self.token = ""
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def sign_in(self) -> bool:
|
||||
st, d = fetch(f"{CLOUD}/api/auth/login", method="POST",
|
||||
body={"email": EMAIL, "password": PASSWORD, "device": "Demo console"})
|
||||
if st == 200 and isinstance(d, dict):
|
||||
self.token = d.get("access_token", "")
|
||||
return True
|
||||
return False
|
||||
|
||||
def call(self, path, method="GET", body=None, retry=True):
|
||||
with self.lock:
|
||||
if not self.token and not self.sign_in():
|
||||
return 0, {"error": "cannot sign in to the platform"}
|
||||
tok = self.token
|
||||
st, d = fetch(f"{CLOUD}{path}", method=method, body=body,
|
||||
headers={"authorization": f"Bearer {tok}"})
|
||||
if st == 401 and retry:
|
||||
with self.lock:
|
||||
self.sign_in()
|
||||
return self.call(path, method, body, retry=False)
|
||||
return st, d
|
||||
|
||||
|
||||
cloud = Cloud()
|
||||
|
||||
# The chain, as the watcher builds it. One dict per recognition, newest first.
|
||||
events: list[dict] = []
|
||||
events_lock = threading.Lock()
|
||||
|
||||
|
||||
def watch():
|
||||
"""Poll the engine's own event log and the cloud feed, and join them.
|
||||
|
||||
They are joined on the identity and the second, not on a shared id,
|
||||
because the engine numbers identities locally and the server numbers them
|
||||
per tenant - the two are deliberately different (see CLAUDE.md). What
|
||||
matters for the demo is the LATENCY between one seeing a person and the
|
||||
other, and that only needs the same person and the same moment.
|
||||
"""
|
||||
seen_local: set[str] = set()
|
||||
while True:
|
||||
try:
|
||||
auth = engine_auth()
|
||||
st, d = fetch(f"{ENGINE}/api/events?limit=25", headers={"authorization": auth})
|
||||
# The engine returns a bare list; a dict with "events" is accepted
|
||||
# too so this survives either shape.
|
||||
evs = d if isinstance(d, list) else (d or {}).get("events", []) if isinstance(d, dict) else []
|
||||
if st == 200:
|
||||
for e in evs:
|
||||
if e.get("type") not in ("person.new", "person.seen"):
|
||||
continue
|
||||
key = f"{e.get('ts')}|{e.get('camera_id')}|{(e.get('data') or {}).get('identity_id')}"
|
||||
if key in seen_local:
|
||||
continue
|
||||
seen_local.add(key)
|
||||
data = e.get("data") or {}
|
||||
with events_lock:
|
||||
events.insert(0, {
|
||||
"key": key,
|
||||
"at": time.time(),
|
||||
"kind": "new" if e["type"] == "person.new" else "seen",
|
||||
"engine": {
|
||||
"label": data.get("label"),
|
||||
"identity_id": data.get("identity_id"),
|
||||
"similarity": data.get("similarity"),
|
||||
"quality": data.get("quality"),
|
||||
"gender": data.get("gender"),
|
||||
"age": data.get("age"),
|
||||
"camera": e.get("camera_id"),
|
||||
"ts": e.get("ts"),
|
||||
},
|
||||
"cloud": None,
|
||||
"latency": None,
|
||||
})
|
||||
del events[40:]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# the other half: has the cloud got it yet?
|
||||
try:
|
||||
with events_lock:
|
||||
pending = [e for e in events if e["cloud"] is None][:6]
|
||||
if pending:
|
||||
st, d = cloud.call("/api/visits?limit=12")
|
||||
arrivals = (d or {}).get("arrivals", []) if isinstance(d, dict) else []
|
||||
for e in pending:
|
||||
for a in arrivals:
|
||||
# same camera, and the cloud's visit is not older than
|
||||
# the engine's sighting
|
||||
if a.get("camera_id") != e["engine"]["camera"]:
|
||||
continue
|
||||
if a.get("visit_id") in [x["cloud"].get("visit_id") for x in events if x["cloud"]]:
|
||||
continue
|
||||
with events_lock:
|
||||
e["cloud"] = {
|
||||
"visit_id": a.get("visit_id"),
|
||||
"visitor_id": a.get("visitor_id"),
|
||||
"label": a.get("label"),
|
||||
"ref": a.get("customer_ref") or a.get("ref"),
|
||||
"is_new": a.get("is_new_visitor"),
|
||||
"similarity": a.get("similarity"),
|
||||
"site": a.get("site"),
|
||||
"occurred_at": a.get("occurred_at"),
|
||||
"image": a.get("image"),
|
||||
}
|
||||
e["latency"] = round(time.time() - e["at"], 1)
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1.0)
|
||||
|
||||
|
||||
def snapshot() -> dict:
|
||||
"""Everything the page draws, in one reply."""
|
||||
auth = engine_auth()
|
||||
_, health = fetch(f"{ENGINE}/api/health", headers={"authorization": auth})
|
||||
_, stats = fetch(f"{ENGINE}/api/stats", headers={"authorization": auth})
|
||||
st_v, visits = cloud.call("/api/visits?limit=3")
|
||||
st_f, foot = cloud.call("/api/reports/footfall?from=%s&to=%s"
|
||||
% (time.strftime("%Y-%m-%d", time.localtime(time.time() - 7 * 86400)),
|
||||
time.strftime("%Y-%m-%d")))
|
||||
with events_lock:
|
||||
chain = json.loads(json.dumps(events[:8]))
|
||||
cams = (stats or {}).get("cameras", []) if isinstance(stats, dict) else []
|
||||
return {
|
||||
"engine": {
|
||||
"up": isinstance(health, dict) and bool(health.get("status")),
|
||||
"model": (health or {}).get("recognition_model") if isinstance(health, dict) else None,
|
||||
"cameras": [{"id": c.get("camera_id"), "connected": c.get("connected"),
|
||||
"frames": c.get("frames_processed") or c.get("frames_total"),
|
||||
"faces": c.get("faces_seen"),
|
||||
"tracks": c.get("active_tracks")} for c in cams],
|
||||
},
|
||||
"cloud_ok": st_v == 200,
|
||||
"chain": chain,
|
||||
"mobile": {"request": "GET /api/visits?limit=3", "status": st_v, "body": visits},
|
||||
"dashboard": {"request": "GET /api/reports/footfall?from=…&to=…", "status": st_f, "body": foot},
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
page = (Path(__file__).parent / "console.html").read_bytes()
|
||||
|
||||
class H(BaseHTTPRequestHandler):
|
||||
def log_message(self, *a): # quiet
|
||||
pass
|
||||
|
||||
def _send(self, code, body, ctype="application/json"):
|
||||
self.send_response(code)
|
||||
self.send_header("content-type", ctype)
|
||||
self.send_header("content-length", str(len(body)))
|
||||
self.end_headers()
|
||||
try:
|
||||
self.wfile.write(body)
|
||||
except (BrokenPipeError, ConnectionResetError):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
u = urlparse(self.path)
|
||||
if u.path == "/":
|
||||
return self._send(200, page, "text/html; charset=utf-8")
|
||||
if u.path == "/api/snapshot":
|
||||
return self._send(200, json.dumps(snapshot()).encode())
|
||||
if u.path == "/api/customer":
|
||||
vid = parse_qs(u.query).get("id", [""])[0]
|
||||
if not vid:
|
||||
return self._send(400, b'{"error":"no id"}')
|
||||
st, d = cloud.call(f"/api/visitors/{vid}/history?limit=8")
|
||||
return self._send(200, json.dumps({"status": st, "history": d}).encode())
|
||||
if u.path == "/camera.jpg":
|
||||
# A polled still, not the MJPEG stream. The engine re-serves its
|
||||
# latest frame until the pipeline produces a new one, so polling
|
||||
# shows the same picture - and a multipart stream through a
|
||||
# proxy is one more thing to fail in front of an audience.
|
||||
cam = parse_qs(u.query).get("id", [""])[0]
|
||||
st, body = fetch(f"{ENGINE}/api/cameras/{cam}/frame.jpg",
|
||||
headers={"authorization": engine_auth()}, timeout=15)
|
||||
if st != 200 or not isinstance(body, (bytes, bytearray)):
|
||||
return self._send(502, b'{"error":"no frame"}')
|
||||
self.send_response(200)
|
||||
self.send_header("content-type", "image/jpeg")
|
||||
self.send_header("cache-control", "no-store")
|
||||
self.send_header("content-length", str(len(body)))
|
||||
self.end_headers()
|
||||
try:
|
||||
self.wfile.write(body)
|
||||
except (BrokenPipeError, ConnectionResetError):
|
||||
pass
|
||||
return
|
||||
self._send(404, b'{"error":"no"}')
|
||||
|
||||
def do_POST(self):
|
||||
u = urlparse(self.path)
|
||||
n = int(self.headers.get("content-length", 0))
|
||||
body = json.loads(self.rfile.read(n) or b"{}")
|
||||
if u.path == "/api/profile":
|
||||
vid = body.pop("id", "")
|
||||
st, d = cloud.call(f"/api/visitors/{vid}/profile", method="PUT", body=body)
|
||||
return self._send(200, json.dumps({"status": st, "body": d}).encode())
|
||||
self._send(404, b'{"error":"no"}')
|
||||
|
||||
def _proxy_stream(self, url):
|
||||
"""The engine's MJPEG, relayed so the browser needs no credential.
|
||||
|
||||
The engine's API is Basic-authenticated with a credential it
|
||||
generated locally; putting that in a page would hand the whole
|
||||
biometric API to anyone who opened it.
|
||||
"""
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"authorization": engine_auth()})
|
||||
up = urllib.request.urlopen(req, timeout=20)
|
||||
except Exception:
|
||||
return self._send(502, b'{"error":"camera not available"}')
|
||||
self.send_response(200)
|
||||
self.send_header("content-type", up.headers.get("content-type", "multipart/x-mixed-replace"))
|
||||
self.end_headers()
|
||||
try:
|
||||
while True:
|
||||
chunk = up.read(8192)
|
||||
if not chunk:
|
||||
break
|
||||
self.wfile.write(chunk)
|
||||
self.wfile.flush()
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
up.close()
|
||||
|
||||
threading.Thread(target=watch, daemon=True).start()
|
||||
print(f"\n Demo console → http://127.0.0.1:{PORT}\n")
|
||||
print(f" engine {ENGINE} · cloud {CLOUD} · signed in as {EMAIL}\n")
|
||||
ThreadingHTTPServer(("127.0.0.1", PORT), H).serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
305
desktop/app.go
@@ -44,6 +44,9 @@ type App struct {
|
||||
broker *agentmqtt.Client
|
||||
stopBridge func()
|
||||
hookURL string
|
||||
// The resolved engine command, so engineMissing() and the supervisor are
|
||||
// never looking at two different paths.
|
||||
engineExe string
|
||||
// Relays camera feeds to the webview so the engine's credential never has
|
||||
// to travel in an <img> src, which a Chromium webview would strip anyway.
|
||||
proxy *streamProxy
|
||||
@@ -66,7 +69,7 @@ func NewApp() *App {
|
||||
return &App{
|
||||
cfg: cfg,
|
||||
cloud: cloud.New(envOr("BEHAVISION_CLOUD", "https://mcp.loyaly.ai")),
|
||||
local: local.New(base, cfg.APIUser, cfg.APIPassword),
|
||||
local: localWithCreds(base, cfg),
|
||||
proxy: newStreamProxy(),
|
||||
}
|
||||
}
|
||||
@@ -82,6 +85,14 @@ func (a *App) startup(ctx context.Context) {
|
||||
if err := a.proxy.start(a.local.Base, a.local.User, a.local.Password); err != nil {
|
||||
log.Printf("camera relay unavailable, tiles will not load: %v", err)
|
||||
}
|
||||
// And the other direction: watching a camera in another building, through
|
||||
// head office's relay. Enabled unconditionally rather than only when a
|
||||
// session already exists, because signing in is a thing that happens
|
||||
// while the app is open - and CameraLive refuses without a session
|
||||
// anyway, so there is nothing to gate.
|
||||
if err := a.proxy.watchRemote(a.cloud.CameraLive); err != nil {
|
||||
log.Printf("remote camera view unavailable: %v", err)
|
||||
}
|
||||
|
||||
// A saved session means a shop PC that rebooted overnight comes back
|
||||
// working instead of waiting for someone to log in.
|
||||
@@ -101,10 +112,21 @@ func (a *App) startup(ctx context.Context) {
|
||||
if exe != "" && !filepath.IsAbs(exe) {
|
||||
exe = filepath.Join(agentpaths.InstallRoot(), exe)
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.engineExe = exe
|
||||
a.mu.Unlock()
|
||||
logFile, _ := agentengine.LogFile(agentpaths.EngineLog())
|
||||
a.sup = agentengine.New(agentengine.Options{
|
||||
Command: func(c context.Context) *exec.Cmd {
|
||||
cmd := exec.CommandContext(c, exe, a.cfg.EngineArgs...)
|
||||
// Run the engine FROM a known directory rather than from whatever
|
||||
// happened to launch us. A double-clicked bundle hands its child
|
||||
// "/", and an engine invoked as `-m behavision` then cannot find
|
||||
// itself - measured on macOS, where it retried forever.
|
||||
cmd.Dir = a.cfg.EngineDir
|
||||
if cmd.Dir == "" {
|
||||
cmd.Dir = agentpaths.InstallRoot()
|
||||
}
|
||||
// How the engine learns where to post its detections. Its config
|
||||
// already reads `events.webhook_url: ${BEHAVISION_WEBHOOK_URL}`,
|
||||
// and python-dotenv does not override a variable the process
|
||||
@@ -125,6 +147,7 @@ func (a *App) startup(ctx context.Context) {
|
||||
})
|
||||
|
||||
a.startPipeline(ctx)
|
||||
go a.watchConfig(ctx)
|
||||
|
||||
// Recognition starts with the app. Until this, the engine only ever
|
||||
// started when somebody pressed Start - which meant a till that rebooted
|
||||
@@ -137,13 +160,55 @@ func (a *App) startup(ctx context.Context) {
|
||||
// not run yet, starting the supervisor would loop on a missing executable
|
||||
// with nothing useful to say. The Start button still exists for the one
|
||||
// case where somebody has deliberately stopped it.
|
||||
if _, err := os.Stat(exe); err == nil {
|
||||
if why := a.engineMissing(); why == "" {
|
||||
a.sup.Start()
|
||||
} else {
|
||||
log.Printf("engine not installed yet (%s); run behavision-setup, then Start", exe)
|
||||
log.Printf("%s (looked for %s)", why, exe)
|
||||
}
|
||||
}
|
||||
|
||||
// engineMissing says, in a sentence somebody can act on, why recognition
|
||||
// cannot start here - or "" when it can.
|
||||
//
|
||||
// It exists because the answer was only ever given at startup, to a log file
|
||||
// nobody on a shop counter opens. Pressing Start went straight to the
|
||||
// supervisor, which reported what exec reported:
|
||||
//
|
||||
// engine failed to start: fork/exec /private/var/folders/c2/.../
|
||||
// AppTranslocation/500A5354-.../d/Behavision.app/Contents/MacOS/engine/
|
||||
// behavision: no such file or directory
|
||||
//
|
||||
// Every word of that is true and none of it says "run the setup tool". One
|
||||
// function, consulted by the startup path, the Start button and the status
|
||||
// panel, so the three cannot give three different accounts of one fact.
|
||||
func (a *App) engineMissing() string {
|
||||
a.mu.RLock()
|
||||
exe := a.engineExe
|
||||
a.mu.RUnlock()
|
||||
if exe == "" {
|
||||
return "The recognition engine is not set up on this computer yet."
|
||||
}
|
||||
if _, err := os.Stat(exe); err == nil {
|
||||
return ""
|
||||
}
|
||||
msg := "The recognition engine is not installed on this computer yet. " +
|
||||
"Run behavision-setup from the folder you unzipped, then press Start."
|
||||
// macOS quarantines a downloaded app it cannot verify and runs it from a
|
||||
// randomly named READ-ONLY copy - App Translocation. Every relative path
|
||||
// then resolves inside that copy, which is why the engine folder appears
|
||||
// to be missing from a bundle that plainly contains one, and why an
|
||||
// install into it would not survive a restart. Detectable, unguessable,
|
||||
// and fixed by one drag; saying nothing leaves somebody re-running a
|
||||
// setup tool that cannot win.
|
||||
if strings.Contains(exe, "/AppTranslocation/") {
|
||||
msg = "macOS is running Behavision from a temporary read-only copy, " +
|
||||
"because it was opened straight from Downloads. Move Behavision " +
|
||||
"to your Applications folder and open it from there, then run " +
|
||||
"behavision-setup."
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// webhookURL is the loopback address the bridge is listening on, or empty
|
||||
// before it has started.
|
||||
func (a *App) webhookURL() string {
|
||||
@@ -225,7 +290,7 @@ func (a *App) startPipeline(ctx context.Context) {
|
||||
}
|
||||
client, err := agentmqtt.NewClient(agentmqtt.ClientOptions{
|
||||
BrokerURL: a.cfg.BrokerURL,
|
||||
ClientID: "behavision-" + a.cfg.ClientID + "-" + a.cfg.SiteID,
|
||||
ClientID: a.cfg.MQTTClientID(),
|
||||
Username: a.cfg.BrokerUsername, Password: a.cfg.BrokerPassword,
|
||||
CAFile: a.cfg.BrokerCAFile, Log: logger,
|
||||
})
|
||||
@@ -435,6 +500,9 @@ func (a *App) Claim(code string) (SessionInfo, error) {
|
||||
a.cfg.BrokerPassword = b.MQTTPass
|
||||
a.cfg.AgentToken = b.AgentToken
|
||||
a.cfg.CloudBase = a.cloud.Base
|
||||
// A new head office: whoever was signed in was signed in somewhere else.
|
||||
a.cfg.SessionToken, a.cfg.SessionRefresh, a.cfg.SessionEmail = "", "", ""
|
||||
a.cloud.Clear()
|
||||
caPath, err := enrol.SaveCA(b.CACert, agentpaths.BrokerCA())
|
||||
if err != nil {
|
||||
return SessionInfo{}, err
|
||||
@@ -464,6 +532,62 @@ func (a *App) Claim(code string) (SessionInfo, error) {
|
||||
// the current config. Only Claim needs it today; it exists as its own method
|
||||
// because "stop everything that reads the config, then start it" is the part
|
||||
// that is easy to get half right.
|
||||
// watchConfig reloads agent.json when something else writes it.
|
||||
//
|
||||
// behavision-setup re-run on a PC with the app open re-claims the shop and
|
||||
// rotates its API token; the running app kept the old one and every camera
|
||||
// sync was refused from then on - heartbeats still flowed, so head office
|
||||
// looked fine while the cameras went stale. A claim from `behavision-agent
|
||||
// claim` does the same. Rather than ask people to restart the app, the app
|
||||
// watches the file and picks the new credentials up itself.
|
||||
func (a *App) watchConfig(ctx context.Context) {
|
||||
path := agentpaths.AgentConfig()
|
||||
last := mtime(path)
|
||||
t := time.NewTicker(10 * time.Second)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
}
|
||||
now := mtime(path)
|
||||
if now.IsZero() || now.Equal(last) {
|
||||
continue
|
||||
}
|
||||
last = now
|
||||
fresh, err := agentcfg.Load(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fresh = fresh.WithEngineCredentials(agentpaths.APICredentials())
|
||||
a.mu.Lock()
|
||||
changed := fresh.AgentToken != a.cfg.AgentToken || fresh.SiteID != a.cfg.SiteID ||
|
||||
fresh.BrokerPassword != a.cfg.BrokerPassword || fresh.CloudBase != a.cfg.CloudBase ||
|
||||
fresh.Standalone != a.cfg.Standalone
|
||||
if changed {
|
||||
// Keep this process's live session; a claim clears it in the file
|
||||
// deliberately, and that is honoured too.
|
||||
a.cfg = fresh
|
||||
if fresh.SessionToken == "" {
|
||||
a.cloud.Clear()
|
||||
}
|
||||
}
|
||||
a.mu.Unlock()
|
||||
if changed {
|
||||
a.restartPipeline()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mtime(path string) time.Time {
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return st.ModTime()
|
||||
}
|
||||
|
||||
func (a *App) restartPipeline() {
|
||||
if a.stopBridge != nil {
|
||||
a.stopBridge()
|
||||
@@ -501,6 +625,8 @@ type EngineStatus struct {
|
||||
Reachable bool `json:"reachable"`
|
||||
Model string `json:"recognition_model,omitempty"`
|
||||
Cameras map[string]bool `json:"cameras,omitempty"`
|
||||
// Progress is the first-run model download, when one is happening.
|
||||
Progress *agentengine.Progress `json:"progress,omitempty"`
|
||||
}
|
||||
|
||||
func (a *App) EngineStatus() EngineStatus {
|
||||
@@ -511,9 +637,17 @@ func (a *App) EngineStatus() EngineStatus {
|
||||
st, err := a.sup.State()
|
||||
out.State = string(st)
|
||||
out.Restarts = a.sup.Restarts()
|
||||
if p := a.sup.Progress(); p.What != "" {
|
||||
out.Progress = &p
|
||||
}
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
}
|
||||
// The supervisor's own error is an exec failure; this replaces it with
|
||||
// the reason, which is the part that tells somebody what to do.
|
||||
if why := a.engineMissing(); why != "" {
|
||||
out.Error = why
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(a.ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
// A running process is not a working engine: on a memory-starved box the
|
||||
@@ -528,6 +662,13 @@ func (a *App) EngineStatus() EngineStatus {
|
||||
}
|
||||
|
||||
func (a *App) StartEngine() EngineStatus {
|
||||
// Refused rather than attempted. Handing a missing path to the supervisor
|
||||
// produces a retry loop and an exec error for a message.
|
||||
if why := a.engineMissing(); why != "" {
|
||||
st := a.EngineStatus()
|
||||
st.Error = why
|
||||
return st
|
||||
}
|
||||
if a.sup != nil {
|
||||
a.sup.Start()
|
||||
}
|
||||
@@ -543,10 +684,52 @@ func (a *App) StopEngine() EngineStatus {
|
||||
|
||||
// ---------------------------------------------------------------- cameras --
|
||||
|
||||
// Cameras lists this PC's cameras, or the company's if this PC has none of
|
||||
// its own.
|
||||
//
|
||||
// The distinction is load-bearing and the UI is told which it got. A camera
|
||||
// from the local engine is one THIS machine can reach, edit and stream. One
|
||||
// from head office is a camera at a shop somewhere else: it has a snapshot
|
||||
// and a connection state, and it cannot be edited from here because the shop
|
||||
// PC on that LAN is the only thing that can reach it. Offering an Edit button
|
||||
// that could not work would be worse than not showing the camera at all.
|
||||
func (a *App) Cameras() ([]map[string]any, error) {
|
||||
ctx, cancel := context.WithTimeout(a.ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
return a.local.Cameras(ctx)
|
||||
|
||||
cams, err := a.local.Cameras(ctx)
|
||||
if err == nil {
|
||||
return cams, nil
|
||||
}
|
||||
if !a.cloud.LoggedIn() {
|
||||
return nil, err
|
||||
}
|
||||
remote, rerr := a.cloud.RemoteCameras(ctx)
|
||||
if rerr != nil {
|
||||
return nil, err // the local failure is the one worth reporting
|
||||
}
|
||||
out := make([]map[string]any, 0, len(remote))
|
||||
for _, c := range remote {
|
||||
out = append(out, map[string]any{
|
||||
"id": c.ID, "camera_id": c.CameraID, "label": c.Label,
|
||||
"site": c.Site, "enabled": c.Enabled,
|
||||
"connected": c.Connected, "last_seen_at": c.LastSeenAt,
|
||||
"state": c.State, "state_note": c.StateNote,
|
||||
"snapshot": c.Snapshot, "snapshot_at": c.SnapshotAt,
|
||||
// What the screen keys off to hide Edit, Test and Check: this
|
||||
// camera is on a network this PC cannot reach.
|
||||
"remote": true,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DiscoverCameras lists the cameras on this PC's network, so the add-camera
|
||||
// form is a pick-list and not a request for an IP address nobody knows.
|
||||
func (a *App) DiscoverCameras() (map[string]any, error) {
|
||||
ctx, cancel := context.WithTimeout(a.ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
return a.local.DiscoverCameras(ctx)
|
||||
}
|
||||
|
||||
func (a *App) TestCamera(cam map[string]any) (map[string]any, error) {
|
||||
@@ -605,25 +788,115 @@ func (a *App) StreamURL(cameraID string) string {
|
||||
return fmt.Sprintf("http://%s/api/cameras/%s/stream.mjpeg", base, cameraID)
|
||||
}
|
||||
|
||||
// RemoteStreamURL is the live view of a camera in another building.
|
||||
//
|
||||
// The picture comes from head office's relay - the shop PC pushes frames
|
||||
// outbound because nothing can reach in - and this app re-emits them as MJPEG
|
||||
// on its own loopback, so a tile is an ordinary <img> either way. A screen
|
||||
// therefore never has to know which building it is looking at.
|
||||
//
|
||||
// Empty when the relay is not running, and the caller shows the last snapshot
|
||||
// instead. There is no useful fallback URL: the head-office endpoint needs
|
||||
// this session's bearer, which an <img> cannot send.
|
||||
func (a *App) RemoteStreamURL(cameraID string) string {
|
||||
if !a.cloud.LoggedIn() {
|
||||
return ""
|
||||
}
|
||||
return a.proxy.urlFor(cameraID, "live.mjpeg")
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- live --
|
||||
|
||||
type LiveSnapshot struct {
|
||||
Stats map[string]any `json:"stats"`
|
||||
Events []map[string]any `json:"events"`
|
||||
// Viewing is true when none of this came from an engine on THIS PC. The
|
||||
// screen must say so: the numbers are the company's, not this machine's,
|
||||
// and a laptop in a hotel showing "2 cameras live" without that word
|
||||
// would be claiming to be watching a shop it cannot see.
|
||||
Viewing bool `json:"viewing"`
|
||||
}
|
||||
|
||||
// Live is what the shop PC sees, and falls back to what HEAD OFFICE sees.
|
||||
//
|
||||
// A PC with no engine is not necessarily broken - it is somebody signed in on
|
||||
// a laptop away from the shop, which is the ordinary way an owner looks at
|
||||
// their estate. Until now that produced "engine not reachable at
|
||||
// 127.0.0.1:8010", an accurate sentence and a useless one when the reader was
|
||||
// never expecting an engine on that machine.
|
||||
//
|
||||
// The local engine always wins when it is there: it is this shop's own
|
||||
// ground truth and it is live rather than a heartbeat old.
|
||||
func (a *App) Live() (LiveSnapshot, error) {
|
||||
ctx, cancel := context.WithTimeout(a.ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats, err := a.local.Stats(ctx)
|
||||
if err == nil {
|
||||
events, eerr := a.local.Events(ctx, 40)
|
||||
if eerr == nil {
|
||||
return LiveSnapshot{Stats: stats, Events: events}, nil
|
||||
}
|
||||
}
|
||||
// No engine here. If nobody is signed in either, the honest answer is
|
||||
// still the local error - there is nothing else to show and the person
|
||||
// is most likely setting this PC up.
|
||||
if !a.cloud.LoggedIn() {
|
||||
return LiveSnapshot{}, err
|
||||
}
|
||||
return a.liveFromCloud(ctx)
|
||||
}
|
||||
|
||||
// liveFromCloud builds the same shape the Live screen already renders, out of
|
||||
// the estate's own feed, so the view needs no second code path.
|
||||
func (a *App) liveFromCloud(ctx context.Context) (LiveSnapshot, error) {
|
||||
sites, err := a.cloud.Sites(ctx)
|
||||
if err != nil {
|
||||
return LiveSnapshot{}, err
|
||||
}
|
||||
events, err := a.local.Events(ctx, 40)
|
||||
arrivals, err := a.cloud.Arrivals(ctx, 40)
|
||||
if err != nil {
|
||||
return LiveSnapshot{}, err
|
||||
}
|
||||
return LiveSnapshot{Stats: stats, Events: events}, nil
|
||||
|
||||
// The counters are summed across the estate, and fraction_below_gate
|
||||
// takes the WORST site rather than an average - one badly placed camera
|
||||
// is a hole in the numbers, and averaging it against three good ones
|
||||
// hides the only site anyone needs to visit. Same rule the heartbeat
|
||||
// already follows.
|
||||
var up, total int
|
||||
worst := 0.0
|
||||
people := map[string]struct{}{}
|
||||
for _, s := range sites {
|
||||
up, total = up+s.CamerasUp, total+s.CamerasTotal
|
||||
if s.FractionBelowGate > worst {
|
||||
worst = s.FractionBelowGate
|
||||
}
|
||||
}
|
||||
events := make([]map[string]any, 0, len(arrivals))
|
||||
for _, v := range arrivals {
|
||||
if v.VisitorID != "" {
|
||||
people[v.VisitorID] = struct{}{}
|
||||
}
|
||||
events = append(events, map[string]any{
|
||||
"type": map[bool]string{true: "person.new", false: "person.seen"}[v.IsNew],
|
||||
"ts": v.OccurredAt, "camera_id": v.CameraID,
|
||||
"data": map[string]any{
|
||||
"label": v.Label, "ref": v.Ref, "site": v.Site,
|
||||
"similarity": v.Similarity, "attributes": v.Attributes,
|
||||
},
|
||||
})
|
||||
}
|
||||
return LiveSnapshot{
|
||||
Viewing: true,
|
||||
Events: events,
|
||||
Stats: map[string]any{
|
||||
"cameras": []map[string]any{},
|
||||
"gallery": map[string]any{"identities": len(people), "sightings": len(arrivals)},
|
||||
"cameras_up": up, "cameras_total": total,
|
||||
"fraction_below_gate": worst,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- reports --
|
||||
@@ -659,6 +932,15 @@ func (a *App) Sites() ([]cloud.SiteHealth, error) {
|
||||
}
|
||||
|
||||
// VisitorHistory is one customer's timeline, for the customer record screen.
|
||||
// Ask is the help panel. It needs head office: the assistant runs there,
|
||||
// against this company's own data, as this signed-in user. A PC running on
|
||||
// its own has nobody to ask, and the panel says so rather than erroring.
|
||||
func (a *App) Ask(history []cloud.AssistantTurn) (cloud.AssistantAnswer, error) {
|
||||
ctx, cancel := context.WithTimeout(a.ctx, 90*time.Second)
|
||||
defer cancel()
|
||||
return a.cloud.Ask(ctx, history)
|
||||
}
|
||||
|
||||
func (a *App) VisitorHistory(id string, limit int) ([]cloud.Visit, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
@@ -733,3 +1015,12 @@ func envOr(key, def string) string {
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// localWithCreds builds the engine client with a credential resolver, so a
|
||||
// first run - where the engine writes its credential after the app has looked
|
||||
// for it - recovers by itself instead of 401ing for the life of the process.
|
||||
func localWithCreds(base string, cfg agentcfg.Config) *local.Client {
|
||||
c := local.New(base, cfg.APIUser, cfg.APIPassword)
|
||||
c.Creds = agentcfg.NewCreds(agentpaths.APICredentials(), cfg.APIUser, cfg.APIPassword)
|
||||
return c
|
||||
}
|
||||
|
||||
BIN
desktop/build/appicon.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
68
desktop/build/darwin/Info.dev.plist
Normal file
@@ -0,0 +1,68 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>{{.Info.ProductName}}</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>{{.OutputFilename}}</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.wails.{{safeBundleID .Name}}</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>{{.Info.ProductVersion}}</string>
|
||||
<key>CFBundleGetInfoString</key>
|
||||
<string>{{.Info.Comments}}</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>{{.Info.ProductVersion}}</string>
|
||||
<key>CFBundleIconFile</key>
|
||||
<string>iconfile</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>10.13.0</string>
|
||||
<key>NSHighResolutionCapable</key>
|
||||
<string>true</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>{{.Info.Copyright}}</string>
|
||||
{{if .Info.FileAssociations}}
|
||||
<key>CFBundleDocumentTypes</key>
|
||||
<array>
|
||||
{{range .Info.FileAssociations}}
|
||||
<dict>
|
||||
<key>CFBundleTypeExtensions</key>
|
||||
<array>
|
||||
<string>{{.Ext}}</string>
|
||||
</array>
|
||||
<key>CFBundleTypeName</key>
|
||||
<string>{{.Name}}</string>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>{{.Role}}</string>
|
||||
<key>CFBundleTypeIconFile</key>
|
||||
<string>{{.IconName}}</string>
|
||||
</dict>
|
||||
{{end}}
|
||||
</array>
|
||||
{{end}}
|
||||
{{if .Info.Protocols}}
|
||||
<key>CFBundleURLTypes</key>
|
||||
<array>
|
||||
{{range .Info.Protocols}}
|
||||
<dict>
|
||||
<key>CFBundleURLName</key>
|
||||
<string>com.wails.{{.Scheme}}</string>
|
||||
<key>CFBundleURLSchemes</key>
|
||||
<array>
|
||||
<string>{{.Scheme}}</string>
|
||||
</array>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>{{.Role}}</string>
|
||||
</dict>
|
||||
{{end}}
|
||||
</array>
|
||||
{{end}}
|
||||
<key>NSAppTransportSecurity</key>
|
||||
<dict>
|
||||
<key>NSAllowsLocalNetworking</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
63
desktop/build/darwin/Info.plist
Normal file
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>{{.Info.ProductName}}</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>{{.OutputFilename}}</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.wails.{{safeBundleID .Name}}</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>{{.Info.ProductVersion}}</string>
|
||||
<key>CFBundleGetInfoString</key>
|
||||
<string>{{.Info.Comments}}</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>{{.Info.ProductVersion}}</string>
|
||||
<key>CFBundleIconFile</key>
|
||||
<string>iconfile</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>10.13.0</string>
|
||||
<key>NSHighResolutionCapable</key>
|
||||
<string>true</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>{{.Info.Copyright}}</string>
|
||||
{{if .Info.FileAssociations}}
|
||||
<key>CFBundleDocumentTypes</key>
|
||||
<array>
|
||||
{{range .Info.FileAssociations}}
|
||||
<dict>
|
||||
<key>CFBundleTypeExtensions</key>
|
||||
<array>
|
||||
<string>{{.Ext}}</string>
|
||||
</array>
|
||||
<key>CFBundleTypeName</key>
|
||||
<string>{{.Name}}</string>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>{{.Role}}</string>
|
||||
<key>CFBundleTypeIconFile</key>
|
||||
<string>{{.IconName}}</string>
|
||||
</dict>
|
||||
{{end}}
|
||||
</array>
|
||||
{{end}}
|
||||
{{if .Info.Protocols}}
|
||||
<key>CFBundleURLTypes</key>
|
||||
<array>
|
||||
{{range .Info.Protocols}}
|
||||
<dict>
|
||||
<key>CFBundleURLName</key>
|
||||
<string>com.wails.{{.Scheme}}</string>
|
||||
<key>CFBundleURLSchemes</key>
|
||||
<array>
|
||||
<string>{{.Scheme}}</string>
|
||||
</array>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>{{.Role}}</string>
|
||||
</dict>
|
||||
{{end}}
|
||||
</array>
|
||||
{{end}}
|
||||
</dict>
|
||||
</plist>
|
||||
BIN
desktop/build/windows/icon.ico
Normal file
|
After Width: | Height: | Size: 67 KiB |
15
desktop/build/windows/info.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"fixed": {
|
||||
"file_version": "{{.Info.ProductVersion}}"
|
||||
},
|
||||
"info": {
|
||||
"0000": {
|
||||
"ProductVersion": "{{.Info.ProductVersion}}",
|
||||
"CompanyName": "{{.Info.CompanyName}}",
|
||||
"FileDescription": "{{.Info.ProductName}}",
|
||||
"LegalCopyright": "{{.Info.Copyright}}",
|
||||
"ProductName": "{{.Info.ProductName}}",
|
||||
"Comments": "{{.Info.Comments}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
15
desktop/build/windows/wails.exe.manifest
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
|
||||
<assemblyIdentity type="win32" name="com.wails.{{.Name}}" version="{{.Info.ProductVersion}}.0" processorArchitecture="*"/>
|
||||
<dependency>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
|
||||
</dependentAssembly>
|
||||
</dependency>
|
||||
<asmv3:application>
|
||||
<asmv3:windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware> <!-- fallback for Windows 7 and 8 -->
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness> <!-- falls back to per-monitor if per-monitor v2 is not supported -->
|
||||
</asmv3:windowsSettings>
|
||||
</asmv3:application>
|
||||
</assembly>
|
||||
25
desktop/darwin_link.go
Normal file
@@ -0,0 +1,25 @@
|
||||
//go:build darwin
|
||||
|
||||
// Link the framework Wails' darwin frontend forgets.
|
||||
//
|
||||
// It references UTType (UniformTypeIdentifiers) without linking it, so a macOS
|
||||
// build fails at the LINK step with `Undefined symbols: _OBJC_CLASS_$_UTType`
|
||||
// - after compiling everything successfully, which makes it read like a broken
|
||||
// toolchain rather than one missing flag. That is why there was no Mac build:
|
||||
// not a design limit, a link error nobody had chased.
|
||||
//
|
||||
// Declared in the source rather than passed as CGO_LDFLAGS on the command
|
||||
// line, for the same reason deploy.sh now finds Go itself: a build that needs
|
||||
// the operator to know an incantation is a build that does not happen. Plain
|
||||
// `go build` and `wails build` both work on a Mac with this file present, and
|
||||
// the build tag makes it inert everywhere else.
|
||||
//
|
||||
// Note for anyone editing: the comment directly above `import "C"` is cgo's C
|
||||
// PREAMBLE, not documentation. This paragraph sits above `package main` on
|
||||
// purpose - put it there and the prose is compiled as C, which is how the
|
||||
// first attempt failed.
|
||||
|
||||
package main
|
||||
|
||||
// #cgo LDFLAGS: -framework UniformTypeIdentifiers
|
||||
import "C"
|
||||
1
desktop/frontend/dist/assets/index-DOJ2bRrM.css
vendored
Normal file
40
desktop/frontend/dist/assets/index-DSNu_2FW.js
vendored
Normal file
40
desktop/frontend/dist/assets/index-Dxm9O68-.js
vendored
BIN
desktop/frontend/dist/assets/loyaly-mark-Ch-P3VSF.png
vendored
Normal file
|
After Width: | Height: | Size: 96 KiB |
4
desktop/frontend/dist/index.html
vendored
@@ -4,8 +4,8 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Behavision</title>
|
||||
<script type="module" crossorigin src="./assets/index-Dxm9O68-.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-Bml5ogS2.css">
|
||||
<script type="module" crossorigin src="./assets/index-DSNu_2FW.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-DOJ2bRrM.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
@@ -2,11 +2,13 @@ import { useCallback, useEffect, useState } from 'react'
|
||||
import { api, isDesktop, message } from './bridge.js'
|
||||
import { usePolled } from './hooks.js'
|
||||
import * as Icon from './ui/icons.jsx'
|
||||
import logo from './assets/loyaly-mark.png'
|
||||
import Login from './views/Login.jsx'
|
||||
import Setup from './views/Setup.jsx'
|
||||
import Live from './views/Live.jsx'
|
||||
import Customers from './views/Customers.jsx'
|
||||
import Cameras from './views/Cameras.jsx'
|
||||
import Assistant from './views/Assistant.jsx'
|
||||
|
||||
// Three screens, and the trim is by AUDIENCE rather than by taste.
|
||||
//
|
||||
@@ -35,12 +37,23 @@ export default function App() {
|
||||
// A standalone PC can join head office later. That is the same Setup screen,
|
||||
// reached deliberately rather than because the app will not open otherwise.
|
||||
const [linking, setLinking] = useState(false)
|
||||
const [helping, setHelping] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try { setSession(await api.session()) } catch { setSession(null) }
|
||||
setBooting(false)
|
||||
})()
|
||||
let alive = true
|
||||
const load = async () => {
|
||||
try {
|
||||
const s = await api.session()
|
||||
if (alive) setSession(prev => JSON.stringify(prev) === JSON.stringify(s) ? prev : s)
|
||||
} catch { if (alive) setSession(null) }
|
||||
if (alive) setBooting(false)
|
||||
}
|
||||
load()
|
||||
// Re-read every few seconds: a session the server has ended - or one
|
||||
// that never belonged to this head office - must put Login back on
|
||||
// screen, not leave "session expired" banners on every page.
|
||||
const id = setInterval(load, 8000)
|
||||
return () => { alive = false; clearInterval(id) }
|
||||
}, [])
|
||||
|
||||
if (!isDesktop()) {
|
||||
@@ -49,7 +62,7 @@ export default function App() {
|
||||
// error nobody will read.
|
||||
return (
|
||||
<div className="login"><div className="box">
|
||||
<span className="mark"><Icon.Shield size={20} /></span>
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<h1>Behavision</h1>
|
||||
<p className="lead">
|
||||
This is the Behavision window running outside the app, so it has no
|
||||
@@ -80,7 +93,7 @@ export default function App() {
|
||||
<div className="shell">
|
||||
<aside className="side">
|
||||
<div className="brand">
|
||||
<span className="mark"><Icon.Shield size={17} /></span>
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<div className="id">
|
||||
<h1>Behavision</h1>
|
||||
<p>{session.site_name || session.user?.client_name || 'This shop'}</p>
|
||||
@@ -119,7 +132,17 @@ export default function App() {
|
||||
</>}
|
||||
</div>
|
||||
</aside>
|
||||
<main className="main"><Current session={session} /></main>
|
||||
<main className="main">
|
||||
<Current session={session} onNavigate={setView} />
|
||||
{/* Loya's door, top right of every screen. A buddy you have to find in
|
||||
a sidebar is not around; one in the corner is. */}
|
||||
{!helping && (
|
||||
<button className="loya-fab" onClick={() => setHelping(true)} aria-label="Ask Loya" title="Ask Loya">
|
||||
<img src={logo} alt="" /><span>Loya</span>
|
||||
</button>
|
||||
)}
|
||||
</main>
|
||||
{helping && <Assistant session={session} onClose={() => setHelping(false)} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -149,6 +172,7 @@ function EngineBox() {
|
||||
// what this panel showed while recognition was visibly running.
|
||||
if (!running && s.reachable) { tone = 'warn'; text = 'Running outside the app' }
|
||||
else if (s.state === 'failed' || s.state === 'backoff') { tone = 'bad'; text = 'Not running' }
|
||||
else if (running && !s.reachable && s.progress) { tone = 'warn'; text = `Downloading ${s.progress.what}… ${s.progress.percent}%` }
|
||||
else if (running && !s.reachable) { tone = 'warn'; text = 'Starting…' }
|
||||
else if (running && cams.length === 0) { tone = 'warn'; text = 'No cameras' }
|
||||
else if (running && up === 0) { tone = 'bad'; text = 'No camera connected' }
|
||||
|
||||
BIN
desktop/frontend/src/assets/loyaly-mark.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
@@ -32,11 +32,15 @@ export const api = {
|
||||
|
||||
cameras: () => call('Cameras'),
|
||||
testCamera: (cam) => call('TestCamera', cam),
|
||||
discoverCameras: () => call('DiscoverCameras'),
|
||||
saveCamera: (id, cam) => call('SaveCamera', id, cam),
|
||||
deleteCamera: (id) => call('DeleteCamera', id),
|
||||
startPlacement: (id, seconds) => call('StartPlacementCheck', id, seconds),
|
||||
placementResult: (id) => call('PlacementResult', id),
|
||||
streamURL: (id) => call('StreamURL', id),
|
||||
// The live view of a camera in another building, relayed through head
|
||||
// office. Empty when nobody is signed in.
|
||||
remoteStreamURL: (id) => call('RemoteStreamURL', id),
|
||||
|
||||
live: () => call('Live'),
|
||||
pipelineStatus: () => call('PipelineStatus'),
|
||||
@@ -51,6 +55,7 @@ export const api = {
|
||||
sales: (from, to) => call('Sales', from, to),
|
||||
customers: (q, limit) => call('Customers', q, limit),
|
||||
saveProfile: (p) => call('SaveProfile', p),
|
||||
ask: (history) => call('Ask', history),
|
||||
recordPurchase: (id, amount, items, notes) =>
|
||||
call('RecordPurchase', id, amount, items, notes),
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
import './styles.css'
|
||||
|
||||
// Dev only: ?mock=<scenario> renders the app in a browser with fake bindings.
|
||||
if (import.meta.env.DEV) await import('./mock.js')
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<React.StrictMode><App /></React.StrictMode>
|
||||
)
|
||||
|
||||
83
desktop/frontend/src/mock.js
Normal file
@@ -0,0 +1,83 @@
|
||||
// A stand-in for the Go bindings, for looking at screens in a browser.
|
||||
//
|
||||
// The app only exists inside Wails, so until this there was no way to put a
|
||||
// screen in front of somebody without a Windows build - which is how the
|
||||
// first-run experience went unreviewed. Loaded ONLY by the dev server and only
|
||||
// with ?mock=<scenario>; a production bundle never contains it.
|
||||
//
|
||||
// ?mock=fresh first launch after install: unclaimed, engine starting
|
||||
// ?mock=standalone chose "run on this PC only", no cameras yet
|
||||
// ?mock=claimed claimed, signed in, one camera, arrivals flowing
|
||||
//
|
||||
// State is in memory and advances as you click, so the flow can be walked.
|
||||
const scenario = new URLSearchParams(location.search).get('mock')
|
||||
|
||||
if (scenario) {
|
||||
const st = {
|
||||
claimed: scenario === 'claimed',
|
||||
standalone: scenario === 'standalone',
|
||||
logged_in: scenario === 'claimed',
|
||||
cameras: scenario === 'claimed' ? [{id: 'entrance', label: 'Entrance', host: '192.168.1.122', port: 554, path: '/ch0_1.264', username: 'admin', has_password: true, enabled: true}] : [],
|
||||
engine: scenario === 'fresh' ? 'starting' : 'running',
|
||||
startedAt: Date.now(),
|
||||
}
|
||||
const user = {id: 'u1', email: 'suriya@tenext.in', full_name: 'Suriya', role: 'owner', client_id: 'c1', client_name: 'TeNext Retail'}
|
||||
const session = () => ({logged_in: st.logged_in, user: st.logged_in ? user : {}, site_name: st.claimed ? 'TeNext Chennai' : '', claimed: st.claimed, standalone: st.standalone && !st.claimed})
|
||||
const now = () => new Date().toISOString()
|
||||
const arrivals = () => st.cameras.length === 0 ? [] : [
|
||||
{type: 'person.seen', ts: now(), camera_id: 'entrance', data: {label: 'Visitor 3', identity_id: 3, similarity: 0.61, gender: 'Male', age: 34, emotion: 'neutral'}},
|
||||
{type: 'person.new', ts: new Date(Date.now() - 95e3).toISOString(), camera_id: 'entrance', data: {label: 'Visitor 7', identity_id: 7, gender: 'Female', age: 28}},
|
||||
{type: 'person.seen', ts: new Date(Date.now() - 410e3).toISOString(), camera_id: 'entrance', data: {label: 'Priya', identity_id: 2, similarity: 0.72, gender: 'Female', age: 41}},
|
||||
]
|
||||
const delay = (v, ms = 120) => new Promise(r => setTimeout(() => r(v), ms))
|
||||
const App = {
|
||||
Session: () => delay(session()),
|
||||
Login: (email) => { st.logged_in = true; user.email = email || user.email; return delay(session()) },
|
||||
Logout: () => { st.logged_in = false; return delay(session()) },
|
||||
Claim: (code) => code.replace(/[^A-Z0-9]/gi, '').length >= 20
|
||||
? (st.claimed = true, st.standalone = false, delay(session(), 900))
|
||||
: Promise.reject(new Error('That installation code is not valid. Ask for a new one.')),
|
||||
RunStandalone: () => { st.standalone = true; return delay(session()) },
|
||||
EngineStatus: () => {
|
||||
// The engine takes a minute or two on first run (models download).
|
||||
if (st.engine === 'starting' && Date.now() - st.startedAt > 20000) st.engine = 'running'
|
||||
const cams = Object.fromEntries(st.cameras.map(c => [c.id, true]))
|
||||
return delay({state: st.engine === 'starting' ? 'running' : 'running', reachable: st.engine !== 'starting', recognition_model: st.engine === 'starting' ? '' : 'w600k_r50', cameras: cams, restarts: 0})
|
||||
},
|
||||
StartEngine: () => delay({state: 'running', reachable: true}),
|
||||
StopEngine: () => delay({state: 'stopped', reachable: false}),
|
||||
Cameras: () => delay(st.cameras.map(c => ({...c, connected: true, frames: 1200, faces: 9}))),
|
||||
DiscoverCameras: () => delay({networks: ['192.168.1.0/24'], cameras: [
|
||||
{host: '192.168.1.122', rtsp: true, onvif: true, name: 'HIKVISION DS-2CD2043G2', make: 'hikvision'},
|
||||
{host: '192.168.1.121', rtsp: true, onvif: true, name: 'IPC-model IPC', make: ''},
|
||||
{host: '192.168.1.40', rtsp: true, onvif: false, name: '', make: ''},
|
||||
]}, 2500),
|
||||
TestCamera: (cam) => delay({ok: Boolean(cam.host), width: 800, height: 448, codec: 'hevc', error: cam.host ? '' : 'no host'}, 1500),
|
||||
SaveCamera: (id, cam) => { const c = {id: id || cam.id || 'cam' + (st.cameras.length + 1), ...cam, has_password: Boolean(cam.password)}; delete c.password; st.cameras = [...st.cameras.filter(x => x.id !== c.id), c]; return delay(c) },
|
||||
DeleteCamera: (id) => { st.cameras = st.cameras.filter(c => c.id !== id); return delay(null) },
|
||||
StartPlacementCheck: () => delay({state: 'running'}),
|
||||
PlacementResult: () => delay({state: 'finished', verdict: 'good', headline: 'faces recognised on a walk-past', advice: []}),
|
||||
StreamURL: () => '',
|
||||
Live: () => delay({stats: {cameras: st.cameras.map(c => ({camera_id: c.id, connected: true, pipeline: {best_quality: {n: 40, fraction_below_gate: 0.18}}})), gallery: {identities: st.cameras.length ? 7 : 0, sightings: st.cameras.length ? 44 : 0}}, events: arrivals()}),
|
||||
PipelineStatus: () => delay({webhook_url: 'http://127.0.0.1:53658/events', queued: 0, dropped: 0, claimed: st.claimed, standalone: st.standalone && !st.claimed, broker_up: st.claimed, accepted: st.claimed ? 12 : 0}),
|
||||
LocalIdentities: () => delay([]), LocalSightings: () => delay([]),
|
||||
Footfall: () => delay({total: 0, buckets: []}), Sites: () => delay([]),
|
||||
VisitorHistory: () => delay([]), VisitorPhoto: () => delay({available: false, reason: 'This system is not storing images.'}),
|
||||
ForgetCustomer: () => delay(null), Sales: () => delay([]),
|
||||
Customers: () => delay([{id: 'v1', label: 'Priya', number: 2, first_seen_at: now(), last_seen_at: now(), visits: 6}]),
|
||||
SaveProfile: () => delay(null), RecordPurchase: () => delay(null),
|
||||
Ask: (history) => {
|
||||
const q = history[history.length - 1]?.text ?? ''
|
||||
if (!st.claimed) return Promise.reject(new Error('The assistant is not switched on for this server.'))
|
||||
const text = /camera/i.test(q)
|
||||
? 'Go to Cameras and press Add camera. The address is on a sticker on the camera itself; pick the make and I fill in the stream path. Test it, save it, then walk past it once so I can tell you whether the placement works.'
|
||||
: /code|install/i.test(q)
|
||||
? 'Whoever runs head office makes one: open the shop there, press Set up a shop PC, and read the code out. It works once.'
|
||||
: /who|morning|came/i.test(q)
|
||||
? 'Three people so far: Priya at 13:12 (her sixth visit), a new face at 13:18 I have called Visitor 7, and Visitor 3 just now.'
|
||||
: 'Chennai is online and the door camera is connected, but nobody has proved it yet. Walk past it once with Check placement running and I will tell you if it can actually see faces - until then a quiet screen might just be a badly aimed camera.'
|
||||
return delay({text, used: /camera|code/i.test(q) ? [] : ['site_status', 'cameras']}, 1400)
|
||||
},
|
||||
}
|
||||
window.go = {main: {App}}
|
||||
}
|
||||
@@ -98,18 +98,15 @@ input, textarea { user-select: text; }
|
||||
|
||||
/* ================================================================ shell == */
|
||||
|
||||
.shell { display: grid; grid-template-columns: 232px 1fr; height: 100%; }
|
||||
.shell { display: grid; grid-template-columns: 232px 1fr auto; height: 100%; }
|
||||
|
||||
.side {
|
||||
background: var(--s1); border-right: 1px solid var(--line);
|
||||
display: flex; flex-direction: column; min-height: 0;
|
||||
}
|
||||
.side .brand { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-5) var(--sp-5) var(--sp-4); }
|
||||
.side .brand .mark {
|
||||
width: 30px; height: 30px; border-radius: 9px; flex: none; display: grid; place-items: center;
|
||||
background: linear-gradient(160deg, var(--accent-2), var(--s2));
|
||||
border: 1px solid #1B4C5A; color: var(--accent);
|
||||
}
|
||||
.side .brand .mark { width: 30px; height: 30px; flex: none; display: grid; place-items: center; }
|
||||
.side .brand .mark img, .login .mark img { width: 100%; height: 100%; object-fit: contain; display: block; }
|
||||
.side .brand .id { min-width: 0; }
|
||||
.side .brand h1 { font-family: var(--font-display); font-size: 15px; font-weight: 600; letter-spacing: -.012em; line-height: 1.2; }
|
||||
.side .brand p { font-size: 11.5px; color: var(--ink-3); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
@@ -150,7 +147,7 @@ input, textarea { user-select: text; }
|
||||
.side .who .id b { display: block; font-size: 12.5px; font-weight: 550; }
|
||||
.side .who .id span { display: block; font-size: 11px; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
|
||||
.main { min-width: 0; min-height: 0; overflow: auto; }
|
||||
.main { min-width: 0; min-height: 0; overflow: auto; position: relative; }
|
||||
|
||||
/* ================================================================= page == */
|
||||
|
||||
@@ -411,11 +408,7 @@ tbody tr[role="button"]:hover, tbody tr.clickable:hover { background: var(--s2);
|
||||
width: 100%; max-width: 396px; background: var(--s1); border: 1px solid var(--line);
|
||||
border-radius: var(--r-lg); padding: var(--sp-7); box-shadow: var(--shadow-lg);
|
||||
}
|
||||
.login .mark {
|
||||
width: 38px; height: 38px; border-radius: 11px; margin-bottom: var(--sp-4);
|
||||
display: grid; place-items: center; color: var(--accent);
|
||||
background: linear-gradient(160deg, var(--accent-2), var(--s2)); border: 1px solid #1B4C5A;
|
||||
}
|
||||
.login .mark { width: 44px; height: 44px; margin-bottom: var(--sp-4); display: grid; place-items: center; }
|
||||
.login h1 { font-family: var(--font-display); font-size: 21px; font-weight: 600; letter-spacing: -.022em; }
|
||||
.login .lead { margin: 6px 0 var(--sp-5); }
|
||||
.login .btn { width: 100%; justify-content: center; margin-top: var(--sp-1); }
|
||||
@@ -474,3 +467,199 @@ tbody tr[role="button"]:hover, tbody tr.clickable:hover { background: var(--s2);
|
||||
inside the panel rather than pushing the metrics off the bottom. */
|
||||
.arrivals-panel { max-height: 64vh; margin-bottom: var(--sp-4); }
|
||||
.arrivals-panel .arrivals { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: var(--sp-2); }
|
||||
|
||||
/* =============================================================== cameras == */
|
||||
|
||||
.pagehead { display: flex; justify-content: space-between; align-items: flex-end; gap: var(--sp-4); }
|
||||
.page > header.pagehead { margin-bottom: var(--sp-5); }
|
||||
|
||||
.camgrid { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fill, minmax(440px, 1fr)); }
|
||||
.camcard { background: var(--s1); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; display: flex; flex-direction: column; }
|
||||
.camview { position: relative; aspect-ratio: 16 / 9; background: #05090C; }
|
||||
.camview img { width: 100%; height: 100%; object-fit: cover; display: block; }
|
||||
.camview .placeholder { width: 100%; height: 100%; display: grid; place-items: center; color: var(--ink-3); }
|
||||
.camview .pill.over { position: absolute; top: 10px; right: 10px; backdrop-filter: blur(6px); }
|
||||
.cambody { padding: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
|
||||
.camtitle { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--sp-3); }
|
||||
.camtitle h3 { font-family: var(--font-display); font-size: 16px; font-weight: 600; letter-spacing: -.012em; text-transform: none; color: var(--ink); margin: 0 0 2px; }
|
||||
.camtitle .note { font-size: 12px; }
|
||||
.camactions { display: flex; gap: 6px; flex: none; }
|
||||
.camproof { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: var(--sp-3);
|
||||
padding: var(--sp-3); border-radius: var(--r); background: var(--s2); border: 1px solid var(--line-2); }
|
||||
.camproof .note { font-size: 12px; line-height: 1.45; }
|
||||
@media (max-width: 640px) { .camproof { grid-template-columns: 1fr; } }
|
||||
|
||||
.empty.tall { padding: var(--sp-8) var(--sp-6); }
|
||||
.empty.tall .btn { margin-top: var(--sp-3); }
|
||||
|
||||
/* The sheet is a form that reads top to bottom: a sentence saying what is
|
||||
needed, three short sections, the result of the test, the actions. */
|
||||
.drawer .sheet { width: min(600px, 100%); }
|
||||
.sheetbody .lead { color: var(--ink-2); font-size: 13.5px; line-height: 1.55; margin-bottom: var(--sp-5); }
|
||||
.formsection { margin-bottom: var(--sp-5); }
|
||||
.formsection h4 { font-size: 11px; font-weight: 600; letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3); margin: 0 0 var(--sp-3); padding-bottom: 6px; border-bottom: 1px solid var(--line-2); }
|
||||
.field .hint { font-style: normal; }
|
||||
.fieldrow { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
|
||||
.fieldrow .field.narrow { max-width: 140px; }
|
||||
.fieldrow:has(.field.narrow) { grid-template-columns: minmax(0, 1fr) 140px; }
|
||||
.field input.mono { font-family: var(--font-mono); font-size: 12.5px; }
|
||||
.sheetactions { display: flex; gap: var(--sp-2); justify-content: flex-end; padding-top: var(--sp-4); border-top: 1px solid var(--line-2); margin-top: var(--sp-2); }
|
||||
.testresult { display: flex; gap: var(--sp-3); align-items: flex-start; padding: var(--sp-3) var(--sp-4); border-radius: var(--r); margin-bottom: var(--sp-4); border: 1px solid; font-size: 13px; }
|
||||
.testresult.ok { background: var(--ok-2); border-color: color-mix(in srgb, var(--ok) 30%, transparent); color: var(--ok); }
|
||||
.testresult.bad { background: var(--bad-2); border-color: color-mix(in srgb, var(--bad) 30%, transparent); color: var(--bad); }
|
||||
.testresult b { display: block; }
|
||||
.testresult span { display: block; color: var(--ink-2); margin-top: 2px; }
|
||||
.testresult img { width: 100%; margin-top: var(--sp-3); border-radius: var(--r-sm); border: 1px solid var(--line); display: block; }
|
||||
|
||||
/* Placement check: two numbered steps, then a verdict box in the tone of the answer. */
|
||||
.steps { list-style: none; counter-reset: step; margin: 0 0 var(--sp-5); padding: 0; display: grid; gap: var(--sp-3); }
|
||||
.steps li { counter-increment: step; position: relative; padding: var(--sp-3) var(--sp-4) var(--sp-3) 52px; border-radius: var(--r); border: 1px solid var(--line-2); background: var(--s2); opacity: .55; }
|
||||
.steps li.now, .steps li.done { opacity: 1; }
|
||||
.steps li::before { content: counter(step); position: absolute; left: 16px; top: 14px; width: 24px; height: 24px; border-radius: 50%;
|
||||
display: grid; place-items: center; font-size: 12px; font-weight: 600; background: var(--s3); color: var(--ink-2); border: 1px solid var(--line); }
|
||||
.steps li.now::before { background: var(--accent); color: #041014; border-color: transparent; }
|
||||
.steps li.done::before { content: '✓'; background: var(--ok-2); color: var(--ok); }
|
||||
.steps b { display: block; font-size: 14px; }
|
||||
.steps span { display: block; font-size: 12.5px; color: var(--ink-2); line-height: 1.5; margin-top: 2px; }
|
||||
.progress { height: 4px; background: var(--s3); border-radius: 2px; overflow: hidden; margin-top: var(--sp-3); }
|
||||
.progress > div { height: 100%; background: var(--accent); transition: width .4s linear; }
|
||||
.verdict { padding: var(--sp-4); border-radius: var(--r); border: 1px solid var(--line); background: var(--s2); }
|
||||
.verdict.ok { border-color: color-mix(in srgb, var(--ok) 35%, transparent); background: var(--ok-2); }
|
||||
.verdict.warn { border-color: color-mix(in srgb, var(--warn) 35%, transparent); background: var(--warn-2); }
|
||||
.verdict.bad { border-color: color-mix(in srgb, var(--bad) 35%, transparent); background: var(--bad-2); }
|
||||
.verdict-head { display: flex; align-items: center; gap: var(--sp-2); font-size: 15px; }
|
||||
.verdict.ok .verdict-head { color: var(--ok); } .verdict.warn .verdict-head { color: var(--warn); } .verdict.bad .verdict-head { color: var(--bad); }
|
||||
.verdict ul { margin: var(--sp-3) 0 0 18px; font-size: 13px; color: var(--ink); line-height: 1.5; }
|
||||
.verdict ul li { margin-bottom: 5px; }
|
||||
.verdict .note { margin-top: var(--sp-3); }
|
||||
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; display: inline-block; }
|
||||
@keyframes spin { to { transform: rotate(360deg) } }
|
||||
|
||||
/* ============================================================== assistant == */
|
||||
|
||||
.helpbtn { display: flex; align-items: center; gap: 9px; margin: 0 var(--sp-3) var(--sp-3); padding: 9px 10px; border-radius: var(--r);
|
||||
border: 1px solid var(--line); background: var(--s2); color: var(--ink); cursor: pointer; font-size: 12.5px; font-weight: 550; text-align: left; }
|
||||
.helpbtn img { width: 18px; height: 18px; object-fit: contain; }
|
||||
.helpbtn span { flex: 1; }
|
||||
.helpbtn:hover, .helpbtn[aria-pressed="true"] { border-color: color-mix(in srgb, var(--accent) 45%, transparent); background: var(--accent-3); }
|
||||
|
||||
.helper { width: 380px; height: 100%; display: flex; flex-direction: column; min-height: 0;
|
||||
background: var(--s1); border-left: 1px solid var(--line); animation: slidein-r .2s cubic-bezier(.2,.8,.3,1); }
|
||||
.helperhead { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-4); border-bottom: 1px solid var(--line); }
|
||||
.helperhead img { width: 26px; height: 26px; object-fit: contain; }
|
||||
.helperhead div { flex: 1; min-width: 0; }
|
||||
.helperhead b { display: block; font-family: var(--font-display); font-size: 14.5px; }
|
||||
.helperhead span { display: block; font-size: 11.5px; color: var(--ink-3); margin-top: 1px; }
|
||||
.helperhead .close { background: none; border: 0; color: var(--ink-3); cursor: pointer; padding: 6px; border-radius: var(--r-sm); display: grid; place-items: center; }
|
||||
.helperhead .close:hover { background: var(--s2); color: var(--ink); }
|
||||
.helperbody { flex: 1; overflow: auto; padding: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
|
||||
.helperintro p { color: var(--ink-2); font-size: 13px; line-height: 1.55; margin-bottom: var(--sp-3); }
|
||||
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.chip { border: 1px solid var(--line); background: var(--s2); color: var(--ink); border-radius: 99px; padding: 6px 11px; font-size: 12px; cursor: pointer; text-align: left; }
|
||||
.chip:hover { border-color: var(--accent); color: var(--accent); }
|
||||
.chip:disabled { opacity: .5; cursor: default; }
|
||||
.helpernote { display: flex; gap: var(--sp-3); padding: var(--sp-3); border-radius: var(--r); background: var(--warn-2); border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent); color: var(--warn); font-size: 12.5px; }
|
||||
.helpernote b { display: block; } .helpernote span { display: block; color: var(--ink-2); margin-top: 2px; line-height: 1.45; }
|
||||
.turn { display: flex; flex-direction: column; gap: 4px; }
|
||||
.turn.user { align-items: flex-end; }
|
||||
.bubble { max-width: 92%; padding: 10px 13px; border-radius: 14px; font-size: 13.5px; line-height: 1.5; white-space: pre-wrap; }
|
||||
.turn.user .bubble { background: var(--accent); color: #041014; border-bottom-right-radius: 4px; }
|
||||
.turn.assistant .bubble { background: var(--s2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
|
||||
.turn .used { font-size: 11px; color: var(--ink-3); padding: 0 4px; }
|
||||
.bubble.thinking { display: flex; gap: 4px; padding: 12px 14px; }
|
||||
.bubble.thinking span { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); animation: blink 1.2s infinite ease-in-out; }
|
||||
.bubble.thinking span:nth-child(2) { animation-delay: .2s } .bubble.thinking span:nth-child(3) { animation-delay: .4s }
|
||||
@keyframes blink { 0%, 80%, 100% { opacity: .25 } 40% { opacity: 1 } }
|
||||
.helperask { display: flex; gap: var(--sp-2); padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--line); }
|
||||
.helperask input { flex: 1; min-width: 0; background: var(--s2); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 12px; font-size: 13.5px; color: var(--ink); }
|
||||
.helperask input:focus { outline: none; border-color: var(--accent); }
|
||||
.helperask .btn { padding: 0 12px; }
|
||||
.quickhelp { margin-top: var(--sp-5); border-top: 1px solid var(--line-2); padding-top: var(--sp-4); }
|
||||
.quickhelp dt { font-size: 12.5px; font-weight: 600; margin-top: var(--sp-3); }
|
||||
.quickhelp dd { font-size: 12.5px; color: var(--ink-2); line-height: 1.5; margin: 3px 0 0; }
|
||||
@media (max-width: 1100px) { .helper { position: fixed; right: 0; top: 0; bottom: 0; z-index: 30; box-shadow: var(--shadow-lg); } }
|
||||
|
||||
.search { display: flex; align-items: center; gap: 8px; background: var(--s1); border: 1px solid var(--line); border-radius: var(--r); padding: 0 12px; height: 36px; width: min(340px, 100%); color: var(--ink-3); }
|
||||
.search input { flex: 1; min-width: 0; background: none; border: 0; outline: none; color: var(--ink); font-size: 13.5px; }
|
||||
.search:focus-within { border-color: var(--accent); }
|
||||
.sheethead.who { align-items: center; gap: var(--sp-3); }
|
||||
.sheethead.who .grow { flex: 1; min-width: 0; }
|
||||
.sheethead.who .note { margin-top: 2px; font-size: 12px; }
|
||||
.sheethead .close { font-size: 14px; line-height: 1; }
|
||||
.formsection .field:last-child { margin-bottom: 0; }
|
||||
.formsection.dangerzone { margin-top: var(--sp-6); padding: var(--sp-4); border: 1px solid color-mix(in srgb, var(--bad) 30%, transparent); border-radius: var(--r); background: var(--bad-2); }
|
||||
.formsection.dangerzone h4 { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 30%, transparent); }
|
||||
tr.click { cursor: pointer; } tr.click:hover td { background: var(--s2); }
|
||||
|
||||
/* Loya's door: fixed to the top-right of the content area, on every screen. */
|
||||
.loya-fab { position: fixed; top: var(--sp-4); right: var(--sp-5); z-index: 20;
|
||||
display: flex; align-items: center; gap: 8px; padding: 8px 14px 8px 10px; border-radius: 99px;
|
||||
border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); background: var(--s1); color: var(--ink);
|
||||
box-shadow: var(--shadow-lg); cursor: pointer; font-size: 13px; font-weight: 600; }
|
||||
.loya-fab img { width: 20px; height: 20px; object-fit: contain; }
|
||||
.loya-fab:hover { background: var(--accent-3); border-color: var(--accent); }
|
||||
|
||||
/* Camera finder: the pick-list that replaces "type an IP address". */
|
||||
.finder { display: flex; flex-direction: column; gap: var(--sp-3); align-items: flex-start; padding: var(--sp-3) var(--sp-4); border: 1px dashed var(--line); border-radius: var(--r); background: var(--s2); }
|
||||
.finder p { font-size: 13px; color: var(--ink-2); line-height: 1.5; margin: 0; }
|
||||
.finder.busy { flex-direction: row; align-items: center; color: var(--ink-2); font-size: 13px; border-style: solid; }
|
||||
.foundlist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
|
||||
.foundlist li button { width: 100%; display: flex; align-items: center; gap: var(--sp-3); padding: 10px 12px; border-radius: var(--r); border: 1px solid var(--line); background: var(--s2); color: var(--ink); cursor: pointer; text-align: left; font-size: 13px; }
|
||||
.foundlist li button:hover { border-color: var(--accent); }
|
||||
.foundlist li.picked button { border-color: var(--accent); background: var(--accent-3); }
|
||||
.foundlist .mono { font-family: var(--font-mono); font-size: 12.5px; min-width: 120px; }
|
||||
.foundlist .what { flex: 1; color: var(--ink-2); }
|
||||
.foundlist li.rescan button { width: auto; border: 0; background: none; padding: 4px 0; color: var(--ink-3); }
|
||||
|
||||
/* Welcome: two paths, each a card. */
|
||||
.login .box.wide { max-width: 640px; }
|
||||
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-3); margin-top: var(--sp-2); }
|
||||
@media (max-width: 720px) { .choices { grid-template-columns: 1fr; } }
|
||||
.choice { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; text-align: left; padding: var(--sp-4);
|
||||
border-radius: var(--r-lg); border: 1px solid var(--line); background: var(--s2); color: var(--ink); cursor: pointer; }
|
||||
.choice:hover { border-color: var(--accent); background: var(--accent-3); }
|
||||
.choice b { font-size: 14px; }
|
||||
.choice span { font-size: 12.5px; color: var(--ink-2); line-height: 1.5; }
|
||||
.choice em { font-family: var(--font-mono); font-style: normal; font-size: 11.5px; }
|
||||
.choice svg { color: var(--accent); }
|
||||
.login .foot em { font-style: normal; color: var(--ink-2); }
|
||||
.linkbtn { display: inline-flex; align-items: center; gap: 6px; }
|
||||
|
||||
/* Getting started */
|
||||
.starter { margin-bottom: var(--sp-4); }
|
||||
.starter .panelhead { padding: var(--sp-3) var(--sp-4); }
|
||||
.starter .steps { margin: 0; padding: var(--sp-3) var(--sp-4) 0; }
|
||||
.starter .steps.compact li { padding: var(--sp-3) var(--sp-4) var(--sp-3) 52px; }
|
||||
.starter .steps li .btn { margin-top: var(--sp-2); }
|
||||
.starter .note { padding: var(--sp-3) var(--sp-4); font-size: 12px; }
|
||||
.btn.ghost { background: none; border-color: transparent; color: var(--ink-3); }
|
||||
.btn.ghost:hover { color: var(--ink); }
|
||||
|
||||
/* Viewer mode: this PC has no engine, so the screens show the company's own
|
||||
data from head office. Informational, not an error - it is the ordinary
|
||||
state of a laptop away from a shop, and styling it red would train people
|
||||
to ignore the red that means something. */
|
||||
.viewing {
|
||||
display: flex; gap: 10px; align-items: flex-start;
|
||||
padding: 12px 14px; margin-bottom: 14px;
|
||||
border: 1px solid var(--line); border-radius: 10px;
|
||||
background: color-mix(in srgb, var(--accent) 7%, transparent);
|
||||
color: var(--ink-2); font-size: 13px; line-height: 1.5;
|
||||
}
|
||||
.viewing b { color: var(--ink); font-weight: 600; }
|
||||
.viewing svg { flex: none; margin-top: 2px; color: var(--accent); }
|
||||
|
||||
/* Watch live sits over the picture, opposite the connection pill. It is on
|
||||
the tile rather than in the button row because it is about the picture, and
|
||||
because the row it would otherwise join is hidden on a remote camera. */
|
||||
.camview .btn.watch {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
bottom: 10px;
|
||||
background: rgba(0, 0, 0, .55);
|
||||
border-color: rgba(255, 255, 255, .25);
|
||||
color: #fff;
|
||||
backdrop-filter: blur(6px);
|
||||
}
|
||||
.camview .btn.watch:hover { background: rgba(0, 0, 0, .72); }
|
||||
.camview .btn.watch.on { background: var(--accent); border-color: var(--accent); color: #fff; }
|
||||
|
||||
123
desktop/frontend/src/views/Assistant.jsx
Normal file
@@ -0,0 +1,123 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api, message } from '../bridge.js'
|
||||
import * as Icon from '../ui/icons.jsx'
|
||||
import logo from '../assets/loyaly-mark.png'
|
||||
|
||||
// The help. A conversation with the head-office assistant, which answers from
|
||||
// the shop's own data and knows how the product is set up - so "why is nobody
|
||||
// being recognised" and "how do I add my camera" are both answered here, by
|
||||
// the same thing, without leaving the app.
|
||||
//
|
||||
// The history lives in this component and is resent whole; nothing is stored
|
||||
// anywhere. A PC running on its own has no head office to ask and is told so.
|
||||
const SUGGESTED = [
|
||||
'Is my shop working right now?',
|
||||
'How do I add my camera?',
|
||||
'Why has nobody been recognised today?',
|
||||
'Who came in this morning?',
|
||||
]
|
||||
|
||||
export default function Assistant({ session, onClose }) {
|
||||
const [turns, setTurns] = useState([])
|
||||
const [draft, setDraft] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const scroller = useRef(null)
|
||||
const standalone = session?.standalone
|
||||
|
||||
useEffect(() => { scroller.current?.scrollTo({ top: 1e9, behavior: 'smooth' }) }, [turns, busy])
|
||||
|
||||
async function ask(text) {
|
||||
const q = (text ?? draft).trim()
|
||||
if (!q || busy) return
|
||||
const history = [...turns, { role: 'user', text: q }]
|
||||
setTurns(history); setDraft(''); setBusy(true); setError(null)
|
||||
try {
|
||||
const a = await api.ask(history.map(t => ({ role: t.role, text: t.text })))
|
||||
setTurns([...history, { role: 'assistant', text: a.text, used: a.used ?? [] }])
|
||||
} catch (e) {
|
||||
const m = message(e)
|
||||
// Her only failure that is not hers: the login is gone. Say what to do,
|
||||
// not "session expired" - the shell returns to Login within seconds.
|
||||
setError(/session expired|unauthori[sz]ed/i.test(m)
|
||||
? 'You’re signed out of head office, so I can’t look anything up. Sign in again and ask me once more.'
|
||||
: m)
|
||||
setTurns(turns) // the question stays in the box, not in the transcript
|
||||
setDraft(q)
|
||||
} finally { setBusy(false) }
|
||||
}
|
||||
|
||||
return (
|
||||
<aside className="helper" role="dialog" aria-label="Loya">
|
||||
<header className="helperhead">
|
||||
<img src={logo} alt="" />
|
||||
<div>
|
||||
<b>Loya</b>
|
||||
<span>Your Behavision buddy — knows your cameras, your customers and your numbers.</span>
|
||||
</div>
|
||||
<button className="close" onClick={onClose} aria-label="Close"><Icon.Close size={16} /></button>
|
||||
</header>
|
||||
|
||||
<div className="helperbody" ref={scroller}>
|
||||
{standalone && (
|
||||
<div className="helpernote">
|
||||
<Icon.CloudOff size={16} />
|
||||
<div>
|
||||
<b>This PC runs on its own</b>
|
||||
<span>Loya lives at head office. Link this PC to a shop to talk to her; the basics are below.</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{turns.length === 0 && (
|
||||
<div className="helperintro">
|
||||
<div className="turn assistant"><div className="bubble">
|
||||
{greeting(session)} Ask me anything about {session?.site_name || 'this shop'} — what’s happening now, or how to set something up.
|
||||
</div></div>
|
||||
<div className="chips">
|
||||
{SUGGESTED.map(s => <button key={s} className="chip" onClick={() => ask(s)} disabled={busy || standalone}>{s}</button>)}
|
||||
</div>
|
||||
{standalone && <QuickHelp />}
|
||||
</div>
|
||||
)}
|
||||
{turns.map((t, i) => (
|
||||
<div key={i} className={`turn ${t.role}`}>
|
||||
<div className="bubble">{t.text}</div>
|
||||
{t.used?.length > 0 && <span className="used">Looked at: {t.used.join(', ')}</span>}
|
||||
</div>
|
||||
))}
|
||||
{busy && <div className="turn assistant"><div className="bubble thinking"><span /><span /><span /></div></div>}
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
</div>
|
||||
|
||||
<form className="helperask" onSubmit={e => { e.preventDefault(); ask() }}>
|
||||
<input value={draft} onChange={e => setDraft(e.target.value)} disabled={busy || standalone}
|
||||
placeholder={standalone ? 'Link this PC to head office to talk to Loya' : 'Ask Loya…'} />
|
||||
<button className="btn primary" disabled={busy || standalone || !draft.trim()} aria-label="Ask"><Icon.Chevron size={16} /></button>
|
||||
</form>
|
||||
</aside>
|
||||
)
|
||||
}
|
||||
|
||||
function greeting(session) {
|
||||
const h = new Date().getHours()
|
||||
const part = h < 12 ? 'Morning' : h < 17 ? 'Afternoon' : 'Evening'
|
||||
const name = session?.user?.full_name?.split(' ')[0]
|
||||
return name ? `${part}, ${name}.` : `${part}.`
|
||||
}
|
||||
|
||||
// What a PC with no head office can still be told. Static on purpose: there is
|
||||
// nobody to ask, and a chat box that always fails is worse than a short list.
|
||||
function QuickHelp() {
|
||||
return (
|
||||
<dl className="quickhelp">
|
||||
<dt>Adding a camera</dt>
|
||||
<dd>Cameras → Add camera. The address is on a label on the camera; pick the make and the stream path fills itself in. Test, then save.</dd>
|
||||
<dt>Proving it works</dt>
|
||||
<dd>Press Check placement and walk past the camera like a customer for 25 seconds. Only “good” means it can recognise faces — otherwise move it to head height, facing the way people approach.</dd>
|
||||
<dt>Nothing showing on Live</dt>
|
||||
<dd>Check the camera is Connected and Proven. A camera aimed from above or the side streams fine and recognises nobody.</dd>
|
||||
<dt>Linking to head office later</dt>
|
||||
<dd>Use the link button at the bottom of the sidebar and type an installation code from head office. Nothing recorded here is lost.</dd>
|
||||
</dl>
|
||||
)
|
||||
}
|
||||
@@ -1,69 +1,104 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api, message } from '../bridge.js'
|
||||
import { usePolled } from '../hooks.js'
|
||||
import * as Icon from '../ui/icons.jsx'
|
||||
import { MAKES, makeById } from '../../../../shared/cameraMakes.js'
|
||||
|
||||
const BLANK = { id: '', host: '', port: 554, path: '', username: '', password: '',
|
||||
max_width: 1280 }
|
||||
// The camera screen is a picture, not a settings table.
|
||||
//
|
||||
// The first version was a table of id / rtsp url / status with three buttons
|
||||
// per row - the view a developer wants. A camera is a thing you look at, so
|
||||
// the live picture is the card, the state sits over it, and the one line that
|
||||
// matters is under it: whether anyone has PROVED this camera can recognise a
|
||||
// face, which is a different claim from "connected" and is the gap a site gets
|
||||
// signed off through.
|
||||
const BLANK = { id: '', host: '', port: 554, path: '', username: '', password: '', max_width: 1280 }
|
||||
|
||||
// "This computer's own camera" - a webcam or a built-in FaceTime camera.
|
||||
//
|
||||
// The engine has supported it since the first version (`webcam: 0` picks a
|
||||
// capture index instead of building an RTSP URL) and no screen has ever
|
||||
// offered it: another case of the API being able to do something the UI
|
||||
// could not reach. It matters most for the thing it was missing from, which
|
||||
// is showing the product to somebody. A laptop's own camera gives real
|
||||
// recognition, of real faces, in the room, depending on no network at all -
|
||||
// where pointing a demo machine at a camera in another building depends on
|
||||
// two internet connections and a tunnel staying up while you talk.
|
||||
const WEBCAM = 'webcam'
|
||||
|
||||
export default function Cameras() {
|
||||
const { data, error, reload } = usePolled(() => api.cameras(), 8000)
|
||||
const [editing, setEditing] = useState(null)
|
||||
const [check, setCheck] = useState(null)
|
||||
const cams = data ?? []
|
||||
// Every camera is remote or none is: this list comes from the engine on
|
||||
// loopback, and when that is unreachable the whole list comes from head
|
||||
// office instead. A remote camera is on a network this computer cannot
|
||||
// reach, so the picture is the shop PC's last snapshot and the buttons that
|
||||
// would talk to the camera are not offered - one that cannot work is worse
|
||||
// than one that is absent.
|
||||
const remote = cams.some(c => c.remote)
|
||||
const streams = useStreamURLs(remote ? [] : cams)
|
||||
// ONE camera at a time, and that is a cost decision rather than a layout
|
||||
// one. A remote view makes the shop computer upload frames for as long as
|
||||
// somebody is watching, so a grid that all went live at once would put an
|
||||
// estate's worth of cameras on the wire because somebody opened a page.
|
||||
const [watching, setWatching] = useState(null)
|
||||
const [watchURL, setWatchURL] = useState('')
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
if (!watching) { setWatchURL(''); return }
|
||||
api.remoteStreamURL(watching).then(u => { if (alive) setWatchURL(u || '') })
|
||||
.catch(() => { if (alive) setWatchURL('') })
|
||||
return () => { alive = false }
|
||||
}, [watching])
|
||||
|
||||
async function remove(id) {
|
||||
if (!confirm(`Remove camera "${id}"? Recognition from it stops immediately.`)) return
|
||||
try { await api.deleteCamera(id); reload() } catch (e) { alert(message(e)) }
|
||||
async function remove(cam) {
|
||||
if (!confirm(`Remove ${cam.id}? Recognition from it stops immediately.`)) return
|
||||
try { await api.deleteCamera(cam.id); reload() } catch (e) { alert(message(e)) }
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="page">
|
||||
<header style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end' }}>
|
||||
<header className="pagehead">
|
||||
<div>
|
||||
<h2>Cameras</h2>
|
||||
<p>Add a camera, check it can see faces properly, then it starts working.</p>
|
||||
<p>{remote
|
||||
? 'The cameras across your shops, as the shop computers last reported them.'
|
||||
: 'Add a camera, then prove it can see faces with a walk-past. Only then is it working.'}</p>
|
||||
</div>
|
||||
<button className="btn primary" onClick={() => setEditing({ ...BLANK })}>
|
||||
Add camera
|
||||
</button>
|
||||
{!remote && <button className="btn primary" onClick={() => setEditing({ ...BLANK })}>
|
||||
<Icon.Plus size={15} />Add camera
|
||||
</button>}
|
||||
</header>
|
||||
|
||||
{error && <div className="err">{error}</div>}
|
||||
{remote && <div className="viewing">
|
||||
<b>Viewing your shops from here.</b> These cameras are wired to the shop
|
||||
computers, so they are set up and checked there. Each tile shows that
|
||||
camera's most recent frame; <b>Watch live</b> asks the shop computer to
|
||||
send video for as long as you are looking.
|
||||
</div>}
|
||||
|
||||
<div className="card">
|
||||
{cams.length === 0
|
||||
? <div className="empty">No cameras yet.</div>
|
||||
: <div className="tablewrap">
|
||||
<table>
|
||||
<thead><tr><th>Name</th><th>Address</th><th>Status</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{cams.map(c => (
|
||||
<tr key={c.id}>
|
||||
<td>{c.id}</td>
|
||||
<td className="mono">{c.url}</td>
|
||||
<td>
|
||||
{c.connected === undefined
|
||||
? <span className="pill"><i className="dot idle" />stopped</span>
|
||||
: c.connected
|
||||
? <span className="pill ok"><i className="dot ok" />live</span>
|
||||
: <span className="pill bad"><i className="dot bad" />offline</span>}
|
||||
</td>
|
||||
<td style={{ textAlign: 'right', whiteSpace: 'nowrap' }}>
|
||||
<button className="btn sm" onClick={() => setCheck(c.id)}>
|
||||
Check placement
|
||||
</button>{' '}
|
||||
<button className="btn sm" onClick={() => setEditing(c)}>Edit</button>{' '}
|
||||
<button className="btn sm danger" onClick={() => remove(c.id)}>
|
||||
Remove
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>}
|
||||
</div>
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
|
||||
{cams.length === 0
|
||||
? <div className="panel">
|
||||
<div className="empty tall">
|
||||
<Icon.NoCamera size={40} />
|
||||
<b>No cameras yet</b>
|
||||
<p>Add the camera by its address. Most cameras print it on a label underneath, or show it in their own app.</p>
|
||||
<button className="btn primary" onClick={() => setEditing({ ...BLANK })}><Icon.Plus size={15} />Add your first camera</button>
|
||||
</div>
|
||||
</div>
|
||||
: <div className="camgrid">
|
||||
{cams.map(c => (
|
||||
<CameraCard key={c.id} cam={c}
|
||||
stream={watching === c.id ? watchURL : streams[c.id]}
|
||||
watching={watching === c.id}
|
||||
onWatch={() => setWatching(watching === c.id ? null : c.id)}
|
||||
onEdit={() => setEditing(c)} onCheck={() => setCheck(c.id)} onRemove={() => remove(c)} />
|
||||
))}
|
||||
</div>}
|
||||
|
||||
{editing && <CameraSheet cam={editing} onClose={() => setEditing(null)}
|
||||
onSaved={() => { setEditing(null); reload() }} />}
|
||||
@@ -72,11 +107,104 @@ export default function Cameras() {
|
||||
)
|
||||
}
|
||||
|
||||
function CameraCard({ cam, stream, watching, onWatch, onEdit, onCheck, onRemove }) {
|
||||
// Three states, not two, and the third is why `connected` is a pointer on
|
||||
// the wire: null means no shop computer has reported on this camera yet,
|
||||
// which reads as waiting rather than as a fault to go and investigate.
|
||||
const conn = cam.remote
|
||||
// Four states, decided once by the server. `stale` is the one that was
|
||||
// missing: the shop computer reports nothing when it cannot reach its own
|
||||
// engine, so its last report used to sit there reading Connected -
|
||||
// measured at 34 minutes on the live estate.
|
||||
? ({ connected: { tone: 'ok', label: 'Connected' },
|
||||
not_connecting: { tone: 'bad', label: 'Not connecting' },
|
||||
stale: { tone: 'warn', label: 'Not reporting' } }[cam.state]
|
||||
|| { tone: 'idle', label: 'Waiting for the shop computer' })
|
||||
: cam.connected === undefined || cam.connected === null
|
||||
? { tone: 'idle', label: 'Engine stopped' }
|
||||
: cam.connected ? { tone: 'ok', label: 'Connected' }
|
||||
: { tone: 'bad', label: 'Not connecting' }
|
||||
// The last placement verdict, so "proven" survives closing the sheet. Only
|
||||
// `good` is a pass: marginal means half the visitors are silently discarded.
|
||||
// Never asked for a remote camera: that answer lives on the shop computer,
|
||||
// and polling loopback for it here only produces an error every 15 seconds.
|
||||
const { data: last } = usePolled(
|
||||
() => cam.remote ? Promise.resolve(null) : api.placementResult(cam.id), 15000, [cam.id])
|
||||
const proof = !last || last.running || !last.verdict || last.verdict === 'starting'
|
||||
? { tone: 'miss', label: 'Not yet proven', text: 'Walk past it once and Behavision will tell you if the placement works.' }
|
||||
: last.verdict === 'good'
|
||||
? { tone: 'seen', label: 'Proven', text: last.headline || 'Faces recognised on a walk-past.' }
|
||||
: { tone: 'miss', label: 'Not proven', text: last.headline || 'Move the camera and check again.' }
|
||||
const shot = cam.snapshot?.available ? cam.snapshot.url : ''
|
||||
return (
|
||||
<article className="camcard">
|
||||
<div className="camview">
|
||||
{stream || shot
|
||||
? <img src={stream || shot} alt={cam.id} />
|
||||
: <div className="placeholder"><Icon.NoCamera size={34} /></div>}
|
||||
<span className={`pill ${conn.tone === 'idle' ? '' : conn.tone} over`}><i className={`dot ${conn.tone}`} />{conn.label}</span>
|
||||
{cam.remote && <button className={`btn sm watch ${watching ? 'on' : ''}`} onClick={onWatch}>
|
||||
<Icon.Play size={13} />{watching ? 'Stop watching' : 'Watch live'}
|
||||
</button>}
|
||||
</div>
|
||||
<div className="cambody">
|
||||
<div className="camtitle">
|
||||
<div>
|
||||
<h3>{cam.label || cam.camera_id || cam.id}</h3>
|
||||
<span className="mono note">{cam.remote
|
||||
? cam.site || cam.site_slug || ''
|
||||
: `${cam.host || cam.url}${cam.path ? ` · ${cam.path}` : ''}`}</span>
|
||||
</div>
|
||||
{!cam.remote && <div className="camactions">
|
||||
<button className="btn sm" onClick={onEdit}>Edit</button>
|
||||
<button className="btn sm danger" onClick={onRemove}>Remove</button>
|
||||
</div>}
|
||||
</div>
|
||||
{cam.remote
|
||||
? <div className="camproof">
|
||||
<span className="note">{cam.state_note
|
||||
? cam.state_note
|
||||
: watching
|
||||
? 'Live from the shop computer. It uploads only while you watch.'
|
||||
: cam.snapshot?.available
|
||||
? 'Last picture from the shop computer. Watch live to see it now.'
|
||||
: cam.snapshot?.reason || 'No picture yet from the shop computer.'}</span>
|
||||
</div>
|
||||
: <div className="camproof">
|
||||
<span className={`tag ${proof.tone}`}>{proof.label}</span>
|
||||
<span className="note">{proof.text}</span>
|
||||
<button className={`btn sm ${proof.tone === 'seen' ? '' : 'primary'}`} onClick={onCheck}><Icon.Play size={13} />{proof.tone === 'seen' ? 'Check again' : 'Check placement'}</button>
|
||||
</div>}
|
||||
</div>
|
||||
</article>
|
||||
)
|
||||
}
|
||||
|
||||
// Stream URLs are fetched once per camera and left alone: reassigning an
|
||||
// MJPEG <img> src restarts the stream, so rebuilding them on every poll makes
|
||||
// every feed flicker forever.
|
||||
function useStreamURLs(cams) {
|
||||
const [urls, setUrls] = useState({})
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
const missing = cams.filter(c => !(c.id in urls))
|
||||
if (!missing.length) return
|
||||
;(async () => {
|
||||
const add = {}
|
||||
for (const c of missing) { try { add[c.id] = await api.streamURL(c.id) } catch { add[c.id] = '' } }
|
||||
if (alive) setUrls(u => ({ ...u, ...add }))
|
||||
})()
|
||||
return () => { alive = false }
|
||||
}, [cams.map(c => c.id).join('|')]) // eslint-disable-line react-hooks/exhaustive-deps
|
||||
return urls
|
||||
}
|
||||
|
||||
function CameraSheet({ cam, onClose, onSaved }) {
|
||||
const isNew = !cam.id
|
||||
const [f, setF] = useState({ ...BLANK, ...cam, password: '',
|
||||
path: cam.path || (isNew ? MAKES[0].path : '') })
|
||||
const [make, setMake] = useState(isNew ? MAKES[0].id : 'manual')
|
||||
const [f, setF] = useState({ ...BLANK, ...cam, password: '', path: cam.path || (isNew ? MAKES[0].path : '') })
|
||||
const [make, setMake] = useState(isNew ? MAKES[0].id : (cam.webcam != null ? WEBCAM : 'manual'))
|
||||
const [index, setIndex] = useState(cam.webcam != null ? String(cam.webcam) : '0')
|
||||
const local = make === WEBCAM
|
||||
const [test, setTest] = useState(null)
|
||||
const [busy, setBusy] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
@@ -85,6 +213,7 @@ function CameraSheet({ cam, onClose, onSaved }) {
|
||||
// Only overwrite the path when the preset has one, so choosing "I know the
|
||||
// path" does not wipe what the installer already typed.
|
||||
function chooseMake(e) {
|
||||
if (e.target.value === WEBCAM) { setMake(WEBCAM); setTest(null); return }
|
||||
const m = makeById(e.target.value)
|
||||
setMake(m.id)
|
||||
setF(prev => ({ ...prev, path: m.path || prev.path }))
|
||||
@@ -98,6 +227,14 @@ function CameraSheet({ cam, onClose, onSaved }) {
|
||||
if (v === '' || v === null || v === undefined) continue
|
||||
out[k] = (k === 'port' || k === 'max_width') ? Number(v) : v
|
||||
}
|
||||
if (local) {
|
||||
// An address and a webcam index are alternatives, not extras: the
|
||||
// engine's source() takes the webcam first, so leaving a half-typed
|
||||
// host behind would make the saved camera describe two different
|
||||
// things and only one of them would be used.
|
||||
for (const k of ['host', 'path', 'username', 'password']) delete out[k]
|
||||
out.webcam = Number(index) || 0
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -114,93 +251,149 @@ function CameraSheet({ cam, onClose, onSaved }) {
|
||||
catch (e) { setError(message(e)) } finally { setBusy(null) }
|
||||
}
|
||||
|
||||
const chosen = makeById(make)
|
||||
|
||||
// The camera is picked from a scan of the shop's network rather than typed.
|
||||
// Nobody knows their camera's address; the sticker is under the camera and
|
||||
// the menu is different in every make's app. The scan names ONVIF cameras
|
||||
// and lists anything with the RTSP port open; picking one fills the
|
||||
// address and, when the make is recognisable, the stream path too.
|
||||
const [scan, setScan] = useState(null) // null | 'busy' | {cameras, networks} | {error}
|
||||
async function findCameras() {
|
||||
setScan('busy')
|
||||
try { setScan(await api.discoverCameras()) } catch (e) { setScan({ error: message(e) }) }
|
||||
}
|
||||
function pick(c) {
|
||||
const m = c.make ? makeById(c.make) : null
|
||||
setF(prev => ({ ...prev, host: c.host, path: m?.path || prev.path,
|
||||
id: prev.id || (m ? '' : ''), }))
|
||||
if (m) setMake(m.id)
|
||||
setTest(null)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="drawer" onMouseDown={e => e.target === e.currentTarget && onClose()}>
|
||||
<div className="panel">
|
||||
<button className="btn sm close" onClick={onClose}>Close</button>
|
||||
<h3>{isNew ? 'Add camera' : cam.id}</h3>
|
||||
<p className="note" style={{ marginBottom: 18 }}>
|
||||
Test the connection before saving — a wrong address is the most common mistake.
|
||||
</p>
|
||||
<form onSubmit={save}>
|
||||
{error && <div className="err">{error}</div>}
|
||||
<label className="field">
|
||||
<span>Name</span>
|
||||
<input value={f.id} onChange={set('id')} disabled={!isNew}
|
||||
placeholder="entrance" required autoComplete="off" />
|
||||
</label>
|
||||
{/* The highest-value field on this form. The address and the
|
||||
password are on a label or in the installer's notes; the RTSP
|
||||
path is not written anywhere a shop owner would look, and getting
|
||||
it wrong produces "could not open stream", which reads like a
|
||||
password problem and is not. */}
|
||||
<label className="field"><span>Make of camera</span>
|
||||
<select value={make} onChange={chooseMake}>
|
||||
{MAKES.map(m => <option key={m.id} value={m.id}>{m.label}</option>)}
|
||||
</select>
|
||||
</label>
|
||||
{makeById(make).note && (
|
||||
<p className="note" style={{ marginTop: -8, marginBottom: 12 }}>
|
||||
{makeById(make).note}
|
||||
</p>
|
||||
)}
|
||||
<div className="fieldrow">
|
||||
<label className="field"><span>Camera address</span>
|
||||
<input value={f.host} onChange={set('host')} placeholder="192.168.0.138"
|
||||
autoComplete="off" />
|
||||
</label>
|
||||
<label className="field"><span>Port</span>
|
||||
<input value={f.port} onChange={set('port')} inputMode="numeric"
|
||||
autoComplete="off" />
|
||||
</label>
|
||||
</div>
|
||||
<label className="field"><span>Stream path</span>
|
||||
<input value={f.path} onChange={set('path')} placeholder="/ch0_0.264"
|
||||
autoComplete="off" />
|
||||
</label>
|
||||
<div className="fieldrow">
|
||||
{/* A text input next to a password input is a sign-in form as far
|
||||
as the webview is concerned, so without this the browser offers
|
||||
the operator's own Behavision email as the camera's username -
|
||||
which fails with a message about credentials that points at the
|
||||
camera. "off" alone is frequently ignored; a non-login name and
|
||||
new-password on the secret are what actually work. */}
|
||||
<label className="field"><span>Username</span>
|
||||
<input value={f.username} onChange={set('username')}
|
||||
name="camera-account" autoComplete="off" />
|
||||
</label>
|
||||
<label className="field"><span>Password</span>
|
||||
<input type="password" value={f.password} onChange={set('password')}
|
||||
name="camera-secret" autoComplete="new-password"
|
||||
placeholder={cam.has_password ? '(unchanged)' : ''} />
|
||||
</label>
|
||||
</div>
|
||||
<div className="sheet">
|
||||
<div className="sheethead">
|
||||
<h2>{isNew ? 'Add a camera' : `Edit ${cam.id}`}</h2>
|
||||
<button className="close" onClick={onClose} aria-label="Close"><Icon.Close size={16} /></button>
|
||||
</div>
|
||||
<form className="sheetbody" onSubmit={save} autoComplete="off">
|
||||
<p className="lead">Three things from the camera: its address, its make, and its password. Test before you save — a wrong address is the most common mistake.</p>
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 4 }}>
|
||||
<button type="button" className="btn" onClick={runTest} disabled={!!busy}>
|
||||
{busy === 'test' ? 'Connecting…' : 'Test connection'}
|
||||
</button>
|
||||
<button className="btn primary" disabled={!!busy || !f.id}>
|
||||
{busy === 'save' ? 'Saving…' : 'Save'}
|
||||
</button>
|
||||
</div>
|
||||
{isNew && !local && (
|
||||
<section className="formsection">
|
||||
<h4>Find it</h4>
|
||||
{scan === null && (
|
||||
<div className="finder">
|
||||
<p>Behavision can look for cameras on this shop’s network.</p>
|
||||
<button type="button" className="btn primary" onClick={findCameras}><Icon.Search size={14} />Find cameras on this network</button>
|
||||
</div>
|
||||
)}
|
||||
{scan === 'busy' && <div className="finder busy"><span className="spinner" />Looking on the network… a few seconds.</div>}
|
||||
{scan?.error && <div className="err"><Icon.Warning size={15} />{scan.error}</div>}
|
||||
{scan?.cameras && (
|
||||
scan.cameras.length === 0
|
||||
? <div className="finder">
|
||||
<p>Nothing answered on {scan.networks?.join(', ') || 'this network'}. The camera may be on a different network, switched off, or not yet connected — check its cable and power, then try again. You can still type its address below.</p>
|
||||
<button type="button" className="btn" onClick={findCameras}>Try again</button>
|
||||
</div>
|
||||
: <ul className="foundlist">
|
||||
{scan.cameras.map(c => (
|
||||
<li key={c.host} className={f.host === c.host ? 'picked' : ''}>
|
||||
<button type="button" onClick={() => pick(c)}>
|
||||
<span className="mono">{c.host}</span>
|
||||
<span className="what">{c.name || (c.rtsp ? 'Streams video (RTSP)' : 'Answers ONVIF')}</span>
|
||||
{c.make && <span className="tag seen">{makeById(c.make).label}</span>}
|
||||
{f.host === c.host && <Icon.Check size={16} />}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
<li className="rescan"><button type="button" className="btn sm" onClick={findCameras}>Scan again</button></li>
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
|
||||
<section className="formsection">
|
||||
<h4>The camera</h4>
|
||||
{isNew && (
|
||||
<label className="field"><span>Name</span>
|
||||
<input value={f.id} onChange={set('id')} placeholder="entrance" required autoFocus />
|
||||
<em className="hint">Short, no spaces. It names this camera everywhere and cannot be changed later.</em>
|
||||
</label>
|
||||
)}
|
||||
{local
|
||||
? <label className="field narrow"><span>Camera number</span>
|
||||
<input value={index} onChange={e => { setIndex(e.target.value); setTest(null) }} inputMode="numeric" />
|
||||
<em className="hint">0 is the built-in camera. Try 1 if a second one is plugged in.</em>
|
||||
</label>
|
||||
: <div className="fieldrow">
|
||||
<label className="field"><span>Address</span>
|
||||
<input value={f.host} onChange={set('host')} placeholder="192.168.1.20" inputMode="decimal" />
|
||||
<em className="hint">On a label on the camera, or in its own app under “network”.</em>
|
||||
</label>
|
||||
<label className="field narrow"><span>Port</span>
|
||||
<input value={f.port} onChange={set('port')} inputMode="numeric" />
|
||||
</label>
|
||||
</div>}
|
||||
</section>
|
||||
|
||||
<section className="formsection">
|
||||
<h4>The stream</h4>
|
||||
<label className="field"><span>Make of camera</span>
|
||||
<select value={make} onChange={chooseMake}>
|
||||
{MAKES.map(m => <option key={m.id} value={m.id}>{m.label}</option>)}
|
||||
<option value={WEBCAM}>This computer’s own camera</option>
|
||||
</select>
|
||||
{local
|
||||
? <em className="hint">Recognition runs on this computer’s built-in or plugged-in camera. Nothing on the network is involved.</em>
|
||||
: chosen.note && <em className="hint">{chosen.note}</em>}
|
||||
</label>
|
||||
{!local && (
|
||||
<label className="field"><span>Stream path</span>
|
||||
<input className="mono" value={f.path} onChange={set('path')} placeholder="/Streaming/Channels/101" />
|
||||
<em className="hint">Filled in from the make. Change it only if the camera’s own app says something else.</em>
|
||||
</label>
|
||||
)}
|
||||
</section>
|
||||
|
||||
{!local && (
|
||||
<section className="formsection">
|
||||
<h4>Sign-in to the camera</h4>
|
||||
<div className="fieldrow">
|
||||
<label className="field"><span>Username</span>
|
||||
<input name="rtsp-account" autoComplete="off" value={f.username} onChange={set('username')} placeholder="admin" />
|
||||
</label>
|
||||
<label className="field"><span>Password</span>
|
||||
<input type="password" name="rtsp-secret" autoComplete="new-password" value={f.password}
|
||||
onChange={set('password')} placeholder={cam.has_password ? '(unchanged)' : ''} />
|
||||
</label>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
|
||||
{test && (
|
||||
<div style={{ marginTop: 14 }}>
|
||||
{test.ok
|
||||
? <>
|
||||
<p style={{ color: 'var(--ok)', fontSize: 13 }}>
|
||||
Connected — {test.width}×{test.height}
|
||||
</p>
|
||||
{test.snapshot && (
|
||||
<img alt="Camera preview" style={{ width: '100%', marginTop: 8,
|
||||
borderRadius: 6, border: '1px solid var(--line)' }}
|
||||
src={`data:image/jpeg;base64,${test.snapshot}`} />
|
||||
)}
|
||||
</>
|
||||
: <div className="err">{test.error}</div>}
|
||||
</div>
|
||||
test.ok
|
||||
? <div className="testresult ok">
|
||||
<Icon.Check size={16} />
|
||||
<div>
|
||||
<b>Connected — {test.width}×{test.height}{test.codec ? ` · ${String(test.codec).toUpperCase()}` : ''}</b>
|
||||
{test.snapshot && <img alt="Camera preview" src={`data:image/jpeg;base64,${test.snapshot}`} />}
|
||||
</div>
|
||||
</div>
|
||||
: <div className="testresult bad"><Icon.Warning size={16} /><div><b>Could not connect</b><span>{test.error}</span></div></div>
|
||||
)}
|
||||
|
||||
<div className="sheetactions">
|
||||
<button type="button" className="btn" onClick={runTest} disabled={!!busy || !f.host}>
|
||||
{busy === 'test' ? 'Connecting…' : 'Test connection'}
|
||||
</button>
|
||||
<button className="btn primary" disabled={!!busy || !f.id || !f.host}>
|
||||
{busy === 'save' ? 'Saving…' : isNew ? 'Add camera' : 'Save changes'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@@ -208,7 +401,7 @@ function CameraSheet({ cam, onClose, onSaved }) {
|
||||
}
|
||||
|
||||
// The commissioning wizard. This is what stops a site being signed off with a
|
||||
// camera that recognises nobody — the failure that otherwise shows up weeks
|
||||
// camera that recognises nobody - the failure that otherwise shows up weeks
|
||||
// later as a footfall report that was always zero.
|
||||
function PlacementSheet({ id, onClose }) {
|
||||
const [state, setState] = useState({ verdict: 'starting', advice: [] })
|
||||
@@ -233,45 +426,54 @@ function PlacementSheet({ id, onClose }) {
|
||||
return () => { alive = false; clearInterval(timer.current) }
|
||||
}, [id])
|
||||
|
||||
const tone = { good: 'ok', marginal: 'warn', poor: 'bad', artifact: 'bad',
|
||||
no_faces: 'warn', inconclusive: 'warn' }[state.verdict]
|
||||
const tone = { good: 'ok', marginal: 'warn', poor: 'bad', artifact: 'bad', no_faces: 'warn',
|
||||
inconclusive: 'warn', no_completed_passes: 'warn' }[state.verdict]
|
||||
const pct = state.seconds ? Math.min(100, (state.elapsed / state.seconds) * 100) : 0
|
||||
const running = state.running || state.verdict === 'starting'
|
||||
|
||||
return (
|
||||
<div className="drawer" onMouseDown={e => e.target === e.currentTarget && onClose()}>
|
||||
<div className="panel">
|
||||
<button className="btn sm close" onClick={onClose}>Close</button>
|
||||
<h3>Placement check — {id}</h3>
|
||||
<p className="note" style={{ marginBottom: 18 }}>
|
||||
Walk past the camera the way a customer would, a few times.
|
||||
</p>
|
||||
<div className="sheet">
|
||||
<div className="sheethead">
|
||||
<h2>Check placement · {id}</h2>
|
||||
<button className="close" onClick={onClose} aria-label="Close"><Icon.Close size={16} /></button>
|
||||
</div>
|
||||
<div className="sheetbody">
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
|
||||
{error && <div className="err">{error}</div>}
|
||||
<ol className="steps">
|
||||
<li className={running ? 'now' : 'done'}>
|
||||
<b>Walk past the camera</b>
|
||||
<span>The way a customer would — in through the door, not looking at the lens. Two or three times, for about 25 seconds.</span>
|
||||
{running && <div className="progress"><div style={{ width: `${pct}%` }} /></div>}
|
||||
</li>
|
||||
<li className={running ? '' : 'now'}>
|
||||
<b>Behavision judges what it saw</b>
|
||||
<span>Not “were the frames sharp”, but “did a person walking past produce a face worth recognising”.</span>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<div className="card">
|
||||
<div style={{ fontSize: 15, fontWeight: 600,
|
||||
color: tone ? `var(--${tone})` : 'var(--ink)' }}>
|
||||
{state.headline || 'Starting…'}
|
||||
</div>
|
||||
{state.running && (
|
||||
<div style={{ height: 5, background: 'var(--surface-2)', borderRadius: 3,
|
||||
overflow: 'hidden', margin: '12px 0' }}>
|
||||
<div style={{ height: '100%', width: `${pct}%`, background: 'var(--accent)',
|
||||
transition: 'width .4s linear' }} />
|
||||
<div className={`verdict ${tone ?? ''}`}>
|
||||
<div className="verdict-head">
|
||||
{running ? <span className="spinner" /> : tone === 'ok' ? <Icon.Check size={18} /> : <Icon.Warning size={18} />}
|
||||
<b>{state.headline || 'Watching…'}</b>
|
||||
</div>
|
||||
{state.advice?.length > 0 && (
|
||||
<ul>{state.advice.map((a, i) => <li key={i}>{a}</li>)}</ul>
|
||||
)}
|
||||
{state.quality?.n > 0 && (
|
||||
<p className="note">
|
||||
{state.quality.n} face{state.quality.n === 1 ? '' : 's'} seen · median quality {state.quality.p50} ·
|
||||
{' '}{Math.round((state.quality.fraction_below_gate ?? 0) * 100)}% too poor to use
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!running && (
|
||||
<div className="sheetactions">
|
||||
<button className="btn" onClick={onClose}>Close</button>
|
||||
<button className="btn primary" onClick={() => { setState({ verdict: 'starting', advice: [] }); api.startPlacement(id, 25).then(setState).catch(e => setError(message(e))) }}>Run again</button>
|
||||
</div>
|
||||
)}
|
||||
{state.advice?.length > 0 && (
|
||||
<ul style={{ margin: '12px 0 0 18px', fontSize: 13, color: 'var(--ink-2)' }}>
|
||||
{state.advice.map((a, i) => <li key={i} style={{ marginBottom: 5 }}>{a}</li>)}
|
||||
</ul>
|
||||
)}
|
||||
{state.quality?.n > 0 && (
|
||||
<p className="note" style={{ marginTop: 12 }}>
|
||||
{state.quality.n} face{state.quality.n === 1 ? '' : 's'} seen ·
|
||||
median quality {state.quality.p50} ·
|
||||
gate {state.gate} ·
|
||||
{' '}{Math.round((state.quality.fraction_below_gate ?? 0) * 100)}% below it
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -54,29 +54,28 @@ export default function CustomerForm({ customer, session, onClose, onSaved }) {
|
||||
|
||||
return (
|
||||
<div className="drawer" onMouseDown={e => e.target === e.currentTarget && onClose()}>
|
||||
<div className="panel">
|
||||
<div className="who">
|
||||
<div className="sheet">
|
||||
<div className="sheethead who">
|
||||
<CustomerPhoto photo={shown} name={customer.full_name || customer.label}
|
||||
customerRef={customer.ref}
|
||||
onBroken={() => setPhoto({ available: false,
|
||||
reason: 'The photo could not be loaded.' })} />
|
||||
<div className="grow">
|
||||
<h3>{customer.full_name || customer.label}</h3>
|
||||
<h2>{customer.full_name || customer.label}</h2>
|
||||
<p className="note">
|
||||
{customer.visit_count} visit{customer.visit_count === 1 ? '' : 's'}
|
||||
{customer.last_seen_at && ` · last seen ${new Date(customer.last_seen_at).toLocaleDateString()}`}
|
||||
{shown && !shown.available && shown.reason && ` · ${shown.reason}`}
|
||||
</p>
|
||||
{shown && !shown.available && shown.reason &&
|
||||
<p className="note">{shown.reason}</p>}
|
||||
</div>
|
||||
<button type="button" className="btn sm" onClick={onClose}>Close</button>
|
||||
<button type="button" className="close" onClick={onClose} aria-label="Close">✕</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={save}>
|
||||
<form className="sheetbody" onSubmit={save}>
|
||||
{error && <div className="err">{error}</div>}
|
||||
|
||||
<div className="card" style={{ marginBottom: 14 }}>
|
||||
<h3>Customer details</h3>
|
||||
<section className="formsection">
|
||||
<h4>Customer details</h4>
|
||||
<label className="field">
|
||||
<span>Full name</span>
|
||||
<input value={f.full_name} onChange={set('full_name')} autoFocus />
|
||||
@@ -109,10 +108,10 @@ export default function CustomerForm({ customer, session, onClose, onSaved }) {
|
||||
<textarea value={f.notes} onChange={set('notes')}
|
||||
placeholder="Preferences, sizes, anything worth remembering" />
|
||||
</label>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div className="card" style={{ marginBottom: 14 }}>
|
||||
<h3>Purchase (optional)</h3>
|
||||
<section className="formsection">
|
||||
<h4>Purchase (optional)</h4>
|
||||
<div className="fieldrow">
|
||||
<label className="field">
|
||||
<span>Amount</span>
|
||||
@@ -127,10 +126,10 @@ export default function CustomerForm({ customer, session, onClose, onSaved }) {
|
||||
</div>
|
||||
<p className="note">Leave the amount blank if they did not buy anything —
|
||||
a visit without a sale is still worth recording.</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div className="card" style={{ marginBottom: 16 }}>
|
||||
<h3>Consent</h3>
|
||||
<section className="formsection">
|
||||
<h4>Consent</h4>
|
||||
<label style={{ display: 'flex', gap: 10, alignItems: 'flex-start',
|
||||
fontSize: 13, cursor: 'pointer' }}>
|
||||
<input type="checkbox" checked={f.consent} style={{ marginTop: 3 }}
|
||||
@@ -142,23 +141,23 @@ export default function CustomerForm({ customer, session, onClose, onSaved }) {
|
||||
Recorded with the date and who collected it. They can withdraw it
|
||||
at any time, which erases their face data.
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div className="card" style={{ marginBottom: 14 }}>
|
||||
<h3>Visits</h3>
|
||||
<section className="formsection">
|
||||
<h4>Visits</h4>
|
||||
<VisitHistory customer={customer} />
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
<div className="sheetactions">
|
||||
<button type="button" className="btn" onClick={onClose}>Cancel</button>
|
||||
<button className="btn primary" disabled={busy}>
|
||||
{busy ? 'Saving…' : 'Save'}
|
||||
</button>
|
||||
<button type="button" className="btn" onClick={onClose}>Cancel</button>
|
||||
</div>
|
||||
|
||||
{canErase && (
|
||||
<div className="card danger-zone">
|
||||
<h3>At the customer's request</h3>
|
||||
<section className="formsection dangerzone">
|
||||
<h4>At the customer's request</h4>
|
||||
{erasing
|
||||
? <EraseCustomer customer={customer}
|
||||
onCancel={() => setErasing(false)}
|
||||
@@ -174,7 +173,7 @@ export default function CustomerForm({ customer, session, onClose, onSaved }) {
|
||||
Erase this customer…
|
||||
</button>
|
||||
</>}
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import { api } from '../bridge.js'
|
||||
import { usePolled, fmtDate } from '../hooks.js'
|
||||
import * as Icon from '../ui/icons.jsx'
|
||||
import CustomerForm from './CustomerForm.jsx'
|
||||
|
||||
// The customer database, and the form staff fill in when someone walks in.
|
||||
@@ -15,34 +16,32 @@ export default function Customers({ session }) {
|
||||
|
||||
return (
|
||||
<div className="page">
|
||||
<header>
|
||||
<h2>Customers</h2>
|
||||
<p>Everyone this business has recognised. Fill in details once and they
|
||||
are known at every store.</p>
|
||||
<header className="pagehead">
|
||||
<div>
|
||||
<h2>Customers</h2>
|
||||
<p>Everyone this business has recognised. Fill in details once and they are known at every store.</p>
|
||||
</div>
|
||||
<label className="search">
|
||||
<Icon.Search size={15} />
|
||||
<input placeholder="Search by name or phone" value={query} onChange={e => setQuery(e.target.value)} />
|
||||
</label>
|
||||
</header>
|
||||
|
||||
{error && <div className="err">{error}</div>}
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
|
||||
<div className="grid cols-4" style={{ marginBottom: 16 }}>
|
||||
<Stat label="Known people" value={rows.length || '—'} />
|
||||
<Stat label="With details" value={named || '—'}
|
||||
sub={rows.length ? `${Math.round(100 * named / rows.length)}% captured` : null} />
|
||||
<Stat label="Returning" value={rows.filter(r => r.visit_count > 1).length || '—'} />
|
||||
<Stat label="With consent" value={rows.filter(r => r.has_consent).length || '—'} />
|
||||
<div className="metrics" style={{ marginBottom: 'var(--sp-4)' }}>
|
||||
<Metric k="Known people" v={rows.length || '—'} />
|
||||
<Metric k="With details" v={named || '—'} s={rows.length ? `${Math.round(100 * named / rows.length)}% captured` : null} />
|
||||
<Metric k="Returning" v={rows.filter(r => r.visit_count > 1).length || '—'} />
|
||||
<Metric k="With consent" v={rows.filter(r => r.has_consent).length || '—'} />
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<div style={{ display: 'flex', gap: 10, marginBottom: 12 }}>
|
||||
<input className="field" style={{ flex: 1, margin: 0, background: 'var(--ground)',
|
||||
border: '1px solid var(--line)', borderRadius: 6, padding: '8px 10px' }}
|
||||
placeholder="Search by name or phone"
|
||||
value={query} onChange={e => setQuery(e.target.value)} />
|
||||
<button className="btn" onClick={reload}>Refresh</button>
|
||||
</div>
|
||||
|
||||
<div className="panel">
|
||||
{rows.length === 0
|
||||
? <div className="empty">
|
||||
No customers yet. They appear here the first time a camera sees them.
|
||||
? <div className="empty tall">
|
||||
<Icon.NoFaces size={34} />
|
||||
<b>No customers yet</b>
|
||||
<p>They appear here the first time a camera recognises them. Click one to add a name and phone number.</p>
|
||||
</div>
|
||||
: <div className="tablewrap">
|
||||
<table>
|
||||
@@ -89,11 +88,12 @@ export default function Customers({ session }) {
|
||||
)
|
||||
}
|
||||
|
||||
function Stat({ label, value, sub }) {
|
||||
function Metric({ k, v, s }) {
|
||||
return (
|
||||
<div className="card stat">
|
||||
<h3>{label}</h3><div className="value">{value}</div>
|
||||
{sub && <div className="sub">{sub}</div>}
|
||||
<div className="metric">
|
||||
<div className="metric-k">{k}</div>
|
||||
<div className="metric-v">{v}</div>
|
||||
{s && <div className="metric-s">{s}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -16,22 +16,29 @@ import * as Icon from '../ui/icons.jsx'
|
||||
// No camera picture here - the person at the counter is not watching CCTV,
|
||||
// and a live video tile costs CPU the recognition pipeline needs. The picture
|
||||
// lives on the Cameras screen, where it is a setup tool.
|
||||
export default function Live() {
|
||||
export default function Live({ onNavigate }) {
|
||||
const { data, error } = usePolled(() => api.live(), 3000)
|
||||
const { data: pipe } = usePolled(() => api.pipelineStatus(), 5000)
|
||||
|
||||
const cameras = data?.stats?.cameras ?? []
|
||||
const gallery = data?.stats?.gallery ?? {}
|
||||
const events = data?.events ?? []
|
||||
// No engine on THIS PC, so everything below came from head office. It has to
|
||||
// be said rather than implied: a laptop in a hotel showing "2 cameras live"
|
||||
// without this line is claiming to watch a shop it cannot see.
|
||||
const viewing = data?.viewing === true
|
||||
|
||||
// fraction_below_gate is the number that decides a site: what share of the
|
||||
// faces this camera saw were too poor to enrol. Surfaced rather than buried,
|
||||
// because a high value looks exactly like "a quiet day".
|
||||
const worst = cameras.reduce((acc, c) => {
|
||||
const f = c?.pipeline?.best_quality?.fraction_below_gate
|
||||
return typeof f === 'number' && f > acc ? f : acc
|
||||
}, 0)
|
||||
const up = cameras.filter(c => c.connected).length
|
||||
const worst = viewing
|
||||
? (data?.stats?.fraction_below_gate ?? 0)
|
||||
: cameras.reduce((acc, c) => {
|
||||
const f = c?.pipeline?.best_quality?.fraction_below_gate
|
||||
return typeof f === 'number' && f > acc ? f : acc
|
||||
}, 0)
|
||||
// Viewing: the server already summed these across the estate.
|
||||
const up = viewing ? (data?.stats?.cameras_up ?? 0) : cameras.filter(c => c.connected).length
|
||||
|
||||
const arrivals = events.filter(e => e.type === 'person.new' || e.type === 'person.seen')
|
||||
const freshest = useFreshest(arrivals[0])
|
||||
@@ -40,13 +47,30 @@ export default function Live() {
|
||||
<div className="page">
|
||||
<header>
|
||||
<h2>Live</h2>
|
||||
<p>Who is in the shop, and whether it is reaching head office.</p>
|
||||
<p>{viewing
|
||||
? 'Your shops, as head office sees them.'
|
||||
: 'Who is in the shop, and whether it is reaching head office.'}</p>
|
||||
</header>
|
||||
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
|
||||
{viewing && (
|
||||
<div className="viewing">
|
||||
<Icon.Cloud size={15} />
|
||||
<span><b>Viewing your shops from here.</b> This computer is not watching
|
||||
any cameras itself — everything below is what your shop PCs reported.
|
||||
To recognise people on this machine, it has to be on the same network
|
||||
as a camera.</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<PipelineStrip pipe={pipe} cameras={cameras} up={up} />
|
||||
|
||||
{/* Getting Started walks somebody through setting up a camera on THIS
|
||||
PC — not what a viewer is doing, and not something they could finish
|
||||
from here. */}
|
||||
{!viewing && <GettingStarted cameras={cameras} arrivals={arrivals} onNavigate={onNavigate} />}
|
||||
|
||||
<div className="panel arrivals-panel">
|
||||
<div className="panelhead">
|
||||
<h3>Who just walked in</h3>
|
||||
@@ -82,6 +106,65 @@ export default function Live() {
|
||||
)
|
||||
}
|
||||
|
||||
// The first five minutes, as a checklist that ticks itself.
|
||||
//
|
||||
// Before this the Live screen after install was "Nobody yet" over a row of
|
||||
// dashes, with an amber "No cameras" in the far corner. Nothing said what to
|
||||
// do next. This says the three things, in order, and each step reads its own
|
||||
// state from the engine: recognition ready, a camera added, the camera
|
||||
// proven by a walk-past. It disappears on its own once someone has actually
|
||||
// been recognised, because at that point the product has explained itself.
|
||||
function GettingStarted({ cameras, arrivals, onNavigate }) {
|
||||
const { data: eng } = usePolled(() => api.engineStatus(), 4000)
|
||||
const [hidden, setHidden] = useState(() => { try { return localStorage.getItem('bv.gettingStarted') === 'done' } catch { return false } })
|
||||
const hasCamera = cameras.length > 0
|
||||
const anyUp = cameras.some(c => c.connected)
|
||||
const { data: checks } = usePolled(async () => {
|
||||
const out = {}
|
||||
for (const c of cameras) { try { out[c.camera_id] = await api.placementResult(c.camera_id) } catch { /* not yet */ } }
|
||||
return out
|
||||
}, 10000, [cameras.map(c => c.camera_id).join('|')])
|
||||
const proven = Object.values(checks ?? {}).some(r => r && !r.running && r.verdict === 'good')
|
||||
const recognised = arrivals.length > 0
|
||||
|
||||
if (hidden || recognised) return null
|
||||
const engineReady = Boolean(eng?.reachable && eng?.recognition_model)
|
||||
const progress = eng?.progress
|
||||
|
||||
const steps = [
|
||||
{ done: engineReady, now: !engineReady,
|
||||
title: engineReady ? `Recognition ready (${eng.recognition_model})` : progress ? `Downloading ${progress.what}… ${progress.percent}%` : 'Starting recognition…',
|
||||
text: engineReady ? null : 'First start downloads about 275 MB of recognition models. A few minutes on a normal connection; nothing to do meanwhile.' },
|
||||
{ done: hasCamera && anyUp, now: engineReady && !(hasCamera && anyUp),
|
||||
title: hasCamera ? (anyUp ? 'Camera connected' : 'Camera added — not connecting yet') : 'Add your camera',
|
||||
text: hasCamera ? (anyUp ? null : 'Check its password and stream path under Cameras → Edit.') : 'Behavision can find it on the network; you type only its password.',
|
||||
action: hasCamera ? null : { label: 'Add camera', go: 'cameras' } },
|
||||
{ done: proven, now: hasCamera && anyUp && !proven,
|
||||
title: proven ? 'Camera proven — it can recognise faces' : 'Walk past the camera',
|
||||
text: proven ? null : 'Run Check placement and walk past like a customer for 25 seconds. Only a “good” verdict means it will recognise people.',
|
||||
action: hasCamera && anyUp && !proven ? { label: 'Check placement', go: 'cameras' } : null },
|
||||
]
|
||||
|
||||
return (
|
||||
<section className="panel starter">
|
||||
<div className="panelhead">
|
||||
<h3>Getting started</h3>
|
||||
<button className="btn sm ghost" onClick={() => { try { localStorage.setItem('bv.gettingStarted', 'done') } catch {} ; setHidden(true) }}>Hide</button>
|
||||
</div>
|
||||
<ol className="steps compact">
|
||||
{steps.map((st, i) => (
|
||||
<li key={i} className={st.done ? 'done' : st.now ? 'now' : ''}>
|
||||
<b>{st.title}</b>
|
||||
{st.text && <span>{st.text}</span>}
|
||||
{st.action && <button className="btn sm primary" onClick={() => onNavigate?.(st.action.go)}>{st.action.label}</button>}
|
||||
</li>
|
||||
))}
|
||||
</ol>
|
||||
<p className="note">The moment a customer is recognised, this list goes away.</p>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
// One customer, big enough to match against the person in front of you.
|
||||
function Arrival({ e, fresh }) {
|
||||
const isNew = e.type === 'person.new'
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import { api, message } from '../bridge.js'
|
||||
import * as Icon from '../ui/icons.jsx'
|
||||
import logo from '../assets/loyaly-mark.png'
|
||||
|
||||
// The gate. Nothing else in the app is reachable until this succeeds, because
|
||||
// the broker credentials and the customer database both live behind it.
|
||||
@@ -25,7 +26,7 @@ export default function Login({ onDone }) {
|
||||
return (
|
||||
<div className="login">
|
||||
<div className="box">
|
||||
<span className="mark"><Icon.Shield size={20} /></span>
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<h1>Behavision</h1>
|
||||
<p className="lead">Sign in to connect this PC to your store.</p>
|
||||
<form onSubmit={submit}>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import { api, message } from '../bridge.js'
|
||||
import * as Icon from '../ui/icons.jsx'
|
||||
import logo from '../assets/loyaly-mark.png'
|
||||
|
||||
// Linking this PC to a shop — the first thing that happens on a new install,
|
||||
// and until now the one thing the app could not do.
|
||||
@@ -14,7 +15,6 @@ export default function Setup({ onDone, onCancel }) {
|
||||
const [code, setCode] = useState('')
|
||||
const [busy, setBusy] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
const [alone, setAlone] = useState(false)
|
||||
|
||||
async function submit(e) {
|
||||
e.preventDefault()
|
||||
@@ -39,13 +39,66 @@ export default function Setup({ onDone, onCancel }) {
|
||||
}
|
||||
}
|
||||
|
||||
// First launch: a choice, not a code box. Two thirds of the people who
|
||||
// open this have no idea what an installation code is; the other third has
|
||||
// one in their hand. Both must see their own path in the first second.
|
||||
const [path, setPath] = useState(onCancel ? 'code' : null)
|
||||
|
||||
if (path === null) {
|
||||
return (
|
||||
<div className="login">
|
||||
<div className="box wide">
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<h1>Welcome to Behavision</h1>
|
||||
<p className="lead">
|
||||
This PC will watch your shop’s cameras and recognise returning customers.
|
||||
First, one question: is this shop managed from a head office?
|
||||
</p>
|
||||
<div className="choices">
|
||||
<button type="button" className="choice" onClick={() => setPath('code')}>
|
||||
<Icon.Cloud size={22} />
|
||||
<b>Yes — I have an installation code</b>
|
||||
<span>Head office gave you a code like <em>ABCDEF-123456-…</em>. This PC joins that shop and gets its cameras from there.</span>
|
||||
</button>
|
||||
<button type="button" className="choice" onClick={() => setPath('alone')}>
|
||||
<Icon.Shield size={22} />
|
||||
<b>No — set up on this PC only</b>
|
||||
<span>Cameras, customers and recognition stay on this PC. Nothing is sent anywhere. You can link to a head office later.</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (path === 'alone') {
|
||||
return (
|
||||
<div className="login">
|
||||
<div className="box">
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<h1>On this PC only</h1>
|
||||
<p className="lead">
|
||||
Behavision will run entirely here. Next you’ll add your camera — it can find it on the network for you — and walk past it once so it can prove it works.
|
||||
</p>
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
<button type="button" className="btn primary" disabled={!!busy} onClick={standalone}>
|
||||
{busy === 'alone' ? 'Setting up…' : 'Continue'}
|
||||
</button>
|
||||
<div className="alt">
|
||||
<button type="button" className="linkbtn" onClick={() => setPath(null)}><Icon.Back size={14} /> Back</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="login">
|
||||
<div className="box">
|
||||
<span className="mark"><Icon.Link size={20} /></span>
|
||||
<h1>{onCancel ? 'Link to head office' : 'Set up this PC'}</h1>
|
||||
<span className="mark"><img src={logo} alt="" /></span>
|
||||
<h1>{onCancel ? 'Link to head office' : 'Join your shop'}</h1>
|
||||
<p className="lead">
|
||||
Type the installation code for this shop. You only do this once.
|
||||
Type the installation code head office gave you. It works once, and this PC becomes that shop.
|
||||
</p>
|
||||
<form onSubmit={submit}>
|
||||
{error && <div className="err"><Icon.Warning size={15} />{error}</div>}
|
||||
@@ -66,38 +119,12 @@ export default function Setup({ onDone, onCancel }) {
|
||||
</button>
|
||||
</form>
|
||||
<p className="foot">
|
||||
The code works once. Ask whoever manages your shops for it — they can
|
||||
create one from the Behavision platform, under the shop.
|
||||
Don’t have one? Whoever runs head office creates it under the shop: <em>Shops → the shop → Set up a shop PC</em>.
|
||||
</p>
|
||||
|
||||
{/* The second way out of this screen, and the reason it exists.
|
||||
Recognition, the cameras and this shop's own gallery all run on
|
||||
this PC and need no server, so a shop with one till and no head
|
||||
office was being blocked from adding a camera until somebody
|
||||
issued it a code — the software refusing to do the thing it is
|
||||
for. Linking later is still one click away, and it keeps the
|
||||
visits already recorded here. */}
|
||||
<div className="alt">
|
||||
{onCancel
|
||||
? <button type="button" className="linkbtn" onClick={onCancel}>
|
||||
Not now — go back
|
||||
</button>
|
||||
: !alone
|
||||
? <button type="button" className="linkbtn" onClick={() => setAlone(true)}>
|
||||
No head office — set this PC up on its own
|
||||
</button>
|
||||
: <>
|
||||
<p className="note">
|
||||
This PC will watch its cameras and recognise returning
|
||||
customers on its own. Nothing is sent anywhere. You can link
|
||||
it to head office later without losing anything recorded
|
||||
here.
|
||||
</p>
|
||||
<button type="button" className="btn" disabled={!!busy}
|
||||
onClick={standalone}>
|
||||
{busy === 'alone' ? 'Setting up…' : 'Use this PC on its own'}
|
||||
</button>
|
||||
</>}
|
||||
? <button type="button" className="linkbtn" onClick={onCancel}>Not now — go back</button>
|
||||
: <button type="button" className="linkbtn" onClick={() => setPath(null)}><Icon.Back size={14} /> Back</button>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -2,28 +2,32 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
agentpaths "github.com/loyaly/behavision-agent/pkg/paths"
|
||||
)
|
||||
|
||||
// iconFor renders the tray icon at run time rather than embedding four PNGs.
|
||||
// iconFor renders the tray icon at run time: the Loyaly mark with a state
|
||||
// dot in the corner. Green, amber, red or grey is the only thing a taskbar
|
||||
// conveys at this size, and the mark is what makes it OURS among a row of
|
||||
// other icons - a plain coloured circle read as a generic status light.
|
||||
//
|
||||
// A 16x16 filled circle is all the taskbar shows at this size, and generating
|
||||
// it means the four states cannot drift apart visually or have one file go
|
||||
// missing from a build.
|
||||
// The mark is embedded once at 128px and scaled down here, so the four
|
||||
// states cannot drift apart and no file can go missing from a build.
|
||||
//
|
||||
// The encoding is per-platform and is NOT cosmetic. systray writes these bytes
|
||||
// to a temp file and, on Windows, hands the path to LoadImageW with
|
||||
// IMAGE_ICON|LR_LOADFROMFILE — which decodes .ico and nothing else. A PNG
|
||||
// IMAGE_ICON|LR_LOADFROMFILE - which decodes .ico and nothing else. A PNG
|
||||
// there returns 0, systray logs "unable to set icon", and the product ships
|
||||
// with no tray icon at all: the one control surface a shop manager has.
|
||||
func iconFor(state string) []byte {
|
||||
img := circle(colorFor(state))
|
||||
img := trayImage(colorFor(state))
|
||||
if runtime.GOOS == "windows" {
|
||||
return encodeICO(img)
|
||||
}
|
||||
@@ -46,30 +50,93 @@ func colorFor(state string) color.RGBA {
|
||||
}
|
||||
}
|
||||
|
||||
const iconSize = 16
|
||||
// 32px rather than 16: Windows shows 16 at 100% scaling and 24 at 150%, and
|
||||
// scaling a 32 down looks right at both, where a 16 scaled up looks like 2005.
|
||||
const iconSize = 32
|
||||
|
||||
func circle(c color.RGBA) *image.RGBA {
|
||||
img := image.NewRGBA(image.Rect(0, 0, iconSize, iconSize))
|
||||
const r = 6.5
|
||||
cx, cy := float64(iconSize)/2-0.5, float64(iconSize)/2-0.5
|
||||
//go:embed tray-logo.png
|
||||
var trayLogoPNG []byte
|
||||
|
||||
var (
|
||||
trayLogoOnce sync.Once
|
||||
trayLogo *image.RGBA
|
||||
)
|
||||
|
||||
// logo is the embedded mark, decoded once and box-filtered down to iconSize.
|
||||
// A box filter rather than nearest-neighbour: 128->32 is an exact 4x4 average
|
||||
// and nearest would drop three pixels in four, which shreds the thin outline.
|
||||
func logo() *image.RGBA {
|
||||
trayLogoOnce.Do(func() {
|
||||
src, err := png.Decode(bytes.NewReader(trayLogoPNG))
|
||||
if err != nil {
|
||||
trayLogo = image.NewRGBA(image.Rect(0, 0, iconSize, iconSize))
|
||||
return
|
||||
}
|
||||
b := src.Bounds()
|
||||
f := b.Dx() / iconSize
|
||||
out := image.NewRGBA(image.Rect(0, 0, iconSize, iconSize))
|
||||
for y := 0; y < iconSize; y++ {
|
||||
for x := 0; x < iconSize; x++ {
|
||||
var r, g, bl, a uint64
|
||||
for dy := 0; dy < f; dy++ {
|
||||
for dx := 0; dx < f; dx++ {
|
||||
// Premultiplied so transparent pixels do not drag the
|
||||
// colour of the edge towards black.
|
||||
pr, pg, pb, pa := src.At(b.Min.X+x*f+dx, b.Min.Y+y*f+dy).RGBA()
|
||||
r += uint64(pr)
|
||||
g += uint64(pg)
|
||||
bl += uint64(pb)
|
||||
a += uint64(pa)
|
||||
}
|
||||
}
|
||||
n := uint64(f * f)
|
||||
out.SetRGBA(x, y, color.RGBA{
|
||||
R: uint8(r / n >> 8), G: uint8(g / n >> 8), B: uint8(bl / n >> 8), A: uint8(a / n >> 8),
|
||||
})
|
||||
}
|
||||
}
|
||||
trayLogo = out
|
||||
})
|
||||
return trayLogo
|
||||
}
|
||||
|
||||
// trayImage is the mark with a state dot over its bottom-right corner, ringed
|
||||
// so it reads against both the yellow of the mark and a dark taskbar.
|
||||
func trayImage(c color.RGBA) *image.RGBA {
|
||||
base := logo()
|
||||
img := image.NewRGBA(base.Bounds())
|
||||
copy(img.Pix, base.Pix)
|
||||
const r = 6.0
|
||||
cx, cy := float64(iconSize)-r-0.5, float64(iconSize)-r-0.5
|
||||
ring := color.RGBA{R: 0x11, G: 0x14, B: 0x18, A: 0xFF}
|
||||
for y := 0; y < iconSize; y++ {
|
||||
for x := 0; x < iconSize; x++ {
|
||||
dx, dy := float64(x)-cx, float64(y)-cy
|
||||
d := dx*dx + dy*dy
|
||||
switch {
|
||||
case d <= (r-1)*(r-1):
|
||||
case d <= (r-1.5)*(r-1.5):
|
||||
img.SetRGBA(x, y, c)
|
||||
case d <= r*r:
|
||||
img.SetRGBA(x, y, ring)
|
||||
case d <= (r+1)*(r+1):
|
||||
// One-pixel feathered edge; a hard-aliased circle looks broken
|
||||
// next to every other icon in the tray.
|
||||
a := uint8(float64(c.A) * (r*r - d) / (r*r - (r-1)*(r-1)))
|
||||
img.SetRGBA(x, y, color.RGBA{R: c.R, G: c.G, B: c.B, A: a})
|
||||
a := uint8(255 * ((r+1)*(r+1) - d) / ((r+1)*(r+1) - r*r))
|
||||
bg := img.RGBAAt(x, y)
|
||||
img.SetRGBA(x, y, blend(bg, ring, a))
|
||||
}
|
||||
}
|
||||
}
|
||||
return img
|
||||
}
|
||||
|
||||
func blend(under, over color.RGBA, a uint8) color.RGBA {
|
||||
fa := float64(a) / 255
|
||||
mix := func(u, o uint8) uint8 { return uint8(float64(u)*(1-fa) + float64(o)*fa) }
|
||||
ua := float64(under.A)/255*(1-fa) + fa
|
||||
return color.RGBA{R: mix(under.R, over.R), G: mix(under.G, over.G), B: mix(under.B, over.B), A: uint8(ua * 255)}
|
||||
}
|
||||
|
||||
// encodeICO writes a single-image .ico holding an uncompressed 32-bit DIB.
|
||||
//
|
||||
// Vista and later also accept a PNG stored inside the .ico container, which
|
||||
@@ -94,8 +161,8 @@ func encodeICO(img *image.RGBA) []byte {
|
||||
// ICONDIRENTRY. 256 is encoded as 0 in these byte fields; at 16px it is moot.
|
||||
b.WriteByte(byte(w))
|
||||
b.WriteByte(byte(h))
|
||||
b.WriteByte(0) // palette size: none
|
||||
b.WriteByte(0) // reserved
|
||||
b.WriteByte(0) // palette size: none
|
||||
b.WriteByte(0) // reserved
|
||||
binary.Write(&b, binary.LittleEndian, uint16(1)) // colour planes
|
||||
binary.Write(&b, binary.LittleEndian, uint16(32)) // bits per pixel
|
||||
binary.Write(&b, binary.LittleEndian, uint32(dib)) // bytes in resource
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// nothing — and nothing on a Mac could notice. These tests are the substitute
|
||||
// for the Windows box we do not have.
|
||||
func TestEncodeICOIsAValidIconFile(t *testing.T) {
|
||||
b := encodeICO(circle(colorFor("ok")))
|
||||
b := encodeICO(trayImage(colorFor("ok")))
|
||||
if len(b) < 22 {
|
||||
t.Fatalf("far too short: %d bytes", len(b))
|
||||
}
|
||||
@@ -48,12 +48,13 @@ func TestEncodeICOIsAValidIconFile(t *testing.T) {
|
||||
|
||||
func TestEncodeICOPixelsAreBGRABottomUp(t *testing.T) {
|
||||
want := colorFor("error") // red: distinguishable from B and G if swapped
|
||||
img := circle(want)
|
||||
img := trayImage(want)
|
||||
b := encodeICO(img)
|
||||
// Centre of the circle, which is solid fill. Bottom-up means image row
|
||||
// iconSize/2 lands at DIB row iconSize/2-1 counting from the start.
|
||||
row := iconSize - 1 - iconSize/2
|
||||
i := 22 + 40 + (row*iconSize+iconSize/2)*4
|
||||
// Centre of the state dot, which is solid fill. Bottom-up means image row
|
||||
// y lands at DIB row iconSize-1-y counting from the start.
|
||||
x, y := iconSize-6-1, iconSize-6-1
|
||||
row := iconSize - 1 - y
|
||||
i := 22 + 40 + (row*iconSize+x)*4
|
||||
got := b[i : i+4]
|
||||
if !bytes.Equal(got, []byte{want.B, want.G, want.R, 0xFF}) {
|
||||
t.Errorf("centre pixel = % x, want % x (BGRA)",
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -39,6 +40,19 @@ type Client struct {
|
||||
// single-use refresh token.
|
||||
refreshMu sync.Mutex
|
||||
onRefresh func(Session)
|
||||
|
||||
// Camera snapshots already fetched, keyed by camera id. The Cameras screen
|
||||
// polls every 8 seconds and a snapshot is ~90 KB, so re-fetching one that
|
||||
// has not changed would put megabytes an hour on the wire to redraw the
|
||||
// same picture - the same trap the web app's useAuthedImage avoids by
|
||||
// keying on the url rather than the object around it.
|
||||
shotMu sync.Mutex
|
||||
shots map[string]cachedShot
|
||||
}
|
||||
|
||||
type cachedShot struct {
|
||||
at string // the server's snapshot_at; a new one is a new picture
|
||||
uri string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
@@ -108,8 +122,16 @@ func (c *Client) do(ctx context.Context, method, path string, body, out any) err
|
||||
}
|
||||
if rerr := c.Refresh(ctx); rerr != nil {
|
||||
// The refresh token is gone too, so this really is a sign-in, not a
|
||||
// transient failure. Report it as such so the UI shows the login sheet
|
||||
// rather than an error dialog.
|
||||
// transient failure. Forget the session - in memory AND on disk, through
|
||||
// the same callback that persists rotations - so the app goes back to
|
||||
// Login instead of showing "session expired" on every screen until
|
||||
// somebody finds Sign out. Seen on a PC that had been claimed against a
|
||||
// demo head office and then re-claimed against the real one: the old
|
||||
// login sat there, dead, for the whole session.
|
||||
c.Clear()
|
||||
if c.onRefresh != nil {
|
||||
c.onRefresh(Session{})
|
||||
}
|
||||
return ErrUnauthorized
|
||||
}
|
||||
return c.send(ctx, method, path, raw, out)
|
||||
@@ -522,6 +544,27 @@ func (c *Client) ForgetVisitor(ctx context.Context, id string) error {
|
||||
"/api/visitors/"+url.PathEscape(id), nil, nil)
|
||||
}
|
||||
|
||||
// AssistantTurn is one message in the help conversation. The browser holds
|
||||
// the history and resends it; nothing is stored server-side.
|
||||
type AssistantTurn struct {
|
||||
Role string `json:"role"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// AssistantAnswer is the reply, and the names of what it looked at - shown to
|
||||
// the user, because an assistant that silently ran a camera check would be
|
||||
// alarming and naming what it consulted makes a wrong answer traceable.
|
||||
type AssistantAnswer struct {
|
||||
Text string `json:"text"`
|
||||
Used []string `json:"used,omitempty"`
|
||||
}
|
||||
|
||||
// Ask puts a question to the head-office assistant as this signed-in user.
|
||||
func (c *Client) Ask(ctx context.Context, history []AssistantTurn) (AssistantAnswer, error) {
|
||||
var out AssistantAnswer
|
||||
return out, c.do(ctx, http.MethodPost, "/api/assistant", map[string]any{"history": history}, &out)
|
||||
}
|
||||
|
||||
func (c *Client) VisitorHistory(ctx context.Context, id string, limit int) ([]Visit, error) {
|
||||
var out []Visit
|
||||
return out, c.do(ctx, http.MethodGet,
|
||||
@@ -600,3 +643,182 @@ func (c *Client) RecordPurchase(ctx context.Context, visitorID string,
|
||||
"items": items, "source": "manual", "notes": notes,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- viewing --
|
||||
//
|
||||
// A PC with no engine of its own is not broken, it is a VIEWER: somebody
|
||||
// signed in on a laptop away from the shop. Everything below reads head
|
||||
// office so those screens have something true to show instead of "engine not
|
||||
// reachable", which is an accurate sentence and a useless one when the reader
|
||||
// was never expecting an engine on that machine.
|
||||
|
||||
// Arrival is one visit as the estate's feed reports it, across every shop -
|
||||
// not just this PC's. `GET /api/visits`.
|
||||
type Arrival struct {
|
||||
VisitID string `json:"visit_id"`
|
||||
VisitRef string `json:"visit_ref"`
|
||||
OccurredAt string `json:"occurred_at"`
|
||||
Site string `json:"site"`
|
||||
SiteSlug string `json:"site_slug"`
|
||||
CameraID string `json:"camera_id"`
|
||||
VisitorID string `json:"visitor_id"`
|
||||
Ref string `json:"ref"`
|
||||
Label string `json:"label"`
|
||||
IsNew bool `json:"is_new_visitor"`
|
||||
Similarity float64 `json:"similarity"`
|
||||
Attributes map[string]any `json:"attributes"`
|
||||
Image Photo `json:"image"`
|
||||
}
|
||||
|
||||
// RemoteCamera is a camera as HEAD OFFICE knows it. Deliberately not the same
|
||||
// type the local engine returns: this one can never be edited from here (the
|
||||
// shop PC on that LAN is the only thing that can reach it) and it carries a
|
||||
// snapshot rather than a stream.
|
||||
type RemoteCamera struct {
|
||||
ID string `json:"id"`
|
||||
CameraID string `json:"camera_id"`
|
||||
Label string `json:"label"`
|
||||
Site string `json:"site"`
|
||||
SiteSlug string `json:"site_slug"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Connected *bool `json:"connected"`
|
||||
LastSeenAt string `json:"last_seen_at"`
|
||||
// State is the server's single answer - connected / not_connecting /
|
||||
// waiting / stale - and the screen renders that rather than deciding
|
||||
// again from Connected. Two places deciding one fact is how a shop came
|
||||
// out labelled Working, in green, above "2 of 3 cameras not connecting".
|
||||
State string `json:"state"`
|
||||
StateNote string `json:"state_note"`
|
||||
Snapshot Photo `json:"snapshot"`
|
||||
SnapshotAt string `json:"snapshot_at"`
|
||||
}
|
||||
|
||||
// Arrivals reads the estate's recent visits, newest last.
|
||||
func (c *Client) Arrivals(ctx context.Context, limit int) ([]Arrival, error) {
|
||||
var out struct {
|
||||
Arrivals []Arrival `json:"arrivals"`
|
||||
}
|
||||
if err := c.send(ctx, http.MethodGet,
|
||||
fmt.Sprintf("/api/visits?limit=%d", limit), nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Arrivals, nil
|
||||
}
|
||||
|
||||
// RemoteCameras lists every camera head office knows about for this company.
|
||||
func (c *Client) RemoteCameras(ctx context.Context) ([]RemoteCamera, error) {
|
||||
var out []RemoteCamera
|
||||
if err := c.send(ctx, http.MethodGet, "/api/cameras", nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range out {
|
||||
out[i].Snapshot = c.resolveShot(ctx, out[i].ID, out[i].SnapshotAt, out[i].Snapshot)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// resolveShot turns a camera snapshot into something the window can render.
|
||||
//
|
||||
// Same problem VisitorImage has and the same answer: a deployment with no
|
||||
// object storage serves the picture from the API itself, so the url is
|
||||
// relative and needs this session's bearer. A webview <img> can supply
|
||||
// neither - it resolves a relative src against wails:// and cannot set a
|
||||
// header - so the bytes are fetched here and passed as a data: URI.
|
||||
//
|
||||
// A failure is an absence with a reason, never an error. Whether the camera is
|
||||
// CONNECTED is the answer this screen exists to give; the photograph is
|
||||
// decoration, and blanking the card because a picture would not load would
|
||||
// hide the part that matters.
|
||||
func (c *Client) resolveShot(ctx context.Context, camID, at string, p Photo) Photo {
|
||||
if !p.Available || !p.Auth || p.URL == "" {
|
||||
return p
|
||||
}
|
||||
c.shotMu.Lock()
|
||||
hit, ok := c.shots[camID]
|
||||
c.shotMu.Unlock()
|
||||
if ok && hit.at == at && at != "" {
|
||||
p.URL, p.Auth = hit.uri, false
|
||||
return p
|
||||
}
|
||||
uri, err := c.fetchImage(ctx, p.URL)
|
||||
if err != nil {
|
||||
return Photo{Reason: "That camera's picture could not be loaded."}
|
||||
}
|
||||
c.shotMu.Lock()
|
||||
if c.shots == nil {
|
||||
c.shots = map[string]cachedShot{}
|
||||
}
|
||||
c.shots[camID] = cachedShot{at: at, uri: uri}
|
||||
c.shotMu.Unlock()
|
||||
p.URL, p.Auth = uri, false
|
||||
return p
|
||||
}
|
||||
|
||||
// CameraLive opens head office's live relay for one camera and returns the
|
||||
// live SSE response for the caller to read and close.
|
||||
//
|
||||
// A response rather than frames, because the consumer is the app's own
|
||||
// loopback relay: it re-emits these frames as MJPEG so an <img> can show them,
|
||||
// and buffering the stream through a channel here would only add a place for
|
||||
// frames to queue. A stale frame is worthless - the only one worth having is
|
||||
// the newest - which is the whole reason LiveHub drops rather than queues.
|
||||
//
|
||||
// There is no client timeout on this request. A live view is endless by
|
||||
// design and any deadline would cut the picture off mid-shift; the context is
|
||||
// what ends it, when the viewer navigates away.
|
||||
func (c *Client) CameraLive(ctx context.Context, cameraID string) (*http.Response, error) {
|
||||
resp, err := c.liveOnce(ctx, cameraID)
|
||||
if errors.Is(err, errTokenExpired) {
|
||||
if rerr := c.Refresh(ctx); rerr != nil {
|
||||
return nil, rerr
|
||||
}
|
||||
resp, err = c.liveOnce(ctx, cameraID)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) liveOnce(ctx context.Context, cameraID string) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||||
c.Base+"/api/cameras/"+url.PathEscape(cameraID)+"/live", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
c.mu.RLock()
|
||||
tok := c.token
|
||||
c.mu.RUnlock()
|
||||
if tok == "" {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
|
||||
// c.http has a 30 s timeout, which covers the whole response and would
|
||||
// therefore sever a working live view every thirty seconds - the same
|
||||
// trap that made the server set WriteTimeout to zero for its own SSE
|
||||
// endpoint. A dedicated client, with the dial bounded instead.
|
||||
hc := &http.Client{Transport: &http.Transport{
|
||||
DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
}}
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot reach %s: %w", c.Base, err)
|
||||
}
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
var e struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 8192))
|
||||
resp.Body.Close()
|
||||
_ = json.Unmarshal(body, &e)
|
||||
if e.Error == "token_expired" {
|
||||
return nil, errTokenExpired
|
||||
}
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
resp.Body.Close()
|
||||
return nil, fmt.Errorf("live view: %s", resp.Status)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
agentconfig "github.com/loyaly/behavision-agent/pkg/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -20,7 +21,11 @@ type Client struct {
|
||||
Base string
|
||||
User string
|
||||
Password string
|
||||
http *http.Client
|
||||
// Creds re-reads the engine's generated credential when one is rejected.
|
||||
// On a first run the app starts the engine, and the engine writes that
|
||||
// file seconds later - after the app has already looked for it.
|
||||
Creds *agentconfig.Creds
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(base, user, password string) *Client {
|
||||
@@ -48,8 +53,12 @@ func (c *Client) do(ctx context.Context, method, path string, body, out any) err
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.User != "" {
|
||||
req.SetBasicAuth(c.User, c.Password)
|
||||
user, pass := c.User, c.Password
|
||||
if c.Creds != nil {
|
||||
user, pass = c.Creds.Get()
|
||||
}
|
||||
if user != "" {
|
||||
req.SetBasicAuth(user, pass)
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
@@ -105,6 +114,12 @@ func (c *Client) DeleteCamera(ctx context.Context, id string) error {
|
||||
return c.do(ctx, http.MethodDelete, "/api/cameras/"+id, nil, nil)
|
||||
}
|
||||
|
||||
// DiscoverCameras asks the engine to scan the shop's network. A few seconds.
|
||||
func (c *Client) DiscoverCameras(ctx context.Context) (map[string]any, error) {
|
||||
var out map[string]any
|
||||
return out, c.do(ctx, http.MethodGet, "/api/cameras/discover", nil, &out)
|
||||
}
|
||||
|
||||
func (c *Client) TestCamera(ctx context.Context, cam map[string]any) (map[string]any, error) {
|
||||
var out map[string]any
|
||||
return out, c.do(ctx, http.MethodPost, "/api/cameras/test", cam, &out)
|
||||
|
||||
@@ -12,10 +12,12 @@ import (
|
||||
"context"
|
||||
"embed"
|
||||
"log"
|
||||
runtime2 "runtime"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/mac"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/windows"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
@@ -43,8 +45,13 @@ func main() {
|
||||
UniqueId: "ai.loyaly.behavision.desktop",
|
||||
OnSecondInstanceLaunch: func(options.SecondInstanceData) {
|
||||
if ctxRef != nil {
|
||||
runtime.Show(ctxRef)
|
||||
runtime.WindowUnminimise(ctxRef)
|
||||
// The same four calls the tray uses, and for the same reasons:
|
||||
// this runs on Wails' own listener goroutine rather than the
|
||||
// window's thread, and the launching process holds the
|
||||
// foreground, so without the flip the window comes back behind
|
||||
// it. Double-clicking the desktop icon while it is already
|
||||
// running is the single most common way anyone reaches this.
|
||||
go openWindow(ctxRef)
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -61,15 +68,30 @@ func main() {
|
||||
// Closing the window hides it rather than quitting: the engine must
|
||||
// keep recognising after a shop assistant clicks the X, and the tray
|
||||
// is where they get the window back.
|
||||
HideWindowOnClose: true,
|
||||
// Windows hides on close because the tray is how the window comes
|
||||
// back and how recognition is stopped. macOS has no tray here (see
|
||||
// tray_run_darwin.go), so hiding would leave a running engine with no
|
||||
// window, no tray and no way to reach either - force-quit or nothing.
|
||||
// Closing the window therefore quits, which also stops the engine
|
||||
// through OnShutdown. Same rule as the tray's Quit: never leave it
|
||||
// watching with no visible control.
|
||||
HideWindowOnClose: runtime2.GOOS == "windows",
|
||||
OnStartup: func(ctx context.Context) {
|
||||
ctxRef = ctx
|
||||
app.startup(ctx)
|
||||
tray.start(ctx)
|
||||
},
|
||||
OnBeforeClose: func(ctx context.Context) bool {
|
||||
runtime.Hide(ctx)
|
||||
return true // prevent the close
|
||||
if runtime2.GOOS != "windows" {
|
||||
return false // let it close, and OnShutdown stops the engine
|
||||
}
|
||||
// WindowHide, not Hide. They are different calls on Windows -
|
||||
// WindowHide locks the OS thread for the Win32 work and Hide does
|
||||
// not - and the tray's reopen uses WindowShow, so hiding through
|
||||
// the other one leaves the pair mismatched. Same call, opposite
|
||||
// direction.
|
||||
runtime.WindowHide(ctx)
|
||||
return true // prevent the close; the tray is how it comes back
|
||||
},
|
||||
OnShutdown: func(ctx context.Context) {
|
||||
tray.stop()
|
||||
@@ -77,6 +99,17 @@ func main() {
|
||||
app.StopEngine()
|
||||
},
|
||||
Bind: []any{app},
|
||||
// This block has to EXIST, not merely be empty. Wails computes
|
||||
// `zoomable = !Mac.DisableZoom` inside `if frontendOptions.Mac !=
|
||||
// nil`, and the variable defaults to 0 - so leaving Mac unset does not
|
||||
// mean "defaults", it means the green maximise button is created dead.
|
||||
// There was a Windows block and no Mac one, so the window could not be
|
||||
// zoomed on macOS and nothing anywhere said why.
|
||||
Mac: &mac.Options{
|
||||
WebviewIsTransparent: false,
|
||||
WindowIsTranslucent: false,
|
||||
DisableZoom: false,
|
||||
},
|
||||
Windows: &windows.Options{
|
||||
WebviewIsTransparent: false,
|
||||
WindowIsTranslucent: false,
|
||||
|
||||
BIN
desktop/rsrc_windows_amd64.syso
Normal file
@@ -21,6 +21,7 @@ package main
|
||||
// session and the bytes are fetched and handed over as an object URL.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
@@ -64,6 +65,12 @@ type streamProxy struct {
|
||||
target string // engine origin, e.g. http://127.0.0.1:8010
|
||||
user string
|
||||
pass string
|
||||
|
||||
// Opens head office's live relay for one camera. Set on a computer that
|
||||
// is signed in, whether or not an engine runs here - which is the whole
|
||||
// point: watching a camera in another building is precisely the case
|
||||
// where there is no engine on this machine to ask.
|
||||
live func(ctx context.Context, cameraID string) (*http.Response, error)
|
||||
}
|
||||
|
||||
func newStreamProxy() *streamProxy { return &streamProxy{} }
|
||||
@@ -71,18 +78,49 @@ func newStreamProxy() *streamProxy { return &streamProxy{} }
|
||||
// start binds a loopback listener and begins relaying. Calling it again while
|
||||
// running is a no-op, so a restarted engine cannot leave two listeners behind.
|
||||
func (p *streamProxy) start(base, user, pass string) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.srv != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(base, "http://") && !strings.HasPrefix(base, "https://") {
|
||||
base = "http://" + base
|
||||
}
|
||||
if _, err := url.Parse(base); err != nil {
|
||||
return fmt.Errorf("engine base %q: %w", base, err)
|
||||
}
|
||||
if err := p.bind(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.target = strings.TrimRight(base, "/")
|
||||
p.user, p.pass = user, pass
|
||||
return nil
|
||||
}
|
||||
|
||||
// watchRemote makes the relay able to serve head office's live view, and
|
||||
// binds it if nothing else has.
|
||||
//
|
||||
// Separate from start() because the two are independent: a shop PC has both
|
||||
// an engine and a session, an owner's laptop has only a session, and a PC
|
||||
// still being set up has only an engine. Folding them together would mean a
|
||||
// computer with no engine could not watch a camera at all - which is the one
|
||||
// computer most likely to be trying to.
|
||||
func (p *streamProxy) watchRemote(fn func(context.Context, string) (*http.Response, error)) error {
|
||||
if err := p.bind(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.live = fn
|
||||
return nil
|
||||
}
|
||||
|
||||
// bind starts the loopback listener once. Calling it again while running is a
|
||||
// no-op, so neither a restarted engine nor a second sign-in can leave two
|
||||
// listeners behind.
|
||||
func (p *streamProxy) bind() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.srv != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// The engine's own credential exists precisely so that the live face feed
|
||||
// is never served open - CLAUDE.md is explicit that an unauthenticated
|
||||
@@ -105,8 +143,6 @@ func (p *streamProxy) start(base, user, pass string) error {
|
||||
|
||||
p.ln = ln
|
||||
p.token = hex.EncodeToString(raw)
|
||||
p.target = strings.TrimRight(base, "/")
|
||||
p.user, p.pass = user, pass
|
||||
// No client timeout: an MJPEG stream is endless by design and any deadline
|
||||
// would cut the picture off mid-shift. The request context ends it when
|
||||
// the webview navigates away or the tile is replaced.
|
||||
@@ -130,7 +166,7 @@ func (p *streamProxy) start(base, user, pass string) error {
|
||||
func (p *streamProxy) stop() {
|
||||
p.mu.Lock()
|
||||
srv, ln := p.srv, p.ln
|
||||
p.srv, p.ln, p.token = nil, nil, ""
|
||||
p.srv, p.ln, p.token, p.live = nil, nil, "", nil
|
||||
p.mu.Unlock()
|
||||
if srv != nil {
|
||||
_ = srv.Close()
|
||||
@@ -155,6 +191,7 @@ func (p *streamProxy) urlFor(cameraID, file string) string {
|
||||
func (p *streamProxy) handle(w http.ResponseWriter, r *http.Request) {
|
||||
p.mu.RLock()
|
||||
token, target, user, pass, client := p.token, p.target, p.user, p.pass, p.client
|
||||
liveFn := p.live
|
||||
p.mu.RUnlock()
|
||||
if token == "" || client == nil {
|
||||
http.NotFound(w, r)
|
||||
@@ -190,6 +227,17 @@ func (p *streamProxy) handle(w http.ResponseWriter, r *http.Request) {
|
||||
// calls; it is here so that adding a still later is a change to a screen
|
||||
// rather than a change to the one file where a mistake is a credentialed
|
||||
// proxy onto the biometric API.
|
||||
// Head office's relay, not the engine. The two are different machines and
|
||||
// different credentials, so this returns rather than falling through.
|
||||
if parts[3] == "live.mjpeg" {
|
||||
if liveFn == nil {
|
||||
http.Error(w, "not signed in to head office", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
p.relayRemote(w, r, cameraID, liveFn)
|
||||
return
|
||||
}
|
||||
|
||||
var enginePath string
|
||||
switch parts[3] {
|
||||
case "stream.mjpeg":
|
||||
|
||||
149
desktop/stream_remote.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package main
|
||||
|
||||
// Watching a camera in another building, from the app.
|
||||
//
|
||||
// The shop PC sits behind a router with no inbound route, so nothing here can
|
||||
// pull its MJPEG stream - that stream is served on the shop PC's own loopback
|
||||
// and always will be. Head office's LiveHub is the way round it: the agent
|
||||
// asks outbound whether anyone is watching and pushes JPEG frames up for
|
||||
// exactly as long as somebody is. The head-office web app already consumes
|
||||
// that; this is the same feed, for the app.
|
||||
//
|
||||
// It arrives as base64 frames over SSE, which an <img> cannot render, so this
|
||||
// re-emits them as multipart MJPEG - which an <img> renders natively, through
|
||||
// the relay that already exists for the local engine. That is what keeps ONE
|
||||
// code path in the screens: a tile points at a loopback URL and does not know
|
||||
// or care which building the picture came from.
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The boundary is ours to choose; it only has to be a string the JPEG bytes
|
||||
// cannot contain, and a marker line never appears inside JPEG data.
|
||||
const mjpegBoundary = "behavisionframe"
|
||||
|
||||
// A frame is base64, so ~1.33 bytes on the wire per byte of picture. The
|
||||
// engine re-encodes to 640 px for the relay and those measure ~20 KB, so this
|
||||
// is roughly a hundredfold headroom - large enough never to clip a real frame
|
||||
// and small enough that a broken or hostile stream cannot grow this process's
|
||||
// memory without bound.
|
||||
const maxFrameLine = 8 << 20
|
||||
|
||||
func (p *streamProxy) relayRemote(w http.ResponseWriter, r *http.Request,
|
||||
cameraID string, open func(context.Context, string) (*http.Response, error)) {
|
||||
|
||||
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary="+mjpegBoundary)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
flusher, _ := w.(http.Flusher)
|
||||
|
||||
// Send the headers NOW, before any frame exists. Go writes them on the
|
||||
// first body write, so without this the whole response - status line
|
||||
// included - waits for the shop computer to start pushing, and a viewer
|
||||
// whose camera is slow to answer sees the REQUEST time out rather than a
|
||||
// stream that has not painted yet. Measured against production: 30
|
||||
// seconds and not even a Content-Type.
|
||||
if flusher != nil {
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
// Reconnecting is normal, not an error. The server caps one push at five
|
||||
// minutes so that a tab left open for a week cannot leave a shop
|
||||
// uploading for a week - so a viewer who IS still there simply asks
|
||||
// again. Doing it here rather than in the page is what lets the <img>
|
||||
// survive the cap: it never sees the stream end.
|
||||
sent := 0
|
||||
for {
|
||||
if r.Context().Err() != nil {
|
||||
return
|
||||
}
|
||||
n, err := p.pumpRemote(w, flusher, r.Context(), cameraID, open)
|
||||
sent += n
|
||||
if r.Context().Err() != nil {
|
||||
return
|
||||
}
|
||||
// Nothing was written and the attempt failed. Writing an error body
|
||||
// now would be writing it into a multipart stream the <img> is
|
||||
// already parsing, so the picture simply stays on whatever it last
|
||||
// showed and the screen's own "not connecting" state is the report.
|
||||
if err != nil && sent == 0 {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
case <-time.After(1500 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pumpRemote runs one SSE connection to exhaustion and returns how many
|
||||
// frames it forwarded.
|
||||
func (p *streamProxy) pumpRemote(w http.ResponseWriter, flusher http.Flusher,
|
||||
ctx context.Context, cameraID string,
|
||||
open func(context.Context, string) (*http.Response, error)) (int, error) {
|
||||
|
||||
resp, err := open(ctx, cameraID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
sc := bufio.NewScanner(resp.Body)
|
||||
sc.Buffer(make([]byte, 0, 64*1024), maxFrameLine)
|
||||
|
||||
var event, data string
|
||||
frames := 0
|
||||
for sc.Scan() {
|
||||
line := sc.Text()
|
||||
switch {
|
||||
case strings.HasPrefix(line, "event: "):
|
||||
event = strings.TrimSpace(line[7:])
|
||||
case strings.HasPrefix(line, "data: "):
|
||||
data = line[6:]
|
||||
case line == "":
|
||||
// End of one SSE event. `waiting` means head office has us
|
||||
// registered and the shop PC has not started pushing yet - a real
|
||||
// second or two while the agent is asked, and nothing to draw.
|
||||
if event == "frame" && data != "" {
|
||||
if err := writeMJPEGFrame(w, flusher, data); err != nil {
|
||||
return frames, err // the webview went away
|
||||
}
|
||||
frames++
|
||||
}
|
||||
event, data = "", ""
|
||||
}
|
||||
}
|
||||
return frames, sc.Err()
|
||||
}
|
||||
|
||||
func writeMJPEGFrame(w http.ResponseWriter, flusher http.Flusher, b64 string) error {
|
||||
jpg, err := base64.StdEncoding.DecodeString(b64)
|
||||
if err != nil || len(jpg) == 0 {
|
||||
// One malformed frame is not a reason to tear down a working view.
|
||||
return nil
|
||||
}
|
||||
if _, err := fmt.Fprintf(w,
|
||||
"--%s\r\nContent-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n",
|
||||
mjpegBoundary, len(jpg)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := w.Write(jpg); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := w.Write([]byte("\r\n")); err != nil {
|
||||
return err
|
||||
}
|
||||
// Flushed per frame. Anything held waiting for a full buffer is a tile
|
||||
// that stays blank, which is indistinguishable from the view not working.
|
||||
if flusher != nil {
|
||||
flusher.Flush()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
103
desktop/stream_remote_live_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/loyaly/behavision-desktop/internal/cloud"
|
||||
)
|
||||
|
||||
// The whole chain against the real head office and a real shop computer:
|
||||
//
|
||||
// TEST_CLOUD_EMAIL=... TEST_CLOUD_PASSWORD=... \
|
||||
// go test ./desktop/ -run RemoteLive -v
|
||||
//
|
||||
// Everything in stream_remote_test.go proves the relay against a fake that
|
||||
// agrees with me. Only this proves the part that cannot be faked: that a shop
|
||||
// computer behind a router with no inbound route actually pushes frames when
|
||||
// asked, that they survive base64 and SSE, and that what comes out of the
|
||||
// loopback relay is a multipart stream an <img> will paint.
|
||||
//
|
||||
// It also costs something to run, which is why it is opt-in: watching makes
|
||||
// the shop computer upload for as long as the test reads.
|
||||
func TestRemoteLiveFromProduction(t *testing.T) {
|
||||
email, pass := os.Getenv("TEST_CLOUD_EMAIL"), os.Getenv("TEST_CLOUD_PASSWORD")
|
||||
if email == "" || pass == "" {
|
||||
t.Skip("set TEST_CLOUD_EMAIL and TEST_CLOUD_PASSWORD to run against production")
|
||||
}
|
||||
base := os.Getenv("TEST_CLOUD_URL")
|
||||
if base == "" {
|
||||
base = "https://mcp.loyaly.ai"
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
c := cloud.New(base)
|
||||
if _, err := c.Login(ctx, email, pass); err != nil {
|
||||
t.Fatalf("login: %v", err)
|
||||
}
|
||||
|
||||
cams, err := c.RemoteCameras(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("cameras: %v", err)
|
||||
}
|
||||
t.Logf("%d cameras", len(cams))
|
||||
target := os.Getenv("TEST_CLOUD_CAMERA")
|
||||
for _, cam := range cams {
|
||||
conn := "waiting"
|
||||
if cam.Connected != nil {
|
||||
conn = map[bool]string{true: "connected", false: "not connecting"}[*cam.Connected]
|
||||
}
|
||||
t.Logf(" %-10s %-16s %-15s snapshot=%v", cam.CameraID, cam.Site, conn, cam.Snapshot.Available)
|
||||
if target == "" && cam.Connected != nil && *cam.Connected {
|
||||
target = cam.CameraID
|
||||
}
|
||||
}
|
||||
if target == "" {
|
||||
t.Skip("no connected camera to watch")
|
||||
}
|
||||
|
||||
p := newStreamProxy()
|
||||
if err := p.watchRemote(c.CameraLive); err != nil {
|
||||
t.Fatalf("watchRemote: %v", err)
|
||||
}
|
||||
defer p.stop()
|
||||
|
||||
rctx, rcancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer rcancel()
|
||||
req, _ := http.NewRequestWithContext(rctx, http.MethodGet, p.urlFor(target, "live.mjpeg"), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("GET relay: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
start := time.Now()
|
||||
acc, buf, frames := make([]byte, 0, 1<<20), make([]byte, 32*1024), 0
|
||||
for frames < 10 {
|
||||
n, rerr := resp.Body.Read(buf)
|
||||
acc = append(acc, buf[:n]...)
|
||||
frames = bytes.Count(acc, []byte("--"+mjpegBoundary))
|
||||
if rerr != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
el := time.Since(start)
|
||||
t.Logf("watching %q: %d frames, %d bytes, %.1fs (%.1f fps, %.0f KB/s)",
|
||||
target, frames, len(acc), el.Seconds(),
|
||||
float64(frames)/el.Seconds(), float64(len(acc))/el.Seconds()/1024)
|
||||
|
||||
if frames < 3 {
|
||||
t.Fatalf("got %d frames from a connected camera - the shop computer is "+
|
||||
"not answering head office's request to push", frames)
|
||||
}
|
||||
// Bytes that are actually a picture, not a framing header that happens to
|
||||
// be well formed. A JPEG begins FFD8.
|
||||
if !bytes.Contains(acc, []byte{0xFF, 0xD8, 0xFF}) {
|
||||
t.Error("no JPEG start marker anywhere in the stream")
|
||||
}
|
||||
}
|
||||
209
desktop/stream_remote_test.go
Normal file
@@ -0,0 +1,209 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// jpg is a byte sequence that is not valid JPEG and does not need to be: what
|
||||
// is under test is that the bytes arrive intact and framed, not that a decoder
|
||||
// likes them.
|
||||
var jpg = []byte{0xFF, 0xD8, 'h', 'e', 'l', 'l', 'o', 0xFF, 0xD9}
|
||||
|
||||
// sseServer answers head office's live endpoint with `pushes` frames and then
|
||||
// ends the response, which is what the server's five-minute cap does.
|
||||
func sseServer(t *testing.T, frames int, hits *int32) *httptest.Server {
|
||||
t.Helper()
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt32(hits, 1)
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fl, _ := w.(http.Flusher)
|
||||
// Registered, nothing being pushed yet. Nothing may be drawn for it.
|
||||
fmt.Fprint(w, "event: waiting\ndata: \n\n")
|
||||
if fl != nil {
|
||||
fl.Flush()
|
||||
}
|
||||
for i := 0; i < frames; i++ {
|
||||
fmt.Fprintf(w, "event: frame\ndata: %s\n\n",
|
||||
base64.StdEncoding.EncodeToString(jpg))
|
||||
if fl != nil {
|
||||
fl.Flush()
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func openerFor(srv *httptest.Server) func(context.Context, string) (*http.Response, error) {
|
||||
return func(ctx context.Context, cam string) (*http.Response, error) {
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/live/"+cam, nil)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
}
|
||||
|
||||
// The whole point: base64 frames over SSE are not something an <img> can show,
|
||||
// and a multipart MJPEG stream is. Without this the app could only ever show a
|
||||
// still, on exactly the computers that cannot reach the camera any other way.
|
||||
func TestRemoteFramesReachTheWebviewAsMJPEG(t *testing.T) {
|
||||
var hits int32
|
||||
srv := sseServer(t, 3, &hits)
|
||||
defer srv.Close()
|
||||
|
||||
p := newStreamProxy()
|
||||
if err := p.watchRemote(openerFor(srv)); err != nil {
|
||||
t.Fatalf("watchRemote: %v", err)
|
||||
}
|
||||
defer p.stop()
|
||||
|
||||
u := p.urlFor("cam2", "live.mjpeg")
|
||||
if u == "" {
|
||||
t.Fatal("no relay url; the proxy did not bind")
|
||||
}
|
||||
|
||||
// The relay reconnects for as long as the viewer is there, so the read is
|
||||
// bounded by us rather than by the stream ending - exactly as an <img>
|
||||
// would behave.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("GET relay: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "multipart/x-mixed-replace") {
|
||||
t.Fatalf("Content-Type = %q, an <img> will not treat that as a stream", ct)
|
||||
}
|
||||
|
||||
// Read the first three frames' worth and stop; the relay would otherwise
|
||||
// go on reconnecting forever, which is the behaviour being relied on.
|
||||
want := append([]byte(fmt.Sprintf("--%s\r\nContent-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n",
|
||||
mjpegBoundary, len(jpg))), jpg...)
|
||||
got := make([]byte, 0, 4096)
|
||||
buf := make([]byte, 512)
|
||||
for len(got) < 3*len(want) {
|
||||
n, rerr := resp.Body.Read(buf)
|
||||
got = append(got, buf[:n]...)
|
||||
if rerr != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if n := bytes.Count(got, []byte("--"+mjpegBoundary)); n < 3 {
|
||||
t.Fatalf("got %d frames in %d bytes, want at least 3", n, len(got))
|
||||
}
|
||||
if !bytes.Contains(got, want) {
|
||||
t.Errorf("a frame was not framed as expected:\n%q", got[:min(len(got), 300)])
|
||||
}
|
||||
// `waiting` is a real state - head office has us registered and the shop
|
||||
// computer has not started pushing - and there is nothing to draw for it.
|
||||
// Emitting an empty part would blank a tile that already had a picture.
|
||||
if bytes.Contains(got, []byte("Content-Length: 0")) {
|
||||
t.Error("an empty frame was written for a waiting event")
|
||||
}
|
||||
}
|
||||
|
||||
// The server caps one push at five minutes so a tab left open for a week
|
||||
// cannot leave a shop uploading for a week. Reconnecting is therefore a normal
|
||||
// event, and doing it here rather than in the page is what lets the <img>
|
||||
// survive the cap - it never sees the stream end.
|
||||
func TestTheRelayReconnectsWhenHeadOfficeEndsAPush(t *testing.T) {
|
||||
var hits int32
|
||||
srv := sseServer(t, 1, &hits)
|
||||
defer srv.Close()
|
||||
|
||||
p := newStreamProxy()
|
||||
if err := p.watchRemote(openerFor(srv)); err != nil {
|
||||
t.Fatalf("watchRemote: %v", err)
|
||||
}
|
||||
defer p.stop()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, p.urlFor("cam2", "live.mjpeg"), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("GET relay: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Two frames means two pushes, because each push carries exactly one.
|
||||
seen, buf := 0, make([]byte, 256)
|
||||
acc := make([]byte, 0, 2048)
|
||||
for seen < 2 {
|
||||
n, rerr := resp.Body.Read(buf)
|
||||
acc = append(acc, buf[:n]...)
|
||||
seen = bytes.Count(acc, []byte("--"+mjpegBoundary))
|
||||
if rerr != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if seen < 2 {
|
||||
t.Fatalf("got %d frames across reconnects, want 2", seen)
|
||||
}
|
||||
if got := atomic.LoadInt32(&hits); got < 2 {
|
||||
t.Errorf("head office was asked %d times, want at least 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Signed out, the relay must not pretend. There is no fallback URL to offer
|
||||
// either: the head-office endpoint needs this session's bearer, which an <img>
|
||||
// cannot send - so a tile that silently failed would be the only alternative.
|
||||
func TestTheRelayRefusesWhenNobodyIsSignedIn(t *testing.T) {
|
||||
p := newStreamProxy()
|
||||
if err := p.watchRemote(nil); err != nil {
|
||||
t.Fatalf("watchRemote: %v", err)
|
||||
}
|
||||
defer p.stop()
|
||||
|
||||
resp, err := http.Get(p.urlFor("cam2", "live.mjpeg"))
|
||||
if err != nil {
|
||||
t.Fatalf("GET relay: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
if resp.StatusCode != http.StatusBadGateway {
|
||||
t.Errorf("status = %d, want 502", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// The relay is credentialed - it is a path to a live view of a shop floor -
|
||||
// and the token is the only thing standing between another local process and
|
||||
// it. live.mjpeg must be behind exactly the same door as the engine routes.
|
||||
func TestTheRemoteRouteIsBehindTheSameToken(t *testing.T) {
|
||||
var hits int32
|
||||
srv := sseServer(t, 1, &hits)
|
||||
defer srv.Close()
|
||||
|
||||
p := newStreamProxy()
|
||||
if err := p.watchRemote(openerFor(srv)); err != nil {
|
||||
t.Fatalf("watchRemote: %v", err)
|
||||
}
|
||||
defer p.stop()
|
||||
|
||||
// The right shape, the wrong value.
|
||||
parts := strings.Split(p.urlFor("cam2", "live.mjpeg"), "/")
|
||||
parts[4] = strings.Repeat("0", len(parts[4]))
|
||||
bad := strings.Join(parts, "/")
|
||||
|
||||
resp, err := http.Get(bad)
|
||||
if err != nil {
|
||||
t.Fatalf("GET relay: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404 - and 404 rather than 403, because there is nothing here to tell an unwelcome caller they found the right door", resp.StatusCode)
|
||||
}
|
||||
if atomic.LoadInt32(&hits) != 0 {
|
||||
t.Error("a request with the wrong token still made the shop computer upload")
|
||||
}
|
||||
}
|
||||
BIN
desktop/tray-logo.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
@@ -55,9 +55,7 @@ func (t *tray) start(ctx context.Context) {
|
||||
if trayDisabled() {
|
||||
return
|
||||
}
|
||||
t.once.Do(func() {
|
||||
go systray.Run(func() { t.onReady(ctx) }, func() {})
|
||||
})
|
||||
t.once.Do(func() { startSystray(func() { t.onReady(ctx) }) })
|
||||
}
|
||||
|
||||
func (t *tray) stop() {
|
||||
@@ -99,11 +97,15 @@ func (t *tray) onReady(ctx context.Context) {
|
||||
case <-t.quit:
|
||||
return
|
||||
case <-t.mOpen.ClickedCh:
|
||||
runtime.Show(ctx)
|
||||
// In a goroutine, like Start and Stop: this sleeps, and a menu
|
||||
// loop that sleeps is a tray that ignores the next click.
|
||||
go openWindow(ctx)
|
||||
case <-t.mStart.ClickedCh:
|
||||
t.app.StartEngine()
|
||||
// Never on the menu loop itself: a stop waits for the process to
|
||||
// exit, and a menu that is deaf for the duration looks broken.
|
||||
go func() { t.app.StartEngine(); t.refresh() }()
|
||||
case <-t.mStop.ClickedCh:
|
||||
t.app.StopEngine()
|
||||
go func() { t.app.StopEngine(); t.refresh() }()
|
||||
case <-t.mLogs.ClickedCh:
|
||||
runtime.BrowserOpenURL(ctx, "file://"+logsDir())
|
||||
case <-t.mQuit.ClickedCh:
|
||||
@@ -129,23 +131,29 @@ func (t *tray) poll(ctx context.Context) {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
s := t.app.EngineStatus()
|
||||
state, label := describe(s)
|
||||
systray.SetIcon(iconFor(state))
|
||||
systray.SetTooltip("Behavision — " + label)
|
||||
if t.mStatus != nil {
|
||||
t.mStatus.SetTitle(label)
|
||||
}
|
||||
running := s.State == "running"
|
||||
if t.mStart != nil && t.mStop != nil {
|
||||
if running {
|
||||
t.mStart.Disable()
|
||||
t.mStop.Enable()
|
||||
} else {
|
||||
t.mStart.Enable()
|
||||
t.mStop.Disable()
|
||||
}
|
||||
}
|
||||
t.refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// refresh redraws the icon and the menu from EngineStatus - the same source
|
||||
// the window reads, so the two cannot disagree.
|
||||
func (t *tray) refresh() {
|
||||
s := t.app.EngineStatus()
|
||||
state, label := describe(s)
|
||||
systray.SetIcon(iconFor(state))
|
||||
systray.SetTooltip("Behavision — " + label)
|
||||
if t.mStatus != nil {
|
||||
t.mStatus.SetTitle(label)
|
||||
}
|
||||
running := s.State == "running" || s.State == "starting" || s.State == "backoff"
|
||||
if t.mStart != nil && t.mStop != nil {
|
||||
if running {
|
||||
t.mStart.Disable()
|
||||
t.mStop.Enable()
|
||||
} else {
|
||||
t.mStart.Enable()
|
||||
t.mStop.Disable()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,9 +168,13 @@ func describe(s EngineStatus) (state, label string) {
|
||||
case s.State == "stopped":
|
||||
return "stopped", "Stopped"
|
||||
case s.State == "failed":
|
||||
return "error", "Failed — " + firstLine(s.Error)
|
||||
// The supervisor's error is already a sentence (port in use, missing
|
||||
// library); show it whole, because it is the thing to act on.
|
||||
return "error", "Not running — " + firstLine(s.Error)
|
||||
case s.State == "backoff":
|
||||
return "error", fmt.Sprintf("Restarting (%d attempts)", s.Restarts)
|
||||
case !s.Reachable && s.Progress != nil:
|
||||
return "warn", fmt.Sprintf("Downloading %s… %d%%", s.Progress.What, s.Progress.Percent)
|
||||
case !s.Reachable:
|
||||
return "warn", "Starting…"
|
||||
case len(s.Cameras) == 0:
|
||||
@@ -202,3 +214,36 @@ func firstLine(s string) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// openWindow brings the dashboard back, and it takes four calls rather than
|
||||
// the one that was here.
|
||||
//
|
||||
// `runtime.Show` was wrong three times over, and the first is the one that
|
||||
// made it fail rather than merely misbehave:
|
||||
//
|
||||
// 1. WRONG THREAD. Wails implements Show() as a bare `mainWindow.Show()`,
|
||||
// while WindowShow() wraps the same work in runtime.LockOSThread. Win32
|
||||
// window operations have to run on the thread owning the window's message
|
||||
// pump; this is called from the SYSTRAY's goroutine, which is never that
|
||||
// thread. An unlocked call from an arbitrary goroutine is why clicking
|
||||
// "Open dashboard" did nothing reliable.
|
||||
//
|
||||
// 2. Showing is not un-minimising. A hidden window and a minimised one are
|
||||
// different states and Show only fixes the first, so a window the user
|
||||
// minimised stayed minimised.
|
||||
//
|
||||
// 3. Windows will not let a process that is not already in the foreground
|
||||
// take it - the shell refuses, and the window comes back BEHIND whatever
|
||||
// is being looked at. Clicking a tray icon is by definition a moment when
|
||||
// this application is not in the foreground, so that is not an edge case
|
||||
// here, it is every time.
|
||||
//
|
||||
// The always-on-top flip is the ordinary way to ask for the foreground anyway.
|
||||
// It is brief and it is why this cannot run on the menu loop.
|
||||
func openWindow(ctx context.Context) {
|
||||
runtime.WindowUnminimise(ctx)
|
||||
runtime.WindowShow(ctx)
|
||||
runtime.WindowSetAlwaysOnTop(ctx, true)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
runtime.WindowSetAlwaysOnTop(ctx, false)
|
||||
}
|
||||
|
||||
28
desktop/tray_run_darwin.go
Normal file
@@ -0,0 +1,28 @@
|
||||
//go:build darwin
|
||||
|
||||
package main
|
||||
|
||||
// There is no tray on macOS, and that is a decision rather than an omission.
|
||||
//
|
||||
// macOS has exactly ONE main run loop and AppKit insists that windows and
|
||||
// status items are created on it. Wails already owns that loop. Two attempts,
|
||||
// both crashing within a second of launch:
|
||||
//
|
||||
// systray.Run -> SIGTRAP inside cgo: nativeLoop() takes the
|
||||
// main loop for itself, and Wails has it
|
||||
// systray.RunWithExternalLoop -> "NSWindow should only be instantiated on
|
||||
// the main thread!" - it registers in the
|
||||
// existing NSApplication but still builds
|
||||
// AppKit objects, and Wails' OnStartup is not
|
||||
// the main thread
|
||||
//
|
||||
// Making it work needs the status item created through a main-queue dispatch
|
||||
// inside Wails' own lifecycle, which is real work for a build whose entire
|
||||
// purpose is demoing on a developer's Mac. Windows is the platform this ships
|
||||
// to and its tray is the shop manager's only control surface; here the window
|
||||
// is right there in the Dock.
|
||||
//
|
||||
// The consequence is handled rather than left: with no tray there would be no
|
||||
// way back from a hidden window and no way to quit, so on macOS closing the
|
||||
// window stops the engine and exits. See main.go.
|
||||
func startSystray(onReady func()) {}
|
||||
10
desktop/tray_run_windows.go
Normal file
@@ -0,0 +1,10 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import "fyne.io/systray"
|
||||
|
||||
// systray.Run owns a message loop, and on Windows it is free to have its own:
|
||||
// the tray lives in its own thread with its own pump, beside the one Wails
|
||||
// runs for the window. A goroutine is all it needs.
|
||||
func startSystray(onReady func()) { go systray.Run(onReady, func() {}) }
|
||||
166
desktop/viewing_test.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/loyaly/behavision-desktop/internal/cloud"
|
||||
"github.com/loyaly/behavision-desktop/internal/local"
|
||||
)
|
||||
|
||||
// Viewer mode: what the app shows on a computer that is signed in and is not
|
||||
// itself watching any cameras.
|
||||
//
|
||||
// This is the friend's-Mac case, and before it existed the app was honest and
|
||||
// useless: Live() and Cameras() read ONLY the engine on 127.0.0.1, so a laptop
|
||||
// with no engine got "engine not reachable at http://127.0.0.1:8010" and
|
||||
// "0 of 0 cameras" - on an account whose shops were running and recognising
|
||||
// people the whole time. Signing in is what the person did; the app answered
|
||||
// as if they had not.
|
||||
//
|
||||
// The engine here is a port nothing listens on, which is precisely what a PC
|
||||
// with no engine is.
|
||||
const noEngine = "http://127.0.0.1:1" // reserved, refuses immediately
|
||||
|
||||
func viewerApp(t *testing.T, srv *httptest.Server) *App {
|
||||
t.Helper()
|
||||
c := cloud.New(srv.URL)
|
||||
c.SetSession(cloud.Session{Token: "test-token"})
|
||||
return &App{
|
||||
ctx: context.Background(),
|
||||
cloud: c,
|
||||
local: local.New(noEngine, "", ""),
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveFallsBackToHeadOfficeWhenThereIsNoEngine(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case r.URL.Path == "/api/sites":
|
||||
// Two shops. One is fine, one is the Office1 case.
|
||||
w.Write([]byte(`[
|
||||
{"slug":"a","name":"A","cameras_up":2,"cameras_total":2,"fraction_below_gate":0.10},
|
||||
{"slug":"b","name":"B","cameras_up":1,"cameras_total":3,"fraction_below_gate":0.73}
|
||||
]`))
|
||||
case strings.HasPrefix(r.URL.Path, "/api/visits"):
|
||||
w.Write([]byte(`{"arrivals":[
|
||||
{"visit_id":"v1","visitor_id":"p1","ref":"V-1","label":"Visitor 1","camera_id":"cam2","is_new_visitor":true},
|
||||
{"visit_id":"v2","visitor_id":"p1","ref":"V-1","label":"Visitor 1","camera_id":"cam2"},
|
||||
{"visit_id":"v3","camera_id":"entrance"}
|
||||
]}`))
|
||||
default:
|
||||
t.Errorf("unexpected request %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
snap, err := viewerApp(t, srv).Live()
|
||||
if err != nil {
|
||||
t.Fatalf("Live: %v", err)
|
||||
}
|
||||
if !snap.Viewing {
|
||||
t.Fatal("the snapshot did not say it was a view of somewhere else")
|
||||
}
|
||||
if got := snap.Stats["cameras_up"]; got != 3 {
|
||||
t.Errorf("cameras_up = %v, want 3 summed across both shops", got)
|
||||
}
|
||||
if got := snap.Stats["cameras_total"]; got != 5 {
|
||||
t.Errorf("cameras_total = %v, want 5", got)
|
||||
}
|
||||
// The WORST site, never an average. Averaging 0.10 against 0.73 reports
|
||||
// 0.42 and hides the only shop anyone needs to go and fix - the same rule
|
||||
// the heartbeat already follows with worst_site.
|
||||
if got := snap.Stats["fraction_below_gate"]; got != 0.73 {
|
||||
t.Errorf("fraction_below_gate = %v, want the worst shop's 0.73", got)
|
||||
}
|
||||
// Three arrivals, two of them the same person, one unidentified. A visit
|
||||
// with no visitor_id is real footfall and an unknown person, so it counts
|
||||
// as a sighting and not as somebody known.
|
||||
g := snap.Stats["gallery"].(map[string]any)
|
||||
if g["identities"] != 1 || g["sightings"] != 3 {
|
||||
t.Errorf("gallery = %v, want 1 identity over 3 sightings", g)
|
||||
}
|
||||
if len(snap.Events) != 3 {
|
||||
t.Fatalf("got %d events, want 3", len(snap.Events))
|
||||
}
|
||||
if snap.Events[0]["type"] != "person.new" || snap.Events[1]["type"] != "person.seen" {
|
||||
t.Errorf("arrival types wrong: %v", snap.Events)
|
||||
}
|
||||
}
|
||||
|
||||
// Nobody signed in: the local failure is the honest answer. There is nothing
|
||||
// else to show, and the person is most likely setting this PC up - telling
|
||||
// them about head office would be telling them about something they have not
|
||||
// got to yet.
|
||||
func TestLiveWithNoEngineAndNoSessionReportsTheEngine(t *testing.T) {
|
||||
a := &App{ctx: context.Background(), cloud: cloud.New("https://example.invalid"),
|
||||
local: local.New(noEngine, "", "")}
|
||||
if _, err := a.Live(); err == nil {
|
||||
t.Fatal("want the engine error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// A remote camera is flagged, because the screen has to withhold every button
|
||||
// that would talk to a camera on a network this computer cannot reach. An Edit
|
||||
// button that cannot work is worse than one that is absent.
|
||||
func TestRemoteCamerasAreFlaggedAndCarryNoCredentials(t *testing.T) {
|
||||
jpeg := base64.StdEncoding.EncodeToString([]byte{0xFF, 0xD8, 0xFF, 0xD9})
|
||||
var shots int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/camera-snapshots/") {
|
||||
shots++
|
||||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
b, _ := base64.StdEncoding.DecodeString(jpeg)
|
||||
w.Write(b)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`[
|
||||
{"id":"c1","camera_id":"cam2","label":"Open office","site":"Coimbatore",
|
||||
"connected":true,"snapshot_at":"2026-09-30T10:00:00Z",
|
||||
"snapshot":{"available":true,"url":"/api/camera-snapshots/c1.jpg","auth":true}}
|
||||
]`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
a := viewerApp(t, srv)
|
||||
cams, err := a.Cameras()
|
||||
if err != nil {
|
||||
t.Fatalf("Cameras: %v", err)
|
||||
}
|
||||
if len(cams) != 1 {
|
||||
t.Fatalf("got %d cameras, want 1", len(cams))
|
||||
}
|
||||
if cams[0]["remote"] != true {
|
||||
t.Error("the camera was not flagged remote")
|
||||
}
|
||||
// The RTSP details are a live path into the camera itself and the server
|
||||
// does not send them to a tenant at all. Nothing here may invent them.
|
||||
for _, k := range []string{"host", "port", "path", "username", "password"} {
|
||||
if _, ok := cams[0][k]; ok {
|
||||
t.Errorf("a remote camera carried %q", k)
|
||||
}
|
||||
}
|
||||
|
||||
// The picture has to be fetched here: a webview <img> resolves a relative
|
||||
// src against wails:// and cannot send the session's bearer.
|
||||
shot := cams[0]["snapshot"].(cloud.Photo)
|
||||
if !strings.HasPrefix(shot.URL, "data:image/jpeg;base64,") || shot.Auth {
|
||||
t.Errorf("snapshot url = %q auth=%v, want an inline data URI", shot.URL, shot.Auth)
|
||||
}
|
||||
|
||||
// And fetched ONCE. This screen polls every 8 seconds and a real snapshot
|
||||
// is ~90 KB, so re-fetching an unchanged picture is megabytes an hour to
|
||||
// redraw the same frame.
|
||||
if _, err := a.Cameras(); err != nil {
|
||||
t.Fatalf("second poll: %v", err)
|
||||
}
|
||||
if shots != 1 {
|
||||
t.Errorf("fetched the same snapshot %d times across two polls", shots)
|
||||
}
|
||||
}
|
||||
521
docs/TECHNICAL-DOSSIER.md
Normal file
@@ -0,0 +1,521 @@
|
||||
# Behavision — Technical Dossier
|
||||
|
||||
Everything below is extracted from the repository as of release 0.4.1 (engine 1.1.0, schema at migration 013). File paths, function names, thresholds, topics, ports and table definitions are the real ones. Where something lives outside the repository (the production host's proxy and container configuration) it is stated as such rather than invented.
|
||||
|
||||
> **Snapshot, not a live document.** Written against release 0.4.1 / schema
|
||||
> 013, and the repository is past that. It predates at least: the engine's
|
||||
> motion gate and one-thread detector (CPU 214% → 16%), `Gallery.health` and
|
||||
> the stalled-camera state, the `tenantOnly` guard, `POST /api/auth/password`,
|
||||
> `POST /api/customers` and the visitor merge, the admin console drill-down,
|
||||
> `GET /api/sales` and `/api/dashboard/summary`, migration 014, and the macOS
|
||||
> desktop build. Everything it *does* describe was extracted from the code and
|
||||
> was true then; nothing here was invented. For the current surface read
|
||||
> `API.md`, which is kept up to date, and `CLAUDE.md` for the decisions.
|
||||
|
||||
Companion documents: `API.md` (every route with request/response shapes), `docs/openapi.yaml` (generated), `docs/Behavision-Architecture.html` (diagrams).
|
||||
|
||||
---
|
||||
|
||||
## 1. System architecture
|
||||
|
||||
### 1.1 Repository layout
|
||||
|
||||
```
|
||||
behavision/ Recognition engine (Python 3.10+)
|
||||
__main__.py CLI: run | enroll | setup-models | calibrate | paths
|
||||
api.py FastAPI on 127.0.0.1:8010 — dashboard, cameras, stream, stats
|
||||
capture.py VideoSource: RTSP capture thread, latest-frame slot, probe_source
|
||||
detection.py FaceDetector (YuNet), Detection
|
||||
geometry.py umeyama, align_face, iou, clip_box
|
||||
recognition.py ArcFaceEncoder (ONNX Runtime), face_quality
|
||||
tracking.py Track, IouTracker
|
||||
engine.py Engine, CameraWorker, PipelineStats — the per-camera pipeline
|
||||
attributes.py AttributeEstimator (gender/age/emotion), aggregate
|
||||
commission.py CommissionRun — placement check verdicts
|
||||
gallery/store.py IdentityStore — SQLite (identities, embeddings, sightings)
|
||||
gallery/index.py VectorIndex — FAISS IndexIDMap2(IndexFlatIP) / numpy fallback
|
||||
gallery/service.py Gallery — resolve, enroll, reinforce, merge, duplicates
|
||||
events.py EventBus + LogSink / WebhookSink / EmailSink
|
||||
cameras.py CameraStore (cameras.json), protect/unprotect (DPAPI)
|
||||
config.py pydantic Config, ${ENV} expansion, RTSP URL building
|
||||
paths.py install_root / state_root / config_path resolution
|
||||
static/dashboard.html Engine's own dashboard (no build step)
|
||||
config/default.yaml Engine config (thresholds, cameras via ${ENV})
|
||||
tests/ Engine tests (204), dependency-light: no camera, no models
|
||||
|
||||
agent/ Shop-PC agent (Go 1.22, module github.com/loyaly/behavision-agent)
|
||||
main.go Headless agent binary
|
||||
cmd/behavision-setup/ Installer: venv, wheel, models, config, smoke test, demo bundle
|
||||
cmd/behavision-demo-pack/ Seals a camera list (AES-256-GCM) — build machine only
|
||||
pkg/spool/ Durable queue: one file per event, bounded, ack by delete
|
||||
pkg/mqtt/ paho adapter (client.go) + Pump + Waker (pump.go)
|
||||
pkg/bridge/ Loopback webhook the engine posts to; derives event_id; queues
|
||||
pkg/engine/ Supervisor (start/stop/restart/backoff), Health, ChildEnv
|
||||
pkg/cameras/ Syncer: pull desired cameras, adopt local, run checks
|
||||
pkg/enrol/ Redeem an installation code
|
||||
pkg/config/ agent.json with DPAPI-protected secrets
|
||||
pkg/paths/ StateRoot / InstallRoot — mirrors behavision/paths.py
|
||||
pkg/demo/ Sealed bundle: NewCode, Seal, Open
|
||||
|
||||
desktop/ Shop-PC app (Wails v2.9.2, Go + React)
|
||||
main.go wails.Run, HideWindowOnClose, tray start/stop
|
||||
app.go Methods bound to the frontend; owns Supervisor, Bridge, Pump, Syncer
|
||||
tray.go / icons.go fyne.io/systray; ICO rendered at runtime on Windows
|
||||
stream_proxy.go Loopback relay for camera MJPEG (credential never in the page)
|
||||
internal/local/ Client for the engine on 127.0.0.1:8010
|
||||
internal/cloud/ Client for the platform API (sessions, refresh, images)
|
||||
frontend/ React + Vite; src/bridge.js calls window.go.main.App.*
|
||||
|
||||
server/ Platform (Go, module github.com/loyaly/behavision-server)
|
||||
cmd/behavision-server/main.go One binary: migrate → store → hub → ingest → API → web
|
||||
Dockerfile Two-stage; static binary on alpine; EXPOSE 8080
|
||||
migrations/001..013_*.sql go:embed'ed; applied at boot under an advisory lock
|
||||
internal/api/ HTTP handlers, middleware, Hub (SSE doorbell), LiveHub (relay)
|
||||
internal/store/ PostgreSQL access (pgx) — every query tenant-scoped
|
||||
internal/ingest/ MQTT consumer: topic → site → visit/heartbeat → store
|
||||
internal/auth/ Passwords (bcrypt 12), tokens, codes, Principal + role checks
|
||||
internal/secret/ secret.Box — AES-256-GCM with AAD
|
||||
internal/blob/ S3-compatible object storage (presign, private ACL check)
|
||||
internal/assistant/ Claude tool loop; tools.go has no LLM import
|
||||
internal/contract/ The MQTT wire contract: Visit, Heartbeat, ParseTopic
|
||||
internal/migrate/ Migration runner (checksums, numeric order, baseline)
|
||||
internal/provision/ CLI: provision key|client|site|user|token
|
||||
internal/web/ go:embed of the built React console (web/dist → here)
|
||||
|
||||
web/ Head-office console (React + Vite); outDir → server/internal/web/dist
|
||||
shared/cameraMakes.js Camera make → RTSP path table, imported by web AND desktop
|
||||
installer/ build.ps1 (PyInstaller path), behavision.iss, INSTALL.txt, LAN launcher
|
||||
run-local.sh Whole platform locally: Postgres + Mosquitto in Docker, server as binary
|
||||
```
|
||||
|
||||
### 1.2 Processes and where they run
|
||||
|
||||
| Process | Language | Runs on | Listens | Talks to |
|
||||
|---|---|---|---|---|
|
||||
| Recognition engine | Python | shop PC | `127.0.0.1:8010` (Basic auth, generated) | cameras (RTSP), agent webhook (loopback) |
|
||||
| Agent (inside the desktop app, or headless) | Go | shop PC | loopback webhook, port 0 | engine API, Mosquitto (TLS 8883), platform API (HTTPS) |
|
||||
| Shop app | Go + webview | shop PC | loopback relay, port 0 | engine API, platform API |
|
||||
| Mosquitto | C | cloud | `8883` TLS (agents), `1883` internal (server) | — |
|
||||
| behavision-server | Go | cloud | `8080` (behind proxy) | PostgreSQL, Mosquitto (subscriber), object storage (optional), Anthropic API (optional) |
|
||||
| PostgreSQL + pgvector | C | cloud | `5432` internal | — |
|
||||
| Head-office console | React | browser | — | platform API |
|
||||
| Mobile app | — | phone | — | platform API |
|
||||
|
||||
### 1.3 Network, domains, TLS
|
||||
|
||||
| Endpoint | Purpose | TLS |
|
||||
|---|---|---|
|
||||
| `https://platform.loyaly.ai` | Head-office console + API (`/api/*`) | Terminated at the reverse proxy (Traefik); the server listens plain HTTP on `LISTEN_ADDR` (default `:8080`) |
|
||||
| `https://mcp.loyaly.ai/api/*` | Same API, the hostname the shop app defaults to (`BEHAVISION_CLOUD`) | Proxy |
|
||||
| `tls://mcp.loyaly.ai:8883` | MQTT for agents (`AGENT_MQTT_URL` default) | Mosquitto's own listener; certificate must carry `DNS:mcp.loyaly.ai`; agents pin the issuing CA (`AGENT_CA_FILE` delivered at enrolment) |
|
||||
| `tcp://behavision-mqtt:1883` | Server ↔ Mosquitto, internal network only (`MQTT_URL` default) | Plaintext on a private network |
|
||||
|
||||
Trust rules enforced in code:
|
||||
- `X-Forwarded-For` is trusted for the login throttle **only because** nothing reaches the server port except through the proxy (`api/throttle.go`).
|
||||
- The agent refuses `tcp://` to any non-loopback host unless `BEHAVISION_ALLOW_PLAINTEXT_MQTT=1` (`agent/pkg/mqtt/client.go`).
|
||||
- No inbound route to a shop PC is ever required: agent → broker, agent → API, app → API are all outbound.
|
||||
|
||||
### 1.4 Server configuration (environment)
|
||||
|
||||
| Variable | Default | Purpose |
|
||||
|---|---|---|
|
||||
| `DATABASE_URL` | required | PostgreSQL DSN |
|
||||
| `LISTEN_ADDR` | `:8080` | HTTP listener (behind proxy) |
|
||||
| `MQTT_URL` / `MQTT_USERNAME` / `MQTT_PASSWORD` | `tcp://behavision-mqtt:1883` | Server's subscriber credential |
|
||||
| `AGENT_MQTT_URL` | `tls://mcp.loyaly.ai:8883` | Broker URL handed to a PC at enrolment |
|
||||
| `AGENT_CA_FILE` | — | CA PEM handed to a PC at enrolment (pinned) |
|
||||
| `AGENT_MODELS_FILE` | — | Model manifest handed at enrolment |
|
||||
| `BEHAVISION_SECRET_KEY` | — | 32-byte key for `secret.Box`; enrolment and camera passwords need it |
|
||||
| `DO_SPACES_*` (`ENDPOINT`, `REGION`, `BUCKET`, `ACCESS_KEY`, `SECRET_KEY`, `PREFIX`) | prefix `behavision/v2` | Optional object storage; absent = images stored in Postgres |
|
||||
| `ANTHROPIC_API_KEY` / `ANTHROPIC_WORKSPACE_ID` / `BEHAVISION_ASSISTANT_MODEL` | model `claude-sonnet-5` | Assistant; absent = `501 assistant_off` |
|
||||
| `BEHAVISION_SKIP_MIGRATE` | — | Escape hatch; default applies migrations at boot |
|
||||
|
||||
### 1.5 Container / deployment shape
|
||||
|
||||
The repository ships `server/Dockerfile` (golang:1.25-alpine build → alpine:3.20 runtime, static binary, `EXPOSE 8080`, non-root user) and `run-local.sh`, which stands the whole platform up locally: `bv-pg` (pgvector/pgvector:pg16, port 55432), `bv-mqtt` (eclipse-mosquitto:2, port 51883, `passwd` + `acl` mounted), and the server as a local binary on 8088 with the console embedded.
|
||||
|
||||
Production host configuration (proxy routes, compose/unit files, certificate issuance) is **outside the repository**. What the code requires of it: a proxy terminating TLS for `platform.loyaly.ai` and forwarding to `LISTEN_ADDR`; Mosquitto with a TLS listener on 8883 whose certificate names `mcp.loyaly.ai`, a `passwd` file the `provision site` command adds to, and an ACL of the form `pattern write bv/%u/#`; PostgreSQL with the `vector` extension.
|
||||
|
||||
---
|
||||
|
||||
## 2. Recognition engine internals
|
||||
|
||||
### 2.1 Pipeline, function by function
|
||||
|
||||
```
|
||||
RTSP ──▶ capture.VideoSource.run() thread per camera; cv2.VideoCapture(CAP_FFMPEG)
|
||||
│ OPENCV_FFMPEG_CAPTURE_OPTIONS = rtsp_transport;tcp | stimeout;5000000 |
|
||||
│ fflags;nobuffer | flags;low_delay | max_delay;200000
|
||||
│ downscale to max_width (1280) with INTER_AREA
|
||||
│ latest frame + timestamp in a lock-protected slot
|
||||
▼
|
||||
engine.CameraWorker.run() thread per camera; takes source.latest_since(ts)
|
||||
│
|
||||
├─ detection.FaceDetector.detect(frame) cv2.FaceDetectorYN (YuNet 2023mar)
|
||||
│ score_threshold 0.82 · nms 0.3 · min_face_px 48 · max_faces 20
|
||||
│ → Detection(box, kps[5], score)
|
||||
│
|
||||
├─ recognition.face_quality(frame, box, kps) weighted: sharpness .35 · size .25 · brightness .15 · frontality .25
|
||||
│
|
||||
├─ tracking.IouTracker.update(dets, ts) greedy IoU association, iou_threshold 0.3, max_misses 25
|
||||
│ → active Track[], ended Track[] one Track == one person on camera
|
||||
│
|
||||
├─ for each active track, _should_identify(): hits ≥ 4 · quality ≥ min_quality_to_encode (0.35)
|
||||
│ ≤ max_id_attempts (8) · spaced id_retry_interval_seconds (0.5)
|
||||
│
|
||||
├─ _identify(track, frame):
|
||||
│ geometry.align_face(frame, kps) Umeyama similarity transform → 112×112 BGR chip
|
||||
│ recognition.ArcFaceEncoder.encode() BGR→RGB, (x−127.5)/127.5, NCHW float32, L2-normalised 512-d
|
||||
│ track.emb_sum += e; when emb_count ≥ min_embeddings_for_id (3): mean → normalise
|
||||
│ gallery.Gallery.resolve(mean, quality, rcfg) → Resolution(kind, identity, similarity)
|
||||
│
|
||||
├─ Resolution.kind:
|
||||
│ known sim ≥ match_threshold (0.42) → person.seen (+ reinforce if 0.32 ≤ sim < 0.55, q ≥ gate, < 5 stored)
|
||||
│ ambiguous 0.32 ≤ sim < 0.42 → wait; retry on a later frame
|
||||
│ new sim < enroll_threshold (0.32) → Gallery.enroll → "Visitor N" · person.new
|
||||
│ skipped quality < min_enroll_quality (0.65) → counted as rejected_quality
|
||||
│
|
||||
├─ attributes.AttributeEstimator.estimate() genderage.onnx on a loose 1.5× crop; FER+ on the chip;
|
||||
│ medianed over the track (attributes.aggregate)
|
||||
│
|
||||
├─ _finish_track(ended) PipelineStats.record(outcome) — exactly once per track
|
||||
│
|
||||
└─ _remember_tracks(active) boxes + labels for the live picture (no encode)
|
||||
|
||||
Live picture: CameraWorker.latest_jpeg_since(ts) — freshest CAPTURED frame + last boxes, encoded on demand.
|
||||
Events: events.EventBus → LogSink · WebhookSink (→ agent bridge) · EmailSink
|
||||
```
|
||||
|
||||
### 2.2 Models (`recognition.MODEL_CANDIDATES`, first loadable wins)
|
||||
|
||||
| Order | File | Role |
|
||||
|---|---|---|
|
||||
| 1–2 | `adaface_ir101.onnx`, `adaface_ir50.onnx` | wired, optional |
|
||||
| **3** | **`w600k_r50.onnx`** (166 MB) | **in use** — IJB-C 97.25; same-person p05 0.719 on the office camera |
|
||||
| 4 | `arcface_int8.onnx` | optional |
|
||||
| 5 | `w600k_mbf.onnx` (13 MB) | always loads; MobileFaceNet fallback (95.02) |
|
||||
| 6 | `arcface.onnx` (r100, 249 MB) | optional |
|
||||
| — | `face_detection_yunet_2023mar.onnx` | detector |
|
||||
| — | `genderage.onnx` (InsightFace buffalo_l) | attributes |
|
||||
|
||||
Every stored embedding is tagged with the model name; `IdentityStore.all_embeddings(model)` loads only same-model vectors into the index.
|
||||
|
||||
### 2.3 Local gallery
|
||||
|
||||
`gallery/store.py` — SQLite (WAL), single source of truth:
|
||||
```
|
||||
identities(id, label, kind auto|named, created_at, sighting_count)
|
||||
embeddings(id, identity_id, model, vector BLOB, quality, created_at)
|
||||
sightings(id, identity_id, camera_id, similarity, at)
|
||||
```
|
||||
`gallery/index.py` — `VectorIndex` over FAISS `IndexIDMap2(IndexFlatIP)` (exact inner product = cosine on L2-normalised vectors), rebuilt from SQLite at boot, −1 ids filtered, identical numpy fallback. Measured: 1k → 0.27 ms, 10k → 2.24 ms, 100k → 21.9 ms.
|
||||
|
||||
`gallery/service.py` — `Gallery.resolve` (three zones), `enroll`, `reinforce_identity` (refuses a view whose nearest neighbour is another identity), `merge_identities` (one transaction; human name outranks "Visitor N"; `sighting_count` recomputed; trimmed to 5 by quality), `duplicate_candidates` (k-NN across identities, O(n·k)).
|
||||
|
||||
### 2.4 What leaves the engine
|
||||
|
||||
`WebhookSink` POSTs each `person.seen` / `person.new` to the agent's loopback bridge with `identity_id`, `label`, `similarity`, `quality`, attributes, and optionally `image_path` (only when `app.store_faces: true`). The bridge fetches the identity's **best** stored embedding once per identity via `GET /api/identities/{id}/embedding`. `person.missed`, `camera.up/down` are diagnostics and never become visits.
|
||||
|
||||
---
|
||||
|
||||
## 3. Backend internals
|
||||
|
||||
### 3.1 Request path
|
||||
|
||||
```
|
||||
proxy (TLS) ─▶ net/http mux (Go 1.22 patterns, method + path)
|
||||
│
|
||||
├─ s.authed(h) Bearer token → SHA-256 → sessions row → Principal{UserID, ClientID, Role}
|
||||
│ token_expired vs unauthorized distinguished; last_used_at touched
|
||||
├─ s.adminOnly(h) Principal.Role == "admin" AND ClientID == "" (both) → else 404
|
||||
├─ s.agentAuthed(h) Agent token (hashed) → AgentPrincipal{ClientID, Client slug, SiteID, AgentID}
|
||||
└─ no wrapper login, refresh, invitation preview, register, enrol
|
||||
│
|
||||
▼
|
||||
handlers_*.go decode (unknown fields rejected) → validate → Store call → writeJSON
|
||||
│ every Store call receives p.ClientID from the session, never the body
|
||||
▼
|
||||
store/*.go (pgx) SQL with client_id in every WHERE / INSERT
|
||||
```
|
||||
|
||||
Login throttle (`api/throttle.go`): per-account 10 failures / 15 min and per-IP 60, in memory, pruned on read; success clears both. Unknown address is verified against `auth.DummyHash` so timing matches a wrong password.
|
||||
|
||||
### 3.2 Handler areas → store methods
|
||||
|
||||
| Area (file) | Routes | Store surface |
|
||||
|---|---|---|
|
||||
| `handlers_auth.go`, `handlers_sessions.go` | login, refresh, logout, me, sessions list/revoke | `UserByEmail`, `CreateSession`, `SessionByAccessHash`, `RotateSession`, `RevokeSession(s)` |
|
||||
| `handlers_team.go` | team, members, password reset, invitations, register | `Team`, `UpdateTeamMember`, `CreateMember`, `ResetMemberPassword`, `CreateInvitation`, `RedeemInvitation`, `OwnerCount` |
|
||||
| `handlers_admin.go` | admin/clients | `ListClients`, `CreateClientWithOwner` (one transaction) |
|
||||
| `handlers_arrivals.go`, `hub.go` | visits, visits/stream | `Arrivals` (keyset by `seq`), `Hub.Notify` doorbell → SSE |
|
||||
| `handlers_people.go` | visitors, history, profile, purchases, erasure | `SearchVisitors`, `VisitorHistory`, `SaveProfile`, `RecordPurchase`, `ForgetVisitor` |
|
||||
| `handlers_images.go`, `handlers_faces.go` | visitor image, face bytes | `VisitorImageKey`, `FaceImage`; `imageFor(key)` decides presigned vs `auth:true` |
|
||||
| `handlers_cameras.go`, `handlers_snapshots.go` | cameras CRUD, snapshot | `Cameras`, `CreateCamera`, `UpdateCamera`, `DeleteCamera` (tombstone), `Snapshot` |
|
||||
| `handlers_checks.go` | camera check, site check | `RequestCheck`, `ClaimChecks`, `ReleaseStaleChecks`, `RecordCheck`, `SiteCheck` |
|
||||
| `handlers_live.go`, `live.go` | cameras/{id}/live, agent live | `LiveHub` — one-slot buffer per viewer, on-demand upload |
|
||||
| `handlers_reports.go` | footfall, conversion | `Footfall`, `Conversion` — unique vs visits, first-ever "new", single currency |
|
||||
| `handlers_enrolment.go`, `handlers_agent.go` | enrol, agent cameras/checks/faces/upload-url | `RedeemEnrolment` (single-use via UPDATE), `AgentCameras`, `AgentReport`, `PutFace`, `UploadTarget` |
|
||||
| `handlers_assistant.go` | assistant | `assistant.Client.Ask` with the Principal passed at the call site |
|
||||
|
||||
### 3.3 Server-side recognition (`store/store.go`, `RecordVisit`)
|
||||
|
||||
```
|
||||
similarity = 1 - (embedding <=> $1::vector) -- pgvector cosine distance
|
||||
ORDER BY embedding <=> $1::vector LIMIT 1 -- within client_id, same model
|
||||
sim ≥ 0.42 → known visitor; reinforce if 0.32 ≤ sim < 0.55 AND quality ≥ floor AND < 5 stored
|
||||
sim < 0.42 → new visitor: clients.visitor_seq += 1 RETURNING (row-locks the client), label "Visitor N"
|
||||
INSERT visits ... ON CONFLICT (client_id, source_event_id) DO NOTHING -- idempotent
|
||||
```
|
||||
|
||||
### 3.4 Single-process composition (`cmd/behavision-server/main.go`)
|
||||
|
||||
```
|
||||
migrate.Apply(embedded FS) → store.Open → hub := api.NewHub()
|
||||
ingest.Consumer{Store, Notify: hub.Notify} ← paho client, SetOrderMatters(true), subscribed bv/+/+
|
||||
api.New(Store, Hub, LiveHub, Blob?, Assistant?) → web.Handler (embedded dist; /api/ keeps JSON 404)
|
||||
http.Server{ReadTimeout, IdleTimeout, WriteTimeout: 0} -- zero: SSE streams must outlive any write deadline
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. MQTT architecture
|
||||
|
||||
### 4.1 Identity and topics
|
||||
|
||||
Broker username = `<client-slug>.<site-slug>` (e.g. `tenext-retail.chennai`). ACL: `pattern write bv/%u/#` — a site physically cannot publish under another site's prefix.
|
||||
|
||||
```
|
||||
bv/<client>.<site>/visit Visit payload QoS 1 spooled, acked per event
|
||||
bv/<client>.<site>/heartbeat Heartbeat payload QoS 1 never spooled — only meaningful now
|
||||
bv/<client>.<site>/status reserved
|
||||
bv/<client>.<site>/cmd/... reserved (server → site)
|
||||
```
|
||||
|
||||
`contract.ParseTopic` → `Topic{Username, Client, Site, Kind, Rest}`; `Kind ∉ {visit, heartbeat, status, cmd}` is dropped as permanent.
|
||||
|
||||
### 4.2 Payloads (`server/internal/contract/contract.go`)
|
||||
|
||||
```json
|
||||
// visit
|
||||
{ "event_id": "tenext-retail.chennai|cam2|7|1757580000", // <site>|<camera>|<identity>|<unix second> — derived, never random
|
||||
"occurred_at": "2026-09-11T05:20:00Z", "camera_id": "cam2",
|
||||
"is_new": false, "similarity": 0.61, "quality": 0.70,
|
||||
"local_visitor_id": 7, "embedding": [512 floats], "model": "w600k_r50",
|
||||
"image_key": "behavision/v2/tenext-retail/chennai/2026/09/11/…jpg", // or "db:<uuid>", or absent
|
||||
"attributes": { "gender": "Male", "age": 32, "emotion": "neutral" } }
|
||||
|
||||
// heartbeat
|
||||
{ "sent_at": "…", "agent_version": "0.4.1", "engine_version": "1.1.0", "recognition_model": "w600k_r50",
|
||||
"cameras": { "cam1": true, "cam2": true }, "queued": 0, "dropped": 0, "fraction_below_gate": 0.47 }
|
||||
```
|
||||
|
||||
`Visit.Validate()` (permanent errors): `event_id` required ≤128; `occurred_at` required and not >24 h in the future; embedding must be exactly 512 and carry `model`.
|
||||
|
||||
### 4.3 Delivery semantics
|
||||
|
||||
| Stage | Component | Guarantee |
|
||||
|---|---|---|
|
||||
| Engine → agent | `WebhookSink` → `bridge.Bridge.Handle` | loopback HTTP; `event_id` derived from `<site>|<camera>|<identity>|<second>` (sighting cooldown is 30 s, so one person/camera cannot share a second) |
|
||||
| Append | `spool.Spool.Append` | one file per event, fsync, bounded (`SpoolMax`, default 50,000); on overflow drops oldest and counts `Dropped()`; corrupt entry quarantined, not retried |
|
||||
| Wake | `mqtt.Waker.Wake` **after** the append | a wake before durability is a drain that finds nothing |
|
||||
| Publish | `mqtt.Pump.Run` → `Client.Publish` | QoS 1, `CleanSession(true)`, publish bounded by a timeout as well as context (half-open TCP otherwise stalls forever); **a failed publish stops the batch** (ordering per visitor) |
|
||||
| Ack | `Spool.Ack(seq)` on PUBACK | per event, never per batch; file deleted only now |
|
||||
| Consume | `ingest.Consumer.Handle` | paho `SetOrderMatters(true)`; permanent error → `drop()` + log (message is acked, never redelivered); transient error → returned → redelivered |
|
||||
| Write | `store.RecordVisit` | `ON CONFLICT (client_id, source_event_id) DO NOTHING` — duplicates from at-least-once delivery are absorbed |
|
||||
| Notify | `hub.Notify(clientID)` | only on a genuine insert; SSE streams re-query from their own cursor |
|
||||
|
||||
Backoff: reconnect 1 → 30 s exponential; supervisor backoff resets only after a run that stayed up 60 s. `describeStall` distinguishes *broker refused the credential* (TCP opens, connect never completes) from *broker unreachable*.
|
||||
|
||||
### 4.4 Failure paths
|
||||
|
||||
| Failure | Behaviour |
|
||||
|---|---|
|
||||
| Internet down | spool grows on disk; heartbeats stop; head office shows site offline after 3 missed beats; on reconnect the backlog drains in order |
|
||||
| Broker rejects credential | pump logs "reachable but not accepted — re-link this PC"; spool retained |
|
||||
| Server down, broker up | broker holds nothing (clean session); agent's PUBACKs still arrive from the broker, so events are acked at the broker — the server's own subscription reconnects and Mosquitto delivers what it queued for the persistent server session |
|
||||
| Duplicate delivery | absorbed by `source_event_id` uniqueness |
|
||||
| Malformed event | dropped with a log line naming the site and reason; never blocks the queue |
|
||||
| Site clock wrong | `occurred_at` > 24 h ahead rejected as permanent; feed ordering uses server `seq`, so a wrong clock cannot hide a visit |
|
||||
|
||||
---
|
||||
|
||||
## 5. Database schema (PostgreSQL + pgvector, migrations 001–013)
|
||||
|
||||
### 5.1 Tables
|
||||
|
||||
```
|
||||
clients id PK · slug UQ (immutable, = MQTT prefix) · name · active · visitor_seq bigint
|
||||
sites id PK · client_id FK · slug (immutable, per client) · name · timezone · address · active
|
||||
agents id PK · client_id FK · site_id FK · mqtt_username UQ · mqtt_password_enc bytea (sealed)
|
||||
api_token_hash bytea · agent_version · engine_version · recognition_model
|
||||
last_heartbeat_at · last_event_at · fraction_below_gate · cameras_total/up · spool_queued/dropped
|
||||
app_users id PK · client_id FK (NULL = platform admin) · email (lower(email) UQ globally, 007)
|
||||
password_hash (bcrypt 12) · full_name · role owner|manager|staff|admin · active · last_login_at
|
||||
sessions id PK · user_id FK · client_id FK · access_hash UQ · refresh_hash UQ (SHA-256)
|
||||
access_expires_at · refresh_expires_at · revoked_at · device · last_used_at
|
||||
invitations id PK · client_id FK · email · full_name · role · code_hash UQ · invited_by FK
|
||||
expires_at · used_at · used_by FK · revoked_at
|
||||
site_enrolment_tokens id PK · client_id FK · site_id FK · token_hash UQ · label · expires_at · used_at · created_by
|
||||
visitors id PK · client_id FK · number bigint (per-client, immutable → "V-42") · label
|
||||
first_seen_at · last_seen_at · visit_count · deleted_at
|
||||
visitor_embeddings id PK · visitor_id FK · client_id FK · model · embedding vector(512) · quality · source_site_id FK
|
||||
visitor_profiles id PK · visitor_id FK · client_id FK · full_name · phone · email · gender · date_of_birth · notes
|
||||
consents id PK · visitor_id FK · client_id FK · scope · method · granted_at · revoked_at · evidence jsonb
|
||||
visits id PK · client_id FK · site_id FK · visitor_id FK (nullable) · source_event_id (UQ per client)
|
||||
occurred_at · received_at · camera_id text · is_new_visitor · similarity · quality
|
||||
attributes jsonb · image_key · image_deleted_at · seq bigserial (feed cursor)
|
||||
purchases id PK · client_id FK · site_id FK · visitor_id FK · visit_id FK · amount numeric(14,2)
|
||||
currency char(3) · items jsonb · source · external_ref · recorded_by · occurred_at
|
||||
site_cameras id PK · client_id FK · site_id FK · camera_id text (immutable, UQ per site) · label
|
||||
host · port · path · username · password_enc bytea (sealed, aad = site_id) · max_width
|
||||
tuning jsonb · enabled · revision · connected (nullable) · last_seen_at · snapshot_key
|
||||
snapshot_at · deleted_at (tombstone) · check_kind · check_* (requested/started/finished/result/image_key)
|
||||
camera_snapshots camera_id PK FK · client_id FK · site_id FK · image bytea · bytes · captured_at
|
||||
visit_faces id PK · client_id FK · site_id FK · image bytea · bytes · captured_at (one survives per visitor)
|
||||
audit_log id bigserial PK · client_id FK (SET NULL) · actor_id · actor_kind · action · entity · entity_id · detail jsonb · at
|
||||
schema_migrations version · checksum · applied_at · baselined
|
||||
```
|
||||
|
||||
### 5.2 Relationships
|
||||
|
||||
```
|
||||
clients ─┬─< sites ─┬─< agents
|
||||
│ ├─< site_cameras ──< camera_snapshots (1:1, PK = camera_id)
|
||||
│ ├─< site_enrolment_tokens
|
||||
│ ├─< visits
|
||||
│ ├─< purchases
|
||||
│ └─< visit_faces
|
||||
├─< app_users ─┬─< sessions
|
||||
│ └─< invitations (invited_by, used_by)
|
||||
├─< visitors ─┬─< visitor_embeddings
|
||||
│ ├─< visitor_profiles
|
||||
│ ├─< consents
|
||||
│ ├─< visits
|
||||
│ └─< purchases
|
||||
└─< audit_log (SET NULL)
|
||||
|
||||
visits ──< purchases (visit_id, SET NULL)
|
||||
```
|
||||
|
||||
Every FK onto `clients` is `ON DELETE CASCADE` except `audit_log` (`SET NULL`). Every tenant-owned table carries `client_id` directly, so no query needs a join to enforce tenancy.
|
||||
|
||||
### 5.3 Invariants enforced in the database
|
||||
|
||||
- `007` — `lower(email)` globally unique; the migration refuses to apply while duplicates exist and names them.
|
||||
- `012` — `visitors.number` per-client sequence from `clients.visitor_seq` (`UPDATE … RETURNING`, row-locked); unique on `(client_id, number)`.
|
||||
- `013` — triggers refuse changes to `clients.slug`, `sites.slug`, `site_cameras.camera_id`, `visitors.number` (`BEFORE UPDATE OF … WHEN OLD IS DISTINCT FROM NEW`). Display names are deliberately not frozen.
|
||||
- `004` — `visits.seq bigserial`; the arrivals cursor is `v1:<seq>` base64, opaque to clients.
|
||||
- `sites_slug_format` — `^[a-z0-9][a-z0-9-]{1,30}[a-z0-9]$`.
|
||||
|
||||
---
|
||||
|
||||
## 6. API specification
|
||||
|
||||
Full request/response shapes: `API.md`. Machine-readable: `docs/openapi.yaml`.
|
||||
|
||||
### 6.1 Authentication and session lifecycle
|
||||
|
||||
```
|
||||
POST /api/auth/login {email,password,device}
|
||||
→ {access_token (12 h), refresh_token (30 d), expires_at, user{id,email,full_name,role,client_id,client_name}}
|
||||
tokens: 256-bit random; only SHA-256 stored; bcrypt cost 12 verify; DummyHash for unknown addresses
|
||||
POST /api/auth/refresh {refresh_token,device}
|
||||
→ same shape; BOTH rotate; old refresh invalid immediately; client must serialise and persist before use
|
||||
401 {error:"token_expired"} → refresh once and retry
|
||||
401 {error:"bad_credentials"} / {error:"unauthorized"} → sign in
|
||||
GET/DELETE /api/auth/sessions[/{id}] · POST /api/auth/sessions/revoke-others
|
||||
```
|
||||
|
||||
### 6.2 Route families and least role
|
||||
|
||||
| Family | Routes | Least role |
|
||||
|---|---|---|
|
||||
| Auth | login, refresh, logout, me, sessions | none / authed |
|
||||
| Joining | `GET /api/auth/invitation?code=`, `POST /api/auth/register` | none |
|
||||
| Team | `GET /api/team` | authed (tenant users) |
|
||||
| | `POST /api/team/members`, `POST /api/team/{id}/password`, `PATCH /api/team/{id}`, `/api/team/invitations*` | manager |
|
||||
| Arrivals | `GET /api/visits`, `GET /api/visits/stream` (SSE) | authed |
|
||||
| Customers | `GET /api/visitors`, `/history`, `/image`, `GET /api/faces/{id}` | authed |
|
||||
| | `PUT /api/visitors/{id}/profile`, `POST /api/purchases` | staff |
|
||||
| | `DELETE /api/visitors/{id}` (erasure) | manager |
|
||||
| Shops & cameras | `GET /api/sites`, `/check`, `GET /api/cameras`, `/snapshot.jpg`, `/live` (SSE) | authed |
|
||||
| | `POST /api/sites/{site}/cameras`, `PATCH`/`DELETE /api/cameras/{id}`, `POST /api/cameras/{id}/check`, `POST /api/sites/{site}/enrolment-code` | manager |
|
||||
| Reports | `GET /api/reports/footfall`, `/conversion` | authed |
|
||||
| Assistant | `POST /api/assistant` | authed |
|
||||
| Admin | `GET`/`POST /api/admin/clients` | platform admin (role admin AND no client) |
|
||||
| Agent | `POST /api/agent/enrol` (none), then `/api/agent/{cameras, checks, faces, upload-url, live, cameras/{c}/snapshot, cameras/{c}/live}` | agent token |
|
||||
|
||||
Identifiers: any `{id}` or `site` accepts a uuid **or** the human reference (`V-42`, `chennai`, `cam1`). Unknown reference in a path → 404; in a query filter → 400. Another tenant's data → 404, never 403.
|
||||
|
||||
### 6.3 Assistant tools (`internal/assistant/tools.go`)
|
||||
|
||||
`list_sites`, `site_health`, `footfall`, `conversion`, `find_customer`, `customer_history`, `check_camera` (manager+; refuses staff in the tool, not the prompt). No tool takes a tenant id; the Principal is bound at the call site. Business tools only — never `execute_sql`. Loop bounded at 8 iterations; text produced alongside a tool call is discarded; failing tools return results, not errors.
|
||||
|
||||
---
|
||||
|
||||
## 7. Deployment topology
|
||||
|
||||
```
|
||||
INTERNET
|
||||
│
|
||||
┌───────────────┼───────────────────┐
|
||||
│ HTTPS 443 │ │ TLS 8883
|
||||
┌────────▼─────────┐ │ ┌────────▼─────────┐
|
||||
│ Traefik │ │ │ Mosquitto │
|
||||
│ platform.loyaly │ │ │ mcp.loyaly.ai │
|
||||
│ mcp.loyaly.ai │ │ │ passwd + ACL │
|
||||
│ TLS termination │ │ │ pattern write │
|
||||
└────────┬─────────┘ │ │ bv/%u/# │
|
||||
│ :8080 plain │ └────────┬─────────┘
|
||||
┌────────▼───────────────────────┐ │ :1883 internal
|
||||
│ behavision-server (one binary) │◄───────────┘ subscribe bv/+/+
|
||||
│ ├ migrate (boot) │
|
||||
│ ├ ingest consumer → Hub │
|
||||
│ ├ API (48 routes) → SSE │
|
||||
│ ├ LiveHub (camera relay) │
|
||||
│ ├ web (embedded React) │
|
||||
│ └ assistant (optional) │
|
||||
└────────┬───────────────┬───────┘
|
||||
│ │ presigned PUT/GET (optional)
|
||||
┌────────▼─────────┐ ┌──▼──────────────────┐
|
||||
│ PostgreSQL 16 │ │ Object storage │
|
||||
│ + pgvector │ │ (S3-compatible) │
|
||||
│ 17 tables │ │ private ACL │
|
||||
└──────────────────┘ └─────────────────────┘
|
||||
|
||||
════════════════════════ trust boundary: no inbound route ════════════════════════
|
||||
|
||||
SHOP NETWORK (one per shop, behind NAT)
|
||||
┌──────────────────────────────────────────────────────────┐
|
||||
│ Cameras ──RTSP/TCP──▶ Engine :8010 ──webhook──▶ Agent │
|
||||
│ │ SQLite │ spool │
|
||||
│ │ FAISS │ │
|
||||
│ Shop app ◄─── relay ───────┘ │
|
||||
│ │
|
||||
│ outbound only: agent ──TLS 8883──▶ Mosquitto │
|
||||
│ agent ──HTTPS────▶ /api/agent/* │
|
||||
│ app ──HTTPS────▶ /api/* │
|
||||
└──────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Failure domains.** A shop PC failing affects one shop; its footfall queues locally and nothing else notices except the heartbeat. Mosquitto failing stops delivery for all shops but loses nothing (every event is on a shop's disk). The server failing stops the console and API; Mosquitto retains the server's subscription backlog. PostgreSQL is the single stateful component in the cloud tier.
|
||||
|
||||
**Data residency.** Video never leaves the shop. Face templates leave the shop only as 512-float vectors inside visit events, over TLS, to the tenant's own prefix. Photographs leave only when `store_faces` is enabled, via presigned upload to a private object, or into Postgres when no bucket is configured. Every image read at head office is audited.
|
||||
|
||||
**Encrypted paths.** Camera credentials: DPAPI on the shop PC, AES-256-GCM (aad = site) in Postgres, plaintext only inside the agent's process and on the LAN RTSP connection to the camera. Broker password: sealed in `agent.json`, sealed in `agents.mqtt_password_enc`, hashed in Mosquitto's `passwd`. Sessions: SHA-256 at rest. User passwords: bcrypt 12.
|
||||
|
||||
---
|
||||
|
||||
## 8. Measured
|
||||
|
||||
| Metric | Value | Source |
|
||||
|---|---|---|
|
||||
| Identity stability | 103 tracks → 7 people, 44 re-recognitions, 5 min | engine `/api/stats`, office cam2 |
|
||||
| Same-person similarity | p05 0.719 (frontal webcam, 18,528 pairs) | `calibrate` |
|
||||
| Gallery search | 0.27 / 2.24 / 21.9 ms at 1k / 10k / 100k | `VectorIndex` benchmark |
|
||||
| Delivery under burst | 120 of 120 simultaneous visits | live broker + Postgres |
|
||||
| Camera → head office | ~3 s | end-to-end run |
|
||||
| Head-office live view | 13 fps, 259 KB/s, 0 duplicates | relay measurement |
|
||||
| Shop-PC live picture | 14.0 pictures/s vs 15 fps camera; engine CPU 90% → 62% | before/after, cam2 sub-stream |
|
||||
| Clean-machine install | 10/10 steps, both cameras connected | fresh container |
|
||||
| Server test suite | < 10 s (bcrypt cost lowered for tests only) | `go test ./...` |
|
||||
@@ -31,6 +31,8 @@
|
||||
#define MyAppExeName "Behavision.exe"
|
||||
|
||||
[Setup]
|
||||
; The Loyaly mark, on the installer and in Add/Remove Programs.
|
||||
SetupIconFile=..\brand\loyaly.ico
|
||||
AppId={{7C4B9E2A-3F51-4C86-9D0A-B1E7A2F65D11}
|
||||
AppName={#MyAppName}
|
||||
AppVersion={#MyAppVersion}
|
||||
|
||||
@@ -38,49 +38,79 @@ function Need($exe, $hint) {
|
||||
}
|
||||
}
|
||||
|
||||
# Run a NATIVE command and stop if it fails.
|
||||
#
|
||||
# $ErrorActionPreference = "Stop" does not do this. It governs PowerShell
|
||||
# errors; a .exe returning non-zero is not one, so the script sails past it.
|
||||
# That is not theoretical here: `npm ci` failing on a fresh Windows box would
|
||||
# have let the build continue, and `go build` would then have embedded the
|
||||
# STALE frontend/dist that is committed to this repository - producing an
|
||||
# installer that works, opens, and shows last month's UI, with nothing
|
||||
# anywhere saying so. The silent-wrong outcome, from the most likely failure.
|
||||
#
|
||||
# Output is NOT swallowed. `| Out-Null` on a failing install is how
|
||||
# run-local.sh once exited with no output at all, which took three runs to
|
||||
# diagnose; the same mistake is not worth repeating in a script that will be
|
||||
# run on a machine nobody is sitting at.
|
||||
function Run($exe) {
|
||||
$rest = $args
|
||||
& $exe @rest
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "$exe $($rest -join ' ') failed with exit code $LASTEXITCODE"
|
||||
}
|
||||
}
|
||||
|
||||
Need python "Install Python 3.11+ and tick 'Add to PATH'."
|
||||
Need go "Install Go 1.21+ from https://go.dev/dl/."
|
||||
Need npm "Install Node.js LTS from https://nodejs.org/."
|
||||
|
||||
Step "Python environment"
|
||||
Push-Location $root
|
||||
if (-not (Test-Path ".venv")) { python -m venv .venv }
|
||||
& .\.venv\Scripts\python -m pip install --upgrade pip | Out-Null
|
||||
& .\.venv\Scripts\python -m pip install -r requirements.txt pyinstaller | Out-Null
|
||||
if (-not (Test-Path ".venv")) { Run python -m venv .venv }
|
||||
Run .\.venv\Scripts\python -m pip install --upgrade pip
|
||||
Run .\.venv\Scripts\python -m pip install -r requirements.txt pyinstaller
|
||||
|
||||
Step "Engine tests"
|
||||
# The package is not worth building if the engine is broken, and finding that
|
||||
# out after the installer is signed is the expensive order to do it in.
|
||||
& .\.venv\Scripts\python -m pytest tests -q
|
||||
if ($LASTEXITCODE -ne 0) { throw "engine tests failed" }
|
||||
Run .\.venv\Scripts\python -m pytest tests -q
|
||||
|
||||
Step "Engine (PyInstaller, one-folder)"
|
||||
if (Test-Path (Join-Path $root "build")) { Remove-Item -Recurse -Force (Join-Path $root "build") }
|
||||
& .\.venv\Scripts\pyinstaller behavision.spec --noconfirm --distpath (Join-Path $dist "engine-build")
|
||||
if ($LASTEXITCODE -ne 0) { throw "pyinstaller failed" }
|
||||
Run .\.venv\Scripts\pyinstaller behavision.spec --noconfirm --distpath (Join-Path $dist "engine-build")
|
||||
|
||||
Step "Desktop app (Wails)"
|
||||
Push-Location (Join-Path $root "desktop\frontend")
|
||||
npm ci
|
||||
npm run build
|
||||
# A built dist is COMMITTED to this repository so `go build` type-checks
|
||||
# without npm (the //go:embed directive requires the directory to exist). That
|
||||
# convenience is a trap at package time: a silently failed npm build leaves
|
||||
# the old one in place and it embeds perfectly. So the marker is removed
|
||||
# first, and its reappearance is what proves this build produced the UI being
|
||||
# shipped rather than inheriting one.
|
||||
$marker = Join-Path $root "desktop\frontend\dist\index.html"
|
||||
if (Test-Path $marker) { Remove-Item -Force $marker }
|
||||
Run npm ci
|
||||
Run npm run build
|
||||
if (-not (Test-Path $marker)) { throw "npm run build reported success and produced no dist\index.html" }
|
||||
Pop-Location
|
||||
Push-Location (Join-Path $root "desktop")
|
||||
# Wails v2 talks to WebView2 through pure-Go bindings, so no cgo and no
|
||||
# toolchain beyond Go itself. Verified by cross-compiling the same package from
|
||||
# a Mac with CGO_ENABLED=0.
|
||||
# toolchain beyond Go itself. A plain go build is used on purpose: the icon
|
||||
# and the manifest are compiled in from rsrc_windows_amd64.syso (go-winres,
|
||||
# from brand/loyaly-icon-512.png), and `wails build` would add a second copy
|
||||
# of both and fail the link with duplicate resources.
|
||||
$env:CGO_ENABLED = "0"
|
||||
if (Get-Command wails -ErrorAction SilentlyContinue) {
|
||||
wails build -platform windows/amd64 -clean -ldflags "-X main.version=$Version"
|
||||
} else {
|
||||
Write-Warning "wails CLI not found - falling back to a plain go build (no icon, no manifest)."
|
||||
go build -ldflags "-H windowsgui -X main.version=$Version" -o (Join-Path $root "desktop\build\bin\Behavision.exe") .
|
||||
}
|
||||
Run go build -tags desktop,production -ldflags "-H windowsgui -X main.version=$Version" -o (Join-Path $root "desktop\build\bin\Behavision.exe") .
|
||||
Pop-Location
|
||||
|
||||
Step "Headless agent"
|
||||
Push-Location (Join-Path $root "agent")
|
||||
$env:CGO_ENABLED = "0" # the cgo resolver forces external linking
|
||||
go build -o (Join-Path $root "dist\behavision-agent.exe") .
|
||||
# `go build -o` does not create the target directory, and on a fresh clone
|
||||
# dist\ is gitignored and absent. It exists here only because PyInstaller ran
|
||||
# first and made it - an ordering dependency nothing states, so state it.
|
||||
New-Item -ItemType Directory -Force -Path $dist | Out-Null
|
||||
Run go build -o (Join-Path $root "dist\behavision-agent.exe") .
|
||||
Pop-Location
|
||||
|
||||
Step "WebView2 bootstrapper"
|
||||
@@ -106,8 +136,11 @@ Copy-Item (Join-Path $root "LICENSE") $stage -ErrorAction SilentlyContinue
|
||||
|
||||
$engineExe = Join-Path $stage "engine\behavision.exe"
|
||||
if (-not (Test-Path $engineExe)) { throw "engine exe missing at $engineExe" }
|
||||
& $engineExe paths
|
||||
if ($LASTEXITCODE -ne 0) { throw "the frozen engine cannot start - `paths` failed" }
|
||||
# The frozen engine has to START, not merely exist. A PyInstaller build that
|
||||
# is missing a native DLL links fine and dies on first launch - the classic
|
||||
# "works in the venv, dies in the bundle" - and finding that out on a shop
|
||||
# counter is the expensive order to do it in.
|
||||
Run $engineExe paths
|
||||
|
||||
Pop-Location
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
[project]
|
||||
name = "behavision"
|
||||
version = "1.1.0"
|
||||
dynamic = ["version"]
|
||||
description = "Production face recognition over RTSP"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"numpy>=1.26,<2.0",
|
||||
"opencv-python>=4.8.1",
|
||||
# See requirements.txt for why numpy is uncapped and opencv is not.
|
||||
"numpy>=1.26,<3.0",
|
||||
"opencv-python>=4.8.1,<5",
|
||||
"onnxruntime>=1.16",
|
||||
"fastapi>=0.110",
|
||||
"uvicorn>=0.29",
|
||||
@@ -21,10 +22,23 @@ dependencies = [
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = ["pytest>=8.0"]
|
||||
# httpx is test-only and never ships in the wheel. The HTTP tests begin with
|
||||
# `pytest.importorskip("httpx")` so a bare checkout still runs - which is
|
||||
# right, and has a cost worth knowing: without it the suite reports 239 passed
|
||||
# and quietly SKIPS 32 API tests. `pip install -e .[dev]` is how to get them.
|
||||
dev = ["pytest>=8.0", "httpx>=0.27"]
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
include = ["behavision*"]
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
behavision = ["static/*"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
|
||||
# One version for the package and the wheel. Two literals in two files is how
|
||||
# they came to disagree (1.0.0 here, 1.1.0 there) and how neither tracked a
|
||||
# release.
|
||||
[tool.setuptools.dynamic]
|
||||
version = {attr = "behavision.__version__"}
|
||||
|
||||
72
release.sh
@@ -1,8 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build the Windows shop-PC package and publish it as a Gitea release.
|
||||
#
|
||||
# ./release.sh v0.4.2 build dist/Behavision-v0.4.2-windows-x64.zip and publish
|
||||
# ./release.sh v0.4.2 build the Windows zip and publish
|
||||
# PUBLISH=0 ./release.sh v0.4.2 build only
|
||||
# MAC=1 ./release.sh v0.4.2 also build and attach the macOS zip
|
||||
# DEMO_PACK=demo-cameras.enc ./release.sh v0.4.4-demo
|
||||
# a demo build: the sealed bundle from
|
||||
# behavision-demo-pack ships in engine-src,
|
||||
# and setup asks for its unlock code
|
||||
#
|
||||
# The package is a SOURCE install: the Go binaries are cross-compiled here, the
|
||||
# engine ships as a pure-Python wheel and behavision-setup.exe builds a venv on
|
||||
@@ -18,6 +23,8 @@ TAG=${1:?usage: release.sh vX.Y.Z}
|
||||
REPO_API=https://gitapp.workolik.com/api/v1/repos/Loyaly/Behavision
|
||||
STAGE=dist/Behavision
|
||||
ZIP="dist/Behavision-$TAG-windows-x64.zip"
|
||||
MACZIP="dist/Behavision-$TAG-macos-arm64.zip"
|
||||
MACSTAGE=dist/Behavision-mac
|
||||
|
||||
step() { printf '\n\033[1m%s\033[0m\n' "$*"; }
|
||||
case "$(git describe --tags --always --dirty)" in *-dirty) echo "refusing to release uncommitted changes" >&2; exit 1;; esac
|
||||
@@ -28,7 +35,10 @@ fi
|
||||
step "1. Desktop app (Wails, pure-Go Windows target)"
|
||||
(cd desktop/frontend && npm run build >/dev/null)
|
||||
rm -rf "$STAGE" && mkdir -p "$STAGE/engine-src"
|
||||
(cd desktop && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath \
|
||||
# -tags desktop,production is what `wails build` passes; without them the
|
||||
# binary starts, shows "Wails applications will not build without the correct
|
||||
# build tags" and exits. Measured on the first Windows install of v0.4.4-demo.
|
||||
(cd desktop && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath -tags desktop,production \
|
||||
-ldflags "-H windowsgui -s -w -X main.version=$TAG" -o "../$STAGE/Behavision.exe" .)
|
||||
|
||||
step "2. Agent and setup tool"
|
||||
@@ -40,20 +50,75 @@ step "2. Agent and setup tool"
|
||||
step "3. Engine source and wheel"
|
||||
# The wheel is built with the checkout's own interpreter; requires-python is a
|
||||
# statement about the SHOP PC, which setup enforces when it finds Python there.
|
||||
# Stamp the tag into the wheel version. Without this every release built
|
||||
# behavision-1.1.0-py3-none-any.whl, and `pip install --upgrade` on a machine
|
||||
# that already had 1.1.0 is a no-op - so an engine fix reached nobody who had
|
||||
# ever run setup, while the Go binaries beside it updated normally. Nothing
|
||||
# reported a version either, so there was no way to tell a shop PC three
|
||||
# releases behind from a current one.
|
||||
#
|
||||
# v0.5.6-demo -> 0.5.6+demo, which is valid PEP 440: a local segment takes
|
||||
# alphanumerics and dots, never hyphens.
|
||||
TMPVER=$(mktemp)
|
||||
PEP440=$(printf '%s' "${TAG#v}" | sed 's/-/+/; s/[^0-9A-Za-z.+]/./g')
|
||||
trap 'git checkout -- behavision/__init__.py 2>/dev/null || true; rm -f "$TMPVER"' EXIT
|
||||
# A temp file rather than `sed -i`, whose argument handling differs between
|
||||
# BSD and GNU - this script is run from a Mac today and that is not a reason
|
||||
# to plant a portability trap in a release path.
|
||||
sed "s/^__version__ = .*/__version__ = \"$PEP440\"/" behavision/__init__.py > "$TMPVER"
|
||||
cat "$TMPVER" > behavision/__init__.py
|
||||
.venv/bin/python -m pip wheel --no-deps --ignore-requires-python -q -w "$STAGE/engine-src" . 2>&1 | grep -v "DEPRECATION\|WARNING: Ignoring" || true
|
||||
git checkout -- behavision/__init__.py
|
||||
rm -f "$TMPVER"
|
||||
trap - EXIT
|
||||
echo " engine wheel version $PEP440"
|
||||
ls "$STAGE"/engine-src/behavision-*.whl >/dev/null || { echo "wheel was not built" >&2; exit 1; }
|
||||
cp pyproject.toml requirements.txt "$STAGE/engine-src/"
|
||||
mkdir -p "$STAGE/engine-src/config" && cp config/default.yaml "$STAGE/engine-src/config/"
|
||||
rsync -a --exclude '__pycache__' behavision/ "$STAGE/engine-src/behavision/"
|
||||
cp installer/INSTALL.txt installer/run-with-lan-head-office.cmd "$STAGE/"
|
||||
if [ -n "${DEMO_PACK:-}" ]; then
|
||||
case "$TAG" in *-demo*) ;; *) echo "a DEMO_PACK build must be tagged -demo" >&2; exit 1;; esac
|
||||
cp "$DEMO_PACK" "$STAGE/engine-src/demo-cameras.enc" && echo " demo bundle: $(basename "$DEMO_PACK")"
|
||||
fi
|
||||
|
||||
step "4. Package"
|
||||
rm -f "$ZIP" && (cd dist && zip -qr "$(basename "$ZIP")" Behavision) && ls -la "$ZIP" | awk '{print " " $5 " bytes " $9}'
|
||||
unzip -l "$ZIP" | grep -E "Behavision\.exe|agent\.exe|setup\.exe|\.whl|INSTALL" | awk '{print " " $4}'
|
||||
|
||||
# --- macOS, same SOURCE-install shape as Windows -------------------------
|
||||
#
|
||||
# Worth stating because it is the reason this is cheap: the Windows package
|
||||
# already ships a pure-Python WHEEL and builds a venv on the target machine,
|
||||
# because PyInstaller cannot cross-compile. macOS needs nothing different -
|
||||
# the same wheel, the same setup tool, a natively built .app instead of an
|
||||
# .exe. No frozen engine, no 200 MB, no second packaging story.
|
||||
#
|
||||
# Two things it is NOT, and the release notes should say so:
|
||||
# * not notarised. macOS blocks an unsigned download outright rather than
|
||||
# warning like SmartScreen, so the first launch needs right-click > Open.
|
||||
# Notarising needs an Apple Developer account.
|
||||
# * arm64 only. Every Mac worth demoing on since 2020, and building a
|
||||
# universal binary doubles the size for machines nobody here has.
|
||||
if [ "${MAC:-0}" = "1" ]; then
|
||||
step "5. macOS package"
|
||||
command -v wails >/dev/null || { echo "wails CLI not found; go install github.com/wailsapp/wails/v2/cmd/wails@latest" >&2; exit 1; }
|
||||
rm -rf "$MACSTAGE" && mkdir -p "$MACSTAGE/engine-src"
|
||||
(cd desktop && wails build -platform darwin/arm64 -tags "desktop,production" -skipbindings >/dev/null)
|
||||
cp -R desktop/build/bin/Behavision.app "$MACSTAGE/"
|
||||
(cd agent && CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -trimpath \
|
||||
-ldflags "-s -w -X main.version=$TAG" -o "../$MACSTAGE/behavision-agent" . \
|
||||
&& CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -trimpath \
|
||||
-ldflags "-s -w -X main.version=$TAG" -o "../$MACSTAGE/behavision-setup" ./cmd/behavision-setup)
|
||||
# The identical engine payload the Windows package carries.
|
||||
cp -R "$STAGE/engine-src/." "$MACSTAGE/engine-src/"
|
||||
rm -f "$MACZIP" && (cd dist && zip -qry "$(basename "$MACZIP")" Behavision-mac)
|
||||
ls -la "$MACZIP" | awk '{print " " $5 " bytes " $9}'
|
||||
fi
|
||||
|
||||
[ "${PUBLISH:-1}" = "1" ] || { echo "built, not published"; exit 0; }
|
||||
|
||||
step "5. Tag and publish"
|
||||
step "6. Tag and publish"
|
||||
git rev-parse -q --verify "refs/tags/$TAG" >/dev/null || git tag -a "$TAG" -m "$TAG"
|
||||
git push -q origin "$TAG"
|
||||
# The notes come from a file so they are reviewed, not typed into a shell.
|
||||
@@ -66,4 +131,5 @@ BODY=$(python3 -c 'import json,sys;print(json.dumps({"tag_name":sys.argv[1],"nam
|
||||
REL=$(curl -sS -u "$USER:$PASS" -H 'content-type: application/json' -d "$BODY" "$REPO_API/releases")
|
||||
ID=$(printf '%s' "$REL" | python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])')
|
||||
curl -sS -u "$USER:$PASS" -F "attachment=@$ZIP" "$REPO_API/releases/$ID/assets?name=$(basename "$ZIP")" >/dev/null
|
||||
[ "${MAC:-0}" = "1" ] && curl -sS -u "$USER:$PASS" -F "attachment=@$MACZIP" "$REPO_API/releases/$ID/assets?name=$(basename "$MACZIP")" >/dev/null
|
||||
echo " published: https://gitapp.workolik.com/Loyaly/Behavision/releases/tag/$TAG"
|
||||
|
||||
@@ -1,5 +1,31 @@
|
||||
numpy>=1.26,<2.0
|
||||
opencv-python>=4.8.1
|
||||
# numpy 2 is allowed, and that is what lets this install on a current Python.
|
||||
# `<2.0` capped the resolver at numpy 1.26.4, whose newest wheel is cp312, so
|
||||
# on a Mac with Python 3.14 pip fell back to BUILDING numpy from source and
|
||||
# died in clang - two screens of C compiler output on a shop counter, for a
|
||||
# version choice made silently by behavision-setup.
|
||||
#
|
||||
# Measured before changing it, nine runs of the detector guard per combination
|
||||
# on one machine:
|
||||
#
|
||||
# numpy 1.26 / cv2 4.11 9 passed, 0 crashed
|
||||
# numpy 2.0 / cv2 4.11 8 passed, 1 crashed
|
||||
# numpy 1.26 / cv2 4.14 3 passed, 6 crashed
|
||||
# numpy 2.0 / cv2 4.14 2 passed, 7 crashed
|
||||
#
|
||||
# numpy is not the variable there; OpenCV is. The crash was a test racing a
|
||||
# shared cv2.FaceDetectorYN on purpose - undefined behaviour in C++, which 4.11
|
||||
# usually turned into an exception and 4.14 usually turns into a segfault. The
|
||||
# product never shares one (Engine._build_worker builds a detector per camera),
|
||||
# and the test now runs that race out of process. With that fixed, the whole
|
||||
# suite is 226 passed / 2 skipped on numpy 2.0.2, five runs out of five.
|
||||
#
|
||||
# opencv IS capped, and the two are not the same call. Everything above was
|
||||
# measured on 4.x; OpenCV 5.0 is a major release this project has never run a
|
||||
# real camera or an emotion model through, and an uncapped `>=4.8.1` means
|
||||
# every NEW install silently gets it while every existing one keeps 4.11. Lift
|
||||
# it after running a camera on 5.x, not before.
|
||||
numpy>=1.26,<3.0
|
||||
opencv-python>=4.8.1,<5
|
||||
onnxruntime>=1.16
|
||||
fastapi>=0.110
|
||||
uvicorn>=0.29
|
||||
|
||||
@@ -28,6 +28,17 @@ REMOTE_DIR=/root/behavision
|
||||
PUBLIC=https://mcp.loyaly.ai
|
||||
SSH=(ssh -i "$KEY" -o BatchMode=yes -o ConnectTimeout=10 "$HOST")
|
||||
|
||||
# Go is not always on an interactive shell's PATH - a Homebrew or tarball
|
||||
# install lands in a directory that .zprofile adds but a script does not
|
||||
# inherit, so this failed at step 1 with "go: command not found" on the very
|
||||
# machine it was written on. Found the only way it could be: by somebody
|
||||
# running it. A deploy that needs the operator to fix their environment first
|
||||
# is a deploy that gets skipped.
|
||||
for d in "$HOME/go/bin" /usr/local/go/bin /opt/homebrew/bin; do
|
||||
[ -x "$d/go" ] && case ":$PATH:" in *":$d:"*) ;; *) PATH="$PATH:$d";; esac
|
||||
done
|
||||
command -v go >/dev/null || { echo "go not found - install it or add it to PATH" >&2; exit 1; }
|
||||
|
||||
VERSION=$(git describe --tags --always --dirty)
|
||||
case "$VERSION" in *-dirty) echo "refusing to deploy uncommitted changes ($VERSION)" >&2; exit 1;; esac
|
||||
|
||||
@@ -47,7 +58,19 @@ git rev-parse HEAD | "${SSH[@]}" "cat > $REMOTE_DIR/release/$VERSION/GIT_SHA"
|
||||
if [ "${DRY_RUN:-}" != "" ]; then echo "DRY_RUN: shipped to $REMOTE_DIR/release/$VERSION, nothing changed"; exit 0; fi
|
||||
|
||||
step "3. Back up the database"
|
||||
"${SSH[@]}" "docker exec behavision-db sh -c 'PGPASSWORD=\$POSTGRES_PASSWORD pg_dump -U behavision -d behavision' | gzip > $REMOTE_DIR/backups/pre-$VERSION-\$(date +%Y%m%d-%H%M%S).sql.gz && ls -la $REMOTE_DIR/backups | tail -1"
|
||||
# The dump's own filename is echoed, not `ls | tail -1`, which reported the
|
||||
# WRONG file: ls sorts alphabetically, so pre-...-demo-12-... sorts before
|
||||
# pre-...-demo-6-... and the line printed a backup from four days earlier. A
|
||||
# deploy that names the wrong safety net is worse than one that names none -
|
||||
# that is the file somebody reaches for at the worst possible moment.
|
||||
#
|
||||
# Bare `-s` on the dump so an empty or failed one cannot be reported as a
|
||||
# backup: pg_dump exiting non-zero already fails the pipeline under pipefail,
|
||||
# but a zero-byte gzip would still satisfy it.
|
||||
"${SSH[@]}" "set -e; f=$REMOTE_DIR/backups/pre-$VERSION-\$(date +%Y%m%d-%H%M%S).sql.gz; \
|
||||
docker exec behavision-db sh -c 'PGPASSWORD=\$POSTGRES_PASSWORD pg_dump -U behavision -d behavision' | gzip > \$f; \
|
||||
[ -s \$f ] || { echo 'backup is empty - refusing to continue' >&2; exit 1; }; \
|
||||
ls -la \$f"
|
||||
|
||||
step "4. Image"
|
||||
"${SSH[@]}" "cd $REMOTE_DIR/release/$VERSION && docker build -q -t behavision-backend:$VERSION -f Dockerfile.runtime . && docker tag behavision-backend:$VERSION behavision-backend:latest"
|
||||
@@ -65,7 +88,33 @@ step "6. Switch"
|
||||
"${SSH[@]}" "cd $REMOTE_DIR && docker compose up -d --no-build --no-deps backend && sleep 4 && docker logs --tail 15 behavision-backend"
|
||||
|
||||
step "7. Verify over $PUBLIC"
|
||||
for p in /healthz /api/admin/clients /api/team /api/visits /api/cameras; do
|
||||
printf ' %-20s %s\n' "$p" "$(curl -s -o /dev/null -w '%{http_code}' -m 15 "$PUBLIC$p")"
|
||||
# 401 is a PASS, and that distinction is the whole point of this step. An
|
||||
# unauthenticated call to a route that EXISTS is refused; a route the binary
|
||||
# never registered is a 404. So this proves the routing rather than the auth -
|
||||
# which is precisely what a deploy gets wrong, and what otherwise surfaces as a
|
||||
# console showing "Backend integration required" against an API that shipped.
|
||||
#
|
||||
# The uuid matches nothing on purpose: the admin drill-down must answer 401
|
||||
# with no session, never 404.
|
||||
NOBODY=00000000-0000-4000-8000-000000000000
|
||||
fail=0
|
||||
for p in /healthz \
|
||||
/api/admin/clients \
|
||||
"/api/admin/clients/$NOBODY" \
|
||||
"/api/admin/clients/$NOBODY/sites" \
|
||||
"/api/admin/clients/$NOBODY/sites/x/cameras" \
|
||||
/api/admin/monitoring/summary \
|
||||
/api/sales \
|
||||
/api/sales/x \
|
||||
/api/dashboard/summary \
|
||||
/api/team /api/visits /api/cameras; do
|
||||
code=$(curl -s -o /dev/null -w '%{http_code}' -m 15 "$PUBLIC$p")
|
||||
case "$code" in
|
||||
200|401) verdict="ok" ;;
|
||||
404) verdict="MISSING - this binary does not serve that route"; fail=1 ;;
|
||||
*) verdict="unexpected"; fail=1 ;;
|
||||
esac
|
||||
printf ' %-46s %s %s\n' "$p" "$code" "$verdict"
|
||||
done
|
||||
curl -s -m 15 "$PUBLIC/healthz" | head -c 300; echo
|
||||
[ "$fail" = 0 ] || { echo; echo "VERIFY FAILED - routes above marked MISSING did not ship" >&2; exit 1; }
|
||||
|
||||
@@ -3,13 +3,13 @@ module github.com/loyaly/behavision-server
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/anthropics/anthropic-sdk-go v1.69.0
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
golang.org/x/crypto v0.42.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/anthropics/anthropic-sdk-go v1.69.0 // indirect
|
||||
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
||||
github.com/buger/jsonparser v1.1.2 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
|
||||
315
server/internal/api/admin_monitor_test.go
Normal file
@@ -0,0 +1,315 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Two merchants, each with a shop and a camera. The whole point of these tests
|
||||
// is that the path names the merchant, so a fixture with only one proves
|
||||
// nothing about scoping.
|
||||
const (
|
||||
merchantA = "11111111-1111-4111-8111-aaaaaaaaaaaa"
|
||||
merchantB = "22222222-2222-4222-8222-bbbbbbbbbbbb"
|
||||
shopA = "33333333-3333-4333-8333-aaaaaaaaaaaa"
|
||||
shopB = "44444444-4444-4444-8444-bbbbbbbbbbbb"
|
||||
)
|
||||
|
||||
func seedTwoMerchants(fs *fakeStore) {
|
||||
seedPlatformAdmin(fs)
|
||||
fs.clientRows = map[string]ClientDetail{
|
||||
merchantA: {ClientRow: ClientRow{ID: merchantA, Slug: "acme", Name: "Acme Retail",
|
||||
Active: true, Sites: 1, Users: 2}, OwnerEmail: "owner@acme.com", OwnerName: "Asha"},
|
||||
merchantB: {ClientRow: ClientRow{ID: merchantB, Slug: "rival", Name: "Rival Stores",
|
||||
Active: true, Sites: 1, Users: 1}, OwnerEmail: "owner@rival.com"},
|
||||
}
|
||||
fs.sites = []SiteHealth{
|
||||
{SiteID: shopA, Slug: "chennai", Name: "Acme Chennai", CamerasUp: 1, CamerasTotal: 1},
|
||||
{SiteID: shopB, Slug: "mumbai", Name: "Rival Mumbai", CamerasUp: 0, CamerasTotal: 1},
|
||||
}
|
||||
fs.siteOwner = map[string]string{shopA: merchantA, shopB: merchantB}
|
||||
yes := true
|
||||
fs.cameras = []Camera{
|
||||
{ID: "cam-a", SiteID: shopA, CameraID: "entrance", Label: "Front door",
|
||||
Host: "192.168.1.121", Port: 554, Path: "/ch0_1.264",
|
||||
Username: "admin", HasPassword: true, Enabled: true, Connected: &yes},
|
||||
{ID: "cam-b", SiteID: shopB, CameraID: "entrance", Label: "Rival door",
|
||||
Host: "10.0.0.9", Port: 554, Username: "root", HasPassword: true},
|
||||
}
|
||||
fs.cameraRefs = map[string]cameraRef{
|
||||
"cam-a": {client: merchantA, site: shopA, engineID: "entrance"},
|
||||
"cam-b": {client: merchantB, site: shopB, engineID: "entrance"},
|
||||
}
|
||||
}
|
||||
|
||||
// adminReads picks out the rows this surface writes. Signing in audits too,
|
||||
// so a bare count would couple these tests to unrelated behaviour.
|
||||
func adminReads(fs *fakeStore) []AuditEntry {
|
||||
var out []AuditEntry
|
||||
for _, a := range fs.audits {
|
||||
if strings.HasPrefix(a.Action, "admin.") {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func adminGet(t *testing.T, s *Server, token, path string) (int, string) {
|
||||
t.Helper()
|
||||
rec := do(t, s, "GET", path, token, nil)
|
||||
return rec.Code, rec.Body.String()
|
||||
}
|
||||
|
||||
// ------------------------------------------------- the surface is invisible
|
||||
|
||||
func TestAMerchantTokenGets404FromEveryAdminDrilldownRoute(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
seedUser(fs) // an ordinary manager inside another tenant
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/admin/clients/" + merchantA,
|
||||
"/api/admin/clients/" + merchantA + "/sites",
|
||||
"/api/admin/clients/" + merchantA + "/sites/" + shopA,
|
||||
"/api/admin/clients/" + merchantA + "/sites/" + shopA + "/cameras",
|
||||
"/api/admin/clients/" + merchantA + "/sites/" + shopA + "/cameras/cam-a",
|
||||
"/api/admin/monitoring/summary",
|
||||
} {
|
||||
if code, body := adminGet(t, s, sess.Token, path); code != http.StatusNotFound {
|
||||
t.Errorf("%s: got %d, want 404 (never 403 - a tenant must not learn "+
|
||||
"this surface exists): %s", path, code, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- scoping
|
||||
|
||||
func TestSitesAreScopedToTheMerchantInThePath(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
code, body := adminGet(t, s, sess.Token, "/api/admin/clients/"+merchantA+"/sites")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("got %d: %s", code, body)
|
||||
}
|
||||
var rows []SiteHealth
|
||||
if err := json.Unmarshal([]byte(body), &rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].SiteID != shopA {
|
||||
t.Fatalf("got %d rows %+v, want only merchant A's shop", len(rows), rows)
|
||||
}
|
||||
if strings.Contains(body, "Rival") {
|
||||
t.Errorf("another merchant's shop leaked into the response: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// The uuid branch is the one that matters. A tenant resolver hands a uuid back
|
||||
// untouched and lets `client_id = $1` downstream do the scoping, which is safe
|
||||
// only because the client id comes from a session. Here the caller names both.
|
||||
func TestAnotherMerchantsShopIs404NotAnEmptyList(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/admin/clients/" + merchantA + "/sites/" + shopB,
|
||||
"/api/admin/clients/" + merchantA + "/sites/" + shopB + "/cameras",
|
||||
"/api/admin/clients/" + merchantA + "/sites/mumbai/cameras",
|
||||
} {
|
||||
code, body := adminGet(t, s, sess.Token, path)
|
||||
if code != http.StatusNotFound {
|
||||
t.Errorf("%s: got %d, want 404 - an empty list says 'this shop has "+
|
||||
"nothing' when the truth is 'not your shop': %s", path, code, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestACameraFromAnotherShopIs404(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
// cam-b exists, and its engine id "entrance" is the same string as cam-a's
|
||||
// - a camera id is unique per SITE, not per tenant, so the chain has to be
|
||||
// checked rather than the name trusted.
|
||||
code, _ := adminGet(t, s, sess.Token,
|
||||
"/api/admin/clients/"+merchantA+"/sites/"+shopA+"/cameras/cam-b")
|
||||
if code != http.StatusNotFound {
|
||||
t.Errorf("got %d, want 404 for a camera belonging to another shop", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnUnknownOrMalformedMerchantIs404NotAServerError(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
for _, id := range []string{
|
||||
"99999999-9999-4999-8999-999999999999", // well formed, no such row
|
||||
"not-a-uuid", // would be a Postgres cast error
|
||||
} {
|
||||
code, body := adminGet(t, s, sess.Token, "/api/admin/clients/"+id+"/sites")
|
||||
if code != http.StatusNotFound {
|
||||
t.Errorf("merchant %q: got %d, want 404: %s", id, code, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- redaction
|
||||
|
||||
// The one that would be a real leak. An RTSP host next to a username is most
|
||||
// of a live path into a customer's camera, and a platform admin browsing
|
||||
// another company's estate has no business with either.
|
||||
func TestAdminCameraRowsCarryNoCredentialFields(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
code, body := adminGet(t, s, sess.Token,
|
||||
"/api/admin/clients/"+merchantA+"/sites/"+shopA+"/cameras")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("got %d: %s", code, body)
|
||||
}
|
||||
// Asserted on the raw JSON, not on a struct: decoding into AdminCamera
|
||||
// would discard exactly the fields this test exists to catch.
|
||||
for _, banned := range []string{
|
||||
"host", "192.168.1.121", "port", "554", "path", "ch0_1.264",
|
||||
"username", "admin", "has_password", "password",
|
||||
} {
|
||||
if strings.Contains(body, banned) {
|
||||
t.Errorf("admin camera row contains %q: %s", banned, body)
|
||||
}
|
||||
}
|
||||
// And it still answers the question the screen asks.
|
||||
for _, want := range []string{"entrance", "Front door", "connected"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("admin camera row is missing %q: %s", want, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Connected is a pointer for a reason: null means no shop PC has ever reported,
|
||||
// false means it is not connecting, and those send an installer to two
|
||||
// different places. `omitempty` would collapse both into absent.
|
||||
func TestAdminCameraKeepsConnectedAsThreeStates(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
fs.cameras[0].Connected = nil
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
_, body := adminGet(t, s, sess.Token,
|
||||
"/api/admin/clients/"+merchantA+"/sites/"+shopA+"/cameras")
|
||||
if !strings.Contains(body, `"connected":null`) {
|
||||
t.Errorf(`want "connected":null for a camera no PC has reported on: %s`, body)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- audit
|
||||
|
||||
func TestEveryAdminReadBelowTheMerchantListIsAudited(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
base := "/api/admin/clients/" + merchantA
|
||||
for _, path := range []string{
|
||||
base + "/sites",
|
||||
base + "/sites/" + shopA,
|
||||
base + "/sites/" + shopA + "/cameras",
|
||||
base + "/sites/" + shopA + "/cameras/cam-a",
|
||||
} {
|
||||
if code, body := adminGet(t, s, sess.Token, path); code != http.StatusOK {
|
||||
t.Fatalf("%s: got %d: %s", path, code, body)
|
||||
}
|
||||
}
|
||||
// Filtered by action: signing in writes its own audit row, and counting
|
||||
// every row would make this test pass or fail on unrelated behaviour.
|
||||
reads := adminReads(fs)
|
||||
if len(reads) != 4 {
|
||||
t.Fatalf("got %d admin read rows, want one per read below the merchant "+
|
||||
"list (all audits: %+v)", len(reads), fs.audits)
|
||||
}
|
||||
for _, a := range reads {
|
||||
if a.ClientID != merchantA {
|
||||
t.Errorf("audit row names client %q, want the merchant being looked at", a.ClientID)
|
||||
}
|
||||
if a.ActorID != "admin-1" || a.ActorKind != "admin" {
|
||||
t.Errorf("audit row must name the admin who looked: %+v", a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Counts across the platform name no merchant and no person, and a console
|
||||
// refreshes them on a timer. Logging that would bury the reads worth finding.
|
||||
func TestTheSummaryIsNotAudited(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
if code, body := adminGet(t, s, sess.Token, "/api/admin/monitoring/summary"); code != http.StatusOK {
|
||||
t.Fatalf("got %d: %s", code, body)
|
||||
}
|
||||
if n := len(adminReads(fs)); n != 0 {
|
||||
t.Errorf("got %d admin read rows for a counts-only header strip, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- suspended
|
||||
|
||||
// "This company is suspended" is precisely what an admin opens the console to
|
||||
// look at. Hiding it would make the one screen that can fix it the one screen
|
||||
// that cannot see it.
|
||||
func TestASuspendedMerchantStaysReadable(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
row := fs.clientRows[merchantA]
|
||||
row.Active = false
|
||||
fs.clientRows[merchantA] = row
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
code, body := adminGet(t, s, sess.Token, "/api/admin/clients/"+merchantA)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("got %d, want a suspended merchant to still read: %s", code, body)
|
||||
}
|
||||
if !strings.Contains(body, `"active":false`) {
|
||||
t.Errorf("the response must say it is suspended: %s", body)
|
||||
}
|
||||
if code, _ := adminGet(t, s, sess.Token, "/api/admin/clients/"+merchantA+"/sites"); code != http.StatusOK {
|
||||
t.Errorf("sites of a suspended merchant: got %d, want 200", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMerchantDetailCarriesTheOwner(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
code, body := adminGet(t, s, sess.Token, "/api/admin/clients/"+merchantA)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("got %d: %s", code, body)
|
||||
}
|
||||
// Who to contact is the whole reason this is not just the list row.
|
||||
if !strings.Contains(body, "owner@acme.com") {
|
||||
t.Errorf("merchant detail must name the owner: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty list must serialise as [] and not null, or a console that maps over
|
||||
// the response breaks on a merchant with no shops - which is every merchant on
|
||||
// the day they are created.
|
||||
func TestAMerchantWithNoShopsReturnsAnEmptyArray(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoMerchants(fs)
|
||||
fs.siteOwner = map[string]string{shopB: merchantB} // A now owns nothing
|
||||
sess := login(t, s, "root@loyaly.ai", "admin123")
|
||||
|
||||
code, body := adminGet(t, s, sess.Token, "/api/admin/clients/"+merchantA+"/sites")
|
||||
if code != http.StatusOK || strings.TrimSpace(body) != "[]" {
|
||||
t.Errorf("got %d %q, want 200 []", code, strings.TrimSpace(body))
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,7 @@ type Store interface {
|
||||
UserSessions(ctx context.Context, userID string) ([]DeviceSession, error)
|
||||
RevokeUserSession(ctx context.Context, userID, sessionID string) error
|
||||
RevokeOtherSessions(ctx context.Context, userID, keepSessionID string) (int, error)
|
||||
SetUserPassword(ctx context.Context, userID, hash string) error
|
||||
|
||||
// --- team and invitations ---
|
||||
// Registration is by invitation: the code carries the address and the role
|
||||
@@ -136,7 +137,19 @@ type Store interface {
|
||||
|
||||
// --- platform administration ---
|
||||
CreateClientWithOwner(ctx context.Context, in NewClientInput) (NewClientResult, error)
|
||||
CreateCustomer(ctx context.Context, clientID string, in Profile, createdBy string) (Customer, error)
|
||||
MergeVisitors(ctx context.Context, clientID, sourceID, targetID string) (MergeResult, error)
|
||||
Sales(ctx context.Context, q SaleQuery) ([]Sale, error)
|
||||
Sale(ctx context.Context, clientID, id string) (Sale, error)
|
||||
|
||||
ListClients(ctx context.Context) ([]ClientRow, error)
|
||||
|
||||
// The admin drill-down. Each takes the merchant's client id explicitly,
|
||||
// because the caller is a platform admin whose session carries none.
|
||||
ClientDetail(ctx context.Context, clientID string) (ClientDetail, error)
|
||||
AdminSiteID(ctx context.Context, clientID, ref string) (string, error)
|
||||
AdminCameraID(ctx context.Context, clientID, siteID, ref string) (string, error)
|
||||
PlatformSummary(ctx context.Context) (PlatformSummary, error)
|
||||
// SetClientActive suspends or reinstates a company. Suspending revokes every
|
||||
// session its users hold in the same transaction - login and ingest already
|
||||
// refuse an inactive client, but a live access token would otherwise keep
|
||||
@@ -159,6 +172,9 @@ type Store interface {
|
||||
// one opened by mistake - and returns its broker username. A shop with
|
||||
// history is closed, not deleted.
|
||||
DeleteEmptySite(ctx context.Context, clientID, siteID string) (string, error)
|
||||
// UpdateSite changes what a person reads - the name, the timezone. Never
|
||||
// the slug: the shop PC and the broker ACL are keyed on it.
|
||||
UpdateSite(ctx context.Context, clientID, siteID string, in SiteUpdate) (SiteHealth, error)
|
||||
|
||||
// --- enrolment ---
|
||||
RedeemEnrolment(ctx context.Context, hash []byte) (Enrolment, error)
|
||||
@@ -281,63 +297,84 @@ func (s *Server) Routes() *http.ServeMux {
|
||||
// Devices. A person may list and revoke their own sessions; removing a
|
||||
// colleague's access is a different question, answered by deactivating them
|
||||
// on the team endpoint below.
|
||||
// Changing your own password. `authed`, not `tenantOnly`: a session is not
|
||||
// a company's data, and a platform admin has no company but must still be
|
||||
// able to do this - they were the account with no route at all.
|
||||
mux.HandleFunc("POST /api/auth/password", s.authed(s.handleChangePassword))
|
||||
mux.HandleFunc("GET /api/auth/sessions", s.authed(s.handleSessions))
|
||||
mux.HandleFunc("DELETE /api/auth/sessions/{id}", s.authed(s.handleRevokeSession))
|
||||
mux.HandleFunc("POST /api/auth/sessions/revoke-others",
|
||||
s.authed(s.handleRevokeOtherSessions))
|
||||
|
||||
// --- the people who work here ---
|
||||
mux.HandleFunc("GET /api/team", s.authed(s.handleTeam))
|
||||
mux.HandleFunc("PATCH /api/team/{id}", s.authed(s.handleUpdateTeamMember))
|
||||
mux.HandleFunc("POST /api/team/members", s.authed(s.handleCreateMember))
|
||||
mux.HandleFunc("POST /api/team/{id}/password", s.authed(s.handleResetPassword))
|
||||
mux.HandleFunc("GET /api/team/invitations", s.authed(s.handleInvitations))
|
||||
mux.HandleFunc("POST /api/team/invitations", s.authed(s.handleInvite))
|
||||
mux.HandleFunc("GET /api/team", s.tenantOnly(s.handleTeam))
|
||||
mux.HandleFunc("PATCH /api/team/{id}", s.tenantOnly(s.handleUpdateTeamMember))
|
||||
mux.HandleFunc("POST /api/team/members", s.tenantOnly(s.handleCreateMember))
|
||||
mux.HandleFunc("POST /api/team/{id}/password", s.tenantOnly(s.handleResetPassword))
|
||||
mux.HandleFunc("GET /api/team/invitations", s.tenantOnly(s.handleInvitations))
|
||||
mux.HandleFunc("POST /api/team/invitations", s.tenantOnly(s.handleInvite))
|
||||
mux.HandleFunc("DELETE /api/team/invitations/{id}",
|
||||
s.authed(s.handleRevokeInvitation))
|
||||
s.tenantOnly(s.handleRevokeInvitation))
|
||||
|
||||
mux.HandleFunc("GET /api/reports/footfall", s.authed(s.handleFootfall))
|
||||
mux.HandleFunc("GET /api/reports/conversion", s.authed(s.handleConversion))
|
||||
mux.HandleFunc("GET /api/sites", s.authed(s.handleSites))
|
||||
mux.HandleFunc("POST /api/sites", s.authed(s.handleCreateSite))
|
||||
mux.HandleFunc("DELETE /api/sites/{site}", s.authed(s.handleDeleteSite))
|
||||
mux.HandleFunc("GET /api/reports/footfall", s.tenantOnly(s.handleFootfall))
|
||||
mux.HandleFunc("GET /api/reports/conversion", s.tenantOnly(s.handleConversion))
|
||||
mux.HandleFunc("GET /api/sites", s.tenantOnly(s.handleSites))
|
||||
mux.HandleFunc("POST /api/sites", s.tenantOnly(s.handleCreateSite))
|
||||
mux.HandleFunc("DELETE /api/sites/{site}", s.tenantOnly(s.handleDeleteSite))
|
||||
mux.HandleFunc("PATCH /api/sites/{site}", s.tenantOnly(s.handleUpdateSite))
|
||||
|
||||
// Cameras, onboarded from head office. The shop PC still does the
|
||||
// connecting - it is the only thing on the camera's network - so these
|
||||
// write desired state that its agent pulls and applies.
|
||||
mux.HandleFunc("GET /api/cameras", s.authed(s.handleCameras))
|
||||
mux.HandleFunc("POST /api/sites/{site}/cameras", s.authed(s.handleCreateCamera))
|
||||
mux.HandleFunc("PATCH /api/cameras/{id}", s.authed(s.handleUpdateCamera))
|
||||
mux.HandleFunc("DELETE /api/cameras/{id}", s.authed(s.handleDeleteCamera))
|
||||
mux.HandleFunc("GET /api/cameras/{id}/snapshot.jpg", s.authed(s.handleGetSnapshot))
|
||||
mux.HandleFunc("GET /api/cameras/{id}/live", s.authed(s.handleWatchLive))
|
||||
mux.HandleFunc("GET /api/cameras", s.tenantOnly(s.handleCameras))
|
||||
mux.HandleFunc("POST /api/sites/{site}/cameras", s.tenantOnly(s.handleCreateCamera))
|
||||
mux.HandleFunc("PATCH /api/cameras/{id}", s.tenantOnly(s.handleUpdateCamera))
|
||||
mux.HandleFunc("DELETE /api/cameras/{id}", s.tenantOnly(s.handleDeleteCamera))
|
||||
mux.HandleFunc("GET /api/cameras/{id}/snapshot.jpg", s.tenantOnly(s.handleGetSnapshot))
|
||||
mux.HandleFunc("GET /api/cameras/{id}/live", s.tenantOnly(s.handleWatchLive))
|
||||
// Prove a camera works: "connection" asks whether the shop PC can open the
|
||||
// stream, "placement" asks whether somebody walking past produces a view
|
||||
// good enough to recognise. Two questions, because a camera passes the
|
||||
// first and fails the second all the time - that is the Office1 case.
|
||||
mux.HandleFunc("POST /api/cameras/{id}/check", s.authed(s.handleRequestCheck))
|
||||
mux.HandleFunc("POST /api/cameras/{id}/check", s.tenantOnly(s.handleRequestCheck))
|
||||
// The end-to-end answer for one shop, assembled from what head office
|
||||
// already knows - so it works even when the shop PC is off, which is one of
|
||||
// the things it reports.
|
||||
mux.HandleFunc("GET /api/sites/{site}/check", s.authed(s.handleSiteCheck))
|
||||
mux.HandleFunc("GET /api/sites/{site}/check", s.tenantOnly(s.handleSiteCheck))
|
||||
mux.HandleFunc("POST /api/sites/{site}/enrolment-code",
|
||||
s.authed(s.handleIssueEnrolmentCode))
|
||||
s.tenantOnly(s.handleIssueEnrolmentCode))
|
||||
|
||||
// The assistant. Every tool it calls runs as the signed-in user, so it can
|
||||
// only ever see what the person asking could already see.
|
||||
mux.HandleFunc("POST /api/assistant", s.authed(s.handleAssistant))
|
||||
mux.HandleFunc("POST /api/assistant", s.tenantOnly(s.handleAssistant))
|
||||
|
||||
// The live feed. `visitors` searches a customer list by name; `visits`
|
||||
// answers the question a shop screen or a mobile app actually asks - who
|
||||
// came through the door just now - and carries each person's photo with
|
||||
// them so rendering four simultaneous arrivals is one request, not nine.
|
||||
mux.HandleFunc("GET /api/visits", s.authed(s.handleArrivals))
|
||||
mux.HandleFunc("GET /api/visits/stream", s.authed(s.handleArrivalStream))
|
||||
mux.HandleFunc("GET /api/visits", s.tenantOnly(s.handleArrivals))
|
||||
mux.HandleFunc("GET /api/visits/stream", s.tenantOnly(s.handleArrivalStream))
|
||||
|
||||
mux.HandleFunc("GET /api/visitors", s.authed(s.handleVisitors))
|
||||
mux.HandleFunc("GET /api/visitors/{id}/history", s.authed(s.handleVisitorHistory))
|
||||
mux.HandleFunc("PUT /api/visitors/{id}/profile", s.authed(s.handleSaveProfile))
|
||||
mux.HandleFunc("POST /api/purchases", s.authed(s.handlePurchase))
|
||||
mux.HandleFunc("GET /api/visitors", s.tenantOnly(s.handleVisitors))
|
||||
mux.HandleFunc("GET /api/visitors/{id}/history", s.tenantOnly(s.handleVisitorHistory))
|
||||
mux.HandleFunc("PUT /api/visitors/{id}/profile", s.tenantOnly(s.handleSaveProfile))
|
||||
|
||||
// A customer registered before any camera has seen them, and the repair
|
||||
// path that creates the need for: with no face template, recognition
|
||||
// cannot match them later and enrols them again.
|
||||
mux.HandleFunc("POST /api/customers", s.tenantOnly(s.handleCreateCustomer))
|
||||
mux.HandleFunc("POST /api/visitors/{id}/merge", s.tenantOnly(s.handleMergeCustomers))
|
||||
mux.HandleFunc("POST /api/purchases", s.tenantOnly(s.handlePurchase))
|
||||
|
||||
// Reading sales, not just aggregating them. /api/reports/conversion has
|
||||
// summed this table since it existed; nothing could read a row of it, so
|
||||
// "revenue was 41,000" could not be checked against a till.
|
||||
mux.HandleFunc("GET /api/sales", s.tenantOnly(s.handleSales))
|
||||
mux.HandleFunc("GET /api/sales/{id}", s.tenantOnly(s.handleSale))
|
||||
|
||||
// The merchant home screen in one call, composed from the functions the
|
||||
// reports already use rather than from new arithmetic.
|
||||
mux.HandleFunc("GET /api/dashboard/summary", s.tenantOnly(s.handleDashboard))
|
||||
|
||||
// Platform administration. Not public registration: an open endpoint that
|
||||
// mints tenants is a far larger thing to secure than one behind an account
|
||||
@@ -349,6 +386,17 @@ func (s *Server) Routes() *http.ServeMux {
|
||||
mux.HandleFunc("POST /api/admin/clients/{id}/owner-password", s.adminOnly(s.handleResetOwnerPassword))
|
||||
mux.HandleFunc("DELETE /api/admin/clients/{id}", s.adminOnly(s.handleDeleteClient))
|
||||
|
||||
// The admin drill-down: merchant -> shop -> camera. Read-only, scoped by
|
||||
// the merchant named in the path rather than by a session that has none,
|
||||
// with every read below the merchant list audited and cameras redacted to
|
||||
// a type that cannot carry an RTSP host or username.
|
||||
mux.HandleFunc("GET /api/admin/clients/{id}", s.adminOnly(s.handleAdminClient))
|
||||
mux.HandleFunc("GET /api/admin/clients/{id}/sites", s.adminOnly(s.handleAdminClientSites))
|
||||
mux.HandleFunc("GET /api/admin/clients/{id}/sites/{site}", s.adminOnly(s.handleAdminClientSite))
|
||||
mux.HandleFunc("GET /api/admin/clients/{id}/sites/{site}/cameras", s.adminOnly(s.handleAdminSiteCameras))
|
||||
mux.HandleFunc("GET /api/admin/clients/{id}/sites/{site}/cameras/{camera}", s.adminOnly(s.handleAdminSiteCamera))
|
||||
mux.HandleFunc("GET /api/admin/monitoring/summary", s.adminOnly(s.handleAdminMonitoringSummary))
|
||||
|
||||
// Not session-authenticated: this is how a PC with no credentials gets
|
||||
// some. The enrolment token is the credential.
|
||||
mux.HandleFunc("POST /api/agent/enrol", s.handleEnrol)
|
||||
@@ -367,15 +415,15 @@ func (s *Server) Routes() *http.ServeMux {
|
||||
mux.HandleFunc("GET /api/agent/checks", s.agentAuthed(s.handleAgentChecks))
|
||||
mux.HandleFunc("POST /api/agent/checks", s.agentAuthed(s.handleAgentCheckResult))
|
||||
|
||||
mux.HandleFunc("GET /api/visitors/{id}/image", s.authed(s.handleVisitorImage))
|
||||
mux.HandleFunc("GET /api/visitors/{id}/image", s.tenantOnly(s.handleVisitorImage))
|
||||
// The bytes of a face this server holds itself. Session-authenticated
|
||||
// rather than a signed link: there is no third party to delegate to, and an
|
||||
// unauthenticated URL would be a way to reach a customer's photograph with
|
||||
// no session at all.
|
||||
mux.HandleFunc("GET /api/faces/{id}", s.authed(s.handleGetFace))
|
||||
mux.HandleFunc("GET /api/faces/{id}", s.tenantOnly(s.handleGetFace))
|
||||
// The erasure path. Destroys the template and the photo; keeps the
|
||||
// anonymous visit counts, which are legitimate aggregate data.
|
||||
mux.HandleFunc("DELETE /api/visitors/{id}", s.authed(s.handleForgetVisitor))
|
||||
mux.HandleFunc("DELETE /api/visitors/{id}", s.tenantOnly(s.handleForgetVisitor))
|
||||
|
||||
return mux
|
||||
}
|
||||
@@ -393,6 +441,36 @@ func PrincipalFrom(ctx context.Context) auth.Principal {
|
||||
return p
|
||||
}
|
||||
|
||||
// tenantOnly gates the routes that read or write one company's data.
|
||||
//
|
||||
// It exists because a platform admin has NO client - that absence is what
|
||||
// defines them - and every tenant query scopes on `client_id = $1::uuid`.
|
||||
// Handing it the empty string makes Postgres cast ” to a uuid, which is an
|
||||
// ERROR rather than an empty result, so five live endpoints answered 500 to a
|
||||
// signed-in platform admin: /api/visits, /api/cameras, /api/sites,
|
||||
// /api/visitors and /api/reports/footfall. Found by calling them.
|
||||
//
|
||||
// 403 and not 404, unlike adminOnly. The two hide opposite things: a tenant
|
||||
// must not learn that a platform surface exists, while a platform admin
|
||||
// already knows the tenant surface does - they are looking at its data through
|
||||
// /api/admin. Nothing is concealed by pretending otherwise, and "use the admin
|
||||
// routes" is the useful answer.
|
||||
//
|
||||
// Guarding here rather than in each query is deliberate: a per-query fix is
|
||||
// one a new query forgets, and the next one would 500 in production exactly
|
||||
// like these did.
|
||||
func (s *Server) tenantOnly(next http.HandlerFunc) http.HandlerFunc {
|
||||
return s.authed(func(w http.ResponseWriter, r *http.Request) {
|
||||
if PrincipalFrom(r.Context()).ClientID == "" {
|
||||
writeErr(w, http.StatusForbidden, "not_a_tenant_account",
|
||||
"This is a company's own data. A platform administrator "+
|
||||
"reads it through /api/admin/clients/{id}/...")
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) authed(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tok := auth.BearerToken(r)
|
||||
@@ -542,6 +620,11 @@ func looksLikeUUID(s string) bool {
|
||||
// without importing the store package.
|
||||
var ErrNoSecrets = errors.New("this server has no encryption key, so camera passwords cannot be stored")
|
||||
|
||||
// ErrSameVisitor is a merge that names one customer twice. Declared here
|
||||
// rather than in the store for the reason ErrNoSecrets is: the store imports
|
||||
// this package, so a sentinel the other way round is an import cycle.
|
||||
var ErrSameVisitor = errors.New("a customer cannot be merged into themselves")
|
||||
|
||||
// ErrNoSnapshot means a camera has no stored picture. An ordinary state - a
|
||||
// camera added a minute ago has none - so it is reported as absence, never as
|
||||
// a failure.
|
||||
|
||||
96
server/internal/api/camera_state_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ptr(b bool) *bool { return &b }
|
||||
|
||||
// The state this was written for. Two cameras read "Connected", in green, on
|
||||
// the live estate thirty-four minutes after the shop computer had stopped
|
||||
// being able to see either of them - because the agent correctly reports
|
||||
// nothing when it cannot reach the engine, and the last value it sent stays
|
||||
// in the database looking current.
|
||||
func TestAStaleReportIsNotAConnectedCamera(t *testing.T) {
|
||||
now := time.Date(2026, 9, 30, 11, 13, 0, 0, time.UTC)
|
||||
c := Camera{Connected: ptr(true),
|
||||
LastSeenAt: now.Add(-34 * time.Minute).Format(time.RFC3339)}
|
||||
c.CameraState(now)
|
||||
|
||||
if c.State != CameraStale {
|
||||
t.Errorf("state = %q, want %q", c.State, CameraStale)
|
||||
}
|
||||
// Cleared, not merely overruled. A stale true left in place stays
|
||||
// available to every client that reads the field directly, and leaves two
|
||||
// fields on one object disagreeing.
|
||||
if c.Connected != nil {
|
||||
t.Errorf("connected = %v, want null - nobody currently knows", *c.Connected)
|
||||
}
|
||||
if c.StateNote == "" {
|
||||
t.Error("a stale camera said nothing about what to do")
|
||||
}
|
||||
}
|
||||
|
||||
// One missed report is a dropped packet. Warning on it would put an alarm on a
|
||||
// healthy estate every few minutes, and an indicator that cries wolf is one
|
||||
// people learn to ignore.
|
||||
func TestOneMissedReportIsStillConnected(t *testing.T) {
|
||||
now := time.Now()
|
||||
c := Camera{Connected: ptr(true),
|
||||
LastSeenAt: now.Add(-90 * time.Second).Format(time.RFC3339)}
|
||||
c.CameraState(now)
|
||||
if c.State != CameraConnected {
|
||||
t.Errorf("state = %q after 90s, want %q", c.State, CameraConnected)
|
||||
}
|
||||
if c.Connected == nil || !*c.Connected {
|
||||
t.Error("a fresh report lost its connected flag")
|
||||
}
|
||||
}
|
||||
|
||||
// "Nobody has ever told us" and "nobody has told us lately" need different
|
||||
// sentences: the first is a camera head office added a minute ago and the
|
||||
// shop computer has not picked up, the second is a shop computer that has
|
||||
// stopped. Sending an installer to the wrong one wastes a journey.
|
||||
func TestNeverReportedIsNotTheSameAsStopped(t *testing.T) {
|
||||
now := time.Now()
|
||||
var never Camera
|
||||
never.CameraState(now)
|
||||
if never.State != CameraWaiting {
|
||||
t.Errorf("state = %q, want %q", never.State, CameraWaiting)
|
||||
}
|
||||
|
||||
stopped := Camera{LastSeenAt: now.Add(-time.Hour).Format(time.RFC3339)}
|
||||
stopped.CameraState(now)
|
||||
if stopped.State == never.State {
|
||||
t.Fatal("a camera nobody has reported and one that stopped read the same")
|
||||
}
|
||||
if stopped.StateNote == never.StateNote {
|
||||
t.Error("two states that need different actions gave the same advice")
|
||||
}
|
||||
}
|
||||
|
||||
// A camera the shop computer CAN see and cannot open is the one case where
|
||||
// "check the cabling" is the right advice, and it must stay distinguishable
|
||||
// from the three where it is not.
|
||||
func TestAFreshFailureSaysCheckTheCamera(t *testing.T) {
|
||||
now := time.Now()
|
||||
c := Camera{Connected: ptr(false), LastSeenAt: now.Format(time.RFC3339)}
|
||||
c.CameraState(now)
|
||||
if c.State != CameraNotConnect {
|
||||
t.Errorf("state = %q, want %q", c.State, CameraNotConnect)
|
||||
}
|
||||
if c.Connected == nil || *c.Connected {
|
||||
t.Error("a reported failure must stay false, not become unknown")
|
||||
}
|
||||
}
|
||||
|
||||
// An unparseable timestamp is not a working camera. It should not be possible,
|
||||
// which is exactly why it must not fall through to "connected".
|
||||
func TestAnUnreadableTimestampIsStale(t *testing.T) {
|
||||
c := Camera{Connected: ptr(true), LastSeenAt: "not a time"}
|
||||
c.CameraState(time.Now())
|
||||
if c.State != CameraStale || c.Connected != nil {
|
||||
t.Errorf("state = %q connected = %v, want stale and unknown", c.State, c.Connected)
|
||||
}
|
||||
}
|
||||
148
server/internal/api/customers_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStaffCanRegisterACustomerNobodyHasPhotographed(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedUser(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
rec := do(t, s, "POST", "/api/customers", sess.Token, map[string]string{
|
||||
"full_name": "Asha Menon", "phone": "9876543210",
|
||||
})
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var c Customer
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// A reference a person can say, from the same counter the engine uses.
|
||||
if c.Ref == "" || c.Label != "Asha Menon" {
|
||||
t.Errorf("got ref=%q label=%q, want a V- reference and the typed name",
|
||||
c.Ref, c.Label)
|
||||
}
|
||||
}
|
||||
|
||||
// A record with no name and no phone is a number nobody can search for, and
|
||||
// the customer at the counter is the only source of either.
|
||||
func TestACustomerNeedsANameOrAPhone(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedUser(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
rec := do(t, s, "POST", "/api/customers", sess.Token,
|
||||
map[string]string{"notes": "regular, likes the window seat"})
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("got %d, want 400: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- merge
|
||||
|
||||
// Uuid-shaped on purpose: resolveVisitor takes a uuid or a V- reference and
|
||||
// correctly refuses anything else, so a made-up id would 404 before reaching
|
||||
// the handler under test.
|
||||
const (
|
||||
vTyped = "aaaaaaaa-1111-4111-8111-aaaaaaaaaaaa" // typed in at the counter
|
||||
vSeen = "bbbbbbbb-2222-4222-8222-bbbbbbbbbbbb" // enrolled by a camera
|
||||
)
|
||||
|
||||
func seedTwoCustomers(fs *fakeStore) {
|
||||
seedUser(fs)
|
||||
fs.visitors = []Customer{
|
||||
{ID: vTyped, Ref: "V-1", Label: "Asha Menon", FullName: "Asha Menon"},
|
||||
{ID: vSeen, Ref: "V-2", Label: "Visitor 2"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergingFoldsOneCustomerIntoTheOtherAndSaysWhatMoved(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoCustomers(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
rec := do(t, s, "POST", "/api/visitors/"+vTyped+"/merge", sess.Token,
|
||||
map[string]string{"into": vSeen})
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var out MergeResult
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The reference that STOPPED resolving has to be named. Staff write these
|
||||
// on cards; discovering it at a counter is the wrong place to find out.
|
||||
if out.RetiredRef != "V-1" || out.Ref != "V-2" {
|
||||
t.Errorf("kept %q retired %q, want V-2 kept and V-1 retired", out.Ref, out.RetiredRef)
|
||||
}
|
||||
}
|
||||
|
||||
// The only irreversible operation on a customer apart from erasure. Two people
|
||||
// welded together cannot be separated: nothing records which visit came from
|
||||
// whom.
|
||||
func TestStaffCannotMerge(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoCustomers(fs)
|
||||
fs.addUser("shopfloor@acme.com", "correct horse battery", UserRecord{
|
||||
ID: "u9", ClientID: "client-acme", Role: "staff", Active: true,
|
||||
})
|
||||
sess := login(t, s, "shopfloor@acme.com", "correct horse battery")
|
||||
|
||||
rec := do(t, s, "POST", "/api/visitors/"+vTyped+"/merge", sess.Token,
|
||||
map[string]string{"into": vSeen})
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Errorf("got %d, want 403 for staff: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergingACustomerIntoThemselvesIsRefused(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoCustomers(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
rec := do(t, s, "POST", "/api/visitors/"+vTyped+"/merge", sess.Token,
|
||||
map[string]string{"into": vTyped})
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("got %d, want 400: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergingNeedsATarget(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoCustomers(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
for _, body := range []map[string]string{{}, {"into": " "}, {"into": "V-999"}} {
|
||||
rec := do(t, s, "POST", "/api/visitors/"+vTyped+"/merge", sess.Token, body)
|
||||
if rec.Code == http.StatusOK {
|
||||
t.Errorf("merge with %v succeeded, want a refusal", body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Every merge leaves a trace: it is destructive and cannot be undone.
|
||||
func TestAMergeIsAudited(t *testing.T) {
|
||||
s, fs := newServer(t)
|
||||
seedTwoCustomers(fs)
|
||||
sess := login(t, s, "manager@acme.com", "correct horse battery")
|
||||
|
||||
do(t, s, "POST", "/api/visitors/"+vTyped+"/merge", sess.Token,
|
||||
map[string]string{"into": vSeen})
|
||||
|
||||
found := false
|
||||
for _, a := range fs.audits {
|
||||
if a.Action == "customer.merge" {
|
||||
found = true
|
||||
if a.Detail["retired_ref"] != "V-1" {
|
||||
t.Errorf("audit must name the retired reference: %+v", a.Detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("no audit row for a merge")
|
||||
}
|
||||
}
|
||||
@@ -41,13 +41,29 @@ type fakeStore struct {
|
||||
byAccess map[string]string // access hash hex -> session id
|
||||
byRefresh map[string]string
|
||||
|
||||
visitors []Customer
|
||||
history []VisitRow
|
||||
footfall []FootfallPoint
|
||||
totals Totals
|
||||
sales SalesReport
|
||||
sites []SiteHealth
|
||||
enrolment map[string]Enrolment
|
||||
visitors []Customer
|
||||
history []VisitRow
|
||||
footfall []FootfallPoint
|
||||
totals Totals
|
||||
sales SalesReport
|
||||
sites []SiteHealth
|
||||
|
||||
// The admin drill-down is the one surface where the fake MUST know which
|
||||
// merchant owns what. Everywhere else the client id comes from the session
|
||||
// and every query scopes on it, so a fake that ignores it still exercises
|
||||
// the handler. Here the client id comes from the PATH and the scoping is
|
||||
// the thing under test - a fake that ignored it would pass the
|
||||
// cross-merchant tests while returning another company's shops.
|
||||
// Camera ownership already has a home: cameraRefs, read through the
|
||||
// cameraOwner method below.
|
||||
siteOwner map[string]string // site id -> client id
|
||||
salesRows []Sale
|
||||
visitorSeq int64
|
||||
lastMerge [2]string
|
||||
saleOwner map[string]string // sale id -> client id
|
||||
lastSaleQuery SaleQuery
|
||||
clientRows map[string]ClientDetail
|
||||
enrolment map[string]Enrolment
|
||||
|
||||
// Recorded calls, so a test can assert what the handler asked for rather
|
||||
// than only what it returned.
|
||||
@@ -288,8 +304,161 @@ func (f *fakeStore) DeleteNewSite(_ context.Context, _ string, siteID string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) SiteHealth(_ context.Context, _ string) ([]SiteHealth, error) {
|
||||
return f.sites, nil
|
||||
func (f *fakeStore) SiteHealth(_ context.Context, clientID string) ([]SiteHealth, error) {
|
||||
// Scoped only when a test has declared ownership; otherwise every existing
|
||||
// tenant test would have to grow a fixture it does not care about.
|
||||
if f.siteOwner == nil {
|
||||
return f.sites, nil
|
||||
}
|
||||
var out []SiteHealth
|
||||
for _, s := range f.sites {
|
||||
if f.siteOwner[s.SiteID] == clientID {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CreateCustomer(_ context.Context, clientID string,
|
||||
in Profile, createdBy string) (Customer, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.visitorSeq++
|
||||
label := in.FullName
|
||||
if label == "" {
|
||||
label = fmt.Sprintf("Visitor %d", f.visitorSeq)
|
||||
}
|
||||
c := Customer{
|
||||
ID: fmt.Sprintf("new-%d", f.visitorSeq), Ref: VisitorRef(f.visitorSeq),
|
||||
Label: label, FullName: in.FullName, Phone: in.Phone, Email: in.Email,
|
||||
HasProfile: true,
|
||||
}
|
||||
f.visitors = append(f.visitors, c)
|
||||
f.lastProfile = in
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) MergeVisitors(_ context.Context, clientID, sourceID, targetID string) (
|
||||
MergeResult, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.lastMerge = [2]string{sourceID, targetID}
|
||||
if sourceID == targetID {
|
||||
return MergeResult{}, ErrSameVisitor
|
||||
}
|
||||
var src, dst *Customer
|
||||
for i := range f.visitors {
|
||||
switch f.visitors[i].ID {
|
||||
case sourceID:
|
||||
src = &f.visitors[i]
|
||||
case targetID:
|
||||
dst = &f.visitors[i]
|
||||
}
|
||||
}
|
||||
if src == nil || dst == nil {
|
||||
return MergeResult{}, pgx.ErrNoRows
|
||||
}
|
||||
out := MergeResult{VisitorID: dst.ID, Ref: dst.Ref, Label: dst.Label,
|
||||
RetiredRef: src.Ref}
|
||||
var kept []Customer
|
||||
for _, c := range f.visitors {
|
||||
if c.ID != sourceID {
|
||||
kept = append(kept, c)
|
||||
}
|
||||
}
|
||||
f.visitors = kept
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) SetUserPassword(_ context.Context, userID, hash string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for email, u := range f.users {
|
||||
if u.ID == userID {
|
||||
u.PasswordHash = hash
|
||||
f.users[email] = u
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("no such user")
|
||||
}
|
||||
|
||||
func (f *fakeStore) Sales(_ context.Context, q SaleQuery) ([]Sale, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.lastSaleQuery = q
|
||||
var out []Sale
|
||||
for _, sale := range f.salesRows {
|
||||
if q.SiteID != "" && sale.SiteID != q.SiteID {
|
||||
continue
|
||||
}
|
||||
if q.VisitorID != "" && sale.VisitorID != q.VisitorID {
|
||||
continue
|
||||
}
|
||||
out = append(out, sale)
|
||||
}
|
||||
if q.Limit > 0 && len(out) > q.Limit {
|
||||
out = out[:q.Limit]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Sale(_ context.Context, clientID, id string) (Sale, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for _, sale := range f.salesRows {
|
||||
// Scoped, so the cross-tenant test is not vacuous.
|
||||
if sale.ID == id && f.saleOwner[id] == clientID {
|
||||
return sale, nil
|
||||
}
|
||||
}
|
||||
return Sale{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) ClientDetail(_ context.Context, clientID string) (ClientDetail, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.clientRows[clientID], nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) AdminSiteID(_ context.Context, clientID, ref string) (string, error) {
|
||||
for _, s := range f.sites {
|
||||
if s.SiteID != ref && s.Slug != ref {
|
||||
continue
|
||||
}
|
||||
if f.siteOwner != nil && f.siteOwner[s.SiteID] != clientID {
|
||||
return "", nil // owned by somebody else: a miss, not a match
|
||||
}
|
||||
return s.SiteID, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) AdminCameraID(_ context.Context, clientID, siteID, ref string) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for _, c := range f.cameras {
|
||||
if c.ID != ref && c.CameraID != ref {
|
||||
continue
|
||||
}
|
||||
if c.SiteID != siteID {
|
||||
return "", nil
|
||||
}
|
||||
if f.cameraRefs != nil && f.cameraOwner(c.ID) != clientID {
|
||||
return "", nil
|
||||
}
|
||||
return c.ID, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) PlatformSummary(_ context.Context) (PlatformSummary, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return PlatformSummary{
|
||||
CamerasTotal: len(f.cameras), MerchantsActive: len(f.clientRows),
|
||||
SitesTotal: len(f.sites), AsOf: "2026-09-28T00:00:00Z",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) SearchVisitors(_ context.Context, clientID, q string, limit int) (
|
||||
@@ -554,6 +723,23 @@ func (f *fakeStore) DeleteClient(_ context.Context, clientID string) (ClientRow,
|
||||
return ClientRow{}, nil, pgx.ErrNoRows
|
||||
}
|
||||
|
||||
func (f *fakeStore) UpdateSite(_ context.Context, _ string, siteID string, in SiteUpdate) (SiteHealth, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for i := range f.sites {
|
||||
if f.sites[i].SiteID == siteID {
|
||||
if in.Name != nil {
|
||||
f.sites[i].Name = *in.Name
|
||||
}
|
||||
if in.Timezone != nil {
|
||||
f.sites[i].Timezone = *in.Timezone
|
||||
}
|
||||
return f.sites[i], nil
|
||||
}
|
||||
}
|
||||
return SiteHealth{}, pgx.ErrNoRows
|
||||
}
|
||||
|
||||
func (f *fakeStore) DeleteEmptySite(_ context.Context, _ string, siteID string) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||