Commit bc1f4a9b authored by Tom Hartley's avatar Tom Hartley

Updates, new stuffs

parent 442b1ad6
#This file takes in inputs from a variety of sensor files, and outputs information to a variety of services
import ConfigParser
import time
import inspect
import os
from sys import exit
from sensors import sensor
def get_subclasses(mod,cls):
for name, obj in inspect.getmembers(mod):
if hasattr(obj, "__bases__") and cls in obj.__bases__:
return obj
if not os.path.isfile('sensors.cfg'):
print "Unable to access config file: sensors.cfg"
exit(1)
sensorConfig = ConfigParser.SafeConfigParser()
sensorConfig.read('sensors.cfg')
sensorNames = sensorConfig.sections()
sensorPlugins = []
for i in sensorNames:
try:
try:
filename = sensorConfig.get(i,"filename")
except:
print("Error: no filename config option found for " + i)
raise
try:
enabled = sensorConfig.getboolean(i,"enabled")
except:
enabled = True
#if enabled, load the plugin
if enabled:
try:
mod = __import__('sensors.'+filename,fromlist=['a']) #Why does this work?
except:
print("Error: could not import module " + filename)
raise
try:
sensorClass = get_subclasses(mod,sensor.Sensor)
if sensorClass == None:
raise AttributeError
except:
print("Error: could not find a subclass of sensor.Sensor in module " + filename)
raise
try:
reqd = sensorClass.requiredData
except:
reqd = []
try:
opt = sensorClass.optionalData
except:
opt = []
pluginData = {}
class MissingField(Exception): pass
for requiredField in reqd:
if sensorConfig.has_option(i,requiredField):
pluginData[requiredField]=sensorConfig.get(i,requiredField)
else:
print "Error: Missing required field '" + requiredField + "' for sensor plugin " + i
raise MissingField
for optionalField in opt:
if sensorConfig.has_option(i,optionalField):
pluginData[optionalField]=sensorConfig.get(i,optionalField)
instClass = sensorClass(pluginData)
sensorPlugins.append(instClass)
print ("Success: Loaded sensor plugin " + i)
except Exception as e: #add specific exception for missing module
print("Error: Did not import sensor plugin " + i )
raise e
if not os.path.isfile("outputs.cfg"):
print "Unable to access config file: outputs.cfg"
outputConfig = ConfigParser.SafeConfigParser()
outputConfig.read("outputs.cfg")
......@@ -154,21 +154,24 @@ for i in outputNames:
lastUpdated = 0
delayTime = 5
while True:
if time.time()-lastUpdated<delayTime:
lastUpdate = time.time()
if (time.time()-lastUpdated)>delayTime:
lastUpdated = time.time()
data = []
#Collect the data from each sensor
for i in sensorPlugins:
dataDict = {}
dataDict["val"] = i.getVal()
dataDict["value"] = i.getVal()
dataDict["unit"] = i.valUnit
dataDict["name"] = i.valName
dataDict["sensor"] = i.sensorName
data.append(dataDict)
working = True
print "Hello"
for i in outputPlugins:
working = working and i.uploadData(data)
if working:
pass
#Blink Green
else:
pass
#Blink Red
[Xively]
filename=xively
enabled=onp
APIKey=temp
FeedID=12345
[Print]
filename=print
enabled=on
import output
class Print(output.Output):
requiredData = []
optionalData = []
def __init__(self,data):
pass
def uploadData(self,dataPoints):
for i in dataPoints:
print i["name"] + ": " + str(i["value"]) + " " + i["unit"]
......@@ -24,4 +24,4 @@ class Bmp085(sensor.Sensor):
return
def getVal(self):
return 0
return 0.7
[BMP085-temp]
filename=bmp085
enabled=on
measurement=temp
i2cport = 1
mslp=on
altitude=40
import sensor
class Bmp085(sensor.Sensor):
requiredData = ["measurement","i2cport"]
optionalData = ["altitude","mslp","unit"]
def __init__(self,data):
print data
if "temp" in data["measurement"].lower():
self.valName = "Temperature"
self.valUnit = "C"
if "unit" in data:
if data["unit"]=="F":
self.valUnit = "F"
self.sigfigs = 3
elif "pres" in data["measurement"].lower():
self.valName = "Pressure"
self.valUnit = "hPa"
self.sigfigs = 3
self._altitude = 0
if "mslp" in data:
if data["mslp"].lower in ["on","true","1","yes"]:
if "altitude" in data:
self.altitude=data["altitude"]
return
def getVal(self):
return 0
class Sensor():
def __init__(data):
raise NotImplementedError
def getData():
raise NotImplementedError
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment