controls/DebugVariablePanel/DebugVariableItem.py
changeset 1962 f41dc3829af6
parent 1881 091005ec69c4
child 1966 08f70b33bc9f
equal deleted inserted replaced
1884:48bd91d7a0ae 1962:f41dc3829af6
    24 
    24 
    25 
    25 
    26 from __future__ import absolute_import
    26 from __future__ import absolute_import
    27 import binascii
    27 import binascii
    28 import numpy
    28 import numpy
       
    29 from datetime import timedelta
    29 from graphics.DebugDataConsumer import DebugDataConsumer, TYPE_TRANSLATOR
    30 from graphics.DebugDataConsumer import DebugDataConsumer, TYPE_TRANSLATOR
    30 
    31 
    31 # -------------------------------------------------------------------------------
    32 # -------------------------------------------------------------------------------
    32 #                 Constant for calculate CRC for string variables
    33 #                 Constant for calculate CRC for string variables
    33 # -------------------------------------------------------------------------------
    34 # -------------------------------------------------------------------------------
   217             self.MinValue = None
   218             self.MinValue = None
   218             self.MaxValue = None
   219             self.MaxValue = None
   219 
   220 
   220         else:
   221         else:
   221             self.Data = None
   222             self.Data = None
   222 
   223             self.MinValue = None
       
   224             self.MaxValue = None
   223         # Init variable value
   225         # Init variable value
   224         self.Value = ""
   226         self.Value = ""
   225 
   227 
   226     def IsNumVariable(self):
   228     def IsNumVariable(self):
   227         """
   229         """
   228         Return if variable data type is numeric. String variables are
   230         Return if variable data type is numeric. String variables are
   229         considered as numeric (string CRC)
   231         considered as numeric (string CRC). Time variables are considered
       
   232         as number of seconds
   230         @return: True if data type is numeric
   233         @return: True if data type is numeric
   231         """
   234         """
   232         return (self.Parent.IsNumType(self.VariableType) or
   235         return (self.Parent.IsNumType(self.VariableType) or
   233                 self.VariableType in ["STRING", "WSTRING"])
   236                 self.VariableType in ["STRING", "WSTRING", "TIME", "TOD", "DT", "DATE"])
   234 
   237 
   235     def NewValues(self, ticks, values):
   238     def NewValues(self, ticks, values):
   236         """
   239         """
   237         Function called by debug thread when a new debug value is available
   240         Function called by debug thread when a new debug value is available
   238         @param tick: PLC tick when value was captured
   241         @param tick: PLC tick when value was captured
   251             data_values = []
   254             data_values = []
   252             for tick, (value, forced) in zip(ticks, values):
   255             for tick, (value, forced) in zip(ticks, values):
   253                 # Translate forced flag to float for storing in Data table
   256                 # Translate forced flag to float for storing in Data table
   254                 forced_value = float(forced)
   257                 forced_value = float(forced)
   255 
   258 
   256                 # String data value is CRC
   259                 if self.VariableType in ["STRING", "WSTRING"]:
   257                 num_value = (binascii.crc32(value) & STRING_CRC_MASK
   260                     # String data value is CRC
   258                              if self.VariableType in ["STRING", "WSTRING"]
   261                     num_value = (binascii.crc32(value) & STRING_CRC_MASK)
   259                              else float(value))
   262                 elif self.VariableType in ["TIME", "TOD", "DT", "DATE"]:
       
   263                     # Numeric value of time type variables
       
   264                     # is represented in seconds
       
   265                     num_value = float(value.total_seconds())
       
   266                 else:
       
   267                     num_value = float(value)
   260 
   268 
   261                 # Update variable range values
   269                 # Update variable range values
   262                 self.MinValue = (min(self.MinValue, num_value)
   270                 self.MinValue = (min(self.MinValue, num_value)
   263                                  if self.MinValue is not None
   271                                  if self.MinValue is not None
   264                                  else num_value)
   272                                  else num_value)
   339             value, forced = \
   347             value, forced = \
   340                 self.RawData[int(self.Data[idx, 2])] \
   348                 self.RawData[int(self.Data[idx, 2])] \
   341                 if self.VariableType in ["STRING", "WSTRING"] \
   349                 if self.VariableType in ["STRING", "WSTRING"] \
   342                 else self.Data[idx, 1:3]
   350                 else self.Data[idx, 1:3]
   343 
   351 
       
   352             if self.VariableType in ["TIME", "TOD", "DT", "DATE"]:
       
   353                 value = timedelta(seconds=value)
       
   354 
   344             # Get raw value if asked
   355             # Get raw value if asked
   345             if not raw:
   356             if not raw:
   346                 value = TYPE_TRANSLATOR.get(
   357                 value = TYPE_TRANSLATOR.get(
   347                     self.VariableType, str)(value)
   358                     self.VariableType, str)(value)
   348 
   359