Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MIKROTIK_H
- #define MIKROTIK_H
- #include <WiFi.h>
- #include "logger.h"
- #include "logger.h"
- class Mikrotik
- {
- public:
- static WiFiClient client;
- static String mtkIp;
- static int mtkPort;
- static String mtkUser;
- static String mtkPass;
- static int mtkRequestInterval;
- static TaskHandle_t ifUserWifiConnectedHandle;
- static void init()
- {
- Logger::syslog(__func__);
- if (config["mtkIp"] == "" || config["mtkPort"] == "" || config["mtkUser"] == "" || config["mtkPass"] == "" || config["mtkRequestInterval"] == "")
- {
- Logger::syslog("I dont have mtk config set, will delete task");
- vTaskDelete(NULL);
- }
- mtkIp = config["mtkIp"].c_str();
- mtkPort = config["mtkPort"].toInt();
- mtkUser = config["mtkUser"];
- mtkPass = config["mtkPass"];
- mtkRequestInterval = config["mtkRequestInterval"].toInt();
- Logger::syslog("mtkIp: " + String(mtkIp) + " ,mtkPort: " + String(mtkPort) + ", mtkRequestInterval: " + String(mtkRequestInterval));
- xTaskCreate(
- ifUserWifiConnectedTask, /* Task function. */
- "ifUserWifiConnectedTask", /* name of task. */
- 10000, /* Stack size of task */
- NULL, /* parameter of the task */
- 1, /* priority of the task */
- &ifUserWifiConnectedHandle); /* Task handle to keep track of created task */
- }
- static void ifUserWifiConnectedTask(void *pvParameters)
- {
- Logger::syslog(__func__);
- String readBuff;
- vTaskDelay(10000);
- while (!WiFi.isConnected())
- vTaskDelay(5000 / portTICK_RATE_MS);
- while (true)
- {
- while (!WiFi.isConnected())
- vTaskDelay(5000 / portTICK_RATE_MS);
- Logger::syslog("Starting cycle");
- if (!client.connect(mtkIp.c_str(), mtkPort))
- {
- Logger::syslog("Unable to connect");
- vTaskDelay(mtkRequestInterval);
- continue;
- }
- sendCmd("/login");
- sendCmd("=name=" + mtkUser);
- sendCmd("=password=" + mtkPass, true);
- vTaskDelay(1000);
- while (client.available())
- {
- readBuff = client.readString();
- }
- if (readBuff.indexOf("done") != -1)
- {
- sendCmd("/caps-man/registration-table/print", false);
- sendCmd("=.proplist=mac-address",true);
- vTaskDelay(1000);
- while (client.available())
- {
- readBuff = client.readString();
- }
- Serial.println(readBuff);
- }
- client.stop();
- Logger::syslog("Reading completed");
- vTaskDelay(mtkRequestInterval);
- }
- vTaskDelete(NULL);
- }
- static void sendCmd(String cmd, bool sendEnd = false)
- {
- writeLength(cmd.length());
- client.print(cmd);
- if (sendEnd)
- client.print(char(0));
- }
- static void writeLength(int messageLength)
- {
- char *encodedLengthData; // encoded length to send to the api socket
- char *lengthData; // exactly what is in memory at &iLen integer
- encodedLengthData = (char *)calloc(sizeof(int), 1);
- lengthData = (char *)&messageLength;
- if (messageLength < 0x80)
- {
- encodedLengthData[0] = (char)messageLength;
- client.write(encodedLengthData, 1);
- }
- else if (messageLength < 0x4000)
- { // write 2 bytes
- encodedLengthData[0] = lengthData[1] | 0x80;
- encodedLengthData[1] = lengthData[0];
- client.write(encodedLengthData, 2);
- }
- else if (messageLength < 0x200000)
- { // write 3 bytes
- encodedLengthData[0] = lengthData[2] | 0xc0;
- encodedLengthData[1] = lengthData[1];
- encodedLengthData[2] = lengthData[0];
- client.write(encodedLengthData, 3);
- }
- else if (messageLength < 0x10000000)
- { // write 4 bytes (untested)
- encodedLengthData[0] = lengthData[3] | 0xe0;
- encodedLengthData[1] = lengthData[2];
- encodedLengthData[2] = lengthData[1];
- encodedLengthData[3] = lengthData[0];
- client.write(encodedLengthData, 4);
- }
- else
- { // this should never happen
- Logger::syslog("Length of word is: " + String(messageLength));
- Logger::syslog("Word is too long.\n");
- }
- delete[] encodedLengthData;
- }
- };
- WiFiClient Mikrotik::client;
- String Mikrotik::mtkIp;
- int Mikrotik::mtkPort;
- String Mikrotik::mtkUser;
- String Mikrotik::mtkPass;
- int Mikrotik::mtkRequestInterval;
- TaskHandle_t Mikrotik::ifUserWifiConnectedHandle;
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement