Subversion Repositories HomeAutomation

Rev

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

Rev 856 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 ServiceBase
21
import ServiceBase
22
import BackendCan
22
import BackendCan
23
import CanModuleTypes
23
import CanModuleTypes
24
import Util
24
import Util
25
 
25
 
26
class Service (ServiceBase.ServiceBase):
26
class Service (ServiceBase.ServiceBase):
27
    Values = {}
27
    Values = {}
28
   
28
   
29
    def __init__(self, id):
29
    def __init__(self, id):
30
        ServiceBase.ServiceBase.__init__(self, id, "Sensor")
30
        ServiceBase.ServiceBase.__init__(self, id, "Sensor")
31
       
31
       
32
        self.events = {"onValueUpdate":[]}
32
        self.events = {"onValueUpdate":[]}
33
 
33
 
34
        self.canBackend = BackendCan.GetBackendCan()
34
        self.canBackend = BackendCan.GetBackendCan()
35
        self.canBackend.AddCallback(self.HandleMsg)
35
        self.canBackend.AddCallback(self.HandleMsg)
36
   
36
   
37
    def HandleMsg(self, canMsg):
37
    def HandleMsg(self, canMsg):
38
        if not self.Online:
38
        if not self.Online:
39
            return False
39
            return False
40
 
40
 
41
        try:
41
        try:
42
            if self.ModuleType == canMsg.ModuleType and self.ModuleId == canMsg.ModuleId:
42
            if self.ModuleType == canMsg.ModuleType and self.ModuleId == canMsg.ModuleId:
43
                if canMsg.Command == 4: # Temp in Celsius
43
                if canMsg.Command == 4: # Temp in Celsius
44
                    if len(canMsg.Data) >= 3:
44
                    if len(canMsg.Data) >= 3:
45
                        sensorId = canMsg.Data[0]
45
                        sensorId = canMsg.Data[0]
46
 
46
 
47
                        self.Values[sensorId] = float(str(canMsg.Data[1]) + "." + str(canMsg.Data[2]/16)) # FIXME: Is this correct?
47
                        self.Values[sensorId] = float(str(canMsg.Data[1]) + "." + str(canMsg.Data[2]/16)) # FIXME: Is this correct?
48
 
48
 
49
                        self.Log(str(self.Values[sensorId]) + " Celsius reported by sensor " + str(sensorId))
49
                        self.Log(str(self.Values[sensorId]) + " Celsius reported by sensor " + str(sensorId))
50
 
50
 
51
                        self.CallEvent("onValueUpdate", sensorId)
51
                        self.CallEvent("onValueUpdate", sensorId)
52
                    else:
52
                    else:
53
                        self.Log("Malformed data received")
53
                        self.Log("Malformed data received")
54
                elif canMsg.Command == 2: # Bus voltage
54
                elif canMsg.Command == 2: # Bus voltage
55
                    if len(canMsg.Data) >= 3:
55
                    if len(canMsg.Data) >= 3:
56
                        sensorId = canMsg.Data[0]
56
                        sensorId = canMsg.Data[0]
57
 
57
 
58
                        self.Values[sensorId] = float(str(canMsg.Data[1]) + str(canMsg.Data[2]))/16 # FIXME: Is this correct?
58
                        self.Values[sensorId] = float(str(canMsg.Data[1]) + str(canMsg.Data[2]))/16 # FIXME: Is this correct?
59
 
59
 
60
                        self.Log(str(self.Values[sensorId]) + " Volts reported by sensor " + str(sensorId))
60
                        self.Log(str(self.Values[sensorId]) + " Volts reported by sensor " + str(sensorId))
61
 
61
 
62
                        self.CallEvent("onValueUpdate", sensorId)
62
                        self.CallEvent("onValueUpdate", sensorId)
63
                    else:
63
                    else:
64
                        self.Log("Malformed data received")
64
                        self.Log("Malformed data received")
65
                else:
65
                else:
66
                    self.Log("Unknown command " + str(canMsg.Command))
66
                    self.Log("Unknown command " + str(canMsg.Command))
67
        except KeyError:
67
        except KeyError:
68
            pass
68
            pass
69
 
69