From 518455b7de32cdec7bd6e76578ca437033ba4f58 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Fri, 1 Nov 2024 19:58:06 +0100
Subject: [PATCH 01/10] Add switch on/off scripts for wittgenstein

---
 files/wittgenstein/switch-off.sh |  7 +++++++
 files/wittgenstein/switch-on.sh  | 11 +++++++++++
 2 files changed, 18 insertions(+)
 create mode 100755 files/wittgenstein/switch-off.sh
 create mode 100755 files/wittgenstein/switch-on.sh

diff --git a/files/wittgenstein/switch-off.sh b/files/wittgenstein/switch-off.sh
new file mode 100755
index 0000000..40a081e
--- /dev/null
+++ b/files/wittgenstein/switch-off.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+gpio write 2 0
+gpio write 3 0
+
+gpio mode 2 tri
+gpio mode 3 tri
diff --git a/files/wittgenstein/switch-on.sh b/files/wittgenstein/switch-on.sh
new file mode 100755
index 0000000..aae9e2e
--- /dev/null
+++ b/files/wittgenstein/switch-on.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# INT
+gpio mode 0 tri
+
+# Power
+gpio mode 2 out
+gpio mode 3 out
+
+gpio write 2 1
+gpio write 3 1

From c1a56fa80b416469501b6d454c32b940e5a5ffc6 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Wed, 2 Nov 2022 20:57:39 +0100
Subject: [PATCH 02/10] Add reboot/unstuck script for wittgenstein

---
 files/wittgenstein/reboot.sh  | 22 ++++++++++++++++++++++
 files/wittgenstein/unstuck.sh |  7 +++++++
 2 files changed, 29 insertions(+)
 create mode 100755 files/wittgenstein/reboot.sh
 create mode 100755 files/wittgenstein/unstuck.sh

diff --git a/files/wittgenstein/reboot.sh b/files/wittgenstein/reboot.sh
new file mode 100755
index 0000000..e463089
--- /dev/null
+++ b/files/wittgenstein/reboot.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games'
+
+echo 'switch-on.sh'
+/home/pi/switch-on.sh
+
+echo 'start i2c-foo'
+sudo modprobe i2c_dev
+sudo modprobe i2c_bcm2708
+
+# wait for network devices
+sleep 30
+
+cd /home/pi
+echo 'start ampel controller'
+tmux new-session -s ampel 'cd /home/pi/netz39_space_notification/raspberry/ledcontrol && ./ledcontrol'
+
+echo 'start lever controller'
+tmux new-window -t ampel:1 'cd /home/pi/netz39_space_notification/raspberry/statusswitch && ./statusswitch'
+
+#echo 'start spaceapi controller'
+#tmux new-window -t ampel:2 'cd /home/pi/netz39_space_notification/SpaceAPI && ./update-json.py --server=platon'
diff --git a/files/wittgenstein/unstuck.sh b/files/wittgenstein/unstuck.sh
new file mode 100755
index 0000000..29da941
--- /dev/null
+++ b/files/wittgenstein/unstuck.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+logger -t unstuck "unstuck $(date)"
+
+killall tmux
+
+sleep 1
+/home/pi/reboot.sh

From f4db6fa39564090b8c3dda967d3ad9ef94cb6c55 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Wed, 2 Nov 2022 20:59:17 +0100
Subject: [PATCH 03/10] Add Ansible setup for wittgenstein

---
 host-wittgenstein.yml                  | 136 +++++++++++++++++++++++++
 host_vars/wittgenstein.n39.eu/vars.yml |   4 +
 2 files changed, 140 insertions(+)
 create mode 100644 host-wittgenstein.yml
 create mode 100644 host_vars/wittgenstein.n39.eu/vars.yml

diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml
new file mode 100644
index 0000000..50d992f
--- /dev/null
+++ b/host-wittgenstein.yml
@@ -0,0 +1,136 @@
+---
+- hosts: wittgenstein.n39.eu
+  become: true
+
+  roles:
+    - role: docker_setup
+      vars:
+        docker_data_root: "/srv/docker"
+    - role: apache
+    - role: apache_letsencrypt  # Uses configuration from dehydrated setup
+    - role: ansible-role-dehydrated
+      vars:
+        dehydrated_contact_email: "{{ server_admin }}"
+    - role: penguineer.dehydrated_cron
+
+  tasks:
+    - name: Install packages needed for the system
+      # This is a list of all packages,
+      # unless they are installed by a specific role
+      ansible.builtin.apt:
+        state: present
+        name:
+          # This is needed for the user-executed tasks
+          - acl
+          # Regular packages
+          - tmux
+          - git-core
+          - cmake
+          - build-essential
+          - libmosquitto-dev
+          - libconfig-dev
+          - mosquitto-clients
+          - i2c-tools
+
+
+#    - name: Set MAC address for proper DHCP recognition
+#      # Uses mac variable from inventory
+#      ansible.builtin.template:
+#        src: templates/network-interfaces-dhcp-mac.j2
+#        dest: /etc/network/interfaces.d/wittgenstein-mac
+#        owner: root
+#        group: root
+#        mode: '0644'
+
+    - name: Disable IPv6
+      # Because it is not working....
+      ansible.builtin.copy:
+        src: files/sysctl-no-ipv6.conf
+        dest: /etc/sysctl.d/99-systcl-no-ipv6.conf
+        owner: root
+        group: root
+        mode: '0644'
+
+
+    ### Gatekeeper user (pi for now)
+    #
+    # All the gatekeeping / door control stuff is here!
+
+    - name: Ensure gatekeeper user is there
+      ansible.builtin.user:
+        name: "{{ gatekeeper_user }}"
+        groups: dialout,audio,plugdev,input,netdev,i2c,gpio
+        append: yes
+
+    - name: Copy management scripts
+      ansible.builtin.copy:
+        src: "files/wittgenstein/{{ item }}"
+        dest: "/home/{{ gatekeeper_user }}/{{ item }}"
+        owner: "{{ gatekeeper_user }}"
+        group: "{{ gatekeeper_user }}"
+        mode: "0750"
+      loop:
+        - reboot.sh
+        - unstuck.sh
+        - switch-on.sh
+        - switch-off.sh
+
+    - name: Install start-up cron
+      ansible.builtin.cron:
+        name: Start the gatekeeper services
+        job: "/home/{{ gatekeeper_user }}/reboot.sh"
+        user: "{{ gatekeeper_user }}"
+        special_time: reboot
+
+
+    - name: Download wiringPi library
+      # WiringPi needs to be installed, but that library seems to be
+      # obsolete. We download something and hope it works...
+      ansible.builtin.get_url:
+        url: https://project-downloads.drogon.net/wiringpi-latest.deb
+        dest: "/home/{{ gatekeeper_user }}/wiringpi-latest.deb"
+        mode: "0644"
+        force: no
+      register: wiringPi_download
+
+    - name: Install wiringPi library  # noqa 503
+      ansible.builtin.apt:
+        state: present
+        deb: "/home/{{ gatekeeper_user }}/wiringpi-latest.deb"
+      when: wiringPi_download.changed
+
+
+    ### Ampelsteuerung
+    - name: Clone netz39_space_notification initial checkout
+      # Do this as the gatekeeper user!
+      become: yes
+      become_user: "{{ gatekeeper_user }}"
+      ansible.builtin.git:
+        repo: https://github.com/netz39/space_notification.git
+        dest: "/home/{{ gatekeeper_user }}/netz39_space_notification"
+        clone: yes
+        update: no
+
+    - name: Compile ledcontrol agent
+      # Do this as the gatekeeper user!
+      become: yes
+      become_user: "{{ gatekeeper_user }}"
+      ansible.builtin.shell:
+        chdir: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/ledcontrol"
+        cmd: make
+        creates: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/ledcontrol/ledcontrol"
+
+    - name: Compile statusswitch agent
+      # Do this as the gatekeeper user!
+      become: yes
+      become_user: "{{ gatekeeper_user }}"
+      ansible.builtin.shell:
+        chdir: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/statusswitch"
+        cmd: make
+        creates: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/statusswitch/statusswitch"
+
+    ### Space API
+    - name: Setup the SpaceAPI Docker container
+
+
+  handlers:
diff --git a/host_vars/wittgenstein.n39.eu/vars.yml b/host_vars/wittgenstein.n39.eu/vars.yml
new file mode 100644
index 0000000..2474184
--- /dev/null
+++ b/host_vars/wittgenstein.n39.eu/vars.yml
@@ -0,0 +1,4 @@
+server_admin: "admin+wittgenstein@netz39.de"
+ansible_python_interpreter: /usr/bin/python3
+gatekeeper_user: pi
+data_dir: "/srv/data"

From c73cff2cee32f815d4c81b891d5b01611ec8ba28 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Wed, 2 Nov 2022 20:59:31 +0100
Subject: [PATCH 04/10] Add wittgenstein to inventory

---
 inventory.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/inventory.yml b/inventory.yml
index b1513b4..4263588 100644
--- a/inventory.yml
+++ b/inventory.yml
@@ -12,6 +12,7 @@ all:
     pottwal.n39.eu:
     radon.n39.eu:
     unicorn.n39.eu:
+    wittgenstein.n39.eu:
     k3s-c1.n39.eu:
     k3s-c2.n39.eu:
     k3s-c3.n39.eu:

