Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/modpoll-wot/AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# modpoll-wot repository conventions

- Review the working tree and current branch before editing. Integrate changes into `main` through a reviewed pull request with passing CI.
- Review the working tree and current branch before editing. This package lives in the modpoll repository's uv workspace; integrate changes into `v2` through a pull request with passing CI.
- This repository owns the shared TM/TD executable profile, its bundled schemas, and the offline CLI. Modpoll, Modsim, Open Modbus Catalog, and ModioHub consume published releases only.
- Run `mise run check` before opening a pull request.
- Released versions and tags are immutable. Release the producer first, then update consumer pins in their own repositories. See [README](README.md#releases).
Expand Down
6 changes: 6 additions & 0 deletions packages/modpoll-wot/modpoll_wot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
server_url,
)
from .verification import (
REPORT_VERSION,
ReportError,
identity_constants,
identity_result,
model_fingerprint,
point_fingerprints,
validate_report,
Expand Down Expand Up @@ -52,7 +55,10 @@
"load_simulation_data",
"scaled_raw",
"parse_parameters",
"REPORT_VERSION",
"ReportError",
"identity_constants",
"identity_result",
"model_fingerprint",
"point_fingerprints",
"validate_report",
Expand Down
2 changes: 1 addition & 1 deletion packages/modpoll-wot/modpoll_wot/schemas/SOURCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Upstream repository and licensing: https://github.com/w3c/wot-thing-description
This binding is a work in progress, not a W3C Recommendation.

acquisition-0.2.json is the executable project schema.
verification-report-3.json and verification-report-4.json are the project's hardware verification report schemas. In schema 4 the product is optional: the receiving service resolves it from the model fingerprint, while older reports may still carry it.
verification-report-3.json, verification-report-4.json and verification-report-5.json are the project's hardware verification report schemas. In schema 4 the product is optional: the receiving service resolves it from the model fingerprint, while older reports may still carry it. Schema 5 adds the optional per-point identity result for points whose DataSchema declares const.
simulation-data-1.json is the project's simulation data schema. Profile 0.1 is not
bundled or executable in current development source. The Modbus schema's TD reference resolves only to bundled TD 1.1;
other remote resolution remains disabled.
Expand Down
269 changes: 269 additions & 0 deletions packages/modpoll-wot/modpoll_wot/schemas/verification-report-5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/top-maker/modpoll-wot/schemas/verification-report-5.json",
"title": "Modbus hardware verification report",
"description": "One reading of a Thing Model on a physical device, without endpoints, serial numbers or raw values. The receiving service resolves the catalog product from the model fingerprint; a report may still carry product. A successfully read point whose DataSchema declares const records whether the value read matched it.",
"type": "object",
"additionalProperties": false,
"required": [
"reportVersion",
"model",
"tool",
"transport",
"date",
"points"
],
"properties": {
"reportVersion": {
"const": 5
},
"product": {
"$ref": "#/$defs/productId"
},
"model": {
"type": "object",
"additionalProperties": false,
"required": [
"sha256",
"fingerprint",
"source"
],
"properties": {
"sha256": {
"type": "string",
"pattern": "^[0-9a-f]{64}$"
},
"fingerprint": {
"type": "string",
"pattern": "^mf1:[0-9a-f]{64}$"
},
"source": {
"enum": [
"catalog",
"workspace",
"local"
]
},
"ref": {
"type": "string",
"minLength": 1,
"maxLength": 128
}
},
"allOf": [
{
"if": {
"properties": {
"source": {
"const": "catalog"
}
}
},
"then": {
"required": [
"ref"
],
"properties": {
"ref": {
"pattern": "^[0-9a-f]{40}$"
}
}
}
},
{
"if": {
"properties": {
"source": {
"const": "workspace"
}
}
},
"then": {
"required": [
"ref"
]
}
},
{
"if": {
"properties": {
"source": {
"const": "local"
}
}
},
"then": {
"not": {
"required": [
"ref"
]
}
}
}
]
},
"tool": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"version",
"interpreter"
],
"properties": {
"name": {
"$ref": "#/$defs/slug"
},
"version": {
"$ref": "#/$defs/version"
},
"interpreter": {
"$ref": "#/$defs/version"
}
}
},
"transport": {
"enum": [
"tcp",
"rtu"
]
},
"date": {
"type": "string",
"pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"
},
"environment": {
"type": "object",
"additionalProperties": false,
"properties": {
"chassis": {
"$ref": "#/$defs/productId"
},
"firmware": {
"type": "string",
"minLength": 1,
"maxLength": 64
},
"configuration": {
"type": "string",
"minLength": 1,
"maxLength": 200
}
}
},
"points": {
"type": "array",
"minItems": 1,
"maxItems": 1000,
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"property",
"fingerprint",
"read",
"expected"
],
"properties": {
"property": {
"type": "string",
"minLength": 1,
"maxLength": 128
},
"fingerprint": {
"type": "string",
"pattern": "^pf1:[0-9a-f]{64}$"
},
"read": {
"enum": [
"ok",
"exception",
"no-response",
"not-tested"
]
},
"exceptionCode": {
"type": "integer",
"minimum": 1,
"maximum": 255
},
"expected": {
"enum": [
"confirmed",
"rejected",
"unchecked"
]
},
"identity": {
"enum": [
"match",
"mismatch"
]
}
},
"allOf": [
{
"if": {
"properties": {
"read": {
"const": "exception"
}
}
},
"then": {
"required": [
"exceptionCode"
]
},
"else": {
"not": {
"required": [
"exceptionCode"
]
}
}
},
{
"if": {
"properties": {
"read": {
"not": {
"const": "ok"
}
}
}
},
"then": {
"properties": {
"expected": {
"const": "unchecked"
}
},
"not": {
"required": [
"identity"
]
}
}
}
]
}
}
},
"$defs": {
"slug": {
"type": "string",
"pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$",
"maxLength": 64
},
"productId": {
"type": "string",
"pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*/[a-z0-9]+(?:-[a-z0-9]+)*$",
"maxLength": 129
},
"version": {
"type": "string",
"pattern": "^[0-9A-Za-z.+-]{1,64}$"
}
}
}
54 changes: 51 additions & 3 deletions packages/modpoll-wot/modpoll_wot/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

