From dbbda95fdfea562900cb3cd34814e8bf9b9ca32c Mon Sep 17 00:00:00 2001
From: "de@itstall.de" <de@itstall.de>
Date: Tue, 28 Jan 2020 05:15:42 +0100
Subject: [PATCH] Working again

---
 .gitignore              |   1 +
 MqttClient.h            |  60 ++++++++++++++++++++++++++++++++++++++++
 backend.cpp             |  23 ++++++++-------
 backend.vcxproj         |   3 +-
 backend.vcxproj.filters |   7 ++++-
 compatibility.h         |   7 +++++
 dbSqlite.h              |  24 ++++++++++++----
 influxdb.h              |   1 +
 openweathermap.h        |  10 +++----
 weatherstation.db       | Bin 552960 -> 552960 bytes
 10 files changed, 114 insertions(+), 22 deletions(-)
 create mode 100644 MqttClient.h
 create mode 100644 compatibility.h

diff --git a/.gitignore b/.gitignore
index 925e5bf..3ea3afb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
 /.vs
 /Debug
 /x64/Release
+/Release
diff --git a/MqttClient.h b/MqttClient.h
new file mode 100644
index 0000000..423413a
--- /dev/null
+++ b/MqttClient.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#include <mosquittopp.h>
+#include <cstring>
+#include <cstdio>
+
+#define MAX_PAYLOAD 50
+#define DEFAULT_KEEP_ALIVE 60
+#define PUBLISH_TOPIC "EXAMPLE_TOPIC"
+
+class MqttClient : public mosqpp::mosquittopp {
+public:
+    MqttClient(const char* id, const char* host, int port) : mosquittopp(id) {
+        int keepalive = DEFAULT_KEEP_ALIVE;
+        connect(host, port, keepalive);
+    }
+
+    ~MqttClient() {}
+
+    void on_connect(int rc) {
+        if (!rc) std::cout << "Connected - code " << rc << std::endl;
+    }
+
+    void on_subscribe(int mid, int qos_count, const int* granted_qos){
+        std::cout << "Subscription succeeded." << std::endl;
+    }
+
+    void on_message(const struct mosquitto_message* message) {
+        int payload_size = MAX_PAYLOAD + 1;
+        char buf[payload_size];
+
+        if (!strcmp(message->topic, PUBLISH_TOPIC)) {
+            memset(buf, 0, payload_size * sizeof(char));
+
+            /* Copy N-1 bytes to ensure always 0 terminated. */
+            memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char));
+
+            std::cout << buf << std::endl;
+
+            // Examples of messages for M2M communications...
+            if (!strcmp(buf, "STATUS")) {
+                snprintf(buf, payload_size, "This is a Status Message...");
+                publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
+                std::cout << "Status Request Recieved." << std::endl;
+            }
+
+            if (!strcmp(buf, "ON")) {
+                snprintf(buf, payload_size, "Turning on...");
+                publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
+                std::cout << "Request to turn on." << std::endl;
+            }
+
+            if (!strcmp(buf, "OFF")) {
+                snprintf(buf, payload_size, "Turning off...");
+                publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
+                std::cout << "Request to turn off." << std::endl;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/backend.cpp b/backend.cpp
index fbb0aa9..310bb33 100644
--- a/backend.cpp
+++ b/backend.cpp
@@ -1,13 +1,17 @@
 #include <iostream>
 #include <thread>
+#include <vector>
+#include "compatibility.h"
 #include "openweathermap.h"
 #include "dbSqlite.h"
-#include <vector>
+#include "MqttClient.h"
 
 using namespace std;
 
+// Database object
 dbSqlite* db;
 
+// Thread for openweathermap:getWeather
 void schedulerWeather(int time) {
     std::cout << "Wetterstation::schedulerWeather" << std::endl;
     while (1) {
@@ -18,6 +22,7 @@ void schedulerWeather(int time) {
     }
 }
 
+// Thread for openweathermap:getForecast
 void schedulerForecast(int time) {
     std::cout << "Wetterstation::schedulerForecast" << std::endl;
     while (1) {
@@ -28,29 +33,27 @@ void schedulerForecast(int time) {
     }
 }
 
+// Main method
 int main() {
     std::cout << "Wetterstation::main" << std::endl;
-    WSADATA wsaData;
 
+    // Sockets aktivieren
+    WSADATA wsaData;
     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
     if (iResult != 0) {
         printf("WSAStartup failed: %d\n", iResult);
         return 1;
     }
-
     db = new dbSqlite();
 
     std::thread thrWeather{ schedulerWeather, 60000 };
-    Sleep(50);
+    csleep(50);
     std::thread thrForecast{ schedulerForecast, 900000 };
+    csleep(100);
 
-    std::vector<WeatherConditions> cond = db->getConditions();
+    //mqttStart();
 
-    /*for (int i = 0; i < cond.size(); i++) {
-        cout << cond[i].condition_id << endl;
-        cout << cond[i].description << endl;
-        cout << "------------------------" << endl;
-    }*/
+    std::vector<WeatherConditions> cond = db->getConditions();
 
     while (1) {
         Sleep(1000);
diff --git a/backend.vcxproj b/backend.vcxproj
index afb5536..f54a234 100644
--- a/backend.vcxproj
+++ b/backend.vcxproj
@@ -148,12 +148,13 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="backend.cpp" />
+    <ClCompile Include="MqttClient.h" />
   </ItemGroup>
   <ItemGroup>
     <None Include="README.md" />
-    <None Include="weatherstation.db" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="compatibility.h" />
     <ClInclude Include="dbSqlite.h" />
     <ClInclude Include="influxdb.h" />
     <ClInclude Include="json.h" />
diff --git a/backend.vcxproj.filters b/backend.vcxproj.filters
index 4436b5f..659c0a4 100644
--- a/backend.vcxproj.filters
+++ b/backend.vcxproj.filters
@@ -18,12 +18,14 @@
     <ClCompile Include="backend.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="MqttClient.h">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <None Include="README.md">
       <Filter>Quelldateien</Filter>
     </None>
-    <None Include="weatherstation.db" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="dbSqlite.h">
@@ -38,5 +40,8 @@
     <ClInclude Include="openweathermap.h">
       <Filter>Quelldateien</Filter>
     </ClInclude>
+    <ClInclude Include="compatibility.h">
+      <Filter>Quelldateien</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/compatibility.h b/compatibility.h
new file mode 100644
index 0000000..2b9134a
--- /dev/null
+++ b/compatibility.h
@@ -0,0 +1,7 @@
+void csleep(int time) {
+#if defined(WIN32) || defined(WIN64)
+    Sleep(time);
+#else
+    usleep((long) time);
+#endif
+}
\ No newline at end of file
diff --git a/dbSqlite.h b/dbSqlite.h
index 696fefa..19d439c 100644
--- a/dbSqlite.h
+++ b/dbSqlite.h
@@ -26,6 +26,13 @@ struct Settings {
     std::string influx_db;
     std::string influx_user;
     std::string influx_pass;
+    std::string mqtt_host;
+    std::string mqtt_user;
+    std::string mqtt_pass;
+    std::string mqtt_topic_frontend;
+    std::string mqtt_topic_backend;
+    int mqtt_qos;
+    std::string mqtt_timeout;
 };
 
 class dbSqlite {
@@ -45,8 +52,7 @@ public:
     };
 
     Settings getSettings() {
-        std::cout << "dbSqlite::getSettings()" << std::endl;
-        std::cout << "owm_plz: " << this->settings.owm_plz << std::endl;
+       std::cout << "dbSqlite::getSettings()" << std::endl;
         return this->settings;
     }
 
@@ -68,6 +74,13 @@ public:
             into(this->settings.influx_db),
             into(this->settings.influx_user),
             into(this->settings.influx_pass),
+            into(this->settings.mqtt_host),
+            into(this->settings.mqtt_user),
+            into(this->settings.mqtt_pass),
+            into(this->settings.mqtt_topic_frontend),
+            into(this->settings.mqtt_topic_backend),
+            into(this->settings.mqtt_qos),
+            into(this->settings.mqtt_timeout),
             range(0, 1); // iterate over result set one row at a time
         select.execute();
     }
@@ -75,6 +88,7 @@ public:
     std::vector<WeatherConditions> queryConditions() {
         std::cout << "dbSqlite::queryConditions()" << std::endl;
         Session session("SQLite", this->dbFile);
+        std::vector<WeatherConditions> result;
         WeatherConditions wc;
 
         Statement select(session);
@@ -86,9 +100,9 @@ public:
 
         while (!select.done()) {
             select.execute();
-            weatherConditions.push_back(wc);
+            result.push_back(wc);
         }
 
-        return weatherConditions;
+        return result;
     }
-};
\ No newline at end of file
+};
diff --git a/influxdb.h b/influxdb.h
index 9c0c57d..380f744 100644
--- a/influxdb.h
+++ b/influxdb.h
@@ -6,6 +6,7 @@
 
   Please see LICENSE file or visit https://github.com/orca-zhang/influxdb-cpp for details.
  */
+#pragma once
 #include <sstream>
 #include <cstring>
 #include <cstdio>
diff --git a/openweathermap.h b/openweathermap.h
index 6c29de0..7920267 100644
--- a/openweathermap.h
+++ b/openweathermap.h
@@ -75,7 +75,7 @@ public:
     }
 
     void setSWeather(json::JSON jObj, std::string plz, std::string lngCode) {
-        std::cout << "openweathermap::setSWeather()" << std::endl;
+        std::cout << "openweathermap::setSWeather(" << plz << ")" << std::endl;
         this->sWeather.plz = plz;
         this->sWeather.lngCode = lngCode;
         this->sWeather.visibility = jObj["visibility"].ToInt();
@@ -98,7 +98,7 @@ public:
     }
 
     void setSForecast(json::JSON jObj, std::string plz, std::string lngCode) {
-        std::cout << "openweathermap::setSForecast()" << std::endl;
+        std::cout << "openweathermap::setSForecast(" << plz << ")" << std::endl;
         for (int i = 0; i < jObj["cnt"].ToInt(); i++) {
             weatherData item;
             item.plz = plz;
@@ -157,14 +157,14 @@ public:
     }
 
     void getWeather(std::string plz, std::string lngCode) {
-        std::cout << "openweathermap::getWeather" << std::endl;
+        std::cout << "openweathermap::getWeather(" << plz << ", " << lngCode << ")" << std::endl;
         lngCode = str_tolower(lngCode);
         this->setSWeather(json::JSON::Load(this->getResponse("http://api.openweathermap.org/data/2.5/weather?zip=" + plz + "," + lngCode + "&appid=" + db->getSettings().owm_appid + "&mode=json&units=metric&lang=de")), plz, lngCode);
 
         std::cout << "owm_appid: " << db->getSettings().owm_appid << std::endl;
 
         influxdb_cpp::server_info si(db->getSettings().influx_host, db->getSettings().influx_port, db->getSettings().influx_db, db->getSettings().influx_user, db->getSettings().influx_pass);
-        std::cout << "Influx Result: " << influxdb_cpp::builder()
+        influxdb_cpp::builder()
             .meas("Weather")
             .tag("plz", this->sWeather.plz)
             .tag("lngCode", this->sWeather.lngCode)
@@ -189,7 +189,7 @@ public:
     }
 
     void getForecast(std::string plz, std::string lngCode) {
-        std::cout << "openweathermap::getForecast" << std::endl;
+        std::cout << "openweathermap::getForecast(" << plz << ", " << lngCode << ")" << std::endl;
         lngCode = str_tolower(lngCode);
         this->setSForecast(json::JSON::Load(this->getResponse("http://api.openweathermap.org/data/2.5/forecase?zip=" + plz + "," + lngCode + "&appid=" + db->getSettings().owm_appid + "&mode=json&units=metric&lang=de")), plz, lngCode);
     }
diff --git a/weatherstation.db b/weatherstation.db
index ab4b5ba99922863f1b38c96767d0377c2af23997..a8ae43969964530748227b2ae3656c445b4e2a2a 100644
GIT binary patch
delta 855
zcmZvaO-vI(6vubAixe#MgFv^t-R-s|tOnYOrGl8MH35wjV`w6pnBdwDbVIw_?oRb&
z#At#C56FaY^5oHj9~`)FlCy~(y>ZaQvk8euU+G7nI?4Yf@Av-m-emID4#H~(;SJF%
zG7Pg0Y8}_>vE+cr#Lwt`fE=z(#=Y48)3v$lec)7Fy)SM%y8V*GzO3$?@B4(Fjw$6T
zAxlNmCdwnt$`>`OiE1{sI#5Nk?G8lDik@FuwoHRyqwwG2LoL7hZ>?(Dif;Hxa~jdh
z*sQhRu=Arog3ui8_M}<o+|I10%X!xq;@Cidy_d@u4U8MnH(6(n;YG6qGp^wRUL~Om
zz><z=gJo*VE7;O)8yi?@AUOqwaYv+)-Jx&!7@DEq_$UlK<iDUC{U8o)3cc<T6^t3S
z+?+^%2UL`$f-nkkRu}<eS*VjOVF3l{FJXc^onw4d5rY8J;uVykPsAA5-a*XL@8a3e
zbqs%1wBRw8vVd2x5#l-4pF)3*k{KEfDoPF)EyX69wc0WXgJ2p(hexJ)ss-cFbR(!h
z(~rTcJ^Opm5qi}JiF8mJV)yrGOd35_(4<H^%hFd;1l^$TCBDsa^s6+iwi?~iv5iYd
zJ$V<$Mnx3MRjhWEit@;@n>S^-on@a_<@KtJKrF}s;y_)XQ=snMs_fj#aAC$Bu5YB@
zJ@D*4^340aNmf<WjH;$D^#r!xm`<hZ;^agsx&7I6Ue-yzGMz}&H#M#k_?c2EUcl4i
hQ&Y*um=J6MBD!guND?Qr)`})Ncyl`w<LBUH{{l_j5rhB$

delta 571
zcmYj~&r2Io5Xbku-88Ev>!yunci--2lZdON5o!{Jg8c!lheAE*LG;pY=`)e6NwaIb
z34%~B9?}ltrMDh?5F4m}MWH=<v(Q4%r9zLLBqTWS9r%9cJ2MAYnS-lLn<W%M5ROoT
zioCm9yrT$nWHXLPjL!(;>&mbA%thj*{3SMecwdaBEQ`K<+W8RsOgcZ6EFmu-sxHwn
zv$L#5WN53&C$|?Rs_Wv^dc&{r`YZmL*LKqj$ckdF&%D-Fg$LVV$m`tcBfE@&dt$cl
z_TZydARNAHIZ}gb?JJzI6kO^W3E_|K5+4jBkA2ygMzUjc$%(N_*5J2MlGL<B!C@xC
zCH9bbu+It@`-I(xOE#OX@U5Ux4e)#5tnv!4r!|SD%H&_qaLAaB6Y^@{gu7l)8-$Dr
z4CTpnFSG-mIR`&jQqhx$XUX3l_Tk)oG}6M|?%D271{2mKZE+}A`5Obz$_||m$5xh9
z;mp#8EQD{?lsh=Ivdu&8_ik!2iRKt7ti1^a(>gGF`o`un+a5aquxTGPZGxgG5hbA_
ys9UJ$Mbnl~9*ggV@$+UT)@iF%am*(p&r^vabzS$g)R3LXLaDqsf4b3-AN&U|Rj3O9

-- 
GitLab