From 7693e0be94e424830d124bd4c47bbd850b4bb91d Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Wed, 2 Nov 2022 20:59:57 +0100
Subject: [PATCH 05/10] Add wittgenstein to main playbook

---
 main.yml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/main.yml b/main.yml
index 5af6b86..177bccc 100644
--- a/main.yml
+++ b/main.yml
@@ -42,3 +42,6 @@
 
 - name: Plumbum specific setup
   import_playbook: host-plumbum.yml
+
+- name: Wittgenstein specific setup
+  import_playbook: host-wittgenstein.yml

From 136c2c89852f4a2a9a072c60df863459b7fc1e9c Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Sat, 2 Nov 2024 15:29:45 +0100
Subject: [PATCH 06/10] Add paho-mqtt library to install dependencies

---
 host-wittgenstein.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml
index 50d992f..57850b5 100644
--- a/host-wittgenstein.yml
+++ b/host-wittgenstein.yml
@@ -30,6 +30,7 @@
           - libmosquitto-dev
           - libconfig-dev
           - mosquitto-clients
+          - python3-paho-mqtt
           - i2c-tools
 
 

From 04c6a6d5861fff211b187bb755e0102ff7e07956 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Sat, 2 Nov 2024 15:40:18 +0100
Subject: [PATCH 07/10] Set up the SpaceAPI service docker container

---
 host-wittgenstein.yml                  | 17 +++++++++++++++++
 host_vars/wittgenstein.n39.eu/vars.yml |  7 +++++++
 2 files changed, 24 insertions(+)

diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml
index 57850b5..9c6112e 100644
--- a/host-wittgenstein.yml
+++ b/host-wittgenstein.yml
@@ -132,6 +132,23 @@
 
     ### Space API
     - name: Setup the SpaceAPI Docker container
+      docker_container:
+        name: spaceapi
+        image: "{{ spaceapi_image }}"
+        pull: true
+        state: started
+        detach: yes
+        restart_policy: unless-stopped
+        ports:
+          - "0.0.0.0:{{ spaceapi_host_port }}:8080" # Must be reached by pottwal
+#          - "127.0.0.1:{{ spaceapi_host_port }}:8080"
+        env:
+          TZ: "{{ timezone }}"
+          MQTT_BROKER: "platon.n39.eu"
+          MQTT_TOPIC_STATUS: "{{ spaceapi_topic_status }}"
+          MQTT_TOPIC_LASTCHANGE:  "{{ spaceapi_topic_lastchange }}"
+      tags:
+        - spaceapi
 
 
   handlers:
diff --git a/host_vars/wittgenstein.n39.eu/vars.yml b/host_vars/wittgenstein.n39.eu/vars.yml
index 2474184..0fd637c 100644
--- a/host_vars/wittgenstein.n39.eu/vars.yml
+++ b/host_vars/wittgenstein.n39.eu/vars.yml
@@ -2,3 +2,10 @@ server_admin: "admin+wittgenstein@netz39.de"
 ansible_python_interpreter: /usr/bin/python3
 gatekeeper_user: pi
 data_dir: "/srv/data"
+
+spaceapi_host_port: 8001
+spaceapi_domain_name: spaceapi.n39.eu
+spaceapi_image: netz39/spaceapi-service:0.1.0
+spaceapi_topic_status: "Netz39/SpaceAPI/isOpen"
+spaceapi_topic_lastchange: "Netz39/SpaceAPI/lastchange"
+

From 5f4e63c1debf91e458350be2a8342758230a9589 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Sat, 2 Nov 2024 15:45:50 +0100
Subject: [PATCH 08/10] Set the wittgenstein MAC address

---
 host-wittgenstein.yml                  | 16 ++++++++--------
 host_vars/wittgenstein.n39.eu/vars.yml |  1 +
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml
index 9c6112e..a51540f 100644
--- a/host-wittgenstein.yml
+++ b/host-wittgenstein.yml
@@ -34,14 +34,14 @@
           - i2c-tools
 
 
-#    - name: Set MAC address for proper DHCP recognition
-#      # Uses mac variable from inventory
-#      ansible.builtin.template:
-#        src: templates/network-interfaces-dhcp-mac.j2
-#        dest: /etc/network/interfaces.d/wittgenstein-mac
-#        owner: root
-#        group: root
-#        mode: '0644'
+    - name: Set MAC address for proper DHCP recognition
+      # Uses mac variable from inventory
+      ansible.builtin.template:
+        src: templates/network-interfaces-dhcp-mac.j2
+        dest: /etc/network/interfaces.d/wittgenstein-mac
+        owner: root
+        group: root
+        mode: '0644'
 
     - name: Disable IPv6
       # Because it is not working....
