tauri-plugin-weighing-scale 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +459 -2
- package/dist-js/index.cjs +89 -0
- package/dist-js/index.d.ts +148 -0
- package/dist-js/index.js +73 -0
- package/package.json +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luis Andres Gonzalez Corzo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,460 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Tauri Plugin weighing-scale
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Read a weighing scale from a Tauri app: the scales of shops, delis and markets, and generic
|
|
4
|
+
weighing indicators. You plug the scale in, the plugin finds out which scale it is, and your app
|
|
5
|
+
gets the weight every time it changes, with its state: stable or in motion, net, zero, negative,
|
|
6
|
+
overload.
|
|
7
|
+
|
|
8
|
+
Scales don't share one protocol, so each protocol is a **driver**: a JSON file in
|
|
9
|
+
[`drivers/`](drivers). Six come built in, and the app can download new ones without being
|
|
10
|
+
updated (see [Updating the drivers](#updating-the-drivers)).
|
|
11
|
+
|
|
12
|
+
| Platform | How the scale connects |
|
|
13
|
+
| -------- | ---------------------- |
|
|
14
|
+
| Windows, macOS, Linux | RS-232 serial port, USB-serial adapter (CH340, FTDI, PL2303, CP210x), USB scale seen as a serial port, Bluetooth paired as a serial port |
|
|
15
|
+
| Android | USB (OTG cable): USB scales and USB-serial adapters. Bluetooth classic (paired, serial profile). Bluetooth LE serial modules |
|
|
16
|
+
| iOS | Bluetooth LE serial modules only: iOS doesn't let apps open USB-serial adapters or Bluetooth classic devices |
|
|
17
|
+
|
|
18
|
+
## Contents
|
|
19
|
+
|
|
20
|
+
- [Quick start](#quick-start)
|
|
21
|
+
- [Try it without a scale](#try-it-without-a-scale)
|
|
22
|
+
- [How your app uses it](#how-your-app-uses-it)
|
|
23
|
+
- [Updating the drivers](#updating-the-drivers)
|
|
24
|
+
- [Platform setup](#platform-setup)
|
|
25
|
+
- [API reference](#api-reference)
|
|
26
|
+
- [Built-in drivers](#built-in-drivers)
|
|
27
|
+
- [Writing a Driver](#writing-a-driver)
|
|
28
|
+
- [Rust API](#rust-api)
|
|
29
|
+
- [Releasing (maintainers)](#releasing-maintainers)
|
|
30
|
+
- [Repository layout](#repository-layout)
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
**1. Add the plugin** to your Tauri app:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
cargo add tauri-plugin-weighing-scale --manifest-path src-tauri/Cargo.toml
|
|
38
|
+
bun add tauri-plugin-weighing-scale # or npm / pnpm
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**2. Register it** in `src-tauri/src/lib.rs`:
|
|
42
|
+
|
|
43
|
+
```rust
|
|
44
|
+
tauri::Builder::default()
|
|
45
|
+
.plugin(tauri_plugin_weighing_scale::init())
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**3. Allow it** in `src-tauri/capabilities/default.json`:
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{ "permissions": ["core:default", "weighing-scale:default"] }
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**4. Read the scale** from your frontend:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { identify, connect, onReading, isUsable } from 'tauri-plugin-weighing-scale'
|
|
58
|
+
|
|
59
|
+
// Which scale is plugged in, and where?
|
|
60
|
+
const [scale] = await identify()
|
|
61
|
+
if (!scale) throw new Error('No scale found')
|
|
62
|
+
|
|
63
|
+
// Start reading it. The plugin keeps reading in the background and reconnects on its own.
|
|
64
|
+
await connect(scale.port, scale.driverId)
|
|
65
|
+
|
|
66
|
+
// Every time the weight changes:
|
|
67
|
+
await onReading(({ reading }) => {
|
|
68
|
+
console.log(reading.weight?.kg, reading.stable ? 'stable' : 'moving')
|
|
69
|
+
if (isUsable(reading)) {
|
|
70
|
+
// stable, above zero, not overloaded: good to put on a sale
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
That's all. On Linux the user must be allowed to open serial ports, and on Android and iOS there
|
|
76
|
+
are permissions: see [Platform setup](#platform-setup).
|
|
77
|
+
|
|
78
|
+
## Try it without a scale
|
|
79
|
+
|
|
80
|
+
The repository has a **simulator**: a window with a drawn scale that behaves like a real one on a
|
|
81
|
+
virtual serial port. You drag boxes onto its plate; it answers your app in the protocol of the
|
|
82
|
+
driver you pick. It's a test tool, not production code.
|
|
83
|
+
|
|
84
|
+
1. **Start the simulator** from the repository:
|
|
85
|
+
```bash
|
|
86
|
+
cd simulator && bun i && bun tauri dev
|
|
87
|
+
```
|
|
88
|
+
2. **Pick a driver** in the simulator ("Driver" box), for example *Systel — W*. The scale now
|
|
89
|
+
speaks that protocol.
|
|
90
|
+
3. **Note its port** ("Port" box):
|
|
91
|
+
- Linux and macOS: `/tmp/weighing-scale-simulator` (it's a pseudo-terminal; the path stays
|
|
92
|
+
the same every time).
|
|
93
|
+
- Windows: install [com0com](https://com0com.sourceforge.net/), which creates two COM ports
|
|
94
|
+
wired to each other (say `COM10` ↔ `COM11`). Open `COM10` in the simulator; your app uses
|
|
95
|
+
`COM11`.
|
|
96
|
+
4. **Point your app at that port.** The system doesn't list virtual ports, so pass it:
|
|
97
|
+
```ts
|
|
98
|
+
const [scale] = await identify({ ports: ['/tmp/weighing-scale-simulator'] })
|
|
99
|
+
```
|
|
100
|
+
Or use the [example app](examples/tauri-app): type the path in **Other port**, press
|
|
101
|
+
**Use**, then **Recognise** and **Connect**.
|
|
102
|
+
5. **Drag a box onto the plate.** Your app sees the weight move, then settle as *stable*. Press
|
|
103
|
+
**Tare** in your app and the simulator tares (with drivers that have a tare command). The
|
|
104
|
+
simulator's **Traffic** box shows every byte both sides send.
|
|
105
|
+
|
|
106
|
+
To try a driver you are writing, use **Try a driver file…** in the simulator and load the same
|
|
107
|
+
file in your app with `loadDrivers`.
|
|
108
|
+
|
|
109
|
+
## How your app uses it
|
|
110
|
+
|
|
111
|
+
A point of sale doesn't need to search for the scale every time it opens. The usual flow:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
First time (a "Scale" settings screen) Every start
|
|
115
|
+
─────────────────────────────────────── ─────────────────────────────
|
|
116
|
+
identify() → [{ port, driverId, … }] connect(saved.port, saved.driverId)
|
|
117
|
+
user confirms the weight shown onReading(...) → show the weight
|
|
118
|
+
save { port, driverId, driverVersion } tare(port) when the cashier asks
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import {
|
|
123
|
+
identify, connect, onReading, onStatus, tare, isUsable, type Reading
|
|
124
|
+
} from 'tauri-plugin-weighing-scale'
|
|
125
|
+
|
|
126
|
+
// Settings screen: find the scale and remember it.
|
|
127
|
+
async function findScale() {
|
|
128
|
+
const found = await identify() // USB ports (and Bluetooth on phones)
|
|
129
|
+
if (found.length === 0) return null // not plugged in, or no driver for it
|
|
130
|
+
const { port, driverId, driverVersion, reading } = found[0]
|
|
131
|
+
localStorage.setItem('scale', JSON.stringify({ port, driverId, driverVersion }))
|
|
132
|
+
return reading // show it, so the user sees it's the right one
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// App start: connect to the saved scale.
|
|
136
|
+
async function startScale(show: (r: Reading) => void) {
|
|
137
|
+
const saved = JSON.parse(localStorage.getItem('scale') ?? 'null')
|
|
138
|
+
if (!saved) return
|
|
139
|
+
await connect(saved.port, saved.driverId)
|
|
140
|
+
await onReading(({ reading }) => show(reading))
|
|
141
|
+
await onStatus(({ state, error }) => {
|
|
142
|
+
// "connecting" | "connected" | "silent" (plugged in, not answering)
|
|
143
|
+
// | "disconnected" (unplugged: the plugin keeps retrying)
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Selling by weight: take the weight only when it's usable.
|
|
148
|
+
function weightForSale(reading: Reading): number | null {
|
|
149
|
+
return isUsable(reading) ? reading.weight!.kg : null
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Things to know:
|
|
154
|
+
|
|
155
|
+
- **`identify()` only tries USB ports** by default (and Bluetooth ones on Android and iOS).
|
|
156
|
+
Sending bytes to every serial port of a computer is slow and may bother other devices. For a
|
|
157
|
+
scale on a built-in RS-232 port, pass it: `identify({ ports: ['COM1'] })`.
|
|
158
|
+
- **The plugin reconnects on its own.** If the scale is unplugged, `onStatus` says
|
|
159
|
+
`disconnected`, and readings come back when it is plugged in again. You don't call
|
|
160
|
+
`connect` again.
|
|
161
|
+
- **`onReading` fires only when something changes**, not on every answer from the scale.
|
|
162
|
+
- **The weight is always also in kg** (`reading.weight.kg`), whatever unit the scale uses.
|
|
163
|
+
|
|
164
|
+
## Updating the drivers
|
|
165
|
+
|
|
166
|
+
The built-in drivers are those of the plugin version your app was built with. To get drivers
|
|
167
|
+
published later (new scales, fixes) without updating your app:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { updateDrivers } from 'tauri-plugin-weighing-scale'
|
|
171
|
+
|
|
172
|
+
const update = await updateDrivers()
|
|
173
|
+
// { version: "0.3.0", drivers: [{ id: "toledo-8217", version: 2 }, { id: "x", error: "…" }] }
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
It downloads `catalog.json` from the plugin's latest GitHub release, **checks its signature**
|
|
177
|
+
(only catalogs signed by the plugin's maintainers are accepted: the public keys are in
|
|
178
|
+
[`catalog-keys.txt`](catalog-keys.txt) and built into the plugin), loads the drivers and keeps the
|
|
179
|
+
file in the app's data folder, so the next start has them without internet. Call it once a day,
|
|
180
|
+
or from a "Check for new drivers" button. Without internet it rejects, and the drivers kept from
|
|
181
|
+
last time stay. A driver
|
|
182
|
+
never replaces a newer one already installed, and a driver made for a newer plugin is skipped.
|
|
183
|
+
|
|
184
|
+
## Platform setup
|
|
185
|
+
|
|
186
|
+
**Linux.** The user must be allowed to open serial ports: `sudo usermod -aG dialout $USER`
|
|
187
|
+
(Fedora and Arch: group `dialout` or `uucp`), then log out and in. To build, install the udev
|
|
188
|
+
headers: `libudev-dev` (Debian/Ubuntu) or `systemd-devel` (Fedora).
|
|
189
|
+
|
|
190
|
+
**Windows.** USB-serial adapters need their driver (CH340, FTDI, Prolific); the scale then
|
|
191
|
+
appears as `COM3`, `COM4`…
|
|
192
|
+
|
|
193
|
+
**macOS.** Nothing to set up. Ports are `/dev/cu.*`. Older macOS versions may need the CH340
|
|
194
|
+
driver.
|
|
195
|
+
|
|
196
|
+
**Android.** Nothing to add to your app: the plugin brings its permissions.
|
|
197
|
+
- **USB**: the scale or adapter goes in with an OTG cable. The first time the app opens it,
|
|
198
|
+
Android asks the user to allow it.
|
|
199
|
+
- **Bluetooth classic**: pair the scale in Android's Bluetooth settings first; it is listed as
|
|
200
|
+
`bt:<address>`.
|
|
201
|
+
- **Bluetooth LE**: listed as `ble:<address>` when it is near and on.
|
|
202
|
+
- The first `listPorts` or `identify` asks for the Bluetooth permission (Android 12+) or
|
|
203
|
+
location (Android 11 and older, only to find Bluetooth LE devices). If the user refuses, USB
|
|
204
|
+
still works.
|
|
205
|
+
- The plugin uses [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android),
|
|
206
|
+
from JitPack; the plugin adds JitPack to your Gradle build by itself.
|
|
207
|
+
|
|
208
|
+
**iOS.** Only Bluetooth LE scales (serial modules such as HM-10 or Nordic UART), listed as
|
|
209
|
+
`ble:<id>`. Add to `src-tauri/Info.ios.plist` (or your Info.plist):
|
|
210
|
+
|
|
211
|
+
```xml
|
|
212
|
+
<key>NSBluetoothAlwaysUsageDescription</key>
|
|
213
|
+
<string>To read the weighing scale.</string>
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## API reference
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import {
|
|
220
|
+
listPorts, listDrivers, loadDrivers, updateDrivers, identify,
|
|
221
|
+
connect, disconnect, status, tare, zero,
|
|
222
|
+
onReading, onStatus, isUsable
|
|
223
|
+
} from 'tauri-plugin-weighing-scale'
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
| Function | What it does |
|
|
227
|
+
| -------- | ------------ |
|
|
228
|
+
| `listPorts()` | The ports a scale could be on: `[{ name, kind, vid, pid, product, … }]`. `kind`: `usb`, `bluetooth`, `pci`, `network`, `unknown`. |
|
|
229
|
+
| `identify({ ports?, drivers? })` | Tries the drivers on the ports and returns the scales it recognises: `[{ port, driverId, driverName, driverVersion, reading }]`. Ports already connected are skipped. |
|
|
230
|
+
| `connect(port, driverId)` | Starts reading that scale in the background. |
|
|
231
|
+
| `onReading(cb)` | `cb({ port, reading })` each time a reading changes. Returns a function that stops listening. |
|
|
232
|
+
| `onStatus(cb)` | `cb({ port, driverId, state, error, lastReading })` when the connection changes. |
|
|
233
|
+
| `status()` | The connected scales, with their last reading. |
|
|
234
|
+
| `tare(port)`, `zero(port)` | Sends the driver's tare or zero command. Fails if the driver has none. |
|
|
235
|
+
| `disconnect(port)` | Stops reading it. |
|
|
236
|
+
| `listDrivers()` | The drivers the plugin knows. |
|
|
237
|
+
| `loadDrivers([json…])` | Adds drivers you have (written by hand, from your server). Replaces those with the same id. Returns, for each, its id and version or the `error` that kept it out. |
|
|
238
|
+
| `updateDrivers(url?)` | Downloads, checks and loads the signed catalog. See [Updating the drivers](#updating-the-drivers). |
|
|
239
|
+
| `isUsable(reading)` | True when the weight is stable, above zero and not overloaded. |
|
|
240
|
+
|
|
241
|
+
A reading:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
{
|
|
245
|
+
weight: { value: 1.234, unit: "kg", kg: 1.234, decimals: 3 } | null, // null: the scale only sent its state
|
|
246
|
+
stable: true, overload: false, negative: false, zero: false, net: false,
|
|
247
|
+
response: "weight", // which of the driver's responses matched
|
|
248
|
+
raw: "[STX]01234[CR]" // what the scale sent, readable
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Commands reject with a message (`can't open /dev/ttyUSB0: Permission denied`,
|
|
253
|
+
`no driver with id …`, `… is not connected`). Once connected, problems come as `onStatus`
|
|
254
|
+
changes, not errors.
|
|
255
|
+
|
|
256
|
+
The permission `weighing-scale:default` allows every command. To allow only some, list them:
|
|
257
|
+
`weighing-scale:allow-list-ports`, `allow-list-drivers`, `allow-load-drivers`,
|
|
258
|
+
`allow-update-drivers`, `allow-identify`, `allow-connect`, `allow-disconnect`, `allow-status`,
|
|
259
|
+
`allow-tare`, `allow-zero`.
|
|
260
|
+
|
|
261
|
+
## Built-in drivers
|
|
262
|
+
|
|
263
|
+
| Driver | Protocol | Scales | Verified with a real scale |
|
|
264
|
+
| ------ | -------- | ------ | -------------------------- |
|
|
265
|
+
| `toledo-8217` | Ask with `W`; weight or a status byte. 9600 7E1 | Mettler Toledo and scales emulating it | Not yet |
|
|
266
|
+
| `systel-passer-w` | Ask with `W`; grams or a status byte. 9600 8N1 | Systel Passer, Checkout (protocol 1) | Not yet |
|
|
267
|
+
| `systel-continuous-xor` | Sends `01000eT` on its own: grams, stable flag, XOR | Systel Passer, Checkout (protocol 7) | Not yet |
|
|
268
|
+
| `systel-enq-dc2` | `ENQ` → `ACK` → `DC2`; grams and a BCC | Systel Passer, Checkout | Not yet |
|
|
269
|
+
| `generic-continuous-st-gs` | Sends `ST,GS, 0.119,kg` lines on its own | Low-cost weighing indicators (DWP-102E, TD-WI 1…) | Not yet |
|
|
270
|
+
| `torrey-p` | Ask with `P`; one line with the weight | Torrey L-EQ, L-PCR, SR50/SR100 | Not yet — layout varies by firmware |
|
|
271
|
+
|
|
272
|
+
**How a scale is recognised.** There is no universal scale id, and most scales use a generic
|
|
273
|
+
USB-serial chip that identifies the chip, not the scale. So `identify` tries the drivers on each
|
|
274
|
+
port (ports in parallel): drivers whose USB hint matches go first; a driver recognises its scale
|
|
275
|
+
by asking it something (Toledo's `Ehello`, Systel's `I1`) and/or by getting a valid reading,
|
|
276
|
+
checksum included. The first that recognises it wins.
|
|
277
|
+
|
|
278
|
+
## Writing a Driver
|
|
279
|
+
|
|
280
|
+
A driver is one JSON file, `drivers/<id>.json`, checked against
|
|
281
|
+
[`drivers/driver.schema.json`](drivers/driver.schema.json) (editors autocomplete it through the
|
|
282
|
+
`$schema` line). A complete example:
|
|
283
|
+
|
|
284
|
+
```json
|
|
285
|
+
{
|
|
286
|
+
"$schema": "./driver.schema.json",
|
|
287
|
+
"id": "systel-passer-w",
|
|
288
|
+
"version": 1,
|
|
289
|
+
"engine": 1,
|
|
290
|
+
"name": "Systel — W (protocol 1)",
|
|
291
|
+
"sources": ["Systel technical note rev. 4: https://…"],
|
|
292
|
+
"connection": { "baud_rate": 9600, "data_bits": 8, "parity": "none", "stop_bits": 1 },
|
|
293
|
+
"unit": "kg",
|
|
294
|
+
"reading": { "mode": "poll", "steps": [{ "send": "W" }], "interval_ms": 200, "timeout_ms": 1000 },
|
|
295
|
+
"framing": { "start": "\u0002", "end": ["\r"] },
|
|
296
|
+
"responses": [
|
|
297
|
+
{ "name": "weight", "pattern": "^\\x02(?<weight>\\d{5})\\r$",
|
|
298
|
+
"weight": { "group": "weight", "decimals": 3 }, "flags": { "stable": true } },
|
|
299
|
+
{ "name": "status", "pattern": "^\\x02\\?(?<status>(?s:.))\\r$",
|
|
300
|
+
"flags": { "stable": { "group": "status", "bit": 7, "inverted": true },
|
|
301
|
+
"overload": { "group": "status", "bit": 6 } } }
|
|
302
|
+
],
|
|
303
|
+
"commands": { "tare": [{ "send": "T" }] },
|
|
304
|
+
"identify": { "steps": [{ "send": "I1" }, { "expect": "\\x02\\d{5}(\\|\\d{5}){5}\\x03" }] },
|
|
305
|
+
"samples": [ { "frame": "\u000200012\r", "expect": { "weight_kg": 0.012, "stable": true } } ],
|
|
306
|
+
"simulation": { "capacity_kg": 15, "divisions": [{ "step_kg": 0.005 }],
|
|
307
|
+
"replies": [{ "on": "W", "reading": true }],
|
|
308
|
+
"responses": [ { "when": { "stable": true }, "template": "\u0002{g:05}\r" },
|
|
309
|
+
{ "template": "\u0002?{bits:0x02,motion=7,overload=6}\r" } ] }
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Bytes are written as characters U+0000–U+00FF**: `"\u0002"` is STX, `"\r"` is CR.
|
|
314
|
+
|
|
315
|
+
### Fields
|
|
316
|
+
|
|
317
|
+
| Field | Meaning |
|
|
318
|
+
| ----- | ------- |
|
|
319
|
+
| `id` | Lowercase letters, digits and dashes. Also the file name. Never changes. |
|
|
320
|
+
| `version` | Raise it on every change. Apps keep the version they tested and can ask before switching. |
|
|
321
|
+
| `engine` | Engine version the driver needs (now `1`). |
|
|
322
|
+
| `models` | Scales known to speak it; `verified: { by, date }` once someone tested it with the scale. |
|
|
323
|
+
| `sources` | Where the protocol comes from. Cite; don't copy manuals. |
|
|
324
|
+
| `usb` | `[{ "vid": "1A86", "pid": "7523" }]`: try this driver first on those ports. Only a hint. |
|
|
325
|
+
| `connection` | `baud_rate`, `data_bits` (5–8), `parity` (`none`/`even`/`odd`), `stop_bits` (1–2), `flow_control`. |
|
|
326
|
+
| `unit` | Unit of the weight when the answer doesn't say it (`kg`, `g`, `lb`, `oz`). |
|
|
327
|
+
| `reading` | `{ "mode": "poll", "steps": […], "interval_ms", "timeout_ms" }` or `{ "mode": "continuous", "timeout_ms" }`. |
|
|
328
|
+
| `steps` | Each step has one of `send` (bytes), `expect` (regex the answer must match, with `timeout_ms`) or `wait_ms`. A handshake: `[{"send":"\u0005"},{"expect":"\\x06"},{"send":"\u0012"}]`. |
|
|
329
|
+
| `framing` | How an answer is cut: `start` (optional) and `end` (list), or a fixed `length`. Fixed-length frames without `start` resynchronise on their own. |
|
|
330
|
+
| `responses` | See below. The first whose pattern matches the whole frame is used. |
|
|
331
|
+
| `commands` | `tare` and `zero`, as steps. |
|
|
332
|
+
| `identify` | `steps` to run, then (unless `"reading": false`) a valid reading must come. Without it, a valid reading is enough. |
|
|
333
|
+
|
|
334
|
+
### Responses and Flags
|
|
335
|
+
|
|
336
|
+
`pattern` is a regular expression over bytes (Unicode off) with named groups:
|
|
337
|
+
|
|
338
|
+
- `weight`: `{ "group", "decimals", "sign_group", "unit_group" }`. `decimals` are implied when
|
|
339
|
+
the number has no point (`00012` with 3 → 0.012). Commas work as decimal points. Without
|
|
340
|
+
`weight`, it's a status-only response.
|
|
341
|
+
- `flags`: `stable`, `overload`, `negative`, `zero`, `net`. Each is `true`/`false`, or read
|
|
342
|
+
from a group: `{ "group": "s", "in": ["ST"] }` (its text is one of those),
|
|
343
|
+
`{ "group": "status", "bit": 0, "inverted": true }` (a bit of its first byte), or
|
|
344
|
+
`{ "group": "net" }` (the group matched something). A missing flag is `false`; a negative
|
|
345
|
+
weight is always `negative`.
|
|
346
|
+
- `checksum`: `{ "type": "xor", "over": "payload", "value": "bcc" }`: the XOR of the bytes of
|
|
347
|
+
group `over` must equal the byte in group `value`.
|
|
348
|
+
|
|
349
|
+
### Samples
|
|
350
|
+
|
|
351
|
+
Real frames (from the manual or a capture) and what they must give:
|
|
352
|
+
|
|
353
|
+
```json
|
|
354
|
+
{ "frame": "01000eT", "expect": { "weight_kg": 1.0, "stable": true } },
|
|
355
|
+
{ "frame": "01000eX", "expect": { "error": "checksum" } }
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
`expect` may check `weight_kg`, `no_weight`, `stable`, `overload`, `negative`, `zero`, `net`,
|
|
359
|
+
`response`, or `error` (`unrecognized`, `checksum`, `badWeight`).
|
|
360
|
+
|
|
361
|
+
### Simulation Templates
|
|
362
|
+
|
|
363
|
+
`simulation` says how a simulated scale answers with this driver. The tests, the CI and the
|
|
364
|
+
simulator app use it: the CI writes frames for several scale states and checks the driver reads
|
|
365
|
+
back the same weight and flags. **A driver that doesn't read its own frames doesn't get in.**
|
|
366
|
+
|
|
367
|
+
- `capacity_kg` and `divisions` (`[{ "up_to_kg": 6, "step_kg": 0.002 }, { "step_kg": 0.005 }]`).
|
|
368
|
+
- `replies`: `{ "on": "W", "send": "…", "reading": true }`, used when the received bytes end with `on`.
|
|
369
|
+
- `responses`: `{ "when": { "stable": true, "overload": false }, "template": "…" }`; the first
|
|
370
|
+
that fits is used, none means silence. `"readable": false` marks answers the driver doesn't
|
|
371
|
+
read (a lone NAK).
|
|
372
|
+
- `interval_ms`: for continuous scales.
|
|
373
|
+
|
|
374
|
+
| Placeholder | Writes |
|
|
375
|
+
| ----------- | ------ |
|
|
376
|
+
| `{kg:06.3}`, `{g:05}`, `{lb:7.2}`, `{oz:5.1}` | The weight in that unit, without sign. `0` pads with zeros (otherwise spaces), then the width, then the decimals. A leading `s` (`{g:s05}`) writes `-` inside the width. |
|
|
377
|
+
| `{stable?S:U}` | `S` if the flag is true, else `U`. Flags: `stable`, `motion`, `overload`, `negative`, `zero`, `net`. |
|
|
378
|
+
| `{xor:1}` | XOR of the bytes written so far, from position 1. |
|
|
379
|
+
| `{bits:0x02,motion=7,overload=6}` | One byte: the base plus each bit whose flag is true. |
|
|
380
|
+
| `{{`, `}}` | A literal brace. |
|
|
381
|
+
|
|
382
|
+
### Contributing a Driver
|
|
383
|
+
|
|
384
|
+
1. Add `drivers/<id>.json` with its `sources`, `samples` and `simulation`.
|
|
385
|
+
2. Run `cargo test -p weighing-scale-engine --all-features`.
|
|
386
|
+
3. Open a pull request. Say which scale you tested it with, if any: verified drivers are worth
|
|
387
|
+
more than many unverified ones.
|
|
388
|
+
|
|
389
|
+
If the engine changed, regenerate the schema with
|
|
390
|
+
`UPDATE_SCHEMA=1 cargo test -p weighing-scale-engine --features schema --test schema`.
|
|
391
|
+
|
|
392
|
+
## Rust API
|
|
393
|
+
|
|
394
|
+
```rust
|
|
395
|
+
use tauri_plugin_weighing_scale::WeighingScaleExt;
|
|
396
|
+
|
|
397
|
+
let scale = app.weighing_scale();
|
|
398
|
+
let found = scale.identify(None, None)?;
|
|
399
|
+
scale.connect(found[0].port.clone(), found[0].driver_id.clone())?;
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
The engine is its own crate, [`weighing-scale-engine`](crates/engine), re-exported as
|
|
403
|
+
`tauri_plugin_weighing_scale::engine`. It has no Tauri dependency: it reads drivers, cuts and
|
|
404
|
+
reads frames, talks through any `Port` (a trait with `write_all` and a `read` with a timeout),
|
|
405
|
+
includes a simulated scale (`Simulator`, `SimulatedPort`) for tests, and builds, signs and
|
|
406
|
+
verifies the catalog (feature `catalog`).
|
|
407
|
+
|
|
408
|
+
```
|
|
409
|
+
Your frontend
|
|
410
|
+
↓ commands ↑ events: weighing-scale://reading, weighing-scale://status
|
|
411
|
+
Plugin (Rust)
|
|
412
|
+
├── Drivers: built-in + downloaded catalog + loaded by the app
|
|
413
|
+
├── Engine: framing, parsing, checksums, commands, identification
|
|
414
|
+
├── One reader thread per connected scale: reads, emits on change, reconnects
|
|
415
|
+
└── Transport: desktop serial ports (serialport) · Android (Kotlin) · iOS (Swift)
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## Releasing (maintainers)
|
|
419
|
+
|
|
420
|
+
Releases are made by the [Release workflow](.github/workflows/release.yml), not by hand:
|
|
421
|
+
|
|
422
|
+
- **Actions → Release → Run workflow**, choosing `patch`, `minor` or `major`. It raises the
|
|
423
|
+
version in every file, commits it to `main`, and releases it.
|
|
424
|
+
- Or raise the version in a pull request (`node scripts/version.mjs patch`) and merge it: the
|
|
425
|
+
workflow sees there is no tag for it yet and releases it.
|
|
426
|
+
|
|
427
|
+
A release has the tag `vX.Y.Z`, notes generated from what changed since the last one, and two
|
|
428
|
+
files: `catalog.json` and `catalog.json.sig`, what `updateDrivers` downloads. The workflow also
|
|
429
|
+
publishes the crates on crates.io and the package on npm.
|
|
430
|
+
|
|
431
|
+
It needs these repository secrets (Settings → Secrets and variables → Actions):
|
|
432
|
+
|
|
433
|
+
| Secret | What | Without it |
|
|
434
|
+
| ------ | ---- | ---------- |
|
|
435
|
+
| `CATALOG_SIGNING_KEY` | The catalog's private key (the line in the key file) | The release fails |
|
|
436
|
+
| `CARGO_REGISTRY_TOKEN` | A crates.io token | Not published to crates.io |
|
|
437
|
+
| `NPM_TOKEN` | An npm token | Not published to npm |
|
|
438
|
+
|
|
439
|
+
A new key pair: `cargo run -p weighing-scale-engine --features catalog --example catalog -- keygen <file>`;
|
|
440
|
+
put the public key in `catalog-keys.txt`. To change keys without breaking installed apps, add the
|
|
441
|
+
new public key, release, and only later remove the old one.
|
|
442
|
+
|
|
443
|
+
## Repository layout
|
|
444
|
+
|
|
445
|
+
```
|
|
446
|
+
crates/engine/ the engine, without Tauri (its own crate); also builds and signs the catalog
|
|
447
|
+
src/ the plugin: transports, reader threads, commands, events, catalog download
|
|
448
|
+
guest-js/ the JavaScript / TypeScript API
|
|
449
|
+
android/, ios/ the native transports (Kotlin, Swift)
|
|
450
|
+
drivers/ one JSON per driver, and driver.schema.json
|
|
451
|
+
catalog-keys.txt public keys a downloaded catalog must be signed with
|
|
452
|
+
examples/ a small app that uses the plugin
|
|
453
|
+
simulator/ a simulated scale on a virtual serial port (test tool)
|
|
454
|
+
scripts/ version.mjs: the version in every file
|
|
455
|
+
tests/ the plugin against a simulated scale (pseudo-terminal) and a local catalog server
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
## License
|
|
459
|
+
|
|
460
|
+
MIT
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@tauri-apps/api/core');
|
|
4
|
+
var event = require('@tauri-apps/api/event');
|
|
5
|
+
|
|
6
|
+
const EVENT_READING = 'weighing-scale://reading';
|
|
7
|
+
const EVENT_STATUS = 'weighing-scale://status';
|
|
8
|
+
/** Serial ports (desktop) or USB / Bluetooth devices (mobile) where a scale could be. */
|
|
9
|
+
async function listPorts() {
|
|
10
|
+
return await core.invoke('plugin:weighing-scale|list_ports');
|
|
11
|
+
}
|
|
12
|
+
/** The drivers the plugin knows: the built-in ones plus any loaded with `loadDrivers`. */
|
|
13
|
+
async function listDrivers() {
|
|
14
|
+
return await core.invoke('plugin:weighing-scale|list_drivers');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Adds drivers (from a catalog, or written by hand). A driver replaces the one with the same id.
|
|
18
|
+
* Each is checked; the ones that fail come back with an `error` and are left out.
|
|
19
|
+
*/
|
|
20
|
+
async function loadDrivers(drivers) {
|
|
21
|
+
return await core.invoke('plugin:weighing-scale|load_drivers', { drivers });
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Downloads the latest driver catalog, checks its signature and loads its drivers. The plugin
|
|
25
|
+
* keeps it, so the next start has those drivers without internet. A driver doesn't replace a
|
|
26
|
+
* newer one already installed. `url`: another catalog (its signature must be at `<url>.sig`,
|
|
27
|
+
* signed with one of the plugin's keys).
|
|
28
|
+
*/
|
|
29
|
+
async function updateDrivers(url) {
|
|
30
|
+
return await core.invoke('plugin:weighing-scale|update_drivers', { url });
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Recognises the connected scales. By default it tries every driver on every USB port (and Bluetooth ones on Android and iOS);
|
|
34
|
+
* pass `ports` to try others (for example a built-in RS-232 port) and `drivers` to narrow it.
|
|
35
|
+
* Ports already connected are skipped.
|
|
36
|
+
*/
|
|
37
|
+
async function identify(options = {}) {
|
|
38
|
+
return await core.invoke('plugin:weighing-scale|identify', {
|
|
39
|
+
ports: options.ports ?? null,
|
|
40
|
+
drivers: options.drivers ?? null
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Starts reading a scale. Readings and status changes arrive through `onReading` and `onStatus`. */
|
|
44
|
+
async function connect(port, driverId) {
|
|
45
|
+
return await core.invoke('plugin:weighing-scale|connect', { port, driverId });
|
|
46
|
+
}
|
|
47
|
+
async function disconnect(port) {
|
|
48
|
+
await core.invoke('plugin:weighing-scale|disconnect', { port });
|
|
49
|
+
}
|
|
50
|
+
/** The connected scales and their last reading. */
|
|
51
|
+
async function status() {
|
|
52
|
+
return await core.invoke('plugin:weighing-scale|status');
|
|
53
|
+
}
|
|
54
|
+
/** Tares the scale, if its driver has a tare command. */
|
|
55
|
+
async function tare(port) {
|
|
56
|
+
await core.invoke('plugin:weighing-scale|tare', { port });
|
|
57
|
+
}
|
|
58
|
+
/** Zeroes the scale, if its driver has a zero command. */
|
|
59
|
+
async function zero(port) {
|
|
60
|
+
await core.invoke('plugin:weighing-scale|zero', { port });
|
|
61
|
+
}
|
|
62
|
+
/** Called when a connected scale's reading changes. */
|
|
63
|
+
async function onReading(handler) {
|
|
64
|
+
return await event.listen(EVENT_READING, (e) => handler(e.payload));
|
|
65
|
+
}
|
|
66
|
+
/** Called when a connected scale's state changes (connected, silent, disconnected…). */
|
|
67
|
+
async function onStatus(handler) {
|
|
68
|
+
return await event.listen(EVENT_STATUS, (e) => handler(e.payload));
|
|
69
|
+
}
|
|
70
|
+
/** A weight a sale can use: stable, not overloaded, above zero. */
|
|
71
|
+
function isUsable(reading) {
|
|
72
|
+
return !!reading && !!reading.weight && reading.stable && !reading.overload && !reading.negative && reading.weight.kg > 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.EVENT_READING = EVENT_READING;
|
|
76
|
+
exports.EVENT_STATUS = EVENT_STATUS;
|
|
77
|
+
exports.connect = connect;
|
|
78
|
+
exports.disconnect = disconnect;
|
|
79
|
+
exports.identify = identify;
|
|
80
|
+
exports.isUsable = isUsable;
|
|
81
|
+
exports.listDrivers = listDrivers;
|
|
82
|
+
exports.listPorts = listPorts;
|
|
83
|
+
exports.loadDrivers = loadDrivers;
|
|
84
|
+
exports.onReading = onReading;
|
|
85
|
+
exports.onStatus = onStatus;
|
|
86
|
+
exports.status = status;
|
|
87
|
+
exports.tare = tare;
|
|
88
|
+
exports.updateDrivers = updateDrivers;
|
|
89
|
+
exports.zero = zero;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { type UnlistenFn } from '@tauri-apps/api/event';
|
|
2
|
+
export type Unit = 'kg' | 'g' | 'lb' | 'oz';
|
|
3
|
+
export interface Weight {
|
|
4
|
+
/** As the scale gave it, in `unit`. */
|
|
5
|
+
value: number;
|
|
6
|
+
unit: Unit;
|
|
7
|
+
/** The same weight in kilograms. */
|
|
8
|
+
kg: number;
|
|
9
|
+
/** Decimals the scale gave. */
|
|
10
|
+
decimals: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Reading {
|
|
13
|
+
/** Missing in status-only answers (for example "in motion" without a number). */
|
|
14
|
+
weight: Weight | null;
|
|
15
|
+
stable: boolean;
|
|
16
|
+
overload: boolean;
|
|
17
|
+
negative: boolean;
|
|
18
|
+
zero: boolean;
|
|
19
|
+
net: boolean;
|
|
20
|
+
/** Name of the driver response that matched. */
|
|
21
|
+
response: string;
|
|
22
|
+
/** The frame, shown for people: `[STX]00012[CR]`. */
|
|
23
|
+
raw: string;
|
|
24
|
+
}
|
|
25
|
+
export type PortKind = 'usb' | 'bluetooth' | 'pci' | 'network' | 'unknown';
|
|
26
|
+
export interface PortInfo {
|
|
27
|
+
/** What to pass to `connect`: `/dev/ttyUSB0`, `COM3`… */
|
|
28
|
+
name: string;
|
|
29
|
+
kind: PortKind;
|
|
30
|
+
/** USB vendor id, hex (`1A86`). */
|
|
31
|
+
vid?: string;
|
|
32
|
+
pid?: string;
|
|
33
|
+
serialNumber?: string;
|
|
34
|
+
manufacturer?: string;
|
|
35
|
+
product?: string;
|
|
36
|
+
}
|
|
37
|
+
export type ConnectionState = 'connecting' | 'connected' | 'silent' | 'disconnected';
|
|
38
|
+
export interface ScaleStatus {
|
|
39
|
+
port: string;
|
|
40
|
+
driverId: string;
|
|
41
|
+
state: ConnectionState;
|
|
42
|
+
error?: string;
|
|
43
|
+
lastReading: Reading | null;
|
|
44
|
+
}
|
|
45
|
+
export interface Found {
|
|
46
|
+
port: string;
|
|
47
|
+
driverId: string;
|
|
48
|
+
driverName: string;
|
|
49
|
+
driverVersion: number;
|
|
50
|
+
/** The reading that proved it, when the driver needs one. */
|
|
51
|
+
reading: Reading | null;
|
|
52
|
+
}
|
|
53
|
+
export interface DriverLoad {
|
|
54
|
+
id: string | null;
|
|
55
|
+
version: number | null;
|
|
56
|
+
/** Why it wasn't loaded. */
|
|
57
|
+
error?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface CatalogUpdate {
|
|
60
|
+
/** Release the catalog comes from. */
|
|
61
|
+
version: string;
|
|
62
|
+
/** Each driver of the catalog: loaded, or why not (`error`). */
|
|
63
|
+
drivers: DriverLoad[];
|
|
64
|
+
}
|
|
65
|
+
export interface ReadingEvent {
|
|
66
|
+
port: string;
|
|
67
|
+
driverId: string;
|
|
68
|
+
reading: Reading;
|
|
69
|
+
}
|
|
70
|
+
/** A driver as written in `drivers/<id>.json`. See `drivers/driver.schema.json` for every field. */
|
|
71
|
+
export interface Driver {
|
|
72
|
+
id: string;
|
|
73
|
+
version: number;
|
|
74
|
+
engine: number;
|
|
75
|
+
name: string;
|
|
76
|
+
description?: string;
|
|
77
|
+
models: {
|
|
78
|
+
brand: string;
|
|
79
|
+
model: string;
|
|
80
|
+
verified?: {
|
|
81
|
+
by: string;
|
|
82
|
+
date: string;
|
|
83
|
+
notes?: string;
|
|
84
|
+
};
|
|
85
|
+
notes?: string;
|
|
86
|
+
}[];
|
|
87
|
+
sources: string[];
|
|
88
|
+
connection: {
|
|
89
|
+
baud_rate: number;
|
|
90
|
+
data_bits: number;
|
|
91
|
+
parity: 'none' | 'even' | 'odd';
|
|
92
|
+
stop_bits: number;
|
|
93
|
+
flow_control?: 'none' | 'software' | 'hardware';
|
|
94
|
+
};
|
|
95
|
+
reading: {
|
|
96
|
+
mode: 'poll' | 'continuous';
|
|
97
|
+
[key: string]: unknown;
|
|
98
|
+
};
|
|
99
|
+
commands: {
|
|
100
|
+
tare?: unknown[];
|
|
101
|
+
zero?: unknown[];
|
|
102
|
+
};
|
|
103
|
+
[key: string]: unknown;
|
|
104
|
+
}
|
|
105
|
+
export declare const EVENT_READING = "weighing-scale://reading";
|
|
106
|
+
export declare const EVENT_STATUS = "weighing-scale://status";
|
|
107
|
+
/** Serial ports (desktop) or USB / Bluetooth devices (mobile) where a scale could be. */
|
|
108
|
+
export declare function listPorts(): Promise<PortInfo[]>;
|
|
109
|
+
/** The drivers the plugin knows: the built-in ones plus any loaded with `loadDrivers`. */
|
|
110
|
+
export declare function listDrivers(): Promise<Driver[]>;
|
|
111
|
+
/**
|
|
112
|
+
* Adds drivers (from a catalog, or written by hand). A driver replaces the one with the same id.
|
|
113
|
+
* Each is checked; the ones that fail come back with an `error` and are left out.
|
|
114
|
+
*/
|
|
115
|
+
export declare function loadDrivers(drivers: unknown[]): Promise<DriverLoad[]>;
|
|
116
|
+
/**
|
|
117
|
+
* Downloads the latest driver catalog, checks its signature and loads its drivers. The plugin
|
|
118
|
+
* keeps it, so the next start has those drivers without internet. A driver doesn't replace a
|
|
119
|
+
* newer one already installed. `url`: another catalog (its signature must be at `<url>.sig`,
|
|
120
|
+
* signed with one of the plugin's keys).
|
|
121
|
+
*/
|
|
122
|
+
export declare function updateDrivers(url?: string): Promise<CatalogUpdate>;
|
|
123
|
+
/**
|
|
124
|
+
* Recognises the connected scales. By default it tries every driver on every USB port (and Bluetooth ones on Android and iOS);
|
|
125
|
+
* pass `ports` to try others (for example a built-in RS-232 port) and `drivers` to narrow it.
|
|
126
|
+
* Ports already connected are skipped.
|
|
127
|
+
*/
|
|
128
|
+
export declare function identify(options?: {
|
|
129
|
+
ports?: string[];
|
|
130
|
+
drivers?: string[];
|
|
131
|
+
}): Promise<Found[]>;
|
|
132
|
+
/** Starts reading a scale. Readings and status changes arrive through `onReading` and `onStatus`. */
|
|
133
|
+
export declare function connect(port: string, driverId: string): Promise<ScaleStatus>;
|
|
134
|
+
export declare function disconnect(port: string): Promise<void>;
|
|
135
|
+
/** The connected scales and their last reading. */
|
|
136
|
+
export declare function status(): Promise<ScaleStatus[]>;
|
|
137
|
+
/** Tares the scale, if its driver has a tare command. */
|
|
138
|
+
export declare function tare(port: string): Promise<void>;
|
|
139
|
+
/** Zeroes the scale, if its driver has a zero command. */
|
|
140
|
+
export declare function zero(port: string): Promise<void>;
|
|
141
|
+
/** Called when a connected scale's reading changes. */
|
|
142
|
+
export declare function onReading(handler: (event: ReadingEvent) => void): Promise<UnlistenFn>;
|
|
143
|
+
/** Called when a connected scale's state changes (connected, silent, disconnected…). */
|
|
144
|
+
export declare function onStatus(handler: (status: ScaleStatus) => void): Promise<UnlistenFn>;
|
|
145
|
+
/** A weight a sale can use: stable, not overloaded, above zero. */
|
|
146
|
+
export declare function isUsable(reading: Reading | null | undefined): reading is Reading & {
|
|
147
|
+
weight: Weight;
|
|
148
|
+
};
|
package/dist-js/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { invoke } from '@tauri-apps/api/core';
|
|
2
|
+
import { listen } from '@tauri-apps/api/event';
|
|
3
|
+
|
|
4
|
+
const EVENT_READING = 'weighing-scale://reading';
|
|
5
|
+
const EVENT_STATUS = 'weighing-scale://status';
|
|
6
|
+
/** Serial ports (desktop) or USB / Bluetooth devices (mobile) where a scale could be. */
|
|
7
|
+
async function listPorts() {
|
|
8
|
+
return await invoke('plugin:weighing-scale|list_ports');
|
|
9
|
+
}
|
|
10
|
+
/** The drivers the plugin knows: the built-in ones plus any loaded with `loadDrivers`. */
|
|
11
|
+
async function listDrivers() {
|
|
12
|
+
return await invoke('plugin:weighing-scale|list_drivers');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Adds drivers (from a catalog, or written by hand). A driver replaces the one with the same id.
|
|
16
|
+
* Each is checked; the ones that fail come back with an `error` and are left out.
|
|
17
|
+
*/
|
|
18
|
+
async function loadDrivers(drivers) {
|
|
19
|
+
return await invoke('plugin:weighing-scale|load_drivers', { drivers });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Downloads the latest driver catalog, checks its signature and loads its drivers. The plugin
|
|
23
|
+
* keeps it, so the next start has those drivers without internet. A driver doesn't replace a
|
|
24
|
+
* newer one already installed. `url`: another catalog (its signature must be at `<url>.sig`,
|
|
25
|
+
* signed with one of the plugin's keys).
|
|
26
|
+
*/
|
|
27
|
+
async function updateDrivers(url) {
|
|
28
|
+
return await invoke('plugin:weighing-scale|update_drivers', { url });
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Recognises the connected scales. By default it tries every driver on every USB port (and Bluetooth ones on Android and iOS);
|
|
32
|
+
* pass `ports` to try others (for example a built-in RS-232 port) and `drivers` to narrow it.
|
|
33
|
+
* Ports already connected are skipped.
|
|
34
|
+
*/
|
|
35
|
+
async function identify(options = {}) {
|
|
36
|
+
return await invoke('plugin:weighing-scale|identify', {
|
|
37
|
+
ports: options.ports ?? null,
|
|
38
|
+
drivers: options.drivers ?? null
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** Starts reading a scale. Readings and status changes arrive through `onReading` and `onStatus`. */
|
|
42
|
+
async function connect(port, driverId) {
|
|
43
|
+
return await invoke('plugin:weighing-scale|connect', { port, driverId });
|
|
44
|
+
}
|
|
45
|
+
async function disconnect(port) {
|
|
46
|
+
await invoke('plugin:weighing-scale|disconnect', { port });
|
|
47
|
+
}
|
|
48
|
+
/** The connected scales and their last reading. */
|
|
49
|
+
async function status() {
|
|
50
|
+
return await invoke('plugin:weighing-scale|status');
|
|
51
|
+
}
|
|
52
|
+
/** Tares the scale, if its driver has a tare command. */
|
|
53
|
+
async function tare(port) {
|
|
54
|
+
await invoke('plugin:weighing-scale|tare', { port });
|
|
55
|
+
}
|
|
56
|
+
/** Zeroes the scale, if its driver has a zero command. */
|
|
57
|
+
async function zero(port) {
|
|
58
|
+
await invoke('plugin:weighing-scale|zero', { port });
|
|
59
|
+
}
|
|
60
|
+
/** Called when a connected scale's reading changes. */
|
|
61
|
+
async function onReading(handler) {
|
|
62
|
+
return await listen(EVENT_READING, (e) => handler(e.payload));
|
|
63
|
+
}
|
|
64
|
+
/** Called when a connected scale's state changes (connected, silent, disconnected…). */
|
|
65
|
+
async function onStatus(handler) {
|
|
66
|
+
return await listen(EVENT_STATUS, (e) => handler(e.payload));
|
|
67
|
+
}
|
|
68
|
+
/** A weight a sale can use: stable, not overloaded, above zero. */
|
|
69
|
+
function isUsable(reading) {
|
|
70
|
+
return !!reading && !!reading.weight && reading.stable && !reading.overload && !reading.negative && reading.weight.kg > 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { EVENT_READING, EVENT_STATUS, connect, disconnect, identify, isUsable, listDrivers, listPorts, loadDrivers, onReading, onStatus, status, tare, updateDrivers, zero };
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tauri-plugin-weighing-scale",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "luis3132",
|
|
5
|
+
"description": "Tauri plugin to read weighing scales (serial, USB, Bluetooth) from protocol drivers written as data",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/luis3132/tauri-plugin-weighing-scale.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"tauri",
|
|
13
|
+
"tauri-plugin",
|
|
14
|
+
"scale",
|
|
15
|
+
"weighing",
|
|
16
|
+
"serial",
|
|
17
|
+
"pos"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"types": "./dist-js/index.d.ts",
|
|
21
|
+
"main": "./dist-js/index.cjs",
|
|
22
|
+
"module": "./dist-js/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
"types": "./dist-js/index.d.ts",
|
|
25
|
+
"import": "./dist-js/index.js",
|
|
26
|
+
"require": "./dist-js/index.cjs"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist-js",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rollup -c",
|
|
34
|
+
"prepublishOnly": "bun run build",
|
|
35
|
+
"pretest": "bun run build"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@tauri-apps/api": "^2.10.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
42
|
+
"rollup": "^4.60.1",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"tslib": "^2.8.1"
|
|
45
|
+
}
|
|
46
|
+
}
|