|
#include <iostream> |
|
#include <string> |
|
#include <sstream> |
|
#include <vector> |
|
#include <fstream> |
|
#include <filesystem> |
|
#include <map> |
|
#include <time.h> |
|
#include <C:\Users\sriff\Downloads\Practicum_Stuff\SoftwareEngineeringTeam2Proj\ArduinoServerTest\include\asio-1.28.0\include\asio.hpp> |
|
|
|
|
|
using namespace std; |
|
using namespace asio; |
|
using namespace asio::ip; |
|
namespace fs = std::filesystem; |
|
|
|
|
|
int portVal = 999; |
|
|
|
|
|
map<string, ofstream> macFiles; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void writeToLog(const string& macAddress, const string& data) { |
|
|
|
fs::path logDir = fs::path(fs::current_path()) / "log"; |
|
if (!fs::exists(logDir) || !fs::is_directory(logDir)) { |
|
fs::create_directory(logDir); |
|
} |
|
|
|
|
|
string macAddressStripped = macAddress; |
|
macAddressStripped.erase(remove(macAddressStripped.begin(), macAddressStripped.end(), ':'), macAddressStripped.end()); |
|
|
|
|
|
fs::path filePath = logDir / (macAddressStripped + ".csv"); |
|
if (!fs::exists(filePath)) { |
|
|
|
ofstream newFile(filePath); |
|
if (newFile.is_open()) { |
|
newFile << data << endl; |
|
newFile.close(); |
|
} |
|
} |
|
|
|
else { |
|
ofstream existingFile(filePath, ios_base::app); |
|
if (existingFile.is_open()) { |
|
existingFile << data << endl; |
|
existingFile.close(); |
|
} |
|
} |
|
} |
|
|
|
|
|
void parseAndWriteMAC(const string& csvData) { |
|
istringstream iss(csvData); |
|
|
|
string line; |
|
while (getline(iss, line)) { |
|
istringstream lineStream(line); |
|
vector<string> tokens; |
|
string token; |
|
while (getline(lineStream, token, ',')) { |
|
tokens.push_back(token); |
|
} |
|
|
|
if (tokens.size() >= 2) { |
|
string macAddress = tokens[1]; |
|
auto it = macFiles.find(macAddress); |
|
if (it == macFiles.end()) { |
|
|
|
ofstream newFile; |
|
macFiles[macAddress] = move(newFile); |
|
writeToLog(macAddress, line); |
|
} |
|
else { |
|
|
|
writeToLog(macAddress, line); |
|
} |
|
} |
|
} |
|
} |
|
|
|
int main() { |
|
|
|
|
|
|
|
|
|
try { |
|
io_context io_context; |
|
tcp::resolver resolver(io_context); |
|
tcp::resolver::query query(host_name(), ""); |
|
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); |
|
tcp::resolver::iterator end; |
|
|
|
while (endpoint_iterator != end) { |
|
tcp::endpoint endpoint = *endpoint_iterator++; |
|
if (endpoint.address().is_v4()) { |
|
cout << "Please ensure port forwarding from external IP to local IP " << endpoint.address().to_string() << " is enabled." << endl; |
|
cout << "Please ensure your board WiFi and external IP are configured." << endl; |
|
cout << "Example: Board(s) configured to report to external IP 111.222.333.444\n Local machine IP address is " << endpoint.address().to_string() << ", and for that local IP:\n External IP start port: 8080, external IP end port: 277\n Local IP start port: 277, local IP end port to land on this server:" << portVal << endl; |
|
break; |
|
} |
|
} |
|
} |
|
catch (exception& e) { |
|
cerr << "Exception: " << e.what() << endl; |
|
} |
|
|
|
|
|
try { |
|
io_context io_context; |
|
|
|
|
|
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), portVal)); |
|
|
|
cout << "Server listening on 0.0.0.0 local end port " << portVal << "\n" << endl; |
|
|
|
while (true) { |
|
|
|
tcp::socket socket(io_context); |
|
acceptor.accept(socket); |
|
|
|
cout << "Connection from: " << socket.remote_endpoint().address().to_string() << endl; |
|
|
|
|
|
asio::streambuf buffer; |
|
asio::read_until(socket, buffer, "\n"); |
|
string data = buffer_cast<const char*>(buffer.data()); |
|
|
|
|
|
time_t now; |
|
time(&now); |
|
struct tm tm_info; |
|
localtime_s(&tm_info, &now); |
|
char timeBuffer[15]; |
|
strftime(timeBuffer, sizeof(timeBuffer), "%d%m%Y%H%M%S", &tm_info); |
|
stringstream ss; |
|
ss << timeBuffer << "," << data; |
|
|
|
|
|
string pkg = ss.str(); |
|
cout << pkg << endl; |
|
|
|
|
|
parseAndWriteMAC(pkg); |
|
|
|
|
|
|
|
} |
|
} |
|
catch (std::exception& e) { |
|
cerr << "Exception: " << e.what() << endl; |
|
} |
|
|
|
return 0; |
|
} |
|
|