Skip to main content
Glama
Pointer Service
by Snille
README.md
# Pointer Service

A small service that knows **where things are in a workshop** and **points at them** with laser
pointers. Any system can ask it to "point at X": a storage system, Home Assistant, a voice agent,
a game, or an AI agent over MCP.

- **Pointers**: ESPHome laser pointers (pan/tilt servos + laser). Any number of them.
- **Targets**: a name for a place. A Storage System `location_id` (for example `IVAR-A-H3-P2`)
  or a free name (`lodstation`).
- **Aims**: for each target, one or more (pointer, pan, tilt), in a preferred order.

`point_at` uses the first **online** pointer in the target's order. With `all: true` every
online pointer that has an aim lights the target.

## Run

Needs Node.js 24.14 or newer. No dependencies.

```sh
cp .env.example .env     # POINTER_DRIVER=fake works without hardware
npm run dev              # http://localhost:3240
npm run check            # syntax, tests, git diff --check
```

Docker, local build:

```sh
docker compose up --build
```

## Configuration

| Variable | Default | Meaning |
| --- | --- | --- |
| `PORT` | `3240` | HTTP port. |
| `POINTER_DB` | `./data/pointer-service.sqlite` | SQLite file (`/data/...` in the image). |
| `POINTER_DRIVER` | `ha` | Driver for new pointers: `ha` or `fake`. Each pointer stores its own driver. |
| `POINTER_API_KEY` | empty | When set, `/api/*` and `/mcp` need it in `x-api-key` or `Authorization: Bearer`. |
| `HA_URL`, `HA_TOKEN` | empty | Home Assistant URL and long-lived access token for the `ha` driver. |
| `STORAGESYSTEM_URL`, `STORAGESYSTEM_KEY` | empty | Storage System Base URL and management key, for the location import. |

## Drivers

- **fake**: no hardware. Keeps the last angles in memory. For tests and UI work.
- **ha**: calls the ESPHome actions through the Home Assistant REST API.
  - `haDevice` = the ESPHome node name: `laser_pointer_01` → `esphome.laser_pointer_01_point_to`
    (`pan`, `tilt`) and `esphome.laser_pointer_01_laser_off`.
  - `haEntity` = the entity id prefix: `workshop_study_laser_pointer_01` → the online check reads
    `switch.workshop_study_laser_pointer_01_laser`. `unavailable` or `unknown` means offline.
    Without `haEntity` the pointer counts as online.

A direct driver (ESPHome web_server or native API) can be added later behind the same interface
(`move`, `off`, `status` in `src/drivers.js`).

## Teach UI

Open `/`. Add the pointers in the Pointers panel. Then:

1. Pick a target (or press **Import from Storage System**).
2. Pick a pointer. With **Live** on, the dot follows the sliders. Arrow keys move it 0.5° (Shift: 2°).
3. Press **Save aim**. Order several aims with ↑ ↓.

## REST API

| Method and path | Body | Does |
| --- | --- | --- |
| `GET /healthz` | | Health and version. No key needed. |
| `GET /api/pointers` | | Pointers with online status. |
| `POST /api/pointers` | `{id, name?, driver?, haDevice?, haEntity?, enabled?}` | Add or update a pointer. |
| `DELETE /api/pointers/:id` | | Delete a pointer and its aims. |
| `GET /api/targets?query=` | | Targets with their aims. |
| `POST /api/targets` | `{name, title?}` | Add a target. |
| `GET` / `DELETE /api/targets/:name` | | Read or delete one target (URL-encode the name). |
| `PUT /api/targets/:name/aims/:pointer` | `{pan, tilt, priority?}` | Save an aim. |
| `DELETE /api/targets/:name/aims/:pointer` | | Delete an aim. |
| `POST /api/point-at` | `{target, all?}` | Point at a target. 404 unknown, 409 untaught, 503 no pointer reached it. |
| `POST /api/point-raw` | `{pointer, pan, tilt}` | Move to angles (0–180, step 0.5) and switch the laser on. |
| `POST /api/laser-off` | `{pointer?}` | One laser off, or all. |
| `POST /api/import/storagesystem` | | Import every Storage System location as a target. Keeps aims. |

Browsers from another origin are refused, so a web page cannot drive the lasers.

## MCP

Streamable HTTP at `POST /mcp` (JSON answers). Tools: `list_targets`, `list_pointers`,
`point_at`, `point_raw`, `laser_off`.

```sh
claude mcp add --transport http --scope user pointer-service http://<host>:3240/mcp
```

## Storage System link (Home Assistant)

Storage System needs no change. Home Assistant calls `point_at` when a search has one result:

```yaml
# configuration.yaml
rest_command:
  pointer_point_at:
    url: "http://<host>:3240/api/point-at"
    method: POST
    content_type: application/json
    payload: '{"target": "{{ target }}"}'
```

```yaml
# automation
triggers:
  - trigger: event
    event_type: storagesystem_result
conditions:
  - condition: template
    value_template: "{{ trigger.event.data.location_id | default('') != '' }}"
actions:
  - action: rest_command.pointer_point_at
    data:
      target: "{{ trigger.event.data.location_id }}"
```

## Deploy (Portainer)

1. Bump `VERSION`, add the entry to `docs/HISTORY.md`, run `npm run check` and commit.
2. Tag and push: `git tag v<version>` and `git push origin main v<version>`. GitHub Actions runs the tests,
   publishes `ghcr.io/snille/pointer-service:<version>` and `:latest`, and creates the release.
3. In Portainer, create or update the stack from `compose.portainer.yaml`. Set `HA_URL`, `HA_TOKEN`
   and the other variables in the stack environment.
4. Check `GET /healthz`.

## Later

- Aims for box ids, so a box with its own position wins over its location.
- A direct driver (ESPHome web_server REST or native API), if the Docker host can reach the IoT network.

## Version

`VERSION` is the only version source. Every change that bumps it adds an entry to
[docs/HISTORY.md](docs/HISTORY.md) in the same commit.