#!/usr/bin/python import sys import time import datetime import glob import os import commands import MySQLdb path = '/opt/sensiplicity' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/SS-L1.conf') db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() def temp_status(): # Select qSQL with id=4. server_temperature = "Fahrenheit" cursor.execute("SELECT value FROM sensors_system WHERE name = 'server_temperature'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: server_temperature = row[0] return server_temperature server_temperature = temp_status() i = 1 textdata = '' sensadata = '' # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_state = 'on' AND sensor_type = 'Temperature' ORDER BY sid ASC") #print "data = "+str(data[1]) #row = cursor.fetchone() row = cursor.fetchall() for row in cursor: if (row[4] == "on" and row[5] == "yes"): sensor_id = row[1] sensor_file = "/sys/bus/w1/devices/"+sensor_id+"/w1_slave" # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. tfile = open(sensor_file) # Read all of the text in the file. text = tfile.read() # Close the file now that the text has been read. tfile.close() # Split the text with new lines (\n) and select the second line. secondline = text.split("\n")[1] # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). temperaturedata = secondline.split(" ")[9] # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. temperature = float(temperaturedata[2:]) # Put the decimal point in the right place and display it. ctemperature = temperature / 1000 ftemperature = (temperature/1000 + 0) * 9/5 + 32 main_temp = ctemperature temp_symb = "C" if (server_temperature == 'Fahrenheit'): main_temp = ftemperature temp_symb = "F" #print "ID="+str(sensor_id)+",Temp="+str(main_temp)+"" timestamp = int(time.time()) cursor.execute("INSERT INTO `sensors`.`sensors_data` (`id`,`sid`,`timestamp`,`value`) VALUES (NULL,'"+str(row[0])+"','"+str(timestamp)+"','"+str(main_temp)+str(temp_symb)+"')") db.commit() i = i + 1 continue # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_state = 'on' AND sensor_type = 'Soil Sensor' ORDER BY sid ASC") #print "data = "+str(data[1]) #row = cursor.fetchone() row = cursor.fetchall() for row in cursor: if (row[4] == "on" and row[5] == "yes"): sensor_id = row[1] sensor_bus = row[2] # Read all of the text in the file. text_cmd = '/opt/sensiplicity/bin/sn-util-rpi get '+ str(sensor_bus) +' '+ str(sensor_id) +' | grep '+ str(sensor_id) +' | grep -v Node ' text_array = commands.getstatusoutput(text_cmd) text = str(text_array[1]) # Split the text with new lines (\n) and select the second line. # Node_Typecode, Type_Description, Bus, Node_ID, Humidity_%, Ambient_Temp_C, Soil_Temp_C, Light_%, Idle_Voltage_V, Sending_Voltage_V, Uptime_s, Measurement_Time_s, Number_of_Zones, Moisture_Zone0, Moisture_Zone1, Moisture_Zone2, Moisture_Zone3 #0X0001, "Soil Sensor, 1-zone", 3, 0x2625FF756E6B6E6F77, 58.0, 21.19, 21.31, 0, 4.81, 0.00, 940, 936, 1, 4392,,, csv_data = text.split(',') # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). #print "csv="+str(csv_data)+"" humidity = float(csv_data[5]) temperature = float(csv_data[6]) atemp = float(csv_data[6]) stemp = float(csv_data[7]) light = float(csv_data[8]) if(csv_data[14]) != '': moist1 = float(csv_data[14]) else: moist1 = 0 if(csv_data[15]) != '': moist2 = float(csv_data[15]) else: moist2 = 0 if(csv_data[16]) != '': moist3 = float(csv_data[16]) else: moist3 = 0 if(csv_data[17]) != '': moist4 = float(csv_data[17]) else: moist4 = 0 # Put the decimal point in the right place and display it. ctemperature = atemp ftemperature = (atemp + 0) * 9/5 + 32 a_main_temp = ctemperature if (server_temperature == 'Fahrenheit'): a_main_temp = ftemperature ctemperature = stemp ftemperature = (stemp + 0) * 9/5 + 32 s_main_temp = ctemperature temp_symb = "C" if (server_temperature == 'Fahrenheit'): s_main_temp = ftemperature temp_symb = "F" soil_data = str(a_main_temp)+str(temp_symb)+':'+str(s_main_temp)+str(temp_symb)+':'+str(humidity)+':'+str(moist1)+':'+str(moist2)+':'+str(moist3)+':'+str(moist4)+':'+str(light) timestamp = int(time.time()) cursor.execute("INSERT INTO `sensors`.`sensors_data` (`id`,`sid`,`timestamp`,`value`) VALUES (NULL,'"+str(row[0])+"','"+str(timestamp)+"','"+str(soil_data)+"')") db.commit() i = i + 1 continue def rserver_store(): cursor.execute("SELECT value FROM sensors_system WHERE name = 'rserver_store'") row = cursor.fetchone() if row: value = row[0] return value storetime = rserver_store() from datetime import datetime,timedelta time = datetime.now() deltatime = time - timedelta(weeks=1) uxtime = time.strftime("%s") rmtime = deltatime.strftime("%s") cursor.execute("DELETE FROM `sensors`.`sensors_data` WHERE `sensors_data`.`timestamp` < "+str(rmtime)+"") db.commit() cursor.close () db.close() commands.getoutput("/bin/rm ./c") sys.exit()