Orvibo S20 socket manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

consolereader.cpp 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************************
  2. * Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 3 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.*
  16. *************************************************************************/
  17. #include <iostream>
  18. #include "consolereader.h"
  19. #include "server.h"
  20. ConsoleReader::ConsoleReader(std::vector<Socket*> *sockets_vector)
  21. {
  22. sockets = sockets_vector;
  23. start();
  24. }
  25. void ConsoleReader::run()
  26. {
  27. std::cout << "Copyright (C) 2015 Andrius Štikonas.\nLicense: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law." << std::endl;
  28. connectSignals();
  29. listSockets();
  30. std::string command;
  31. activeSocket = 0;
  32. bool cont = true;
  33. while (cont) {
  34. std::cin >> command;
  35. switch (command[0]) {
  36. case 'a': {
  37. std::cout << "Please set your Orvibo socket to pair mode (rapidly blinking blue light) and wait until new wifi network (WiWo-S20) appears" << std::endl;
  38. std::string ssid, password;
  39. std::cout << "Wireless name (\"c\" for current wifi): ";
  40. std::cin >> ssid;
  41. std::cout << "Password: ";
  42. std::cin >> password;
  43. Server *server = new Server(48899, QByteArray::fromStdString(ssid), QByteArray::fromStdString(password)); // HF-LPB100 chip can be controlled over port 48899
  44. break;
  45. }
  46. case 'A': {
  47. std::cout << "Please set your Orvibo socket to factory reset mode (rapidly blinking red light)" << std::endl;
  48. std::string password;
  49. std::cout << "Password: ";
  50. std::cin >> password;
  51. broadcastPassword(QString::fromStdString(password)); // HF-LPB100 chip can be controlled over port 49999
  52. break;
  53. }
  54. case 'd':
  55. (*sockets) [activeSocket]->tableData();
  56. break;
  57. case 'D':
  58. (*sockets) [activeSocket]->discover();
  59. break;
  60. case 'n': {
  61. std::string name;
  62. std::cout << "Please enter a new name: ";
  63. std::cin >> name;
  64. (*sockets) [activeSocket]->changeSocketName(QString::fromStdString(name));
  65. break;
  66. }
  67. case 'p':
  68. (*sockets) [activeSocket]->toggle();
  69. break;
  70. case 'o':
  71. if (command == "off")
  72. (*sockets) [activeSocket]->powerOff();
  73. else if (command == "on")
  74. (*sockets) [activeSocket]->powerOn();
  75. else {
  76. uint16_t offTime;
  77. std::cout << "Off time in seconds: ";
  78. std::cin >> offTime;
  79. (*sockets) [activeSocket]->setOffTimer(offTime);
  80. }
  81. break;
  82. case 'O':
  83. (*sockets) [activeSocket]->toggleOffTimer();
  84. break;
  85. case 'P': {
  86. std::string password;
  87. std::cout << "Please enter a new password: ";
  88. std::cin >> password;
  89. (*sockets) [activeSocket]->changeSocketPassword(QString::fromStdString(password));
  90. break;
  91. }
  92. case 'q':
  93. cont = false;
  94. emit(QCoreApplication::quit());
  95. break;
  96. case 's':
  97. std::cin >> activeSocket;
  98. --activeSocket; // count from 0
  99. listSockets();
  100. break;
  101. case 't': {
  102. int timezone;
  103. std::cout << "Please enter a new timezone (integer from -11 to 12): ";
  104. std::cin >> timezone;
  105. if (timezone >= -11 && timezone <= 12)
  106. (*sockets) [activeSocket]->changeTimezone(timezone);
  107. else
  108. std::cout << "Invalid timezone";
  109. break;
  110. }
  111. default:
  112. std::cout << "Invalid command: try again" << std::endl;
  113. }
  114. }
  115. }
  116. void ConsoleReader::listSockets()
  117. {
  118. for (const auto &socket : *sockets) {
  119. std::cout << "_____________________________________________________________________________\n" << std::endl;
  120. std::cout << "IP Address: " << socket->ip.toString().remove("::ffff:").toStdString() << "\t MAC Address: " << socket->mac.toHex().toStdString() << "\t Power: " << (socket->powered ? "On" : "Off") << std::endl;
  121. std::cout << "Socket Name: " << socket->socketName.toStdString() << "\t Remote Password: " << socket->remotePassword.toStdString() << "\t Timezone: " << + socket->timezone << (socket->dst ? " (DST)" : "") << std::endl;
  122. std::cout << "Off timer: " << socket->offTime << " " << (socket->offTimerEnabled ? "(enabled)" : "(disabled)") << "\t\t Time: " << socket->socketDateTime.toString().toStdString() << std::endl;
  123. }
  124. std::cout << "_____________________________________________________________________________\n" << std::endl;
  125. if (sockets->size() > 0) {
  126. std::cout << "Active socket: " << (*sockets) [activeSocket]->socketName.toStdString() << std::endl;
  127. }
  128. std::cout << "a - add unpaired socket (WiFi needed)\n";
  129. std::cout << "A - add unpaired socket (no WiFi needed)\n";
  130. if (sockets->size() > 0) {
  131. std::cout << "d - update table data\n";
  132. std::cout << "D - resend discovery packet to the current socket\n";
  133. std::cout << "n - change socket name (max 16 characters)\n";
  134. std::cout << "o - set switch off timer\n";
  135. std::cout << "O - enable/disable switch off timer\n";
  136. std::cout << "p - toggle power state (there are also \"on\" and \"off\" commands)\n";
  137. std::cout << "P - change remote password (max 12 characters)\n";
  138. }
  139. std::cout << "q - quit\n";
  140. if (sockets->size() > 0) {
  141. std::cout << "s - select another socket (default is 1)\n";
  142. std::cout << "t - change timezone" << std::endl;
  143. }
  144. std::cout << "Enter command: " << std::endl;
  145. }
  146. void ConsoleReader::connectSignals()
  147. {
  148. for (const auto &socket : *sockets) {
  149. connect(socket, &Socket::stateChanged, this, &ConsoleReader::listSockets);
  150. }
  151. }