outsourced main method of ip refresh and added some doc

This commit is contained in:
2019-08-02 22:44:42 +02:00
parent 700d7912df
commit f325c3a371
8 changed files with 82 additions and 43 deletions

47
src/IPRefresher.cpp Normal file
View File

@ -0,0 +1,47 @@
//
// Created by lukas on 02.08.19.
//
#include <string>
#include <Logger.h>
#include <api/IPAPI.h>
#include <api/DynuAPI.h>
#include <api/TelegramAPI.h>
#include "IPRefresher.h"
void IPRefresher::checkIPAdress() {
Logger logger;
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) {
//no internet connection
logger.logToLogfile("[WARNING] no internet connection");
std::cout << "[WARNING] no internet connection" << std::endl;
} else {
std::string oldip = logger.readip();
if (oldip == ip) {
std::cout << "[INFO] no change -- ip: " << ip << std::endl;
logger.logToLogfile(" [INFO] no change -- ip: " + ip);
} else {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl;
DynuAPI dynu;
if (dynu.refreshIp(ip)) {
TelegramAPI tele;
tele.init("717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4","618154204");
tele.sendMessage(oldip + " moved to " + ip);
} else {
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
}
logger.safeip(ip);
}
}
}

View File

@ -14,3 +14,8 @@ void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
// std::cout << "[DEBUG] " << reply << std::endl;
}
void TelegramAPI::init(std::string apikey, std::string chatid) {
this->apikey = apikey;
this->chatid = chatid;
}

View File

@ -1,12 +1,5 @@
#include <iostream>
#include <ctime>
#include <unordered_map>
#include "api/API.h"
#include "Logger.h"
#include "api/TelegramAPI.h"
#include "api/DynuAPI.h"
#include "api/IPAPI.h"
#include <IPRefresher.h>
int main(int argc, char *argv[]) {
@ -22,38 +15,8 @@ int main(int argc, char *argv[]) {
std::cout << "wrong arguments! -h for help" << std::endl;
}
} else {
Logger logger;
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) {
//no internet connection
logger.logToLogfile("[WARNING] no internet connection");
std::cout << "[WARNING] no internet connection" << std::endl;
} else {
std::string oldip = logger.readip();
if (oldip == ip) {
std::cout << "[INFO] no change -- ip: " << ip << std::endl;
logger.logToLogfile(" [INFO] no change -- ip: " + ip);
} else {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl;
DynuAPI dynu;
if (dynu.refreshIp(ip)) {
TelegramAPI tele;
tele.sendMessage(oldip + " moved to " + ip);
} else {
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
}
logger.safeip(ip);
}
}
IPRefresher ipr;
ipr.checkIPAdress();
}
return 0;