Kaynağa Gözat

FHEM-Base + mySQL-Logging

Matthias Kleine 9 yıl önce
ebeveyn
işleme
da32020200

+ 2 - 1
.gitignore

@@ -1,4 +1,5 @@
 .DS_Store
 .idea
 !.gitkeep
-/fhem/log/*
+/fhem/log/*
+/mysql/data/*

+ 28 - 0
README.md

@@ -0,0 +1,28 @@
+# FHEM Docker Base
+
+## Contains
+
+- FHEM + haus-automatisierung.com FHEM frontend style
+- Apache 2 as a reverse proxy to FHEM
+- Homebridge + FHEM Homebridge
+- mySQL-Logging
+- alexa-FHEM
+
+## Requirements
+
+- Docker
+
+## Install
+
+```
+git clone https://github.com/klein0r/fhem-docker.git fhem-docker
+cd fhem-docker
+docker-compose up
+```
+
+## Defaults
+
+- FHEM-WEB: 8083 (8084 and 8085 have been deleted)
+- mySQL-Root-Password: adgbciBOZjwYXeATg
+- mySQL-User: fhemuser
+- mySQL-Password: 2jRHnEi3WuNSQAcX7

+ 20 - 13
docker-compose.yml

@@ -4,27 +4,31 @@ services:
     fhem:
         expose:
             - "8083"
+            - "7072"
         ports:
             - "8083:8083"
+            - "7072:7072"
         build: fhem
         privileged: true
         devices:
           - "/dev/ttyUSB0:/dev/ttyUSB0"
-#        volumes:
-#            - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
-#            - ./es_data:/usr/share/elasticsearch/data
-        networks:
-            - code-network
-
-    homebridge:
-        expose:
-            - "51826"
-        ports:
-            - "51826:51826"
-        build: homebridge
+        volumes:
+            - ./fhem/log/:/opt/fhem/log
+            - ./fhem/fhem.cfg:/opt/fhem/fhem.cfg
+            - ./fhem/FHEM/99_myUtils.pm:/opt/fhem/FHEM/99_myUtils.pm
+            - ./fhem/contrib/dblog/db.conf:/opt/fhem/contrib/dblog/db.conf
         networks:
             - code-network
 
+#    homebridge:
+#        expose:
+#            - "51826"
+#        ports:
+#            - "51826:51826"
+#        build: homebridge
+#        networks:
+#            - code-network
+#
     mysql:
         expose:
             - "3306"
@@ -33,8 +37,11 @@ services:
             - "3306:3306"
             - "33060:33060"
         image: mysql/mysql-server:5.7
+        volumes:
+            - ./mysql/init.sql:/docker-entrypoint-initdb.d/fhem-init.sql
+            - ./mysql/data:/var/lib/mysql
         environment:
-            - MYSQL_ROOT_PASSWORD=fhemlog
+            - MYSQL_ROOT_PASSWORD=adgbciBOZjwYXeATg
         networks:
             - code-network
 

+ 14 - 5
fhem/Dockerfile

@@ -7,12 +7,17 @@ ENV TERM xterm
 
 # Install dependencies
 RUN apt-get update && apt-get upgrade -y --force-yes && apt-get install -y --force-yes --no-install-recommends apt-utils
-RUN apt-get -y --force-yes install perl wget git apt-transport-https libavahi-compat-libdnssd-dev sudo nodejs etherwake
+RUN apt-get -y --force-yes install perl wget git apt-transport-https sudo etherwake dfu-programmer build-essential snmpd snmp vim
 
 # Install perl packages
-RUN apt-get -y --force-yes install libalgorithm-merge-perl \
+RUN apt-get -y --force-yes install \
+libavahi-compat-libdnssd-dev \
+libalgorithm-merge-perl \
+libclass-dbi-mysql-perl \
 libclass-isa-perl \
 libcommon-sense-perl \
+libdatetime-format-strptime-perl \
+libdbi-perl \
 libdevice-serialport-perl \
 libdpkg-perl \
 liberror-perl \
@@ -39,8 +44,12 @@ RUN apt-get -y --force-yes install fhem telnet
 
 RUN echo Europe/Berlin > /etc/timezone && dpkg-reconfigure tzdata
 
-RUN chown fhem /opt/fhem/fhem.cfg
+RUN cd /opt/fhem/www/ && git clone https://github.com/klein0r/fhem-style-haus-automatisierung.git hausautomatisierung-com && chown -R fhem:dialout /opt/fhem/www/hausautomatisierung-com
 
-VOLUME ["/opt/fhem"]
+WORKDIR "/opt/fhem"
 
-EXPOSE 8083
+COPY start.sh ./
+
+EXPOSE 8083 7072
+
+CMD bash /opt/fhem/start.sh

+ 22 - 0
fhem/FHEM/99_myUtils.pm

@@ -0,0 +1,22 @@
+##############################################
+# $Id: myUtilsTemplate.pm 7570 2015-01-14 18:31:44Z rudolfkoenig $
+#
+# Save this file as 99_myUtils.pm, and create your own functions in the new
+# file. They are then available in every Perl expression.
+
+package main;
+
+use strict;
+use warnings;
+use POSIX;
+
+sub
+myUtils_Initialize($$)
+{
+  my ($hash) = @_;
+}
+
+# Enter you functions below _this_ line.
+
+
+1;

+ 5 - 0
fhem/contrib/dblog/db.conf

@@ -0,0 +1,5 @@
+%dbconfig= (
+	connection => "mysql:database=fhem;host=mysql;port=3306",
+	user => "fhemuser",
+	password => "2jRHnEi3WuNSQAcX7",
+);

+ 34 - 0
fhem/fhem.cfg

@@ -0,0 +1,34 @@
+attr global userattr DbLogExclude DbLogInclude cmdIcon devStateIcon devStateStyle icon sortby webCmd widgetOverride
+attr global autoload_undefined_devices 1
+attr global logfile ./log/fhem-%Y-%m.log
+attr global modpath .
+attr global motd none
+attr global sendStatistics onUpdate
+attr global statefile ./log/fhem.save
+attr global updateInBackground 0
+attr global verbose 2
+
+
+define telnetPort telnet 7072 global
+
+define WEB FHEMWEB 8083 global
+attr WEB CssFiles hausautomatisierung-com/styles.css
+attr WEB JavaScripts codemirror/fhem_codemirror.js
+attr WEB editConfig 1
+attr WEB menuEntries haus-automatisierung.com,https://haus-automatisierung.com/
+attr WEB plotWeekStartDay 1
+
+# Fake FileLog entry, to access the fhem log from FHEMWEB
+define Logfile FileLog ./log/fhem-%Y-%m.log fakelog
+
+define autocreate autocreate
+attr autocreate filelog ./log/%NAME-%Y.log
+
+define eventTypes eventTypes ./log/eventTypes.txt
+
+# Disable this to avoid looking for new USB devices on startup
+define initialUsbCheck notify global:INITIALIZED usb create
+define DBLogging DbLog /opt/fhem/contrib/dblog/db.conf .*:.*
+attr DBLogging room Log
+define DBLogging_Reopen at +*00:15:00 set DBLogging reopen
+attr DBLogging_Reopen room Log

+ 7 - 0
fhem/start.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+sudo service fhem start
+perl fhem.pl 7072 "update all"
+perl fhem.pl 7072 "shutdown restart"
+
+tail -f /dev/null

+ 2 - 2
homebridge/Dockerfile

@@ -9,7 +9,7 @@ ENV TERM xterm
 RUN apt-get update && apt-get upgrade -y --force-yes && apt-get install -y --force-yes --no-install-recommends apt-utils
 RUN apt-get install -y --force-yes xz-utils wget python libavahi-compat-libdnssd-dev
 
-RUN echo Europe/Vienna > /etc/timezone && dpkg-reconfigure tzdata
+RUN echo Europe/Berlin > /etc/timezone && dpkg-reconfigure tzdata
 
 RUN wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz -P /tmp && cd /usr/local && tar -xvf /tmp/node-v6.9.2-linux-x64.tar.xz --strip=1
 RUN ln -s /usr/local/bin/node /usr/bin/node
@@ -24,4 +24,4 @@ EXPOSE 51826
 
 COPY start.sh ./
 RUN chmod +x ./start.sh
-CMD ["./start.sh"]
+CMD ["bash ./start.sh"]

+ 16 - 18
homebridge/config.json

@@ -1,20 +1,18 @@
 {
-    "bridge": {
-        "name": "Homebridge FHEM",
-        "username": "CC:22:3D:E3:CE:29",
-        "port": 51826,
-        "pin": "012-34-567"
-    },
-
-    "platforms": [
-        {
-            "platform": "FHEM",
-            "name": "FHEM",
-            "server": "fhem",
-            "port": "8083",
-            "filter": "room=Homekit"
-        }
-     ],
-
-    "accessories": []
+  "bridge": {
+    "name": "Homebridge FHEM",
+    "username": "CC:22:3D:E3:CE:29",
+    "port": 51826,
+    "pin": "012-34-567"
+  },
+  "platforms": [
+    {
+      "platform": "FHEM",
+      "name": "FHEM",
+      "server": "fhem",
+      "port": "8083",
+      "filter": "room=Homekit"
+    }
+  ],
+  "accessories": []
 }

+ 0 - 0
mysql/data/.gitkeep


+ 34 - 0
mysql/init.sql

@@ -0,0 +1,34 @@
+CREATE DATABASE `fhem` DEFAULT CHARACTER SET = `utf8`;
+
+CREATE USER 'fhemuser'@'%' IDENTIFIED BY '2jRHnEi3WuNSQAcX7';
+
+REVOKE CREATE ROUTINE, CREATE VIEW, CREATE USER, ALTER, SHOW VIEW, CREATE, ALTER ROUTINE, EVENT, SUPER, INSERT, RELOAD, SELECT, DELETE, FILE, SHOW DATABASES, TRIGGER, SHUTDOWN, REPLICATION CLIENT, GRANT OPTION, PROCESS, REFERENCES, UPDATE, DROP, REPLICATION SLAVE, EXECUTE, LOCK TABLES, CREATE TEMPORARY TABLES, INDEX ON *.* FROM 'fhemuser'@'%';
+
+UPDATE mysql.user SET max_questions = 0, max_updates = 0, max_connections = 0 WHERE User = 'fhemuser' AND Host = '%';
+
+GRANT CREATE ROUTINE, CREATE VIEW, ALTER, SHOW VIEW, CREATE, ALTER ROUTINE, EVENT, INSERT, SELECT, DELETE, TRIGGER, GRANT OPTION, REFERENCES, UPDATE, DROP, EXECUTE, LOCK TABLES, CREATE TEMPORARY TABLES, INDEX ON `fhem`.* TO 'fhemuser'@'%';
+
+USE `fhem`;
+
+CREATE TABLE history (
+    TIMESTAMP TIMESTAMP,
+    DEVICE varchar(32),
+    TYPE varchar(32),
+    EVENT varchar(512),
+    READING varchar(32),
+    VALUE varchar(32),
+    UNIT varchar(32),
+    KEY `IDX_HISTORY` (`DEVICE`,`READING`,`TIMESTAMP`,`VALUE`)
+);
+
+CREATE TABLE current (
+    TIMESTAMP TIMESTAMP,
+    DEVICE varchar(32),
+    TYPE varchar(32),
+    EVENT varchar(512),
+    READING varchar(32),
+    VALUE varchar(32),
+    UNIT varchar(32)
+);
+
+FLUSH PRIVILEGES;