#include "mainwindow.h"
#include "ui_mianwindow.h"
#include "ipwindow.h"
#include <QTcpSocket>
LconWindow::LconWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LconWindow)
{
pic_socket = NULL;
pic_timer = NULL;
env_socket = NULL;
env_timer = NULL;
ui->setupUi(this);
ui->btn_Start->setEnabled(true);
ui->btn_Stop->setEnabled(false);
ui->btn_back->setEnabled(false);
ui->btn_light->setEnabled(false);
ui->btn_fan->setEnabled(false);
ui->btn_buzz->setEnabled(false);
ui->btn_shuma->setEnabled(false);
if (env_socket == NULL){
env_socket = new QTcpSocket();
connect(env_socket, SIGNAL(connected()), this, SLOT(net_env_connected()));
connect(env_socket, SIGNAL(disconnected()), this, SLOT(net__env_disconnected()));
connect(env_socket, SIGNAL(readyRead()), this, SLOT(net_recv_env()));
}
if (env_timer == NULL) {
env_timer = new QTimer(this);
connect(env_timer, SIGNAL(timeout()), this, SLOT(net_send_env()));
}
env_socket->connectToHost(ip, port);
}
LconWindow::~LconWindow()
{
delete ui;
}
#define REQ_DATA_SIZE 32
#define HDR_DATA_SIZE 128
void LconWindow::net_connected()
{
pic_timer->start(10);
state = 0;
}
void LconWindow::net_disconnected()
{
if (pic_timer->isActive()){
pic_timer->stop();
}
pic_socket->close();
ui->btn_Start->setEnabled(true);
ui->btn_Stop->setEnabled(false);
}
void LconWindow::net_send_pic()
{
int ret;
char request[REQ_DATA_SIZE] = {0};
if (state == 0) {
memset(request, 0, sizeof(request));
strcat(request, "PIC_SEND");
ret = pic_socket->write(request, sizeof(request));
if (ret != sizeof(request)) {
qDebug() << "send request failed";
pic_timer->stop();
pic_socket->close();
ui->btn_Start->setEnabled(true);
ui->btn_Stop->setEnabled(false);
}
pic_socket->flush();
state = 1;
}
}
void LconWindow::net_recv_data()
{
int ret;
char response[HDR_DATA_SIZE] = {0};
switch (state) {
case 1:
if (pic_socket->bytesAvailable() < sizeof(response))
return;
ret = pic_socket->read(response, sizeof(response));
if (ret != sizeof(response)) {
qDebug() << "recv response failed";
goto err;
}
ret = sscanf(response, "%dlen", &piclen);
if (ret != 1) {
qDebug() << "response format error";
goto err;
}
// qDebug()<<"piclen"<<piclen;
state = 2;
if (pic_socket->bytesAvailable() >= piclen)
goto readdat;
break;
case 2:
readdat:
if (pic_socket->bytesAvailable() < piclen){
// qDebug()<<pic_socket->bytesAvailable();
return;
}
if (piclen > 0) {
unsigned int ret;
memset(picbuf, 0, sizeof(picbuf));
ret = pic_socket->read(picbuf, piclen);
if (ret < piclen) {
qDebug() << "recv pic failed";
pic_timer->stop();
pic_socket->close();
state = 0;
}
// qDebug()<<"show pic";
QPixmap pixmap;
pixmap.loadFromData((const uchar *)picbuf, piclen, "JPEG");
ui->lab_pic->setPixmap(pixmap);
state = 0;
}
state = 0;
break;
}
return;
err:
pic_timer->stop();
pic_socket->close();
state = 0;
ui->btn_Start->setEnabled(true);
ui->btn_Stop->setEnabled(false);
return;
}
void LconWindow::on_btn_Start_clicked()
{
ui->btn_Start->setEnabled(false);
ui->btn_Stop->setEnabled(true);
if(socket != NULL)
socket->close();
if (pic_socket == NULL){
pic_socket = new QTcpSocket();
connect(pic_socket, SIGNAL(connected()), this, SLOT(net_connected()));
connect(pic_socket, SIGNAL(disconnected()), this, SLOT(net_disconnected()));
connect(pic_socket, SIGNAL(readyRead()), this, SLOT(net_recv_data()));
}
if (pic_timer == NULL) {
pic_timer = new QTimer(this);
connect(pic_timer, SIGNAL(timeout()), this, SLOT(net_send_pic()));
}
pic_socket->connectToHost(ip, port);
state = 0;
}
void LconWindow::net_env_connected()
{
ui->btn_light->setText("开灯");
ui->btn_fan->setText("开风扇");
ui->btn_buzz->setText("开警报");
ui->btn_shuma->setText("开数码");
ui->btn_light->setEnabled(true);
ui->btn_fan->setEnabled(true);
ui->btn_buzz->setEnabled(true);
ui->btn_shuma->setEnabled(true);
env_timer = new QTimer(this);
connect(env_timer, SIGNAL(timeout()), this, SLOT(net_send_env()));
env_timer->start(1000);
}
void LconWindow::net_env_disconnected()
{
ui->btn_light->setText("开灯");
ui->btn_fan->setText("开风扇");
ui->btn_buzz->setText("开警报");
ui->btn_shuma->setText("开数码");
if(env_timer->isActive())
env_timer->stop();
env_socket->close();
env_socket = NULL;
}
void LconWindow::net_send_env()
{
char req_buf[] = "ENV_SEND";
int ret = env_socket->write(req_buf, sizeof(req_buf));
if (ret != sizeof(req_buf)){
perror("ENV_SEND");
}
// qDebug()<<"ret = "<< ret;
env_socket->flush();
}
void LconWindow::net_recv_env()
{
char buf[128];
char string[6] = {0};
env_socket->read(buf, sizeof(buf));
sscanf(buf, "%did%dt%dh%dl%dx%dy%dz%dled%dfan%dbuzz%dsev", &id, &tmp, &hum, &light, &x, &y, &z, &led, &fan, &buzz, &sev_led);
// qDebug()<<"buf:"<<buf;
/*实时刷新显示环境信息*/
snprintf(string, sizeof(string), "%d", id);
ui->edt_id->setText(string);
snprintf(string, sizeof(string), "%d", tmp);
ui->edt_tem->setText(string);
snprintf(string, sizeof(string), "%d", hum);
ui->edt_hum->setText(string);
snprintf(string, sizeof(string), "%d", light);
ui->edt_light->setText(string);
snprintf(string, sizeof(string), "%d", x);
ui->edt_x->setText(string);
snprintf(string, sizeof(string), "%d", y);
ui->edt_y->setText(string);
snprintf(string, sizeof(string), "%d", z);
ui->edt_z->setText(string);
// /*实时刷新控制按建状态*/
// if(fan == 0){
// ui->btn_fan->setText("开风扇");
// } else {
// ui->btn_fan->setText("关风扇");
// }
// if(sev_led == 0){
// ui->btn_shu->setText("关数码");
// } else {
// ui->btn_shu->setText("开数码");
// }
}
void LconWindow::on_btn_light_clicked()
{
static bool state1 = false;
if (state1 == false){
env_socket->write("LIGHT_ON", sizeof("LIGHT_ON"));
state1 = true;
ui->btn_light->setText("关灯");
} else {
env_socket->write("LIGHT_OFF", sizeof("LIGHT_OFF"));
state1 = false;
ui->btn_light->setText("开灯");
}
}
void LconWindow::on_btn_fan_clicked()
{
static bool state1 = false;
if (state1 == false){
env_socket->write("FAN_ON", sizeof("FAN_ON"));
state1 = true;
ui->btn_fan->setText("关风扇");
} else {
env_socket->write("FAN_OFF", sizeof("FAN_OFF"));
state1 = false;
ui->btn_fan->setText("开风扇");
}
}
void LconWindow::on_btn_buzz_clicked()
{
static bool state1 = false;
if (state1 == false){
env_socket->write("BUZZ_ON", sizeof("BUZZ_ON"));
state1 = true;
ui->btn_buzz->setText("关警报");
} else {
env_socket->write("BUZZ_OFF", sizeof("BUZZ_OFF"));
state1 = false;
ui->btn_buzz->setText("开警报");
}
}
void LconWindow::on_btn