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.

socket.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef SOCKET_H
  18. #define SOCKET_H
  19. #include <QByteArray>
  20. #include <QDateTime>
  21. #include <QHostAddress>
  22. #include <QQueue>
  23. #include <QTimer>
  24. #include <QThread>
  25. class QUdpSocket;
  26. const QByteArray magicKey = QByteArray::fromHex("68 64"); // recognize datagrams from the socket
  27. class Socket : public QThread
  28. {
  29. Q_OBJECT
  30. Q_SIGNALS:
  31. void stateChanged();
  32. void datagramQueued();
  33. public:
  34. Socket(QHostAddress, QByteArray); // from discovery packet
  35. void toggle();
  36. void powerOff();
  37. void powerOn();
  38. void discover();
  39. void tableData();
  40. void changeSocketName(QString newName);
  41. void changeSocketPassword(QString newPassword);
  42. void changeTimezone(int8_t newTimezone);
  43. void setOffTimer(uint16_t offTime);
  44. void toggleOffTimer();
  45. bool parseReply(QByteArray);
  46. QHostAddress ip;
  47. QByteArray mac;
  48. bool powered;
  49. bool dst;
  50. QByteArray socketName, remotePassword;
  51. int8_t timezone;
  52. uint16_t offTime;
  53. bool offTimerEnabled;
  54. QDateTime socketDateTime;
  55. private:
  56. enum Datagram {
  57. QueryAll,
  58. Discover,
  59. Subscribe,
  60. PowerOff,
  61. PowerOn,
  62. ReadTable,
  63. SocketData,
  64. TimingData,
  65. TableModify,
  66. MaxCommands
  67. };
  68. void sendDatagram(Datagram);
  69. void subscribe();
  70. void processQueue() {
  71. start();
  72. }
  73. void run();
  74. void writeSocketData(QByteArray socketName, QByteArray remotePassword, int8_t timeZone, uint16_t offTime);
  75. QByteArray intToHex(unsigned int value, unsigned int length, bool littleEndian = true);
  76. int hexToInt(QByteArray value, bool littleEndian = true);
  77. QByteArray commandID[MaxCommands];
  78. QByteArray datagram[MaxCommands];
  79. QByteArray rmac; // Reveresed mac
  80. QByteArray versionID;
  81. QByteArray icon; // displayed icon
  82. QByteArray hardwareVersion;
  83. QByteArray firmwareVersion;
  84. QByteArray wifiFirmwareVersion; // CC3000 firmware version
  85. QByteArray staticServerIP;
  86. QByteArray port = QByteArray::fromHex("10:27"); // 10000 in hex (little endian)
  87. QByteArray domainServerName;
  88. QByteArray localIP; // same as QHostAddress ip but in QByteArray, maybe fix later...
  89. QByteArray localGatewayIP;
  90. QByteArray localNetMask;
  91. QByteArray DST_TimezoneOffset;
  92. QByteArray discoverable;
  93. QByteArray timeZoneSet;
  94. QByteArray socketTableNumber, socketTableVersion, timingTableNumber, timingTableVersion; // FIXME: not used yet
  95. const QByteArray twenties = QByteArray::fromHex("20 20 20 20 20 20"); // mac address padding, 6 spaces
  96. const QByteArray zeros = QByteArray::fromHex("00 00 00 00");
  97. const QByteArray zero = QByteArray::fromHex("00");
  98. const QByteArray one = QByteArray::fromHex("01");
  99. QUdpSocket *udpSocket;
  100. QTimer *subscribeTimer;
  101. bool subscribed;
  102. QQueue<Datagram> commands;
  103. };
  104. #endif /* SOCKET_H */