wiki-plugin-mech 0.1.2 → 0.1.3

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.
package/client/mech.js CHANGED
@@ -36,9 +36,9 @@
36
36
  for (part of more) {
37
37
  const key = `${unique}.${path.join('.')}`
38
38
  part.key = key
39
- if(part.command)
39
+ if('command' in part)
40
40
  html.push(`<span id=${key}>${expand(part.command)}</span>`)
41
- else if(typeof part == 'array')
41
+ else
42
42
  html.push(`<div id=${key} style="padding-left:15px">${block(part,[...path,0])}</div> `)
43
43
  path[path.length-1]++
44
44
  }
@@ -53,47 +53,91 @@
53
53
  elem.outerHTML += `<span style="width:80%;color:gray;">${message}</span>` })
54
54
  }
55
55
 
56
- function click_emit (elem) {
56
+ function click_emit ({elem,body,state}) {
57
57
  if(elem.innerHTML.match(/button/)) return
58
- const body = this
59
- if (!body.length) return trouble(elem,'CLICK expects indented blocks to follow.')
58
+ if (!body?.length) return trouble(elem,'CLICK expects indented blocks to follow.')
60
59
  elem.innerHTML += '<button style="border-width:0;">◉</button>'
61
- elem.querySelector('button').addEventListener('click',event => run(this))
60
+ elem.querySelector('button').addEventListener('click',event => run(body,state))
62
61
  }
63
62
 
64
- function click_bind (elem) {
65
- }
66
-
67
- function hello_emit (elem,what) {
68
- const want = what == 'world' ? ' 🌎' : ' 😀'
63
+ function hello_emit ({elem,args}) {
64
+ const want = args[0] == 'world' ? ' 🌎' : ' 😀'
69
65
  elem.innerHTML += want
70
66
  }
71
67
 
72
- function hello_bind (elem) {
68
+ function from_emit ({elem,args,body,state}) {
69
+ const line = elem.innerHTML
70
+ elem.innerHTML = line + ' ⏳'
71
+ fetch(`//${args[0]}.json`)
72
+ .then(res => res.json())
73
+ .then(page => {
74
+ state.page = page
75
+ elem.innerHTML = line + ' ⌛'
76
+ run(body,state)
77
+ })
78
+ }
79
+
80
+ function sensor_emit ({elem,args,body,state}) {
81
+ const line = elem.innerHTML.replaceAll(/ ⌛/g,'')
82
+ if(!('page' in state)) return trouble(elem,`Expect "page" as with FROM.`)
83
+ const datalog = state.page.story.find(item => item.type == 'datalog')
84
+ if(!datalog) return trouble(elem, `Expect Datalog plugin in the page.`)
85
+ const want = args[0]
86
+ if(!want) return trouble(elem, 'SENSOR needs a sensor name.')
87
+ const sensor = datalog.text.split(/\n/)
88
+ .map(line => line.split(/ +/))
89
+ .filter(fields => fields[0] == 'SENSOR')
90
+ .find(fields => fields[1] == want)
91
+ if(!sensor) return trouble(elem, `Expect to find "${want}" in Datalog.`)
92
+ const url = sensor[2]
93
+
94
+ const f = c => 9/5*(c/16)+32
95
+ const avg = a => a.reduce((s,e)=>s+e,0)/a.length
96
+ elem.innerHTML = line + ' ⏳'
97
+ fetch(url)
98
+ .then (res => res.json())
99
+ .then (data => {
100
+ elem.innerHTML = line + ' ⌛'
101
+ const value = f(avg(Object.values(data)))
102
+ state.temperature = `${value.toFixed(2)}°F`
103
+ run(body,state)
104
+ })
105
+ }
106
+
107
+ function report_emit ({elem,command,state}) {
108
+ const value = state?.temperature
109
+ if (!value) return trouble(elem,`Expect data, as from SENSOR.`)
110
+ elem.innerHTML = command + `<br><font face=Arial size=32>${value}</font>`
73
111
  }
74
112
 
75
113
  const blocks = {
76
- CLICK: {emit:click_emit, bind:click_bind},
77
- HELLO: {emit:hello_emit, bind:hello_bind}
114
+ CLICK: {emit:click_emit, bind:null},
115
+ HELLO: {emit:hello_emit, bind:null},
116
+ FROM: {emit:from_emit, bind:null},
117
+ SENSOR: {emit:sensor_emit, bind:null},
118
+ REPORT: {emit:report_emit, bind:null}
78
119
  }
79
120
 
80
- function run (nest) {
121
+ function run (nest,state={}) {
81
122
  const scope = nest.slice()
82
123
  while (scope.length) {
83
124
  const code = scope.shift()
84
- if (code.command) {
125
+ if ('command' in code) {
126
+ const command = code.command
85
127
  const elem = document.getElementById(code.key)
86
128
  const [op, ...args] = code.command.split(/ +/)
87
- const more = scope[0]?.command ? null : scope.shift()
129
+ const next = scope[0]
130
+ const body = next && ('command' in next) ? null : scope.shift()
131
+ const stuff = {command,op,args,body,elem,state}
88
132
  if (blocks[op])
89
- blocks[op].emit.apply(more||[],[elem,...args])
133
+ blocks[op].emit.apply(null,[stuff])
90
134
  else
91
135
  if (op.match(/^[A-Z]+$/))
92
136
  trouble(elem,`${op} doesn't name a block we know.`)
93
137
  else if (code.command.match(/\S/))
94
138
  trouble(elem, `Expected line to begin with all-caps keyword.`)
95
139
  } else if(typeof code == 'array') {
96
- run(code)
140
+ run(code,state)
97
141
  }
98
142
  }
99
143
  }
package/factory.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "Mech",
3
- "title": "Mech Plugin",
3
+ "title": "Script Mechanisms",
4
4
  "category": "other"
5
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-mech",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Federated Wiki - Mechanism Scripting Plugin",
5
5
  "keywords": [
6
6
  "mech",