JS-Client and systemd unit added
authorBenjamin Braatz <bb@bbraatz.eu>
Wed, 13 Jan 2021 05:11:17 +0000 (06:11 +0100)
committerBenjamin Braatz <bb@bbraatz.eu>
Wed, 13 Jan 2021 05:11:17 +0000 (06:11 +0100)
controlpi.service [new file with mode: 0644]
web/index.html [new file with mode: 0644]

diff --git a/controlpi.service b/controlpi.service
new file mode 100644 (file)
index 0000000..0ecd8de
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=Control Pi Service
+
+[Service]
+WorkingDirectory=/home/pi
+Environment=PYTHONUNBUFFERED=1
+ExecStart=/home/pi/controlpi/bin/python -m graphit_controlpi.main conf.json web/
+
+[Install]
+WantedBy=multi-user.target
diff --git a/web/index.html b/web/index.html
new file mode 100644 (file)
index 0000000..ab135ba
--- /dev/null
@@ -0,0 +1,90 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>ControlPi</title>
+       <link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
+    </head>
+    <body>
+        <script>
+            var ws = new WebSocket("ws://" + window.location.host)
+            ws.addEventListener('message', function (event) {
+                var data = JSON.parse(event.data)
+                switch (data.event) {
+                    case 'pinstate':
+                        var section_name = data.pin.split('-')[0]
+                        var section_root = document.getElementById('sections')
+                        if (section_root == null) {
+                            section_root = document.createElement('ul')
+                            document.body.appendChild(section_root)
+                            section_root.setAttribute('id', 'sections')
+                        }
+                        var section = document.getElementById(section_name)
+                        if (section == null) {
+                            var li = document.createElement('li')
+                            section_root.appendChild(li)
+                            li.setAttribute('id', section_name + '-li')
+                            var h2 = document.createElement('h2')
+                            li.appendChild(h2)
+                            var h2_content = document.createTextNode(section_name)
+                            h2.appendChild(h2_content)
+                            section = document.createElement('ul')
+                            li.appendChild(section)
+                            section.setAttribute('id', section_name)
+                            var sections = section_root.childNodes
+                            var section_array = []
+                            for (var s in sections) {
+                                if (sections[s].nodeType == 1) {
+                                    section_array.push(sections[s])
+                                }
+                            }
+                            section_array.sort(function (a, b) {
+                                ai = a.getAttribute('id')
+                                bi = b.getAttribute('id')
+                                return ai == bi ? 0 : ai > bi ? 1 : -1
+                            })
+                            for (var i = 0; i < section_array.length; i++) {
+                                section_root.appendChild(section_array[i])
+                            }
+                        }
+                        var pin = document.getElementById(data.pin)
+                        if (pin == null) {
+                            pin = document.createElement('li')
+                            section.appendChild(pin)
+                            pin.setAttribute('id', data.pin)
+                            var pin_content = document.createTextNode(data.pin)
+                            pin.appendChild(pin_content)
+                            if (data.settable) {
+                                var on = document.createElement('button')
+                                pin.appendChild(on)
+                                var on_content = document.createTextNode('On')
+                                on.appendChild(on_content)
+                                on.addEventListener('click', function (event) {
+                                    ws.send(JSON.stringify({command: 'setpin', pin: data.pin, value: true}))
+                                })
+                                var off = document.createElement('button')
+                                pin.appendChild(off)
+                                var off_content = document.createTextNode('Off')
+                                off.appendChild(off_content)
+                                off.addEventListener('click', function (event) {
+                                    ws.send(JSON.stringify({command: 'setpin', pin: data.pin, value: false}))
+                                })
+                            }
+                        }
+                        if (data.value) {
+                            pin.setAttribute('style', 'background: green; margin-bottom: 5px;')
+                        } else {
+                            pin.setAttribute('style', 'background: red; margin-bottom: 5px;')
+                        }
+                        break
+                    default:
+                        console.log(event.data)
+               }
+            })
+            ws.addEventListener('open', function (event) {
+                ws.send(JSON.stringify({command: 'getallpins'}))
+            })
+        </script>
+        <h1>ControlPi</h1>
+    </body>
+</html>