#include <QCoreApplication>
#include <QDebug>
#include<VSMD12XControl.h>
#include "qtextstream.h"
QTextStream cout1(stdout, QIODevice::WriteOnly);
void logout(QString str){
    std::cout<<str.toStdString()<<"<br>";
    std::fflush(stdout);
}
int main(int argc, char *argv[]) {
    int aaa=1000;
    QString str(argv[1]);
    QCoreApplication a(argc, argv);
    CVSMD12XControl Motercontrol;
    QString m_qstrDeviceConfigFilePath;
  //  cout1<<"adfasdfsadfsd"<<flush;
    cout1<<"Your position number is "<<str<<"<br>"<<flush;
#ifdef WIN32
     m_qstrDeviceConfigFilePath = ("D:\\03MyGit\\linux\\movingliner\\DeviceSettings.ini");
#else
    m_qstrDeviceConfigFilePath = ("/home/data/Setting/DeviceSettings.ini");
#endif

    QSettings *m_qsDeviceConfig;
    m_qsDeviceConfig = new QSettings(m_qstrDeviceConfigFilePath, QSettings::IniFormat);
    long positon =m_qsDeviceConfig->value("LINEAR SHUTTER/Position"+str).toLongLong();
    if (positon==0)
    {
        logout("<b style=\"color:red\">Can not Find the position<\/b>");
        return 0;
    }
    cout1<<"Find Configfile in "<<m_qstrDeviceConfigFilePath<<" <br> The Port of Motor is "<<m_qsDeviceConfig->value("LINEAR SHUTTER/Port").toString()<<"<br>"<<flush;
    cout1<<"Position Find  Value is "<<m_qsDeviceConfig->value("LINEAR SHUTTER/Position"+str).toLongLong()<<"<br>"<<flush;
    cout1<<"Configing The Motor <br>"<<flush;
    // m_qsDeviceConfig->value("LINEAR SHUTTER/Port").toString()
   if( m_qsDeviceConfig->value("LINEAR SHUTTER/Port").toString()=="GPIO")
   {
       //cout1<<"ERROR number position"<<flush;
       int positionnumber=m_qsDeviceConfig->value("LINEAR SHUTTER/TotalPosition").toInt();
       if (str.toInt()>=positionnumber)
       {
           cout1<<"ERROR number position"<<flush;
           return 0;
       }

        int targetpos=m_qsDeviceConfig->value("LINEAR SHUTTER/Position"+str).toLongLong();

       for (int ii = 0; ii < positionnumber; ++ii) {
           int tempgpio=m_qsDeviceConfig->value("LINEAR SHUTTER/Position"+QString::number(ii)).toInt();
           QString com="gpio mode "+QString::number(tempgpio)+" out";
           system(com.toStdString().c_str());
           com="gpio write "+QString::number(tempgpio)+" 0";
           system(com.toStdString().c_str());

       }
       QThread::msleep(500);
       QString com="gpio write "+QString::number(targetpos)+" 1";
       system(com.toStdString().c_str());
       QThread::msleep(500);
       cout1<<"FINISH Change Shutter <br>"<<flush;

       return 0;



   }
    Motercontrol.ILMES_InitializeComm(m_qsDeviceConfig->value("LINEAR SHUTTER/Port").toString());
    ControllerParams cpTest;
    Motercontrol.ILMES_InitializeParams(cpTest);




    logout("Moving  to Zero");
    Motercontrol.StartBackZero();
    //std::cout<<"beging to move position";
    logout("Moving to The Position");
    Motercontrol.MoveTo(positon);

    logout("<b style=\"color:green\">Finish the Task!!!<\/b> <br><b>Thanks For Using<\/b>");
    return 0;
   // return QCoreApplication::exec();
}
