openhmi-nx: decode, encode, render Nextion HMI projects; .tft research
Black-box reverse-engineered .HMI format (container, three checksums, pages, all 31 component types, pictures, fonts), readable JSON, page renderer matching Nextion Editor, demo project built from code and a test suite (66 tests). See docs/COVERAGE.md for status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69e22cfb06
70 files changed
+25058
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import unittest
|
||||
|
||||
from helpers import FIXTURE
|
||||
|
||||
from nxhmi.container import HMIFile
|
||||
from nxhmi.page import Attr, Component, Group, Page
|
||||
|
||||
|
||||
def small_page():
|
||||
pc = Component([Group("att", [Attr.encode("type", "u8", 121), Attr.encode("id", "u8", 0),
|
||||
Attr.encode("objname", "str", "p0"), Attr.encode("w", "u16", 480)]),
|
||||
Group("codesload", []), Group("codesdown", [b"page 1"])])
|
||||
t = Component([Group("att", [Attr.encode("type", "u8", 116), Attr.encode("id", "u8", 1),
|
||||
Attr.encode("objname", "str", "t0"), Attr.encode("txt", "str", "hi"),
|
||||
Attr.encode("txt_maxl", "u16", 10), Attr.encode("dir", "u8", 1)])])
|
||||
return Page("p0", [pc, t])
|
||||
|
||||
|
||||
class PageModel(unittest.TestCase):
|
||||
def test_roundtrip(self):
|
||||
blob = small_page().pack()
|
||||
self.assertEqual(Page.parse(blob).pack(), blob)
|
||||
|
||||
def test_checksum_verified_on_parse(self):
|
||||
blob = bytearray(small_page().pack())
|
||||
blob[-5] ^= 1
|
||||
with self.assertRaises(ValueError):
|
||||
Page.parse(bytes(blob))
|
||||
Page.parse(bytes(blob), verify=False)
|
||||
|
||||
def test_header_fields(self):
|
||||
p = Page.parse(small_page().pack())
|
||||
self.assertEqual((p.name, p.b21, p.version), ("p0", 0x4F, 0x21))
|
||||
self.assertEqual(len(p.components), 2)
|
||||
|
||||
def test_string_detection_by_companion(self):
|
||||
t = Page.parse(small_page().pack()).component("t0")
|
||||
self.assertEqual(t.attr("txt").decoded()[0], "str") # has txt_maxl
|
||||
self.assertEqual(t.attr("dir").decoded(), ("u8", 1)) # no dir_m -> number
|
||||
self.assertEqual(t.attr("objname").decoded()[0], "str")
|
||||
|
||||
def test_set_get_and_grow(self):
|
||||
p = Page.parse(small_page().pack())
|
||||
t = p.component("t0")
|
||||
t.set("txt", "a much longer text")
|
||||
t.set_event("codesdown", ["click t0,1", "page 0"])
|
||||
q = Page.parse(p.pack())
|
||||
self.assertEqual(q.component("t0").get("txt"), "a much longer text")
|
||||
self.assertEqual(q.component("t0").event("codesdown"), ["click t0,1", "page 0"])
|
||||
|
||||
def test_signed_values(self):
|
||||
a = Attr.encode("val", "u32", -1234567)
|
||||
self.assertEqual(a.value, (-1234567 & 0xFFFFFFFF).to_bytes(4, "little"))
|
||||
|
||||
def test_editor_pages_roundtrip(self):
|
||||
h = HMIFile.load(FIXTURE)
|
||||
for e in h.live():
|
||||
if e.name.endswith(".pa"):
|
||||
blob = h.read(e.name)
|
||||
self.assertEqual(Page.parse(blob).pack(), blob, e.name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user