/* #########################################
 * Traffic monitoring using Raspberry PI
 * Author: Michal Zacpal
 * Library: Raspberry Pi temperature reader
 * 2015
 * ######################################### */
#include "RaspiTemp.h"

float ReturnTemperatureF(QString sSensor)
{
    float  fTemp = 999;
    QString fileName("/sys/bus/w1/devices/" + sSensor + "/w1_slave");
    QFile inputFile(fileName);

    if(inputFile.open(QIODevice::ReadOnly))
    {
       QTextStream in(&inputFile);
       QString line = in.readLine();
       QString Temp("");
       if (line.contains("YES"))
       {
           line = in.readLine();
           for (int i = 0; i < line.length(); i++)
           {
               if (line[i] == '='){
                   int pos = 0;
                   for (int j = i+1; j < line.length(); j++){
                       Temp = Temp + line[j];
                       pos++;
                   }
                   break;
               }
           }
           fTemp = (Temp.toFloat()/1000);
           fTemp = round(fTemp);
       }
       inputFile.close();
    }
    return fTemp;
}

