Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

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