#!/usr/bin/python import sys import time 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') # find the path of a sensor directory that starts with 28 db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() i = 1 textdata = '' sensadata = '' #devicelist = glob.glob('/sys/bus/w1/devices/28*') devicelist = glob.glob('/sys/bus/w1/devices/*/w1_slave') for sensor in devicelist: #sensor_file = sensor + "/w1_slave" sensor_file = sensor sensor_id = sensor_file.split('/') sensor_bus = sensor_id[5].split('-') print "==========================================================================" print "= " print "= The sensor with ID "+ str(sensor_id[5]) + "" print "= " # Select qSQL with id=4. cursor.execute("SELECT sid FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"'") #data = cursor.fetchall() #print "data = "+str(data[1]) if cursor.fetchone(): print "= is already in the database" print "= " print "==========================================================================" print " " continue else: #cursor.execute("INSERT INTO `sensors`.`sensors_info` (`sensor_setup`, `sensor_id`, `sensor_bus`, `sensor_state`, `sensor_plot`, `sensor_error`, `sensor_comment`) VALUES ('no', '"+ str(sensor_id[5]) +"', '"+ str(sensor_bus[0]) +"', 'off', 'off', '', '')") cursor.execute("INSERT INTO `sensors`.`sensors_info` (`sensor_setup`, `sensor_id`, `sensor_bus`, `sensor_state`, `sensor_plot`, `sensor_error`, `sensor_comment`, `sensor_type`) VALUES ('no', '"+ str(sensor_id[5]) +"', '1-wire', 'off', 'off', '', '', 'Temperature')") db.commit() print "= has been added to the database" print "= " print "==========================================================================" print " " i = i + 1 devicelist = glob.glob('/sys/bus/w1/devices/*/state') for sensor in devicelist: #sensor_file = sensor + "/w1_slave" sensor_file = sensor sensor_id = sensor_file.split('/') sensor_bus = sensor_id[5].split('-') print "==========================================================================" print "= " print "= The sensor with ID "+ str(sensor_id[5]) + "" print "= " # Select qSQL with id=4. cursor.execute("SELECT sid FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"'") #data = cursor.fetchall() #print "data = "+str(data[1]) if cursor.fetchone(): print "= is already in the database" print "= " print "==========================================================================" print " " continue else: #cursor.execute("INSERT INTO `sensors`.`sensors_info` (`sensor_setup`, `sensor_id`, `sensor_bus`, `sensor_state`, `sensor_plot`, `sensor_error`, `sensor_comment`) VALUES ('no', '"+ str(sensor_id[5]) +"', '"+ str(sensor_bus[0]) +"', 'off', 'off', '', '')") cursor.execute("INSERT INTO `sensors`.`sensors_info` (`sensor_setup`, `sensor_id`, `sensor_bus`, `sensor_state`, `sensor_plot`, `sensor_error`, `sensor_comment`, `sensor_type`) VALUES ('no', '"+ str(sensor_id[5]) +"', '1-wire', 'off', 'off', '', '', 'Emergency Button')") db.commit() print "= has been added to the database" print "= " print "==========================================================================" print " " i = i + 1 for bus in range(1,4): sensor_file = commands.getoutput("/opt/sensiplicity/bin/sn-util-rpi fast " + str(bus) + " | grep -v Typecode ") for sensor_csv in sensor_file.splitlines(): sensor_id = sensor_csv.split(',') print "sensor_id ="+str(sensor_id) if (len(sensor_id) == 2): print "==========================================================================" print "= " print "= The sensor with ID " + str(sensor_id[0]) + "" print "= " # Select qSQL with id=4. cursor.execute("SELECT sid FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[0]) +"'") #data = cursor.fetchall() #print "data = "+str(data[1]) if cursor.fetchone(): cursor.execute("SELECT sensor_bus FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[0]) +"' AND sensor_bus = '"+ str(bus) +"'") if cursor.fetchone(): print "= This Sensor ID is already in the database" else: cursor.execute("UPDATE sensors_info SET sensor_bus = '"+ str(bus) +"' WHERE `sensor_id` = '"+ str(sensor_id[0]) +"'") db.commit() print "= This Sensor ID is already in the database" print "= Updating Bus ID to '"+ str(bus) +"'" print "= " print "==========================================================================" print " " continue else: cursor.execute("INSERT INTO `sensors`.`sensors_info` (`sensor_setup`, `sensor_id`, `sensor_bus`, `sensor_state`, `sensor_plot`, `sensor_error`, `sensor_comment`, `sensor_type`) VALUES ('no', '"+ str(sensor_id[0]) +"', '"+ str(bus) +"', 'off', 'off', '', '', 'Soil Sensor')") db.commit() print "= has been added to the database" print "= " print "==========================================================================" print " " i = i + 1 cursor.close () db.close() os.system("/bin/rm ./c") sys.exit()