Subversion Repositories HomeAutomation

Rev

Rev 857 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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