|
| 1 | +#include <jansson.h> |
| 2 | +#include <fstream> |
| 3 | +#include <iostream> |
| 4 | +#include <sstream> |
| 5 | +#include "config.h" |
| 6 | +#include <unistd.h> |
| 7 | +#include <pwd.h> |
| 8 | +#include <grp.h> |
| 9 | + |
| 10 | +using std::string; |
| 11 | + |
| 12 | +string ssid = "*"; |
| 13 | +string password = ""; |
| 14 | +string mqttBrokerIP = "192.168.x.y"; |
| 15 | +string brokerusername; |
| 16 | +string brokerpassword; |
| 17 | +string publishTopic = "kmpub"; |
| 18 | +string responseTopic = "kmcmd"; |
| 19 | +string locationTopic = "Solaranlage"; |
| 20 | +string deviceTopic = "Pylontech Akku Monitor"; |
| 21 | + |
| 22 | +string getMQTTtopic() |
| 23 | +{ |
| 24 | + // update from config |
| 25 | + readConfigFromJson(); |
| 26 | + return publishTopic + "/" + locationTopic + "/" + deviceTopic; |
| 27 | +} |
| 28 | + |
| 29 | +bool saveDefaultConfigToJson() |
| 30 | +{ |
| 31 | + // Create a new JSON object |
| 32 | + json_t *root = json_object(); |
| 33 | + |
| 34 | + // Insert data into the JSON object |
| 35 | + json_object_set_new(root, "ssid", json_string(ssid.c_str())); |
| 36 | + json_object_set_new(root, "password", json_string(password.c_str())); |
| 37 | + json_object_set_new(root, "mqttBrokerIP", json_string(mqttBrokerIP.c_str())); |
| 38 | + json_object_set_new(root, "brokerusername", json_string(brokerusername.c_str())); |
| 39 | + json_object_set_new(root, "brokerpassword", json_string(brokerpassword.c_str())); |
| 40 | + json_object_set_new(root, "publishTopic", json_string(publishTopic.c_str())); |
| 41 | + json_object_set_new(root, "responseTopic", json_string(responseTopic.c_str())); |
| 42 | + json_object_set_new(root, "locationTopic", json_string(locationTopic.c_str())); |
| 43 | + json_object_set_new(root, "deviceTopic", json_string(deviceTopic.c_str())); |
| 44 | + |
| 45 | + // Dump the JSON object to a string |
| 46 | + char *jsonString = json_dumps(root, JSON_INDENT(4)); |
| 47 | + if (!jsonString) { |
| 48 | + // Cleanup |
| 49 | + json_decref(root); |
| 50 | + return false; // Failed to dump JSON string |
| 51 | + } |
| 52 | + |
| 53 | + // Write the JSON string to a file |
| 54 | + // Check if the file already exists |
| 55 | + if (access("/var/www/html/wxdata/configData.json", F_OK) != -1) { |
| 56 | + std::cerr << "File already exists.\n"; |
| 57 | + // Cleanup |
| 58 | + json_decref(root); |
| 59 | + free(jsonString); |
| 60 | + return false; // Do not overwrite the file |
| 61 | + } else { |
| 62 | + // File does not exist, proceed to create it |
| 63 | + std::ofstream outFile("/var/www/html/wxdata/configData.json"); |
| 64 | + if (!outFile.is_open()) { |
| 65 | + std::cerr << "Failed to open file for writing.\n"; |
| 66 | + // Cleanup |
| 67 | + json_decref(root); |
| 68 | + free(jsonString); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + outFile << jsonString; |
| 73 | + outFile.close(); |
| 74 | + |
| 75 | + // Get the UID and GID for www-data |
| 76 | + struct passwd *pwd = getpwnam("www-data"); |
| 77 | + struct group *grp = getgrnam("www-data"); |
| 78 | + if (!pwd || !grp) { |
| 79 | + std::cerr << "Failed to get UID or GID for www-data.\n"; |
| 80 | + // Cleanup |
| 81 | + json_decref(root); |
| 82 | + free(jsonString); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // Change file ownership to www-data:www-data |
| 87 | + if (chown("/var/www/html/wxdata/configData.json", pwd->pw_uid, grp->gr_gid) == -1) { |
| 88 | + std::cerr << "Failed to change file ownership.\n"; |
| 89 | + // Cleanup, although not critical to return false here since the file was written successfully |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Cleanup JSON resources |
| 94 | + json_decref(root); |
| 95 | + free(jsonString); |
| 96 | + |
| 97 | + return true; // Indicate success |
| 98 | +} |
| 99 | + |
| 100 | +bool readConfigFromJson() |
| 101 | +{ |
| 102 | + // Open the JSON file |
| 103 | + std::ifstream inFile("/var/www/html/wxdata/configData.json"); |
| 104 | + if (!inFile.is_open()) { |
| 105 | + std::cerr << "Failed to open file for reading.\n"; |
| 106 | + return false; // Failed to open file |
| 107 | + } |
| 108 | + |
| 109 | + // Read file into a string |
| 110 | + std::stringstream strStream; |
| 111 | + strStream << inFile.rdbuf(); // Read the file |
| 112 | + std::string jsonString = strStream.str(); // str holds the content of the file |
| 113 | + inFile.close(); |
| 114 | + |
| 115 | + // Parse the JSON string |
| 116 | + json_error_t error; |
| 117 | + json_t *root = json_loads(jsonString.c_str(), 0, &error); |
| 118 | + if (!root) { |
| 119 | + std::cerr << "Error parsing JSON: " << error.text << "\n"; |
| 120 | + return false; // Failed to parse JSON |
| 121 | + } |
| 122 | + |
| 123 | + // Extract values |
| 124 | + ssid = json_string_value(json_object_get(root, "ssid")); |
| 125 | + password = json_string_value(json_object_get(root, "password")); |
| 126 | + mqttBrokerIP = json_string_value(json_object_get(root, "mqttBrokerIP")); |
| 127 | + brokerusername = json_string_value(json_object_get(root, "brokerusername")); |
| 128 | + brokerpassword = json_string_value(json_object_get(root, "brokerpassword")); |
| 129 | + publishTopic = json_string_value(json_object_get(root, "publishTopic")); |
| 130 | + responseTopic = json_string_value(json_object_get(root, "responseTopic")); |
| 131 | + locationTopic = json_string_value(json_object_get(root, "locationTopic")); |
| 132 | + deviceTopic = json_string_value(json_object_get(root, "deviceTopic")); |
| 133 | + |
| 134 | + // Cleanup |
| 135 | + json_decref(root); |
| 136 | + |
| 137 | + return true; // Success |
| 138 | +} |
0 commit comments