code wiki / _hdl_build / nx_household_gate.nx
nx_household_gate.nx source
↩ module page · 132 lines · 6815 B
1// nx_household_gate.nx -- GATE: R-HOME. The household command center -- one dashboard, every engine. Proves:
2// T1 tonight's dinner (live from the pantry), T2 expiring tile, T3 shopping-list tile, T4 grow-&-save tile,
3// T5 estate net-worth tile, T6 sovereign, T7 SHIPPED via publisher. license_tier: ORIGINAL
4import "nx_household.nx"
5import "nx_inventory.nx"
6import "nx_garden.nx"
7import "nx_publisher.nx"
8import "nx_food_science.nx"
9import "nx_syscalls.nx"
10
11const HG_FOOD: *u8 = "knowledge/store/food-"
12const HG_INV: *u8 = "knowledge/store/inv-"
13const HG_PAN: *u8 = "household"
14const HG_EST: *u8 = "hhestate"
15const HG_Q: *u8 = "knowledge/publish/home-queue.tsv"
16const HG_LED: *u8 = "knowledge/publish/home-ledger.tsv"
17const HG_STG: *u8 = "knowledge/publish/home-stage"
18const HG_LIVE: *u8 = "knowledge/publish/home-live"
19const HG_STAGE_FILE: *u8 = "knowledge/staging/home/dashboard.html"
20const HG_LIVE_FILE: *u8 = "knowledge/publish/home-live/dashboard.html"
21
22func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
23func g_i(v: i64) -> i64 {
24 let bb: *u8 = sys_mmap(28); var m: i64 = v
25 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
26 let t: *u8 = sys_mmap(28); var k: i64 = 0
27 if m == 0 { t[0] = 48 as u8; k = 1 }
28 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
29 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
30}
31func g_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
32func hg_stock(id: *u8, name: *u8, qty: i64, unit: *u8, expiry: i64, par: i64) -> i64 {
33 iv_add(HG_INV, HG_PAN, id, name, "food" as *u8, qty, unit, "kitchen" as *u8, 0, expiry, "-" as *u8)
34 iv_set_par(HG_INV, HG_PAN, id, par)
35 return 0
36}
37
38func main() -> i64 {
39 g_p("=== nx_household_gate (R-HOME: the household command center) ===\n" as *u8)
40 fd_seed(HG_FOOD)
41 gd_seed(HG_FOOD)
42
43 // a realistic household kitchen (today = day 0)
44 hg_stock("chicken" as *u8, "Chicken" as *u8, 2, "lb" as *u8, 3, 2)
45 hg_stock("tofu" as *u8, "Tofu" as *u8, 1, "block" as *u8, 2, 2)
46 hg_stock("broccoli" as *u8, "Broccoli" as *u8, 2, "head" as *u8, 5, 2)
47 hg_stock("onion" as *u8, "Onion" as *u8, 3, "ea" as *u8, 20, 2)
48 hg_stock("garlic" as *u8, "Garlic" as *u8, 2, "bulb" as *u8, 60, 1)
49 hg_stock("ginger" as *u8, "Ginger" as *u8, 2, "knob" as *u8, 14, 1)
50 hg_stock("soy" as *u8, "Soy Sauce" as *u8, 2, "bottle" as *u8, 365, 1)
51 hg_stock("oyster" as *u8, "Oyster Sauce" as *u8, 1, "bottle" as *u8, 200, 1)
52 hg_stock("rice" as *u8, "Rice" as *u8, 2, "bag" as *u8, 0, 1)
53 hg_stock("eggs" as *u8, "Eggs" as *u8, 1, "dozen" as *u8, 10, 3)
54 hg_stock("milk" as *u8, "Milk" as *u8, 0, "carton" as *u8, 7, 2)
55 // a small estate cataloged for the will
56 iv_add(HG_INV, HG_EST, "clock" as *u8, "Grandfather clock" as *u8, "heirloom" as *u8, 1, "ea" as *u8, "living room" as *u8, 250000, 0, "Sarah" as *u8)
57 iv_add(HG_INV, HG_EST, "ring" as *u8, "Wedding ring" as *u8, "jewelry" as *u8, 1, "ea" as *u8, "safe" as *u8, 800000, 0, "Emma" as *u8)
58
59 let dash: *u8 = sys_mmap(65536)
60 let nd: i64 = hh_render_dashboard(HG_FOOD, HG_INV, HG_PAN, HG_EST, "The Family Home" as *u8, dash)
61 g_p("dashboard = " as *u8); g_i(nd); g_p(" bytes\n" as *u8)
62
63 // derive tonight's protein for the assertion
64 let world: *i64 = fd_world_open(HG_FOOD)
65 let nmarr: *i64 = world[4] as *i64
66 let avail: *i64 = sys_mmap(8 * 64) as *i64
67 fp_pantry_avail(world, HG_INV, HG_PAN, avail)
68 let b: *i64 = sys_mmap(8 * 16) as *i64; let r: *i64 = sys_mmap(8 * 16) as *i64
69 let nb: i64 = fp_choose(HG_FOOD, world, avail, b, r)
70 let protein: *u8 = nmarr[b[0]] as *u8
71
72 var pass: i64 = 0
73 var tot: i64 = 0
74
75 // T1 tonight's dinner
76 tot = tot + 1
77 var ok1: i64 = 1
78 if as_contains(dash, nd, "Tonight you can make" as *u8) != 1 { ok1 = 0 }
79 if nb != 5 { ok1 = 0 }
80 if as_contains(dash, nd, protein) != 1 { ok1 = 0 }
81 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 tonight's dinner tile (live: protein=" as *u8); g_p(protein); g_p(")\n" as *u8) } else { g_p("FAIL T1\n" as *u8) }
82
83 // T2 expiring tile (tofu d2 + chicken d3)
84 tot = tot + 1
85 var ok2: i64 = 1
86 if as_contains(dash, nd, "expiring" as *u8) != 1 { ok2 = 0 }
87 if as_contains(dash, nd, "Tofu" as *u8) != 1 { ok2 = 0 }
88 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 expiring tile (Tofu flagged to use soon)\n" as *u8) } else { g_p("FAIL T2\n" as *u8) }
89
90 // T3 shopping-list tile (milk q0, eggs q1<3)
91 tot = tot + 1
92 var ok3: i64 = 1
93 if as_contains(dash, nd, "Shopping list" as *u8) != 1 { ok3 = 0 }
94 if as_contains(dash, nd, "Milk" as *u8) != 1 { ok3 = 0 }
95 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 shopping-list tile (Milk to buy)\n" as *u8) } else { g_p("FAIL T3\n" as *u8) }
96
97 // T4 grow-&-save tile
98 tot = tot + 1
99 var ok4: i64 = 1
100 if as_contains(dash, nd, "Grow" as *u8) != 1 { ok4 = 0 }
101 if as_contains(dash, nd, "/ year" as *u8) != 1 { ok4 = 0 }
102 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 grow-and-save tile (annual savings)\n" as *u8) } else { g_p("FAIL T4\n" as *u8) }
103
104 // T5 estate tile (net worth $10500.00)
105 tot = tot + 1
106 var ok5: i64 = 1
107 if as_contains(dash, nd, "Estate" as *u8) != 1 { ok5 = 0 }
108 if as_contains(dash, nd, "$10500.00" as *u8) != 1 { ok5 = 0 }
109 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 estate tile (net worth $10500.00)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) }
110
111 // T6 sovereign
112 tot = tot + 1
113 if as_has_thirdparty_js(dash, nd) == 0 { pass = pass + 1; g_p("PASS T6 sovereign (0 third-party JS in the dashboard)\n" as *u8) } else { g_p("FAIL T6\n" as *u8) }
114
115 // T7 ship
116 sys_mkdir("knowledge/staging" as *u8, 0x1ed)
117 sys_mkdir("knowledge/staging/home" as *u8, 0x1ed)
118 let sfd: i64 = sys_openat_wr(HG_STAGE_FILE, 420); if sfd >= 0 { sys_write(sfd, dash, nd); sys_close(sfd) }
119 pub_init()
120 sys_mkdir(HG_STG, 0x1ed)
121 sys_mkdir(HG_LIVE, 0x1ed)
122 pub_submit_to(HG_Q, HG_STAGE_FILE, "dashboard.html" as *u8, "nishifamily" as *u8, "nishi-home" as *u8, "internal" as *u8)
123 let pubd: i64 = pub_run_full(HG_Q, HG_LED, HG_STG, HG_LIVE, "publish:home" as *u8)
124 g_p(" pub_run_full -> published_this_pass=" as *u8); g_i(pubd); g_p("\n" as *u8)
125 tot = tot + 1
126 if g_exists(HG_LIVE_FILE) == 1 { pass = pass + 1; g_p("PASS T7 SHIPPED the command center to liveroot\n" as *u8) } else { g_p("FAIL T7\n" as *u8) }
127
128 g_p("nx_household_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
129 if pass == tot { g_p(" verdict=GREEN (the household command center: every engine, one sovereign dashboard)\n" as *u8); return 0 }
130 g_p(" verdict=RED\n" as *u8)
131 return 1
132}