sme-sim-common 1.0.0__tar.gz
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.
- sme_sim_common-1.0.0/LICENSE +21 -0
- sme_sim_common-1.0.0/PKG-INFO +229 -0
- sme_sim_common-1.0.0/README.md +203 -0
- sme_sim_common-1.0.0/pyproject.toml +48 -0
- sme_sim_common-1.0.0/setup.cfg +4 -0
- sme_sim_common-1.0.0/sme_sim_common/__init__.py +3 -0
- sme_sim_common-1.0.0/sme_sim_common/__pyinstaller/__init__.py +8 -0
- sme_sim_common-1.0.0/sme_sim_common/__pyinstaller/hook-sme_sim_common.py +14 -0
- sme_sim_common-1.0.0/sme_sim_common/behaviors.py +424 -0
- sme_sim_common-1.0.0/sme_sim_common/cli.py +104 -0
- sme_sim_common-1.0.0/sme_sim_common/config.py +132 -0
- sme_sim_common-1.0.0/sme_sim_common/engine.py +321 -0
- sme_sim_common-1.0.0/sme_sim_common/static/brand/company-logo.svg +2 -0
- sme_sim_common-1.0.0/sme_sim_common/static/i18n/common.js +131 -0
- sme_sim_common-1.0.0/sme_sim_common/static/shell/shell.css +243 -0
- sme_sim_common-1.0.0/sme_sim_common/static/shell/shell.js +511 -0
- sme_sim_common-1.0.0/sme_sim_common/static/vendor/vendor.js +76 -0
- sme_sim_common-1.0.0/sme_sim_common/templates.py +331 -0
- sme_sim_common-1.0.0/sme_sim_common/testing/__init__.py +91 -0
- sme_sim_common-1.0.0/sme_sim_common/testing/plugin.py +51 -0
- sme_sim_common-1.0.0/sme_sim_common/web.py +616 -0
- sme_sim_common-1.0.0/sme_sim_common/xlsx.py +455 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/PKG-INFO +229 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/SOURCES.txt +38 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/dependency_links.txt +1 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/entry_points.txt +5 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/requires.txt +15 -0
- sme_sim_common-1.0.0/sme_sim_common.egg-info/top_level.txt +1 -0
- sme_sim_common-1.0.0/tests/test_behaviors.py +73 -0
- sme_sim_common-1.0.0/tests/test_cli.py +67 -0
- sme_sim_common-1.0.0/tests/test_config.py +119 -0
- sme_sim_common-1.0.0/tests/test_engine.py +134 -0
- sme_sim_common-1.0.0/tests/test_extension_points.py +313 -0
- sme_sim_common-1.0.0/tests/test_replay.py +118 -0
- sme_sim_common-1.0.0/tests/test_static.py +65 -0
- sme_sim_common-1.0.0/tests/test_templates.py +342 -0
- sme_sim_common-1.0.0/tests/test_testing.py +53 -0
- sme_sim_common-1.0.0/tests/test_web.py +59 -0
- sme_sim_common-1.0.0/tests/test_web_app.py +201 -0
- sme_sim_common-1.0.0/tests/test_xlsx.py +236 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 smartedge-tech-dev
|
|
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.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sme-sim-common
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Shared web shell, brand, i18n and Python helpers for the smart-edge simulator family
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/smartedge-tech-dev/sme-sim-common
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.11
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: starlette
|
|
13
|
+
Requires-Dist: fastapi>=0.110
|
|
14
|
+
Requires-Dist: uvicorn[standard]
|
|
15
|
+
Requires-Dist: pydantic>=2
|
|
16
|
+
Requires-Dist: pyyaml>=6
|
|
17
|
+
Requires-Dist: python-multipart
|
|
18
|
+
Provides-Extra: xlsx
|
|
19
|
+
Requires-Dist: openpyxl>=3.1; extra == "xlsx"
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest; extra == "dev"
|
|
22
|
+
Requires-Dist: httpx; extra == "dev"
|
|
23
|
+
Requires-Dist: openpyxl>=3.1; extra == "dev"
|
|
24
|
+
Requires-Dist: pyinstaller; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# sme-sim-common
|
|
28
|
+
|
|
29
|
+
**English** | [日本語](README.ja.md) | [简体中文](README.zh-CN.md)
|
|
30
|
+
|
|
31
|
+
Shared runtime for the smart-edge simulator series (SME BACnetSim, SME ModbusSim, the OCPP simulator,
|
|
32
|
+
SME SerialHub): the web shell (header, menus, theme, languages, About), vendor texts, the `common.*`
|
|
33
|
+
UI strings, and the protocol-independent Python layer (web helpers, config I/O, behaviors, device
|
|
34
|
+
templates, test helpers, PyInstaller hook). The `templates/` directory holds the build-time skeleton
|
|
35
|
+
(CI, release, packaging, docs, page) that products copy by hand; it is not part of the wheel.
|
|
36
|
+
|
|
37
|
+
MIT-licensed, developed by [smart-edge (智届科技)](https://smartedge-tech.com).
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install "sme-sim-common>=1.0,<2" # in a product's pyproject.toml dependencies
|
|
43
|
+
pip install "sme-sim-common[xlsx]>=1.0,<2" # with the .xlsx import / export helpers (openpyxl)
|
|
44
|
+
pip install -e ../sme-sim-common # local development against a checkout
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python 3.11 or newer.
|
|
48
|
+
|
|
49
|
+
## Using it in a product
|
|
50
|
+
|
|
51
|
+
### Mount the common static files
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from fastapi import FastAPI
|
|
55
|
+
from sme_sim_common.web import mount_common, mount_product_static
|
|
56
|
+
|
|
57
|
+
app = FastAPI()
|
|
58
|
+
mount_common(app) # serves /sme-sim-common/{shell,vendor,i18n,brand}/...
|
|
59
|
+
mount_product_static(app, STATIC_DIR) # the product's own /static (skipped if the directory is missing)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The common files live under their own prefix `/sme-sim-common`, never under `/static`: Starlette matches
|
|
63
|
+
mounts in registration order, so a nested path would be swallowed by the product's `/static` mount.
|
|
64
|
+
|
|
65
|
+
### Minimal app
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import argparse
|
|
69
|
+
from pathlib import Path
|
|
70
|
+
|
|
71
|
+
from sme_sim_common.cli import add_common_args, run_web
|
|
72
|
+
from sme_sim_common.web import make_base_app
|
|
73
|
+
|
|
74
|
+
ap = add_common_args(argparse.ArgumentParser(), default_http_port=8080, app_name="sme-my-sim")
|
|
75
|
+
args = ap.parse_args()
|
|
76
|
+
engine = MyEngine(...) # follows sme_sim_common.engine.EngineProtocol (or subclass BaseEngine)
|
|
77
|
+
app = make_base_app(engine, config_path=args.config, static_dir=Path(__file__).parent / "static")
|
|
78
|
+
run_web(app, args) # uvicorn on args.host / args.http_port, opens the browser
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Page skeleton
|
|
82
|
+
|
|
83
|
+
Start from `templates/static/index.html`. The load order is fixed:
|
|
84
|
+
|
|
85
|
+
1. `/sme-sim-common/shell/shell.css` then the product `/static/style.css`
|
|
86
|
+
2. `/sme-sim-common/shell/shell.js` in `<head>` with `data-storage="<dist name>"` (localStorage prefix;
|
|
87
|
+
the stored theme is applied before first paint)
|
|
88
|
+
3. `/sme-sim-common/vendor/vendor.js`, then `/sme-sim-common/i18n/common.js`
|
|
89
|
+
4. the product dictionaries `/static/i18n/en.js`, `ja.js`, `zh.js`, then `/static/app.js`
|
|
90
|
+
|
|
91
|
+
The common assets carry a `?v=` version query so browsers do not keep stale shell files after an upgrade;
|
|
92
|
+
`Shell.version` reports the sme-sim-common version and the About dialog shows it.
|
|
93
|
+
|
|
94
|
+
### Vendor
|
|
95
|
+
|
|
96
|
+
`vendor/vendor.js` registers the company preset `Shell.vendors.smartEdge` and the `vendor.*` strings.
|
|
97
|
+
Products pass it explicitly:
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
Shell.init({ ...PRODUCT, vendor: Shell.vendors.smartEdge });
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Company texts are maintained only here, so a change reaches every product with the next common release.
|
|
104
|
+
|
|
105
|
+
### i18n prefixes
|
|
106
|
+
|
|
107
|
+
| Prefix | Owner |
|
|
108
|
+
|---|---|
|
|
109
|
+
| `shell.*` | sme-sim-common: UI framework (menus, theme, language, help, About) — `shell.js` |
|
|
110
|
+
| `vendor.*` | sme-sim-common: company texts — `vendor/vendor.js` |
|
|
111
|
+
| `common.*` | sme-sim-common: features provided by the common layer — `i18n/common.js` |
|
|
112
|
+
| anything else | the product — `static/i18n/{en,ja,zh}.js` |
|
|
113
|
+
|
|
114
|
+
Products must not define the three reserved prefixes. Guard it in the product tests:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from sme_sim_common.testing import check_i18n_keys
|
|
118
|
+
|
|
119
|
+
def test_no_reserved_i18n_keys():
|
|
120
|
+
check_i18n_keys(STATIC_DIR)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`common.*` keys are named after the feature, not the menu position, and must be unambiguous in every
|
|
124
|
+
product of the series (e.g. `common.backend.connected`, not `ws.on`).
|
|
125
|
+
|
|
126
|
+
### Accent color
|
|
127
|
+
|
|
128
|
+
Each product has its own accent color. `shell.css` defaults to the brand blue `#087def`; a product
|
|
129
|
+
overrides `--accent`, `--accent-ink` and `--accent-weak` in three blocks of its `style.css`
|
|
130
|
+
(`:root`, `:root[data-theme="dark"]` and the `prefers-color-scheme: dark` block) — see
|
|
131
|
+
`templates/static/style.css`. The vendor area uses `--vendor-accent` (always brand blue) and must not be
|
|
132
|
+
overridden.
|
|
133
|
+
|
|
134
|
+
### Extension points
|
|
135
|
+
|
|
136
|
+
All optional; products pass them instead of re-implementing the standard routes.
|
|
137
|
+
|
|
138
|
+
- Device keys: `key_type` (default: the type of the `device_key` field, `int` or `str`) converts the path
|
|
139
|
+
parameter — `make_base_app(engine, device_key="device_id")` reads `/api/devices/1001/config` as `1001`.
|
|
140
|
+
- `create_hook(engine, body) -> dict` completes a new device before validation:
|
|
141
|
+
`create_hook=lambda e, b: {**b, "device_id": b.get("device_id") or free_id(e)}`.
|
|
142
|
+
- `duplicate_hook(engine, dev, new_key, body) -> dict` runs before the "already exists" check and may change the
|
|
143
|
+
key: `duplicate_hook=lambda e, d, k, b: {**d, "unit": free_unit(e)}`. Without a hook the copy is keyed
|
|
144
|
+
`{key}_copy` (`str` keys) or largest key + 1 (`int` keys).
|
|
145
|
+
- `simulation_hook(engine, rest) -> dict | None` gets the `POST /api/simulation` fields other than `running` /
|
|
146
|
+
`time_scale`; a dict result is merged into the response: `simulation_hook=lambda e, rest: e.apply_set_time(rest)`.
|
|
147
|
+
- `TemplateRegistry(..., extra_keys=("identification",))` allows extra top-level keys in user templates;
|
|
148
|
+
`registry.expand_device_extras(dev)` returns them merged with the device's own fields (non-empty device value
|
|
149
|
+
wins, dicts merged shallowly) and `device_as_template` exports them.
|
|
150
|
+
- `import_hooks={".csv": import_ede, ".zip": import_ede, ".yaml": import_text}` for `POST /api/config/import`:
|
|
151
|
+
`hook(engine, files: dict[name, bytes]) -> (config | None, messages[, config_text])`; `.xlsx` (and any other
|
|
152
|
+
extension) still goes to the `XlsxSchema`. The event log records the warning count and the first 5 warnings.
|
|
153
|
+
- `add_common_args(ap, ..., open_browser=False)`: do not open the browser by default (`--browser` opens it,
|
|
154
|
+
`--no-browser` is kept).
|
|
155
|
+
- `engine.wake()`: run one `_on_tick` (scan / push) now, without advancing simulated time or shifting the tick
|
|
156
|
+
schedule; thread-safe: `stack.on_write = lambda *a: engine.wake()`.
|
|
157
|
+
- `logger_name = "sme_my_sim.engine"` on a `BaseEngine` subclass names `self.logger`, used for all engine logging.
|
|
158
|
+
- `duplicate_hook` with a `src_key` keyword (or `**kwargs`) also gets the source device's key, e.g. "first free
|
|
159
|
+
key after the source": `def dup(engine, dev, new_key, body, *, src_key)`. Hooks without it are called as before;
|
|
160
|
+
`dev` is a fresh copy (source dump + new key), the source device is never passed.
|
|
161
|
+
- 404 for an unknown device shows the converted key: `unknown device 1008` (`int` keys unquoted).
|
|
162
|
+
- `Sheet(..., open_columns=True)`: on export the scalar fields without a column become dynamic columns (after the
|
|
163
|
+
known ones, in order), so `text_1` ... `text_12` round-trip; `export_rows` may also return
|
|
164
|
+
`(rows, {sheet: [(header, help), ...]})` to name extra columns explicitly (they come first).
|
|
165
|
+
- `export_rows` may return `(rows, extra_columns, warnings)` (`extra_columns` may be `None`): the product's own
|
|
166
|
+
export warnings (e.g. a dangling reference) come back from `export_xlsx` before the shared ones (cell limit).
|
|
167
|
+
- `Sheet(..., required_headers=("value",))`: the column must exist but cells may be empty (`required` keeps
|
|
168
|
+
"present and non-empty"); `skip_blank=("value",)` silently skips rows whose `value` is empty (key / value
|
|
169
|
+
sheets such as `Network`).
|
|
170
|
+
|
|
171
|
+
### Migration notes
|
|
172
|
+
|
|
173
|
+
- `state` is an instance attribute of `BaseEngine` (the result of `_build`); a subclass must not define a
|
|
174
|
+
`state()` method.
|
|
175
|
+
- `BaseEngine.event_history(limit)` is the common event log; it does not clash with a product's own history
|
|
176
|
+
(e.g. a per-point `history(device_id, key)` API).
|
|
177
|
+
- The routes of a FastAPI `APIRouter` passed in `product_routers` are flattened into `app.routes` (no
|
|
178
|
+
`include_router` wrapper); they remain `APIRoute`s, dependency injection works as usual.
|
|
179
|
+
|
|
180
|
+
### Tests and packaging
|
|
181
|
+
|
|
182
|
+
- The pytest plugin (asyncio timeout + hang dump) is activated automatically through the `pytest11`
|
|
183
|
+
entry point.
|
|
184
|
+
- The PyInstaller hook (`pyinstaller40` entry point) collects the common static files and the uvicorn
|
|
185
|
+
hidden imports, so product spec files list only their own files.
|
|
186
|
+
|
|
187
|
+
## Series port table
|
|
188
|
+
|
|
189
|
+
Default web UI ports, so that sibling products can run side by side. New products register here first.
|
|
190
|
+
|
|
191
|
+
| Product | Default HTTP port |
|
|
192
|
+
|---|---|
|
|
193
|
+
| SME BACnetSim | 8080 |
|
|
194
|
+
| SME ModbusSim | 8081 |
|
|
195
|
+
| OCPP simulator | TBD |
|
|
196
|
+
| SME SerialHub | TBD |
|
|
197
|
+
|
|
198
|
+
## Series accent table
|
|
199
|
+
|
|
200
|
+
| Product | `--accent` | `--accent-ink` | `--accent-weak` | Dark theme |
|
|
201
|
+
|---|---|---|---|---|
|
|
202
|
+
| SME BACnetSim | `#087def` (brand blue, shell default) | `#fff` | `#e7f2fe` | shell default (`#3d9bff` / `#fff` / `#11304f`) |
|
|
203
|
+
| SME ModbusSim | `#0f766e` (teal) | `#ffffff` | `#ccfbf1` | `#2dd4bf` / `#042f2e` / `#134e4a` |
|
|
204
|
+
| OCPP simulator | TBD | TBD | TBD | TBD |
|
|
205
|
+
| SME SerialHub | TBD | TBD | TBD | TBD |
|
|
206
|
+
|
|
207
|
+
## Versioning
|
|
208
|
+
|
|
209
|
+
- Semantic Versioning, first release `1.0.0`. Within 1.x only backward-compatible additions (new keys,
|
|
210
|
+
new optional parameters, new modules) and fixes; a breaking change would be 2.0 and is avoided.
|
|
211
|
+
- Products depend on `sme-sim-common>=1.0,<2`.
|
|
212
|
+
- The single version source is `sme_sim_common.__version__`; `Shell.version` carries the same value.
|
|
213
|
+
|
|
214
|
+
## Templates
|
|
215
|
+
|
|
216
|
+
See [`templates/CHECKLIST.md`](templates/CHECKLIST.md) for adopting the templates in a new or migrated
|
|
217
|
+
product (placeholders, port / accent registration, i18n rules, PyInstaller, PyPI trusted publishing).
|
|
218
|
+
|
|
219
|
+
## Releasing sme-sim-common
|
|
220
|
+
|
|
221
|
+
1. Update `__version__` and the `CHANGELOG.md` section `## [x.y.z] - YYYY-MM-DD`.
|
|
222
|
+
2. Push the tag `vx.y.z`. `.github/workflows/release.yml` builds the sdist and wheel, checks them with
|
|
223
|
+
`twine`, publishes to PyPI with trusted publishing (environment `pypi`) and creates the GitHub Release
|
|
224
|
+
with the CHANGELOG section as notes. The workflow file name `release.yml` and the environment name
|
|
225
|
+
`pypi` are registered on PyPI; do not rename them.
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# sme-sim-common
|
|
2
|
+
|
|
3
|
+
**English** | [日本語](README.ja.md) | [简体中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Shared runtime for the smart-edge simulator series (SME BACnetSim, SME ModbusSim, the OCPP simulator,
|
|
6
|
+
SME SerialHub): the web shell (header, menus, theme, languages, About), vendor texts, the `common.*`
|
|
7
|
+
UI strings, and the protocol-independent Python layer (web helpers, config I/O, behaviors, device
|
|
8
|
+
templates, test helpers, PyInstaller hook). The `templates/` directory holds the build-time skeleton
|
|
9
|
+
(CI, release, packaging, docs, page) that products copy by hand; it is not part of the wheel.
|
|
10
|
+
|
|
11
|
+
MIT-licensed, developed by [smart-edge (智届科技)](https://smartedge-tech.com).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install "sme-sim-common>=1.0,<2" # in a product's pyproject.toml dependencies
|
|
17
|
+
pip install "sme-sim-common[xlsx]>=1.0,<2" # with the .xlsx import / export helpers (openpyxl)
|
|
18
|
+
pip install -e ../sme-sim-common # local development against a checkout
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Python 3.11 or newer.
|
|
22
|
+
|
|
23
|
+
## Using it in a product
|
|
24
|
+
|
|
25
|
+
### Mount the common static files
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from fastapi import FastAPI
|
|
29
|
+
from sme_sim_common.web import mount_common, mount_product_static
|
|
30
|
+
|
|
31
|
+
app = FastAPI()
|
|
32
|
+
mount_common(app) # serves /sme-sim-common/{shell,vendor,i18n,brand}/...
|
|
33
|
+
mount_product_static(app, STATIC_DIR) # the product's own /static (skipped if the directory is missing)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The common files live under their own prefix `/sme-sim-common`, never under `/static`: Starlette matches
|
|
37
|
+
mounts in registration order, so a nested path would be swallowed by the product's `/static` mount.
|
|
38
|
+
|
|
39
|
+
### Minimal app
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import argparse
|
|
43
|
+
from pathlib import Path
|
|
44
|
+
|
|
45
|
+
from sme_sim_common.cli import add_common_args, run_web
|
|
46
|
+
from sme_sim_common.web import make_base_app
|
|
47
|
+
|
|
48
|
+
ap = add_common_args(argparse.ArgumentParser(), default_http_port=8080, app_name="sme-my-sim")
|
|
49
|
+
args = ap.parse_args()
|
|
50
|
+
engine = MyEngine(...) # follows sme_sim_common.engine.EngineProtocol (or subclass BaseEngine)
|
|
51
|
+
app = make_base_app(engine, config_path=args.config, static_dir=Path(__file__).parent / "static")
|
|
52
|
+
run_web(app, args) # uvicorn on args.host / args.http_port, opens the browser
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Page skeleton
|
|
56
|
+
|
|
57
|
+
Start from `templates/static/index.html`. The load order is fixed:
|
|
58
|
+
|
|
59
|
+
1. `/sme-sim-common/shell/shell.css` then the product `/static/style.css`
|
|
60
|
+
2. `/sme-sim-common/shell/shell.js` in `<head>` with `data-storage="<dist name>"` (localStorage prefix;
|
|
61
|
+
the stored theme is applied before first paint)
|
|
62
|
+
3. `/sme-sim-common/vendor/vendor.js`, then `/sme-sim-common/i18n/common.js`
|
|
63
|
+
4. the product dictionaries `/static/i18n/en.js`, `ja.js`, `zh.js`, then `/static/app.js`
|
|
64
|
+
|
|
65
|
+
The common assets carry a `?v=` version query so browsers do not keep stale shell files after an upgrade;
|
|
66
|
+
`Shell.version` reports the sme-sim-common version and the About dialog shows it.
|
|
67
|
+
|
|
68
|
+
### Vendor
|
|
69
|
+
|
|
70
|
+
`vendor/vendor.js` registers the company preset `Shell.vendors.smartEdge` and the `vendor.*` strings.
|
|
71
|
+
Products pass it explicitly:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
Shell.init({ ...PRODUCT, vendor: Shell.vendors.smartEdge });
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Company texts are maintained only here, so a change reaches every product with the next common release.
|
|
78
|
+
|
|
79
|
+
### i18n prefixes
|
|
80
|
+
|
|
81
|
+
| Prefix | Owner |
|
|
82
|
+
|---|---|
|
|
83
|
+
| `shell.*` | sme-sim-common: UI framework (menus, theme, language, help, About) — `shell.js` |
|
|
84
|
+
| `vendor.*` | sme-sim-common: company texts — `vendor/vendor.js` |
|
|
85
|
+
| `common.*` | sme-sim-common: features provided by the common layer — `i18n/common.js` |
|
|
86
|
+
| anything else | the product — `static/i18n/{en,ja,zh}.js` |
|
|
87
|
+
|
|
88
|
+
Products must not define the three reserved prefixes. Guard it in the product tests:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from sme_sim_common.testing import check_i18n_keys
|
|
92
|
+
|
|
93
|
+
def test_no_reserved_i18n_keys():
|
|
94
|
+
check_i18n_keys(STATIC_DIR)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`common.*` keys are named after the feature, not the menu position, and must be unambiguous in every
|
|
98
|
+
product of the series (e.g. `common.backend.connected`, not `ws.on`).
|
|
99
|
+
|
|
100
|
+
### Accent color
|
|
101
|
+
|
|
102
|
+
Each product has its own accent color. `shell.css` defaults to the brand blue `#087def`; a product
|
|
103
|
+
overrides `--accent`, `--accent-ink` and `--accent-weak` in three blocks of its `style.css`
|
|
104
|
+
(`:root`, `:root[data-theme="dark"]` and the `prefers-color-scheme: dark` block) — see
|
|
105
|
+
`templates/static/style.css`. The vendor area uses `--vendor-accent` (always brand blue) and must not be
|
|
106
|
+
overridden.
|
|
107
|
+
|
|
108
|
+
### Extension points
|
|
109
|
+
|
|
110
|
+
All optional; products pass them instead of re-implementing the standard routes.
|
|
111
|
+
|
|
112
|
+
- Device keys: `key_type` (default: the type of the `device_key` field, `int` or `str`) converts the path
|
|
113
|
+
parameter — `make_base_app(engine, device_key="device_id")` reads `/api/devices/1001/config` as `1001`.
|
|
114
|
+
- `create_hook(engine, body) -> dict` completes a new device before validation:
|
|
115
|
+
`create_hook=lambda e, b: {**b, "device_id": b.get("device_id") or free_id(e)}`.
|
|
116
|
+
- `duplicate_hook(engine, dev, new_key, body) -> dict` runs before the "already exists" check and may change the
|
|
117
|
+
key: `duplicate_hook=lambda e, d, k, b: {**d, "unit": free_unit(e)}`. Without a hook the copy is keyed
|
|
118
|
+
`{key}_copy` (`str` keys) or largest key + 1 (`int` keys).
|
|
119
|
+
- `simulation_hook(engine, rest) -> dict | None` gets the `POST /api/simulation` fields other than `running` /
|
|
120
|
+
`time_scale`; a dict result is merged into the response: `simulation_hook=lambda e, rest: e.apply_set_time(rest)`.
|
|
121
|
+
- `TemplateRegistry(..., extra_keys=("identification",))` allows extra top-level keys in user templates;
|
|
122
|
+
`registry.expand_device_extras(dev)` returns them merged with the device's own fields (non-empty device value
|
|
123
|
+
wins, dicts merged shallowly) and `device_as_template` exports them.
|
|
124
|
+
- `import_hooks={".csv": import_ede, ".zip": import_ede, ".yaml": import_text}` for `POST /api/config/import`:
|
|
125
|
+
`hook(engine, files: dict[name, bytes]) -> (config | None, messages[, config_text])`; `.xlsx` (and any other
|
|
126
|
+
extension) still goes to the `XlsxSchema`. The event log records the warning count and the first 5 warnings.
|
|
127
|
+
- `add_common_args(ap, ..., open_browser=False)`: do not open the browser by default (`--browser` opens it,
|
|
128
|
+
`--no-browser` is kept).
|
|
129
|
+
- `engine.wake()`: run one `_on_tick` (scan / push) now, without advancing simulated time or shifting the tick
|
|
130
|
+
schedule; thread-safe: `stack.on_write = lambda *a: engine.wake()`.
|
|
131
|
+
- `logger_name = "sme_my_sim.engine"` on a `BaseEngine` subclass names `self.logger`, used for all engine logging.
|
|
132
|
+
- `duplicate_hook` with a `src_key` keyword (or `**kwargs`) also gets the source device's key, e.g. "first free
|
|
133
|
+
key after the source": `def dup(engine, dev, new_key, body, *, src_key)`. Hooks without it are called as before;
|
|
134
|
+
`dev` is a fresh copy (source dump + new key), the source device is never passed.
|
|
135
|
+
- 404 for an unknown device shows the converted key: `unknown device 1008` (`int` keys unquoted).
|
|
136
|
+
- `Sheet(..., open_columns=True)`: on export the scalar fields without a column become dynamic columns (after the
|
|
137
|
+
known ones, in order), so `text_1` ... `text_12` round-trip; `export_rows` may also return
|
|
138
|
+
`(rows, {sheet: [(header, help), ...]})` to name extra columns explicitly (they come first).
|
|
139
|
+
- `export_rows` may return `(rows, extra_columns, warnings)` (`extra_columns` may be `None`): the product's own
|
|
140
|
+
export warnings (e.g. a dangling reference) come back from `export_xlsx` before the shared ones (cell limit).
|
|
141
|
+
- `Sheet(..., required_headers=("value",))`: the column must exist but cells may be empty (`required` keeps
|
|
142
|
+
"present and non-empty"); `skip_blank=("value",)` silently skips rows whose `value` is empty (key / value
|
|
143
|
+
sheets such as `Network`).
|
|
144
|
+
|
|
145
|
+
### Migration notes
|
|
146
|
+
|
|
147
|
+
- `state` is an instance attribute of `BaseEngine` (the result of `_build`); a subclass must not define a
|
|
148
|
+
`state()` method.
|
|
149
|
+
- `BaseEngine.event_history(limit)` is the common event log; it does not clash with a product's own history
|
|
150
|
+
(e.g. a per-point `history(device_id, key)` API).
|
|
151
|
+
- The routes of a FastAPI `APIRouter` passed in `product_routers` are flattened into `app.routes` (no
|
|
152
|
+
`include_router` wrapper); they remain `APIRoute`s, dependency injection works as usual.
|
|
153
|
+
|
|
154
|
+
### Tests and packaging
|
|
155
|
+
|
|
156
|
+
- The pytest plugin (asyncio timeout + hang dump) is activated automatically through the `pytest11`
|
|
157
|
+
entry point.
|
|
158
|
+
- The PyInstaller hook (`pyinstaller40` entry point) collects the common static files and the uvicorn
|
|
159
|
+
hidden imports, so product spec files list only their own files.
|
|
160
|
+
|
|
161
|
+
## Series port table
|
|
162
|
+
|
|
163
|
+
Default web UI ports, so that sibling products can run side by side. New products register here first.
|
|
164
|
+
|
|
165
|
+
| Product | Default HTTP port |
|
|
166
|
+
|---|---|
|
|
167
|
+
| SME BACnetSim | 8080 |
|
|
168
|
+
| SME ModbusSim | 8081 |
|
|
169
|
+
| OCPP simulator | TBD |
|
|
170
|
+
| SME SerialHub | TBD |
|
|
171
|
+
|
|
172
|
+
## Series accent table
|
|
173
|
+
|
|
174
|
+
| Product | `--accent` | `--accent-ink` | `--accent-weak` | Dark theme |
|
|
175
|
+
|---|---|---|---|---|
|
|
176
|
+
| SME BACnetSim | `#087def` (brand blue, shell default) | `#fff` | `#e7f2fe` | shell default (`#3d9bff` / `#fff` / `#11304f`) |
|
|
177
|
+
| SME ModbusSim | `#0f766e` (teal) | `#ffffff` | `#ccfbf1` | `#2dd4bf` / `#042f2e` / `#134e4a` |
|
|
178
|
+
| OCPP simulator | TBD | TBD | TBD | TBD |
|
|
179
|
+
| SME SerialHub | TBD | TBD | TBD | TBD |
|
|
180
|
+
|
|
181
|
+
## Versioning
|
|
182
|
+
|
|
183
|
+
- Semantic Versioning, first release `1.0.0`. Within 1.x only backward-compatible additions (new keys,
|
|
184
|
+
new optional parameters, new modules) and fixes; a breaking change would be 2.0 and is avoided.
|
|
185
|
+
- Products depend on `sme-sim-common>=1.0,<2`.
|
|
186
|
+
- The single version source is `sme_sim_common.__version__`; `Shell.version` carries the same value.
|
|
187
|
+
|
|
188
|
+
## Templates
|
|
189
|
+
|
|
190
|
+
See [`templates/CHECKLIST.md`](templates/CHECKLIST.md) for adopting the templates in a new or migrated
|
|
191
|
+
product (placeholders, port / accent registration, i18n rules, PyInstaller, PyPI trusted publishing).
|
|
192
|
+
|
|
193
|
+
## Releasing sme-sim-common
|
|
194
|
+
|
|
195
|
+
1. Update `__version__` and the `CHANGELOG.md` section `## [x.y.z] - YYYY-MM-DD`.
|
|
196
|
+
2. Push the tag `vx.y.z`. `.github/workflows/release.yml` builds the sdist and wheel, checks them with
|
|
197
|
+
`twine`, publishes to PyPI with trusted publishing (environment `pypi`) and creates the GitHub Release
|
|
198
|
+
with the CHANGELOG section as notes. The workflow file name `release.yml` and the environment name
|
|
199
|
+
`pypi` are registered on PyPI; do not rename them.
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sme-sim-common"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Shared web shell, brand, i18n and Python helpers for the smart-edge simulator family"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"License :: OSI Approved :: MIT License",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"starlette",
|
|
18
|
+
"fastapi>=0.110",
|
|
19
|
+
"uvicorn[standard]",
|
|
20
|
+
"pydantic>=2",
|
|
21
|
+
"pyyaml>=6",
|
|
22
|
+
"python-multipart",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
xlsx = ["openpyxl>=3.1"]
|
|
27
|
+
dev = ["pytest", "httpx", "openpyxl>=3.1", "pyinstaller"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/smartedge-tech-dev/sme-sim-common"
|
|
31
|
+
|
|
32
|
+
[project.entry-points.pytest11]
|
|
33
|
+
sme_sim_common = "sme_sim_common.testing.plugin"
|
|
34
|
+
|
|
35
|
+
[project.entry-points.pyinstaller40]
|
|
36
|
+
hook-dirs = "sme_sim_common.__pyinstaller:get_hook_dirs"
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.dynamic]
|
|
39
|
+
version = { attr = "sme_sim_common.__version__" }
|
|
40
|
+
|
|
41
|
+
[tool.setuptools.packages.find]
|
|
42
|
+
include = ["sme_sim_common*"]
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.package-data]
|
|
45
|
+
sme_sim_common = ["static/**/*"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""PyInstaller integration (``pyinstaller40`` entry point): products bundling sme-sim-common get its static
|
|
2
|
+
assets and the uvicorn / multipart hidden imports without listing them in their spec."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_hook_dirs():
|
|
8
|
+
return [os.path.dirname(__file__)]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# PyInstaller hook for sme_sim_common (found through the pyinstaller40 "hook-dirs" entry point).
|
|
2
|
+
# Mirrors what sme-bac-sim v0.4.0 packaging/sme-bacnet-sim.spec listed by hand for the web stack.
|
|
3
|
+
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
|
|
4
|
+
|
|
5
|
+
datas = collect_data_files("sme_sim_common", includes=["static/**/*"])
|
|
6
|
+
|
|
7
|
+
hiddenimports = collect_submodules("uvicorn") + [
|
|
8
|
+
"multipart",
|
|
9
|
+
"python_multipart",
|
|
10
|
+
"yaml",
|
|
11
|
+
"h11",
|
|
12
|
+
"websockets",
|
|
13
|
+
"httptools",
|
|
14
|
+
]
|