diff --git a/host_vars/wittgenstein.n39.eu/vars.yml b/host_vars/wittgenstein.n39.eu/vars.yml
index 0fd637c..e1be7b9 100644
--- a/host_vars/wittgenstein.n39.eu/vars.yml
+++ b/host_vars/wittgenstein.n39.eu/vars.yml
@@ -1,4 +1,5 @@
 server_admin: "admin+wittgenstein@netz39.de"
+mac: "b8:27:eb:48:f1:59"
 ansible_python_interpreter: /usr/bin/python3
 gatekeeper_user: pi
 data_dir: "/srv/data"

From 2108f905845a3bed1e0e37cc64f94988a75f6290 Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Sat, 2 Nov 2024 16:33:47 +0100
Subject: [PATCH 09/10] Change forwarding for SpaceAPI according to the new
 service

---
 host-pottwal.yml                          | 2 ++
 templates/pottwal/spaceapi-apache-site.j2 | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/host-pottwal.yml b/host-pottwal.yml
index 8d7ea6d..f2cff3e 100644
--- a/host-pottwal.yml
+++ b/host-pottwal.yml
@@ -634,6 +634,8 @@
         site_name: "spaceapi.n39.eu"
         proxy_preserve_host: "On"
       notify: Restart apache2
+      tags:
+        - spaceapi
 
     - name: Ensure renovate bot cronjob is present
       ansible.builtin.template:
diff --git a/templates/pottwal/spaceapi-apache-site.j2 b/templates/pottwal/spaceapi-apache-site.j2
index 5d6961d..9959e90 100644
--- a/templates/pottwal/spaceapi-apache-site.j2
+++ b/templates/pottwal/spaceapi-apache-site.j2
@@ -44,9 +44,9 @@
     RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}
     ProxyPreserveHost {{ proxy_preserve_host | default("Off") }}
 
-    ProxyPass /json http://172.23.48.7/spaceapi
-    ProxyPass /text http://172.23.48.7/state.txt
-    ProxyPass /state.png  http://172.23.48.7/state.png
+    ProxyPass /json http://172.23.48.7:8001/json
+    ProxyPass /text http://172.23.48.7:8001/text
+    ProxyPass /state.png  http://172.23.48.7:8001/state.png
 </VirtualHost>
 </IfFile>
 </IfFile>

From 7fdfe89ed84dfc9e599418aa883265a70a2fd98b Mon Sep 17 00:00:00 2001
From: Stefan Haun <tux@netz39.de>
Date: Sat, 2 Nov 2024 21:03:31 +0100
Subject: [PATCH 10/10] Setup docker container Ampel Controller

---
 host-wittgenstein.yml                  | 18 ++++++++++++++++++
 host_vars/wittgenstein.n39.eu/vars.yml |  4 ++++
 2 files changed, 22 insertions(+)

diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml
index a51540f..f4b2190 100644
--- a/host-wittgenstein.yml
+++ b/host-wittgenstein.yml
@@ -150,5 +150,23 @@
       tags:
         - spaceapi
 
+    - name: Setup the Ampel Controller Docker container
+      docker_container:
+        name: ampelcontroller
+        image: "{{ ampelcontroller_image }}"
+        pull: true
+        state: started
+        detach: yes
+        restart_policy: unless-stopped
+        env:
+          TZ: "{{ timezone }}"
+          MQTT_BROKER: "platon.n39.eu"
+          MQTT_LEVER_STATE_TOPIC: "{{ topic_lever_state }}"
+          MQTT_DOOR_EVENTS_TOPIC: "{{ topic_door_events }}"
+          MQTT_SPACESTATUS_ISOPEN_TOPIC: "{{ spaceapi_topic_status }}"
+          MQTT_SPACESTATUS_LASTCHANGE_TOPIC: "{{ spaceapi_topic_lastchange }}"
+          MQTT_TRAFFIC_LIGHT_TOPIC: "{{ topic_traffic_light }}"
+      tags:
+        - spaceapi
 
   handlers:
diff --git a/host_vars/wittgenstein.n39.eu/vars.yml b/host_vars/wittgenstein.n39.eu/vars.yml
index e1be7b9..914da6d 100644
--- a/host_vars/wittgenstein.n39.eu/vars.yml
+++ b/host_vars/wittgenstein.n39.eu/vars.yml
@@ -10,3 +10,7 @@ spaceapi_image: netz39/spaceapi-service:0.1.0
 spaceapi_topic_status: "Netz39/SpaceAPI/isOpen"
 spaceapi_topic_lastchange: "Netz39/SpaceAPI/lastchange"
 
+ampelcontroller_image: netz39/ampel-controller:0.1.0
+topic_lever_state: "Netz39/Things/StatusSwitch/Lever/State"
+topic_door_events: "Netz39/Things/Door/Events"
+topic_traffic_light: "Netz39/Things/Ampel/Light"