Example #1
0
class SensorLog(PollingProcessor):
    """ Handles updating sensors of all types to the database """
    def __init__(self, channels, name=''):
        super(SensorLog, self).__init__()
        self._dao = DataAccess().sensors
        self._channels = channels
        self._logCache = {}
        self._name = name

    def start(self):
        """ Begin asynchronously updating changes in sensors """
        if self._name != '':
            print "Started updating database for %s sensor changes" % (
                self._name)
        else:
            print "Started updating database for [unknown] sensor changes"
        self._addPollingProcessor('sensorHistory', self.checkUpdateSensors,
                                  (self._channels, ), 0.01)

    def stop(self):
        """ Stop updating the sensor table """
        if self._name != '':
            print "Stopped updating database for %s sensor changes" % (
                self._name)
        else:
            print "Stopped updating database for [unknown] sensor changes"

        self._removePollingProcessor('sensorHistory')

    def checkUpdateSensors(self, channels):
        """ Check the specified channels and update the database whenever changes are detected to the 'value' or 'status'"""
        for uuid, sensor in channels.items():
            if not self._logCache.has_key(uuid):
                current = self._dao.getSensor(sensor['id'])
                self._logCache[uuid] = {
                    'value': current['value'],
                    'status': current['status']
                }

            status = str(sensor['status']).capitalize()
            if self._logCache[uuid]['status'] != status or self._logCache[
                    uuid]['value'] != sensor['value']:
                val = sensor['value'] if len(
                    sensor['value']) > 1 else sensor['value'][0]
                timestamp = datetime.datetime.now().strftime(
                    '%Y-%m-%d %H:%M:%S')
                success = self._dao.saveSensorLog(sensor['id'], val, status,
                                                  timestamp, sensor['room'],
                                                  sensor['channel'])
                if success:
                    print "Updated sensor log for %(id)s to %(status)s (%(value)s)" % {
                        'id': sensor['channel'],
                        'status': status,
                        'value': val,
                    }
                    self._logCache[uuid]['value'] = sensor['value']
                    self._logCache[uuid]['status'] = status
Example #2
0
class SensorLog(PollingProcessor):
    """ Handles updating sensors of all types to the database """
    def __init__ (self, channels, name=''):
        super(SensorLog, self).__init__()
        self._dao = DataAccess().sensors
        self._channels = channels
        self._logCache = {}
        self._name = name
                
    def start(self):
        """ Begin asynchronously updating changes in sensors """
        if self._name != '':
            print "Started updating database for %s sensor changes" % (self._name)
        else:
            print "Started updating database for [unknown] sensor changes"
        self._addPollingProcessor('sensorHistory', self.checkUpdateSensors, (self._channels,), 0.01)

    def stop(self):
        """ Stop updating the sensor table """
        if self._name != '':
            print "Stopped updating database for %s sensor changes" % (self._name)
        else:
            print "Stopped updating database for [unknown] sensor changes"

        self._removePollingProcessor('sensorHistory')

    def checkUpdateSensors(self, channels):
        """ Check the specified channels and update the database whenever changes are detected to the 'value' or 'status'"""
        for uuid, sensor in channels.items():
            if not self._logCache.has_key(uuid):
                current = self._dao.getSensor(sensor['id'])
                self._logCache[uuid] = { 'value': current['value'], 'status': current['status']}

            status = str(sensor['status']).capitalize()
            if self._logCache[uuid]['status'] != status or self._logCache[uuid]['value'] != sensor['value']:
                val = sensor['value'] if len(sensor['value']) > 1 else sensor['value'][0]
                timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                success = self._dao.saveSensorLog(
                                                  sensor['id'],
                                                  val,
                                                  status,
                                                  timestamp,
                                                  sensor['room'],
                                                  sensor['channel'])
                if success:
                    print "%(time)s: Updated sensor log for %(id)s to %(status)s (%(value)s)" % { 
                                                                           'time':timestamp,
                                                                           'id':sensor['channel'],
                                                                           'status': status,
                                                                           'value': val,
                                                                           }
                    self._logCache[uuid]['value'] = sensor['value']
                    self._logCache[uuid]['status'] = status
Example #3
0
class SensorLog(PollingProcessor):    
    def __init__ (self, channels, name=''):
        super(SensorLog, self).__init__()
        self._dao = DataAccess().sensors
        self._channels = channels
        self._logCache = {}
        self._name = name
                
    def start(self):
        if self._name != '':
            print "Started updating database for %s sensor changes" % (self._name)
        else:
            print "Started updating database for [unknown] sensor changes"
        self._addPollingProcessor('sensorHistory', self.checkUpdateSensors, (self._channels,), 0.01)

    def stop(self):
        if self._name != '':
            print "Stopped updating database for %s sensor changes" % (self._name)
        else:
            print "Stopped updating database for [unknown] sensor changes"

        self._removePollingProcessor('sensorHistory')

    def checkUpdateSensors(self, channels):
        for uuid, sensor in channels.items():
            if not self._logCache.has_key(uuid):
                current = self._dao.getSensor(sensor['id'])
                self._logCache[uuid] = { 'value': current['value'], 'status': current['status']}

            status = str(sensor['status']).capitalize()
            if self._logCache[uuid]['status'] != status:
                timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                success = self._dao.saveSensorLog(
                                                  sensor['id'],
                                                  sensor['value'],
                                                  status,
                                                  timestamp,
                                                  sensor['room'],
                                                  sensor['channel'])
                if success:
                    print "Updated sensor log for %(id)s to %(status)s" % { 
                                                                           'id':sensor['channel'],
                                                                           'status': status
                                                                           }
                    self._logCache[uuid]['value'] = sensor['value']
                    self._logCache[uuid]['status'] = status
Example #4
0
class SensorLog(PollingProcessor):    
    def __init__ (self, channels, name=''):
        super(SensorLog, self).__init__()
        self._dao = DataAccess().sensors
        self._channels = channels
        self._logCache = {}
        self._name = name
                
    def start(self):
        if self._name != '':
            print "Started polling sensor changes for %s" % (self._name)
        else:
            print "Started polling sensor changes"
        self._addPollingProcessor('sensorHistory', self.checkUpdateSensors, (self._channels, ), 0.01)

    def stop(self):
        if self._name != '':
            print "Stopped polling sensor changes for %s" % (self._name)
        else:
            print "Stopped polling sensor changes"

        self._removePollingProcessor('sensorHistory')

    def checkUpdateSensors(self, channels):
        for k in channels.keys():
            if not self._logCache.has_key(k):
                current = self._dao.getSensor(channels[k]['id'])
                self._logCache.setdefault(k, { 'value': current['value'], 'status': current['status']})
            if self._logCache[k]['status'] != channels[k]['status']:
                timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                success = self._dao.saveSensorLog(
                                                  channels[k]['id'], 
                                                  channels[k]['value'], 
                                                  channels[k]['status'],
                                                  timestamp,
                                                  channels[k]['room'],
                                                  channels[k]['channel'])
                if success:
                    print "Updated sensor log for %(id)s to %(status)s" % { 
                                                                           'id':channels[k]['channel'], 
                                                                           'status': channels[k]['status']
                                                                           }
                    self._logCache[k]['value'] = channels[k]['value']
                    self._logCache[k]['status'] = channels[k]['status']