92 lines
3.2 KiB
C++
92 lines
3.2 KiB
C++
|
|
#include "iobox_api.h"
|
|
#include <thread>
|
|
|
|
iobox_api ioBoxApp((char*)"239.255.255.250", 12345);
|
|
|
|
|
|
|
|
|
|
|
|
void userInputThread(iobox_api* api) {
|
|
std::string current_ip;
|
|
|
|
while (true) {
|
|
std::string input;
|
|
std::cout << std::endl;
|
|
std::cout << "Enter command followed by Enter: exit, scan, setip, connect, disconnect, show, get, set" << std::endl;
|
|
// std::cin >> input; // This will only get the first word
|
|
std::getline(std::cin, input);
|
|
if (input == "exit") {
|
|
break;
|
|
}
|
|
else if(input == "scan") {
|
|
std::vector<std::string> devices = api->scanNetworkDevicesMulticast(5);
|
|
std::cout << "Found devices: " << devices.size() << std::endl;
|
|
for (const std::string& device : devices) {
|
|
std::cout << device << std::endl;
|
|
}
|
|
continue;
|
|
}
|
|
else if(input == "setip") {
|
|
std::cout << "Enter IP: ";
|
|
std::getline(std::cin, current_ip);
|
|
continue;
|
|
}
|
|
else if(input == "connect") {
|
|
if(current_ip == "") {
|
|
std::cout << "Please setip address first" << std::endl;
|
|
continue;
|
|
}
|
|
bool connect = api->connectToIobox(current_ip, DEVICE_TCP_PORT);
|
|
std::cout << "Connection to " << current_ip << (connect ? " succeeded." : " failed.") << std::endl;
|
|
continue;
|
|
}
|
|
else if(input == "disconnect") {
|
|
bool disconnect = api->disconnectToIobox(current_ip);
|
|
std::cout << "Disconnect to " << current_ip << (disconnect ? " succeeded." : " failed.") << std::endl;
|
|
// current_ip = "";
|
|
continue;
|
|
}
|
|
else if(input == "show") {
|
|
api->show_profile_ioboxs();
|
|
continue;
|
|
}
|
|
else if(input == "get") {
|
|
std::string channel;
|
|
std::cout << "Enter channel (\"all\" for get all): ";
|
|
std::cin >> channel;
|
|
if(channel == "all") {
|
|
bool get = api->getAllValueIobox(current_ip);
|
|
api->show_profile_iobox(current_ip);
|
|
std::cout << "Get all value from " << current_ip << (get ? " succeeded." : " failed.") << std::endl;
|
|
} else {
|
|
std::string value = api->getValueDataStringIoboxFromChannelName(current_ip, channel);
|
|
std::cout << "Value of " << channel << ": " << value << std::endl;
|
|
}
|
|
continue;
|
|
}
|
|
else if(input == "set") {
|
|
std::string value;
|
|
std::string channel;
|
|
std::cout << "Enter channel: ";
|
|
std::cin >> channel;
|
|
std::cout << "Enter value: ";
|
|
std::cin >> value;
|
|
bool set = api->setValueDataStringIoboxFromChannelName(current_ip, channel, value);
|
|
std::cout << "Set value to " << current_ip << (set ? " succeeded." : " failed.") << std::endl;
|
|
}
|
|
else {
|
|
std::cout << "Invalid command" << std::endl;
|
|
}
|
|
}
|
|
}
|
|
int main() {
|
|
|
|
std::thread userThread(userInputThread, &ioBoxApp);
|
|
userThread.join();
|
|
|
|
std::cout << "Main thread is done" << std::endl;
|
|
return 0;
|
|
}
|