Skip to main content

pyhj212

HJ 212-2017 协议核心协议的 Python 实现,提供消息编解码、CRC 校验、协议验证以及 CP 字段解析功能。

特性

功能 状态
消息解析 ✅
消息编码 ✅
CRC-16 计算与校验 ✅
CP 字段解析与编码 ✅
协议验证 ✅
Dataclass 数据模型 ✅

快速开始

从 PyPI 安装

pip install pyhj212

项目地址:https://pypi.org/project/pyhj212/

从源码安装(开发模式)

git clone https://github.com/tinychen/pyhj212.git
cd pyhj212
uv sync --all-extras   # 或 pip install -e ".[dev]"

解析消息

from pyhj212 import parse

raw = (
    "##0101QN=20160801085857223;ST=32;CN=1062;PW=100000;"
    "MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&&"
    "1C80\r\n"
)

msg = parse(raw)

print(msg.header.cn)   # "1062"
print(msg.header.mn)   # "010000A8900016F000169DC0"
print(msg.data)        # "RtdInterval=30"
print(msg.crc)         # "1C80"

# 自动解析 CP 字段
for f in msg.cp_fields:
    print(f.to_dict())
# {"RtdInterval": "30"}

编码消息

from pyhj212 import encode, Header

# 字符串 data
header = Header(
    qn="20160801085857223", st="32", cn="1062", pw="100000",
    mn="010000A8900016F000169DC0", flag="5",
)
raw = encode(header, "RtdInterval=30")

# list[dict] data(推荐)
raw = encode(
    header={"CN": "1062", "PW": "100000", "MN": "ST0001"},
    data=[
        {"DataTime": "20261118134500"},
        {"tn0112": {"Rtd": "228.200", "Flag": "N"}},
    ],
)

验证消息

from pyhj212 import validate

result = validate(raw)

if not result.is_valid:
    for error in result.errors:
        print(f"错误: {error}")

CP 字段解析

CP 数据区由 ; 分隔的键值对组成,, 分隔的逗号段属于同一监测因子的多个属性。

from pyhj212 import parse_cp, encode_cp

# 解析
fields = parse_cp("DataTime=20261118134500;tn0112-Rtd=228.200,tn0112-Flag=N")

for f in fields:
    if f.children:
        print(f"{f.key}:")
        for c in f.children:
            print(f"  {c.key} = {c.value}")
    else:
        print(f"{f.key} = {f.value}")
# DataTime = 20261118134500
# tn0112:
#   Rtd = 228.200
#   Flag = N

# 编码
raw = encode_cp(fields)

# JSON 序列化
import json
print(json.dumps([f.to_dict() for f in fields], ensure_ascii=False, indent=2))

开发

参见 DEVELOPMENT.md。

# 运行测试
uv run pytest

# 代码检查
uv run ruff check src/pyhj212 tests
uv run ruff format src/pyhj212 tests
uv run mypy src/pyhj212

协议

  • HJ 212-2017 《污染物在线监控(监测)系统数据传输标准》

License

MIT

Metadata

Release files for pyhj212 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhj212 0.1.2
File Size Uploaded Provenance Actions
pyhj212-0.1.2.tar.gz 15.0 kB Without provenance Details for pyhj212-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyhj212 0.1.2
File Interpreter ABI Platform Actions
pyhj212-0.1.2-py3-none-any.whl Python 3 none any Details for pyhj212-0.1.2-py3-none-any.whl

Total release size: 33.6 kB

Release files / pyhj212-0.1.2.tar.gz

Download URL pyhj212-0.1.2.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d8da758058fdac43f2948a11332a15ab0686c95173024e9ee275c06c0c88b8cd
BLAKE2b-256 checksum
How to use checksums
00fa92f93564ad4d046b7da98d61226309c79de160b328ba8ceb9ec0242a963b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release files / pyhj212-0.1.2-py3-none-any.whl

Download URL pyhj212-0.1.2-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51cff4ea1e41b42a1f8c9e64b9db053705adaf8805c7d0444e717fa4e1190472
BLAKE2b-256 checksum
How to use checksums
b71a8ac129cf2eeb2c4f1c8ac0056b372fe3b62586928607d2647c58ecf7c418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Security for pyhj212 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page