1 /*
2 LinKNX KNX home automation platform
3 Copyright (C) 2007 Jean-François Meessen <linknx@ouaye.net>
4
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
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "services.h"
21 #include "ioport.h"
22
23 Services* Services::instance_m;
24
Services()25 Services::Services() : xmlServer_m(0), persistentStorage_m(0)
26 {}
27
~Services()28 Services::~Services()
29 {
30 stop();
31 if (xmlServer_m)
32 delete xmlServer_m;
33 if (persistentStorage_m)
34 delete persistentStorage_m;
35 IOPortManager::reset();
36 }
37
instance()38 Services* Services::instance()
39 {
40 if (instance_m == 0)
41 instance_m = new Services();
42 return instance_m;
43 }
44
start()45 void Services::start()
46 {
47 timers_m.startManager();
48 knxConnection_m.startConnection();
49 }
50
stop()51 void Services::stop()
52 {
53 infoStream("Services") << "Stopping services" << endlog;
54 timers_m.stopManager();
55 knxConnection_m.stopConnection();
56 }
57
createDefault()58 void Services::createDefault()
59 {
60 if (xmlServer_m)
61 delete xmlServer_m;
62 xmlServer_m = new XmlInetServer(1028);
63 }
64
importXml(ticpp::Element * pConfig)65 void Services::importXml(ticpp::Element* pConfig)
66 {
67 ticpp::Element* pSmsGateway = pConfig->FirstChildElement("smsgateway", false);
68 if (pSmsGateway)
69 smsGateway_m.importXml(pSmsGateway);
70 ticpp::Element* pEmailGateway = pConfig->FirstChildElement("emailserver", false);
71 if (pEmailGateway)
72 emailGateway_m.importXml(pEmailGateway);
73 ticpp::Element* pXmlServer = pConfig->FirstChildElement("xmlserver", false);
74 if (pXmlServer)
75 {
76 if (xmlServer_m)
77 delete xmlServer_m;
78 xmlServer_m = XmlServer::create(pXmlServer);
79 }
80 ticpp::Element* pKnxConnection = pConfig->FirstChildElement("knxconnection", false);
81 if (pKnxConnection)
82 knxConnection_m.importXml(pKnxConnection);
83 ticpp::Element* pExceptionDays = pConfig->FirstChildElement("exceptiondays", false);
84 if (pExceptionDays)
85 exceptionDays_m.importXml(pExceptionDays);
86 ticpp::Element* pLocationInfo = pConfig->FirstChildElement("location", false);
87 if (pLocationInfo)
88 locationInfo_m.importXml(pLocationInfo);
89 ticpp::Element* pPersistence = pConfig->FirstChildElement("persistence", false);
90 if (pPersistence)
91 {
92 if (persistentStorage_m)
93 delete persistentStorage_m;
94 persistentStorage_m = PersistentStorage::create(pPersistence);
95 }
96 ticpp::Element* pIOPorts = pConfig->FirstChildElement("ioports", false);
97 if (pIOPorts)
98 IOPortManager::instance()->importXml(pIOPorts);
99 }
100
exportXml(ticpp::Element * pConfig)101 void Services::exportXml(ticpp::Element* pConfig)
102 {
103 ticpp::Element pSmsGateway("smsgateway");
104 smsGateway_m.exportXml(&pSmsGateway);
105 pConfig->LinkEndChild(&pSmsGateway);
106
107 ticpp::Element pEmailGateway("emailserver");
108 emailGateway_m.exportXml(&pEmailGateway);
109 pConfig->LinkEndChild(&pEmailGateway);
110
111 if (xmlServer_m)
112 {
113 ticpp::Element pXmlServer("xmlserver");
114 xmlServer_m->exportXml(&pXmlServer);
115 pConfig->LinkEndChild(&pXmlServer);
116 }
117
118 ticpp::Element pKnxConnection("knxconnection");
119 knxConnection_m.exportXml(&pKnxConnection);
120 pConfig->LinkEndChild(&pKnxConnection);
121
122 ticpp::Element pExceptionDays("exceptiondays");
123 exceptionDays_m.exportXml(&pExceptionDays);
124 pConfig->LinkEndChild(&pExceptionDays);
125
126 if (!locationInfo_m.isEmpty())
127 {
128 ticpp::Element pLocationInfo("location");
129 locationInfo_m.exportXml(&pLocationInfo);
130 pConfig->LinkEndChild(&pLocationInfo);
131 }
132
133 if (persistentStorage_m)
134 {
135 ticpp::Element pPersistence("persistence");
136 persistentStorage_m->exportXml(&pPersistence);
137 pConfig->LinkEndChild(&pPersistence);
138 }
139
140 ticpp::Element pIOPorts("ioports");
141 IOPortManager::instance()->exportXml(&pIOPorts);
142 pConfig->LinkEndChild(&pIOPorts);
143 }
144