Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. import os
  3. import sys
  4. import ControllerBase
  5. import Settings
  6.  
  7. gControllerMan = False
  8.  
  9. def GetControllerManager():
  10.     global gControllerMan
  11.     if gControllerMan == False:
  12.         gControllerMan = ControllerManager()
  13.     return gControllerMan
  14.  
  15. class ControllerManager ():
  16.     modules = {}
  17.     controllers = {}
  18.    
  19.     def __init__(self):
  20.         self.ImportAvailableController()
  21.    
  22.     def ImportAvailableController(self):
  23.         controllersPath = Settings.Settings["PATH"] + "/Controllers"
  24.         sys.path.append(controllersPath)
  25.  
  26.         for controllerName in os.listdir(controllersPath):
  27.             if controllerName[0] != '.' and controllerName.endswith(".py"):
  28.                 controllerName = controllerName[:controllerName.find(".")]
  29.                 self.modules[controllerName] = __import__(controllerName)
  30.                 print "Loaded controller: " + controllerName
  31.  
  32.         print ""
  33.  
  34.     def StartConfiguredControllers(self):
  35.         configPath = Settings.Settings["PATH"] + "/Config"
  36.         sys.path.append(configPath)
  37.  
  38.         for configFile in os.listdir(configPath):
  39.             if configFile[0] != '.' and configFile.endswith(".controller"):
  40.                
  41.                 name = configFile[:configFile.find(".")]
  42.  
  43.                 settingsFile = open(configPath + "/" + configFile, 'r')
  44.                 lines = settingsFile.readlines()
  45.                 settingsFile.close()
  46.    
  47.                 settings = {}
  48.  
  49.                 for line in lines:
  50.                     line = line.strip("\n").strip(" ")
  51.                     if len(line) > 0:
  52.                         if line[0] != '#':
  53.                             settingName = line[:line.find("=")]
  54.                             settingValue = line[line.find("=")+1:]
  55.  
  56.                             settings[settingName] = settingValue
  57.    
  58.                 if not settings.has_key("CONTROLLER"):
  59.                     print configFile + " has no CONTROLLER defined, skipping"
  60.                 elif not self.modules.has_key(settings["CONTROLLER"]):
  61.                     print settings["CONTROLLER"] + " is not a recognized controller name, skipping"
  62.                 else:
  63.                     self.controllers[name] = self.modules[settings["CONTROLLER"]].Controller(settings)
  64.                     print "Started controller: " + settings["CONTROLLER"] + " with Config/" + configFile
  65.  
  66.         print ""