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
#    Copyright (C) 2008 by Mattias Runge   #
2
#    Copyright (C) 2008 by Mattias Runge   #
3
#    mattias@runge.se   #
3
#    mattias@runge.se   #
4
#                                                                          #
4
#                                                                          #
5
#    This program is free software; you can redistribute it and#or modify  #
5
#    This program is free software; you can redistribute it and#or modify  #
6
#    it under the terms of the GNU General Public License as published by  #
6
#    it under the terms of the GNU General Public License as published by  #
7
#    the Free Software Foundation; either version 2 of the License, or     #
7
#    the Free Software Foundation; either version 2 of the License, or     #
8
#    (at your option) any later version.                                   #
8
#    (at your option) any later version.                                   #
9
#                                                                          #
9
#                                                                          #
10
#    This program is distributed in the hope that it will be useful,       #
10
#    This program is distributed in the hope that it will be useful,       #
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
13
#    GNU General Public License for more details.                          #
13
#    GNU General Public License for more details.                          #
14
#                                                                          #
14
#                                                                          #
15
#    You should have received a copy of the GNU General Public License     #
15
#    You should have received a copy of the GNU General Public License     #
16
#    along with this program; if not, write to the                         #
16
#    along with this program; if not, write to the                         #
17
#    Free Software Foundation, Inc.,                                       #
17
#    Free Software Foundation, Inc.,                                       #
18
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
18
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
19
############################################################################
19
############################################################################
20
 
20
 
21
import os
21
import os
22
import sys
22
import sys
23
import ServiceBase
23
import ServiceBase
24
import Settings
24
import Settings
25
 
25
 
26
gServiceMan = False
26
gServiceMan = False
27
 
27
 
28
def GetServiceManager():
28
def GetServiceManager():
29
    global gServiceMan
29
    global gServiceMan
30
    if gServiceMan == False:
30
    if gServiceMan == False:
31
        gServiceMan = ServiceManager()
31
        gServiceMan = ServiceManager()
32
    return gServiceMan
32
    return gServiceMan
33
 
33
 
34
class ServiceManager:
34
class ServiceManager:
35
    services = {}
35
    services = {}
36
    modules = {}
36
    modules = {}
37
   
37
   
38
    def __init__(self):
38
    def __init__(self):
39
        self.ImportAvailableServices()
39
        self.ImportAvailableServices()
40
   
40
   
41
    def ImportAvailableServices(self):
41
    def ImportAvailableServices(self):
42
        servicesPath = Settings.Settings["PATH"] + "/Services"
42
        servicesPath = Settings.Settings["PATH"] + "/Services"
43
        sys.path.append(servicesPath)
43
        sys.path.append(servicesPath)
44
 
44
 
45
        for serviceName in os.listdir(servicesPath):
45
        for serviceName in os.listdir(servicesPath):
46
            if serviceName[0] != '.' and serviceName.endswith(".py"):
46
            if serviceName[0] != '.' and serviceName.endswith(".py"):
47
                serviceName = serviceName[:serviceName.find(".")]
47
                serviceName = serviceName[:serviceName.find(".")]
48
                self.modules[serviceName] = __import__(serviceName)
48
                self.modules[serviceName] = __import__(serviceName)
49
                print "Loaded service: " + serviceName
49
                print "Loaded service: " + serviceName
50
 
50
 
51
        print ""
51
        print ""
52
   
52
   
53
    def HasService(self, serviceId, serviceType):
53
    def HasService(self, serviceId, serviceType):
54
        return self.services.has_key(str(serviceType) + "_" + str(serviceId))
54
        return self.services.has_key(str(serviceType) + "_" + str(serviceId))
55
   
55
   
56
    def AddService(self, serviceId, serviceType):
56
    def AddService(self, serviceId, serviceType):
57
        if not self.HasService(serviceId, serviceType):
57
        if not self.HasService(serviceId, serviceType):
58
           
58
           
59
            if self.modules[serviceType]:
59
            if self.modules[serviceType]:
60
                self.services[str(serviceType) + "_" + str(serviceId)] = self.modules[serviceType].Service(serviceId)
60
                self.services[str(serviceType) + "_" + str(serviceId)] = self.modules[serviceType].Service(serviceId)
61
            else:
61
            else:
62
                print "Warning: Unknown service: Type: " + serviceType
62
                print "Warning: Unknown service: Type: " + serviceType
63
                return False
63
                return False
64
 
64
 
65
        return self.services[str(serviceType) + "_" + str(serviceId)]
65
        return self.services[str(serviceType) + "_" + str(serviceId)]
66
       
66
       
67
    def GetService(self, serviceId, serviceType):
67
    def GetService(self, serviceId, serviceType):
68
        return self.services[str(serviceType) + "_" + str(serviceId)]
68
        return self.services[str(serviceType) + "_" + str(serviceId)]
69
   
69
   
70
 
70