#include "dogobject.h"

#include <QDebug>

MyEHomeShm *ytShm;

void setFullTime(FullTime *fTime, QDateTime qTime){
    fTime->YEAR = qTime.date().year()&0xffff;
    fTime->MONTH = qTime.date().month()&0xff;
    fTime->DAY = qTime.date().day()&0xff;
    fTime->HOUR = qTime.time().hour()&0xff;
    fTime->MINUTE = qTime.time().minute()&0xff;
    fTime->SECOND = qTime.time().second()&0xff;
    fTime->TIMESTAMP = qTime.toTime_t();
}

DogObject::DogObject(QObject *parent) :
    QObject(parent)
{
    shmLoad = false;
    isWorking = false;
    db = QSqlDatabase::addDatabase("QMYSQL","conf_db");
    db.setDatabaseName("ytIoT");
    db.setHostName("47.98.201.187");
    db.setUserName("root");
    db.setPassword("Yt2018IoT");
    db.open();
    shmLoad = load_shm();
    start_t = QDateTime::currentDateTime().toTime_t();
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
    for(int i=0;i<16;i++)
        ytShm->workingTime[i] = 0;
    devConfList.clear();
    if(db.isOpen()){
        QSqlQuery qry = db.exec("select device_code, comm_type, company_code from ytiot_v_video order by device_code");
        while(qry.next()){
            devConfList.append(DevConf(qry.value(0).toString(),qry.value(1).toUInt()&0xff,qry.value(2).toString()));
        }
        qry.clear();
    }
    QDateTime zeroTime = QDateTime::fromTime_t(0);
    for(int i=0;i<DEVICES_COUNT;i++){
        ytShm->eHomeDevice[i].io[8] = 0x00;
    }
    for(int i=0;i<devConfList.length();i++){
        ytShm->eHomeDevice[i].LoginId = -1;
        ytShm->eHomeDevice[i].SessionId = -1;
        ytShm->eHomeDevice[i].listen_preview_handle = -1;
        ytShm->eHomeDevice[i].listen_alarm_handle = -1;
        ytShm->eHomeDevice[i].lLinkHandle = -1;
        ytShm->eHomeDevice[i].previewId = -1;
        ytShm->eHomeDevice[i].previewport = 58000+i;
        ytShm->eHomeDevice[i].alarmport = 0;
        ytShm->eHomeDevice[i].Connected = 0x00;
        ytShm->eHomeDevice[i].Inited = 0x00;
        ytShm->eHomeDevice[i].onLined = 0x00;
        ytShm->eHomeDevice[i].savetmp = 0x00;
        ytShm->eHomeDevice[i].transed = 0x00;
        for(int j=0;j<16;j++)
            ytShm->eHomeDevice[i].io[j] = 0x00;
        ytShm->eHomeDevice[i].time[0]=0;
        ytShm->eHomeDevice[i].time[1]=0;
        bzero(ytShm->eHomeDevice[i].deviceID,sizeof(ytShm->eHomeDevice[i].deviceID));
        ytShm->eHomeDevice[i].tmpfile = NULL;
        setFullTime(&(ytShm->eHomeDevice[i].onLinetime),zeroTime);
        setFullTime(&(ytShm->eHomeDevice[i].transtime),zeroTime);
        sprintf(ytShm->eHomeDevice[i].deviceID,"%s",devConfList.at(i).DeviceId.toUtf8().data());
        sprintf(ytShm->eHomeDevice[i].rtsppath,"%s",devConfList.at(i).rtspPath.toUtf8().data());
        ytShm->eHomeDevice[i].io[8] = devConfList.at(i).CommType;
        printf("%d)%s\n",i+1,ytShm->eHomeDevice[i].deviceID);
    }
}

bool DogObject::load_shm()
{
    int shmid;
    key_t key;
    if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
        return false;
    if((shmid = shmget(key,sizeof(MyEHomeShm),IPC_CREAT|0666))==-1)
        return false;
    ytShm = (MyEHomeShm *)shmat(shmid,NULL,0);
    return true;
}

void DogObject::start()
{
    if(shmLoad&&db.isOpen())
        timer->start(1000);
}

void DogObject::time_out()
{
    if(isWorking)
        return;
    isWorking = true;
    uint t = QDateTime::currentDateTime().toTime_t();
    if(db.isOpen()){
        devConfList.clear();
        QSqlQuery qry = db.exec("select device_code, comm_type, company_code from ytiot_v_video order by device_code");
//        qDebug()<<"select devicd_code, comm_type from ytiot_v_video";
        while(qry.next()){
            devConfList.append(DevConf(qry.value(0).toString(),qry.value(1).toUInt()&0xff,qry.value(2).toString()));
//            qDebug()<<qry.value(0).toString().toUtf8().data()<<QString("%1").arg(qry.value(1).toUInt()&0xff).toUtf8().data();
        }
        qry.clear();
    }else
        db.open();
    for(int i=0;i<DEVICES_COUNT;i++){
        for(int j=0;j<devConfList.length();j++){
            if(strcmp(ytShm->eHomeDevice[i].deviceID,devConfList.at(j).DeviceId.toUtf8().data())==0)
            {
                ytShm->eHomeDevice[i].io[8] = devConfList.at(j).CommType;
                break;
            }
        }
        if(ytShm->eHomeDevice[i].Inited==0x01){
            if(ytShm->eHomeDevice[i].io[1]==0x01)
                ytShm->eHomeDevice[i].io[0]=0x00;
            if(ytShm->eHomeDevice[i].Connected==0x01){
                if((ytShm->eHomeDevice[i].time[0]>0)&&((t-ytShm->eHomeDevice[i].time[0])>10)){
                    if(ytShm->eHomeDevice[i].io[0]==0x01)
                        ytShm->eHomeDevice[i].io[0]=0x00;
//                }else if((ytShm->eHomeDevice[i].time[1]>0)&&((t-ytShm->eHomeDevice[i].time[1])>300)){
//                    if(ytShm->eHomeDevice[i].io[0]==0x01)
//                        ytShm->eHomeDevice[i].io[0]=0x00;
                }
            }
        }
    }

    if((t-ytShm->workingTime[0])>5){
        system("killall ytCamCore");
        system("rm -f /usr/local/nginx/html/hls/*");
        system("rm /tmp/*.mp4");
        system("ytCamCore &");
        ytShm->workingTime[0]=t;
    }
    isWorking = false;
}
