Fotobot
Get data from your photovoltaic plant
interfacebox.cpp
Go to the documentation of this file.
1 
7 #include "interfacebox.h"
8 #include "qextserialport.h"
9 #include "database.h"
10 #include <QTimer>
11 #include <QDebug>
12 
13 INTERFACEBOX::~INTERFACEBOX() {
14 }
15 
16 
17 INTERFACEBOX::INTERFACEBOX(QCoreApplication *) : QThread(0) {
18  moveToThread(this);
19 }
20 
21 void INTERFACEBOX::setDatabase(DATABASE *db) {
22  m_db = db;
23 }
24 
25 void INTERFACEBOX::start(Priority priority) {
26  QTimer::singleShot(10, this, SLOT(slotInit()));
27  QThread::start(priority);
28 }
29 
30 
31 void INTERFACEBOX::slotInit() {
32  QTimer *t = new QTimer(this);
33  t->setSingleShot(false);
34  t->setInterval(600000);
35  t->start();
36  connect(t, SIGNAL(timeout()), this, SLOT(slotThreadAlive()) );
37  connect(this, SIGNAL(sigRestart()), this, SLOT(slotRestart()), Qt::QueuedConnection);
38 }
39 
40 
42  if (!isRunning()) return;
43  QThread::quit();
44 }
45 
46 
48  emit sigRestart();
49 }
50 
51 void INTERFACEBOX::slotRestart() {
52  qDebug() << "INTERFACEBOX::restart()";
53  /* sem nutno doplnit restart interfaceboxu */
54  QList<DBT_INTERFACEBOXES> ifcbox = m_db->interfaceboxes();
55  for (int i=0; i<ifcbox.size(); i++) {
56  int line = ifcbox[i].line;
57  QList<DBT_LINES> lines = m_db->lines(line);
58  if (lines.size() > 0) {
59  restart(lines[0]);
60  }
61  }
62 }
63 
64 
65 void INTERFACEBOX::restart(const DBT_LINES& line) {
66  BaudRateType speed = BAUD9600;
67  switch (line.speed) {
68  case 110: speed = BAUD110; break;
69  case 300: speed = BAUD300; break;
70  case 600: speed = BAUD600; break;
71  case 1200: speed = BAUD1200; break;
72  case 2400: speed = BAUD2400; break;
73  case 4800: speed = BAUD4800; break;
74  case 9600: speed = BAUD9600; break;
75  case 19200: speed = BAUD19200; break;
76  case 38400: speed = BAUD38400; break;
77  case 57600: speed = BAUD57600; break;
78  case 115200: speed = BAUD115200; break;
79  }
80  PortSettings spset;
81  spset.BaudRate = speed;
82  spset.DataBits = DATA_8;
83  spset.Parity = PAR_NONE;
84  spset.StopBits = STOP_1;
85  spset.FlowControl = FLOW_OFF;
86  spset.Timeout_Millisec = line.timeout;
87 
88  QextSerialPort *port = new QextSerialPort(line.device, spset, QextSerialPort::Polling);
89  port->open(QIODevice::ReadWrite);
90  if (port == NULL || !port->isOpen()) {
91  return;
92  }
93 
94  port->write("?", 1); port->flush();
95  sleep(1); port->write("1111", 4); port->flush();
96  sleep(1); port->write("1111", 4); port->flush();
97  sleep(4);
98 
99  port->write("?", 1); port->flush();
100  sleep(1); port->write("0000", 4); port->flush();
101  sleep(1); port->write("0000", 4); port->flush();
102  sleep(1); port->write("0000", 4); port->flush();
103 
104  port->close();
105 }
106 
107 
108 void INTERFACEBOX::slotThreadAlive() {
109  qDebug() << "INTERFACEBOX::slotThreadAlive() Thread is alive :=)";
110 }
111 
QList< DBT_INTERFACEBOXES > interfaceboxes(int line=0)
Returns list of interfaceboxes filtered by line number.
Definition: database.cpp:524
QList< DBT_LINES > lines(int line=0)
Returns list of communications lines.
Definition: database.cpp:380
void restart()
Restarts all connected interfaceboxes.
void start(Priority priority=InheritPriority)
Starts the thread.
Class for communication with database.
Definition: database.h:47
void quit()
Quits the running thread.
Class describing database table LINES.