Esempio n. 1
0
 def request(self):
     """
         request data
     """
     self.prev_data = self.data
     self.data = {}
     self.time = RecordedTime(timestamp=time())
     self.time.save()
     for idx in self.clients:
         self.data[idx] = self.clients[idx].request_data()
         if self.prev_data.has_key(idx):
             if not self.prev_data[idx]:
                 self.prev_data = {}
Esempio n. 2
0
    def run(self):
        """
            request data
        """
        dt = time()
        self.data = {}
        # take time
        self.time = time()
        for idx in self._clients:
            self.data[idx] = self._clients[idx].request_data()

        if not self.data:
            return

        self._dvf = []
        self._dvi = []
        self._dvb = []
        self._dvc = []
        del_idx = []
        upd_idx = []
        timestamp = RecordedTime(timestamp=self.time)
        timestamp.save()
        for idx in self._clients:
            for var_idx in self._clients[idx].variables:

                store_value = False
                value = 0
                if self.data[idx]:
                    if self.data[idx].has_key(var_idx):
                        if (self.data[idx][var_idx] != None):
                            value = self.data[idx][var_idx]
                            store_value = True
                            if self._prev_data.has_key(var_idx):
                                if value == self._prev_data[var_idx]:
                                    store_value = False

                            self._prev_data[var_idx] = value
                if store_value:
                    self._dvc.append(
                        RecordedDataCache(variable_id=var_idx,
                                          value=value,
                                          time=timestamp,
                                          last_change=timestamp))
                    del_idx.append(var_idx)
                else:
                    upd_idx.append(var_idx)

                if store_value and self._clients[idx].variables[var_idx][
                        'record']:
                    variable_class = self._clients[idx].variables[var_idx][
                        'value_class']
                    if variable_class.upper() in [
                            'FLOAT', 'FLOAT64', 'DOUBLE'
                    ]:
                        self._dvf.append(
                            RecordedDataFloat(time=timestamp,
                                              variable_id=var_idx,
                                              value=float(value)))
                    elif variable_class.upper() in [
                            'FLOAT32', 'SINGLE', 'REAL'
                    ]:
                        self._dvf.append(
                            RecordedDataFloat(time=timestamp,
                                              variable_id=var_idx,
                                              value=float(value)))
                    elif variable_class.upper() in ['INT32']:
                        self._dvi.append(
                            RecordedDataInt(time=timestamp,
                                            variable_id=var_idx,
                                            value=int(value)))
                    elif variable_class.upper() in ['WORD', 'UINT', 'UINT16']:
                        self._dvi.append(
                            RecordedDataInt(time=timestamp,
                                            variable_id=var_idx,
                                            value=int(value)))
                    elif variable_class.upper() in ['INT16', 'INT']:
                        self._dvi.append(
                            RecordedDataInt(time=timestamp,
                                            variable_id=var_idx,
                                            value=int(value)))
                    elif variable_class.upper() in ['BOOL']:
                        self._dvb.append(
                            RecordedDataBoolean(time=timestamp,
                                                variable_id=var_idx,
                                                value=bool(value)))

        RecordedDataCache.objects.filter(variable_id__in=del_idx).delete()
        RecordedDataCache.objects.filter(variable_id__in=upd_idx).update(
            time=timestamp)
        RecordedDataCache.objects.bulk_create(self._dvc)

        RecordedDataFloat.objects.bulk_create(self._dvf)
        RecordedDataInt.objects.bulk_create(self._dvi)
        RecordedDataBoolean.objects.bulk_create(self._dvb)

        return self._dt - (time() - dt)
Esempio n. 3
0
class client():
    def __init__(self):
        self.clients        = {}
        self.client_config    = {}
        self.data             = {}
        self.prev_data        = {}
        self._prepare_clients()
        self.backup_data      = {}
    def _prepare_clients(self):
        """
        prepare clients for query
        """
        self.client_config    = ClientConfig.config.get_active_client_config()
        for idx in self.client_config:
            self.clients[idx] = mb.client(self.client_config[idx])


    def request(self):
        """
            request data
        """
        self.prev_data = self.data
        self.data = {}
        self.time = RecordedTime(timestamp=time())
        self.time.save()
        for idx in self.clients:
            self.data[idx] = self.clients[idx].request_data()
            if self.prev_data.has_key(idx):
                if not self.prev_data[idx]:
                    self.prev_data = {}



    def store(self):
        """
        save changed values to the database
        """
        count = 0
        dvf = []
        dvi = []
        dvb = []
        self.backup_data      = {}
        self.backup_data['time'] = (float64(self.time.timestamp)/86400)+719529
        for idx in self.data:
            if not self.data[idx]:
                continue
            for var_idx in self.data[idx]:
                variable_class = self.client_config[idx]['variable_input_config'][var_idx]['class']
                if self.prev_data.has_key(idx):
                    skip = (self.data[idx][var_idx] == self.prev_data[idx][var_idx])
                else:
                    skip = False
                # check for NaNs
                if (self.data[idx][var_idx] == "NaN"):
                    skip = True
                    value = 0
                else:
                    value = self.data[idx][var_idx]

                name = self.client_config[idx]['variable_input_config'][var_idx]['variable_name']
                if variable_class.upper() in ['FLOAT32','SINGLE','FLOAT','FLOAT64','REAL'] :
                    self.backup_data[name] = float64(value)
                elif  variable_class.upper() in ['INT32','INT16','INT','WORD','UINT','UINT16']:
                    self.backup_data[name] = int32(value)
                elif variable_class.upper() in ['BOOL']:
                    self.backup_data[name] = uint8(value)
                if skip:
                    count += 1
                else:
                    if variable_class.upper() in ['FLOAT32','SINGLE','FLOAT','FLOAT64','REAL'] :
                        dvf.append(RecordedDataFloat(time=self.time,variable_id=var_idx,value=self.data[idx][var_idx]))
                    elif variable_class.upper() in ['INT32','UINT32','INT16','INT','WORD','UINT','UINT16']:
                        dvi.append(RecordedDataInt(time=self.time,variable_id=var_idx,value=self.data[idx][var_idx]))
                    elif variable_class.upper() in ['BOOL']:
                        dvb.append(RecordedDataBoolean(time=self.time,variable_id=var_idx,value=self.data[idx][var_idx]))
        RecordedDataFloat.objects.bulk_create(dvf)
        RecordedDataInt.objects.bulk_create(dvi)
        RecordedDataBoolean.objects.bulk_create(dvb)
        #print(count)
        return True