Fotobot
Get data from your photovoltaic plant
networkcontroller.cpp
Go to the documentation of this file.
1 
7 #include "networkcontroller.h"
8 #include "static.h"
9 #include <QVariant>
10 #include <QDateTime>
11 #include <QFile>
12 #include <QRegExp>
13 #include <QDebug>
14 #include <QProcess>
15 
16 #define CONFIG_NET "/etc/conf.d/net"
17 #define CONFIG_HOSTNAME "/etc/conf.d/hostname"
18 
20 }
21 
22 void NetworkController::servicePrivate(HttpRequest& request) {
23  QByteArray action = request.getParameter("action");
24  if (action.startsWith("apply")) {
25  actionApply(request);
26  return;
27  }
28 
29  getConfigValues();
30 
31  write(QString(
32  "<h2>%1</h2>\n"
33  "<table class=\"formTable\">\n"
34  "<form>\n"
35  "<input type=\"hidden\" name=\"action\" value=\"apply\"/>\n"
36  "<tr><td>%2</td><td><input type=\"text\" name=\"hostname\" value=\"%3\"></td></tr>\n"
37  "<tr><td>%4</td><td><input type=\"checkbox\" name=\"ipv4_dhcp\" value=\"Y\" %5></td></tr>\n"
38  "<tr><td>%6</td><td><input type=\"text\" name=\"ipv4_addr\" value=\"%7\"></td></tr>\n"
39  "<tr><td>%8</td><td><input type=\"text\" name=\"ipv4_mask\" value=\"%9\"></td></tr>\n"
40  "<tr><td>%10</td><td><input type=\"text\" name=\"ipv4_router\" value=\"%11\"></td></tr>\n"
41  "<tr><td>%12</td><td><input type=\"text\" name=\"ipv4_dns\" value=\"%13\"></td></tr>\n"
42  "<tr><td></td><td><input type=\"submit\" value=\"OK\"></td></tr>\n"
43  "</form></table>\n" )
44  .arg(tr("Network settings"))
45  .arg(tr("Hostname:")) .arg( m_hostname )
46  .arg(tr("Use DHCP:")) .arg( (m_ipv4_dhcp) ? "checked" : "" )
47  .arg(tr("IPv4 address:")) .arg( m_ipv4_addr )
48  .arg(tr("IPv4 mask:")) .arg( m_ipv4_mask )
49  .arg(tr("IPv4 router:")) .arg( m_ipv4_router )
50  .arg(tr("IPv4 dns server:")) .arg( m_ipv4_dns )
51  .toUtf8()
52  );
53 
54 }
55 
56 
57 void NetworkController::getConfigValues() {
58  m_hostname = "hostname";
59  m_ipv4_dhcp = false;
60  m_ipv4_addr = "192.168.1.135";
61  m_ipv4_mask = "255.255.255.0";
62  m_ipv4_router = "192.168.1.1";
63  m_ipv4_dns = "8.8.8.8";
64 
65  QRegExp rx_dhcp ("config_eth0=\"dhcp\"");
66  QRegExp rx_addr ("config_eth0=\"([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\\snetmask\\s([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\"");
67  QRegExp rx_route ("routes_eth0=\"default via ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\"");
68  QRegExp rx_dns ("dns_servers_eth0=\"([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\"");
69  QRegExp rx_hostname ("hostname=\"(.*)\"");
70 
71  QFile net(CONFIG_NET);
72  if (!net.open(QIODevice::ReadOnly | QIODevice::Text)) {
73  qDebug() << "Could not open /etc/conf.d/net";
74  return;
75  }
76 
77  while (!net.atEnd()) {
78  QString line = QString::fromUtf8(net.readLine()).trimmed();
79  if (rx_dhcp.exactMatch(line)) {
80  m_ipv4_dhcp = true;
81  break;
82  }
83  if (rx_addr.exactMatch(line)) {
84  m_ipv4_addr = rx_addr.cap(1);
85  m_ipv4_mask = rx_addr.cap(2);
86  continue;
87  }
88  if (rx_route.exactMatch(line)) {
89  m_ipv4_router = rx_route.cap(1);
90  continue;
91  }
92  if (rx_dns.exactMatch(line)) {
93  m_ipv4_dns = rx_dns.cap(1);
94  continue;
95  }
96  }
97 
98  net.close();
99 
100  QFile host(CONFIG_HOSTNAME);
101  if (!host.open(QIODevice::ReadOnly | QIODevice::Text)) {
102  qDebug() << "Could not open /etc/conf.d/hostname";
103  return;
104  }
105 
106  while (!host.atEnd()) {
107  QString line = QString::fromUtf8(host.readLine()).trimmed();
108  if (rx_hostname.exactMatch(line)) {
109  m_hostname = rx_hostname.cap(1);
110  break;
111  }
112  }
113 
114  host.close();
115 
116 }
117 
118 
119 void NetworkController::actionApply(HttpRequest& request) {
120  m_hostname = request.getParameter("hostname");
121  m_ipv4_dhcp = ( request.getParameter("ipv4_dhcp") == "Y") ? true : false;
122  m_ipv4_addr = request.getParameter("ipv4_addr");
123  m_ipv4_mask = request.getParameter("ipv4_mask");
124  m_ipv4_router = request.getParameter("ipv4_router");
125  m_ipv4_dns = request.getParameter("ipv4_dns");
126 
127  QFile host(CONFIG_HOSTNAME);
128  if (!host.open(QIODevice::WriteOnly | QIODevice::Text)) {
129  qDebug() << "Could not open /etc/conf.d/hostname for writing";
130  write( tr("Sorry, it is not possible to write to /etc/conf.d/hostname").toUtf8() );
131  return;
132  }
133  host.write("# This file is generated by fotobot application\n");
134  host.write("# Your changes will be overwritten\n\n");
135  host.write( QString("hostname=\"%1\"\n").arg(m_hostname).toUtf8() );
136  host.close();
137 
138  QFile net(CONFIG_NET);
139  if (!net.open(QIODevice::WriteOnly | QIODevice::Text)) {
140  qDebug() << "Could not open /etc/conf.d/hostname for writing";
141  write( tr("Sorry, it is not possible to write to /etc/conf.d/net").toUtf8() );
142  return;
143  }
144  net.write("# This file is generated by fotobot application\n");
145  net.write("# Your changes will be overwritten\n\n");
146  if (m_ipv4_dhcp) {
147  net.write( "dns_servers_eth0=\"8.8.8.8\"\n" );
148  net.write( "config_eth0=\"dhcp\"\n" );
149  net.write( "fallback_eth0=\"192.168.1.111 netmask 255.255.255.0\"\n" );
150  } else {
151  net.write( QString("config_eth0=\"%1 netmask %2\"\n").arg(m_ipv4_addr).arg(m_ipv4_mask).toUtf8() );
152  net.write( QString("routes_eth0=\"%1\"\n").arg(m_ipv4_router).toUtf8() );
153  net.write( QString("dns_servers_eth0=\"%1\"\n").arg(m_ipv4_dns).toUtf8() );
154  }
155  net.close();
156 
157  write( tr("<h2>Restarting....</h2>\n<p>Device is restarting now. Please wait few minutes and then refresh this page.</p>").toUtf8() );
158 
159  QProcess *restart = new QProcess();
160  restart->start("/usr/bin/fotobot-reboot");
161 
162 }
163 
void servicePrivate(HttpRequest &request)
void actionApply(HttpRequest &request)