alarmserver.alarmserver

Package alarmserver :: Module alarmserver |
[hide private]
[frames] | no frames]

# Source Code for Module alarmserver.alarmserver

      1  """
      2  Created on 23.09.2013
      3  @author: Stefan Lehmann
      4  """
      5  from datetime import datetime
      6
      7
      8  SIGNAL_ALARM_RAISED = "alarmRaised(int, QString)"
      9  BIT_COUNT = 16
     10
     11
     12 -class AlarmNotDefinedError(Exception):
     13      pass
     14
     15
     16 -class Alarm():
     17      """
     18      An instance of this class represents a defined or active alarm.
     19
     20      @ivar alarm_nr: unique alarm number, used as key value
     21      @type alarm_nr: int
     22
     23      @ivar text: alarm text
     24      @type text: basestring
     25
     26      @ivar time_coming: time the alarm started
     27      @type time_coming: datetime
     28
     29      @ivar time_going: time the alarm finished
     30      @type time_going: datetime
     31
     32      @ivar time_acknowledged: time when alarm got acknowledged by the user
     33      @type time_acknowledged: datetime
     34
     35      @ivar counter: number of times the alarm has been raised since active
     36      @type counter: int
     37
     38      @ivar is_acknowledged: shows if the alarm has been acknowledged
     39      @type is_acknowledged: bool
     40
     41      @ivar is_active: shows if the alarm is currently active
     42      @type is_active: bool
     43      """
     44 -    def __init__(self, alarm_nr, text):
     45          self.alarm_nr = alarm_nr
     46          self.text = text
     47          self.time_coming = None
     48          self.time_going = None
     49          self.time_acknowledged = None
     50          self.counter = 1
     51          self.is_acknowledged = False
     52          self.is_active = False
     53
     54 -    def __str__(self):
     55          return self.text
     56
     57 -    def acknowledge(self):
     58          """
     59          Acknowledge the alarm by setting time_acknowledge and the acknowledged property.
     60
     61          """
     62          self.time_acknowledged = datetime.now()
     63          self.is_acknowledged = True
     64
     65 -    def clear(self):
     66          """
     67          Clear the alarm by setting back all instance variables to the init values.
     68
     69          """
     70          self.time_coming = None
     71          self.time_going = None
     72          self.time_acknowledged = None
     73          self.counter = 1
     74          self.is_acknowledged = False
     75          self.is_active = False
     76
     77
     78 -class AlarmServer():
     79      """
     80      An alarm server with the possibility to define alarms, raise, acknowledge and clear them.
     81
     82      @ivar current_alarms: list of current alarms
     83      @type current_alarms: list
     84
     85      @ivar defined_alarms: dictionary of all defined alarms, key is the alarm number
     86      @type defined_alarms: dict
     87
     88      """
     89 -    def __init__(self):
     90          self.current_alarms = []
     91          self.defined_alarms = dict()
     92
     93 -    def acknowledge(self, alarm_nr):
     94          """
     95          Acknowledge a specific alarm identified via alarm_nr.
     96          @type alarm_nr: int
     97
     98          """
     99          active_alarm = self.defined_alarms.get(alarm_nr)
    100          if active_alarm is None:
    101              return
    102          active_alarm.acknowledge()
    103
    104 -    def acknowledge_all(self):
    105          """
    106          Acknowledge all current alarms.
    107
    108          """
    109          for alarm in self.current_alarms:
    110              alarm.acknowledge()
    111
    112 -    def alarm_coming(self, alarm_nr):
    113          """
    114          Set the alarm with the given number to active.
    115          If the alarm is not active but in the current alarm list
    116          the counter will be raised by one.
    117          If the alarm is not in the current alarm list it will
    118          be inserted.
    119          The C{time_coming} attribute is set to the current time
    120          if the alarm has been inactive.
    121
    122          @type alarm_nr: int
    123
    124          """
    125          alarm = self.defined_alarms.get(alarm_nr)
    126
    127          if alarm is None:
    128              raise AlarmNotDefinedError(alarm_nr)
    129
    130          if not alarm.is_active:
    131              alarm.time_coming = datetime.now()
    132              alarm.is_acknowledged = False
    133              alarm.time_acknowledged = None
    134
    135              if alarm in self.current_alarms:
    136                  alarm.counter += 1
    137              else:
    138                  self.current_alarms.append(alarm)
    139
    140          alarm.is_active = True
    141
    142 -    def alarm_going(self, alarm_nr):
    143          """
    144          Set the alarm with the given number to inactive.
    145          The C{time_going} attribute is set to the current time
    146          if the alarm has been active.
    147
    148          @type alarm_nr: int
    149
    150          """
    151          alarm = self.defined_alarms.get(alarm_nr)
    152          if alarm is None:
    153              raise AlarmNotDefinedError(alarm_nr)
    154
    155          if alarm.is_active:
    156              alarm.is_active = False
    157              alarm.time_going = datetime.now()
    158
    159 -    def clear(self, alarm_nr):
    160          """
    161          Remove the alarm with the given number from the list of
    162          current alarms. All instance variables will be set to
    163          their initial value.
    164
    165          @type alarm_nr: int
    166
    167          """
    168          active_alarm = self.defined_alarms.get(alarm_nr)
    169          if active_alarm is not None:
    170              active_alarm.clear()
    171              self.current_alarms.remove(active_alarm)
    172
    173 -    def clear_all(self):
    174          """
    175          Remove all alarms from the list of current alarms.
    176
    177          """
    178          while len(self.current_alarms) > 0:
    179              active_alarm = self.current_alarms[0]
    180              self.clear(active_alarm.alarm_nr)
    181
    182 -    def define_alarm(self, alarm_nr, alarm_text):
    183          """
    184          Define a new alarm and add it to the list C{defined_alarms}.
    185
    186          @type alarm_nr: int
    187          @param alarm_nr: key value for accessing the alarm
    188
    189          @type alarm_text: basestring
    190          @param alarm_text: text describing the alarm
    191
    192          """
    193          alarm = Alarm(alarm_nr, alarm_text)
    194          self.defined_alarms[alarm_nr] = alarm
    195
    196      @property
    197 -    def unacknowledged_alarms(self):
    198          """
    199          list of all unacknowledged alarms
    200
    201          @rtype: list
    202
    203          """
    204          retVal = []
    205          for active_alarm in self.current_alarms:
    206              if not active_alarm.is_acknowledged:
    207                  retVal.append(active_alarm)
    208          return retVal
    209
    210
    211 -class AlarmWord(object):
    212      """
    213      Hold the value of an alarm word and raise alarms for each alarm bit.
    214
    215      @type alarmserver: AlarmServer
    216      @ivar alarmserver: alarm server with the define alarms
    217
    218      @type offset: int
    219      @ivar offset: offset added to the alarm number
    220
    221      """
    222
    223 -    def __init__(self, alarmserver, offset=0):
    224          """
    225          @type alarmserver: AlarmServer
    226
    227          @type offset: int
    228          @param offset: offset added to the alarm number
    229
    230          """
    231
    232          self.alarmserver = alarmserver
    233          self._value = 0
    234          self.offset = offset
    235
    236 -    def _bit_value(self, n):
    237          """
    238          Return value of bit n of value
    239
    240          @type n: int
    241          @param n: bit number, starting with 0
    242
    243          @rtype: bool
    244          @return: value of bit n
    245
    246          """
    247          return ((self._value >> n) & 1) == 1
    248
    249 -    def check(self):
    250          """
    251          Check alarm word for active alarms identified by their bit number.
    252          Call alarm_coming function of the alarmserver for each active alarm.
    253
    254          """
    255
    256          for bit_nr in range(32):
    257              b = self._bit_value(bit_nr)
    258              if b:
    259                  self.alarmserver.alarm_coming(self.offset + bit_nr)
    260
    261      @property
    262 -    def value(self):
    263          return self._value
    264
    265      @value.setter
    266 -    def value(self, value):
    267          self._value = value
    268          self.check()
    269