FINGERPRINT_VERSION = "pf1"
MODEL_FINGERPRINT_VERSION = "mf1"
REPORT_VERSION = 3
# The newest verification report schema; validate_report also accepts the older ones.
REPORT_VERSION = 5
REPORT_VERSIONS = (3, 4, 5)
IDENTITY_RESULTS = ("match", "mismatch")
# Form fields that decide which bytes are read and how they become a value.
OPTIONAL_FIELDS = (
"modpoll:codec",
Expand Down Expand Up @@ -109,6 +112,34 @@ def combine_fingerprints(fingerprints) -> str:
return f"{MODEL_FINGERPRINT_VERSION}:{sha256(encoded).hexdigest()}"


def identity_constants(model) -> dict[str, object]:
"""Return the expected value of each readable point whose DataSchema declares ``const``.

These are identity points, such as a meter code or a module name: a tool
compares the value it reads with the constant and reports only whether
they match. ``const`` does not contribute to point or model fingerprints.
"""
try:
_, effective = instantiate(_document(model))
except (TypeError, RecursionError) as exc:
raise AcquisitionError(str(exc)) from exc
return {
name: prop["const"]
for name, prop in effective["properties"].items()
if "const" in prop and canonical_point(prop) is not None
}


def identity_result(expected, value) -> str:
"""Compare a decoded value with an identity point's ``const``: ``match`` or ``mismatch``.

Equality follows JSON Schema ``const``: numbers compare by value (137 equals
137.0), booleans never equal numbers, and strings compare exactly.
"""
valid = Draft202012Validator({"const": expected}).is_valid(value)
return "match" if valid else "mismatch"


def model_fingerprint(model) -> str:
"""Return the fingerprint of the set of readable point mappings of a model.

Expand All @@ -121,7 +152,7 @@ def model_fingerprint(model) -> str:
@lru_cache(maxsize=1)
def _report_validators():
validators = {}
for version in (3, 4):
for version in REPORT_VERSIONS:
schema = json.loads(
files("modpoll_wot")
.joinpath("schemas", f"verification-report-{version}.json")
Expand All @@ -133,7 +164,11 @@ def _report_validators():


def validate_report(report, *, model: bytes | None = None):
"""Validate a verification report (schema 3 or 4), optionally against its exact TM bytes."""
"""Validate a verification report (schema 3, 4 or 5), optionally against its exact TM bytes.

With the model, a schema 5 report must carry ``identity`` exactly for the
successfully read points whose DataSchema declares ``const``.
"""
version = report.get("reportVersion") if isinstance(report, dict) else None
validator = _report_validators().get(version)
if validator is None:
Expand Down Expand Up @@ -167,3 +202,16 @@ def validate_report(report, *, model: bytes | None = None):
)
if actual != point["fingerprint"]:
raise ReportError(f"/points/{index}/fingerprint: does not match the model")
if report["reportVersion"] < 5:
return
constants = identity_constants(model)
for index, point in enumerate(report["points"]):
declared = point["property"] in constants
if "identity" in point and not declared:
raise ReportError(
f"/points/{index}/identity: {point['property']} declares no const"
)
if declared and point["read"] == "ok" and "identity" not in point:
raise ReportError(
f"/points/{index}: identity is required for a read of a const point"
)
Loading
Loading