from datetime import datetime
from time import sleep
import time
import RPi.GPIO as GPIO
import subprocess
FILENAME = ""
WRITE_FREQUENCY = 1
def get_dallas_data(): 
  subprocess.call(['modprobe', 'w1-gpio']) 
  subprocess.call(['modprobe', 'w1-therm'])

  filenameD = "/sys/bus/w1/devices/28-0000093b782d/w1_slave"
  tfile = open(filenameD) 
  text = tfile.read() 
  tfile.close()
  secondline = text.split("\n")[1] 
  temperaturedata = secondline.split(" ")[9] 
  temperature = float(temperaturedata[2:]) 
  temperature = temperature / 1000 
  temp = float(temperature) 
  temp = temp*1.8+32 
  tempDallas = str(round(temp,1)) 
  return tempDallas 
def get_sense_data():
  sense_data=[] 
  dateNow = str(datetime.now())
  sense_data.append("['" + dateNow[11:16] + "'") 
  sense_data.append(get_dallas_data() +"],") 

  return sense_data
def log_data():
  output_string = ",".join(str(value)for value in sense_data)
  batch_data.append(output_string)
def file_setup(filename):
  filename = str(filename)
  with open(filename,'w') as f:
    print 'starting file_setup'  
    f.write('<!doctype html>\n')
  with open(filename,"a")as f:
    f.write('<html>\n')
    f.write('<head>\n')
    f.write('<meta charset="UTF-8">\n')
    f.write('<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>\n')
    f.write('<script type="text/javascript">\n')
    f.write("google.charts.load('current',{'packages':['corechart']});\n")
    f.write('google.charts.setOnLoadCallback(drawChart);\n')
    f.write('function drawChart(){\n')
    f.write('var data=google.visualization.arrayToDataTable([\n')
    f.write("['time','temp*F'],\n")
#####MAINPROGRAMBOI#####
batch_data=[]
if FILENAME=="":
    timeLog = str(datetime.now())
    timeLog= timeLog[0:9]
    filename="TempLog"+timeLog+".htm"
else:
    timeLog = str(datetime.now())
    timeLog= timeLog[0:9]
    filename=FILENAME+"-"+timeLog+".htm"
file_setup(filename)
readingstotake=int(input("enter the numbet of resaadings to take"))
numReadings = 0
dayNow = str(datetime.now())
while numReadings < readingstotake:
    numReadings = numReadings + 1
    sense_data = get_sense_data()
    log_data()
    if len(batch_data) >= WRITE_FREQUENCY:
        print("writing to file plsw wait")
        with open(filename, "a") as f:
            for line in batch_data:
                f.write(line + "\n")
                sleep(60)
            batch_data=[]
with open(filename, "a") as f:
    f.write(']);\n')
    f.write('var options = {\n')
    f.write("tile:'GEAR WETHERT STATION FO" + dayNow[0:10] +"',\n")
    f.write("curveType: 'function',\n")
    f.write("legend: {position: 'bottom'}\n")
    f.write("}\n")
    f.write("var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));\n")
    f.write('chart.draw(data, options);\n')
    f.write("</script>\n")
    f.write("</head>\n")
    f.write("<body>\n")
    f.write('<div id="curve_chart" style="width: 900px; height: 500px"></div>\n')
    f.write("</body>\n")
    f.write("</html>")
print("Program is work and done")
