Class: RSMP::AlarmState

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/alarm_state.rb

Overview

class that tracks the state of an alarm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component:, code:) ⇒ AlarmState

Returns a new instance of AlarmState.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rsmp/alarm_state.rb', line 6

def initialize component:, code:
  @component = component
  @component_id = component.c_id
  @code = code
  @suspended = false
  @acknowledged = false
  @suspended = false
  @active = false
  @timestamp = nil
  @category = 'D'
  @priority = 2
  @rvs = []
end

Instance Attribute Details

#acknowledgedObject (readonly)

Returns the value of attribute acknowledged.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def acknowledged
  @acknowledged
end

#activeObject (readonly)

Returns the value of attribute active.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def active
  @active
end

#categoryObject (readonly)

Returns the value of attribute category.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def category
  @category
end

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def code
  @code
end

#component_idObject (readonly)

Returns the value of attribute component_id.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def component_id
  @component_id
end

#priorityObject (readonly)

Returns the value of attribute priority.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def priority
  @priority
end

#rvsObject (readonly)

Returns the value of attribute rvs.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def rvs
  @rvs
end

#suspendedObject (readonly)

Returns the value of attribute suspended.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def suspended
  @suspended
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



4
5
6
# File 'lib/rsmp/alarm_state.rb', line 4

def timestamp
  @timestamp
end

Instance Method Details

#activateObject



45
46
47
48
49
# File 'lib/rsmp/alarm_state.rb', line 45

def activate
  change, @active = !@active, true
  update_timestamp if change
  change
end

#deactivateObject



51
52
53
54
55
# File 'lib/rsmp/alarm_state.rb', line 51

def deactivate
  change, @active = @active, false
  update_timestamp if change
  change
end

#differ_from_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
# File 'lib/rsmp/alarm_state.rb', line 76

def differ_from_message? message
  return true if @timestamp != message.attribute('aTs')
  return true if message.attribute('ack') && @acknowledged != (message.attribute('ack') == 'True')
  return true if message.attribute('sS') && @suspended != (message.attribute('sS') == 'True')
  return true if message.attribute('aS') && @active != (message.attribute('aS') == 'True')
  return true if message.attribute('cat') && @category != message.attribute('cat')
  return true if message.attribute('pri') && @priority != message.attribute('pri').to_i
  #return true @rvs = message.attribute('rvs')
  false
end

#resumeObject



39
40
41
42
43
# File 'lib/rsmp/alarm_state.rb', line 39

def resume
  change, @suspended = @suspended, false
  update_timestamp if change
  change
end

#suspendObject



33
34
35
36
37
# File 'lib/rsmp/alarm_state.rb', line 33

def suspend
  change, @suspended = !@suspended, true
  update_timestamp if change
  change
end

#to_hashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rsmp/alarm_state.rb', line 20

def to_hash
  {
    'cId' => component_id,
    'aCId' => code,
    'aTs' => Clock.to_s(timestamp),
    'ack' => (acknowledged ? 'Acknowledged' : 'notAcknowledged'),
    'sS' => (suspended ? 'suspended' : 'notSuspended'),
    'aS' => (active ? 'Active' : 'inActive'),
    'cat' => category,
    'pri' => priority.to_s,
    'rvs' => rvs
  }
end

#to_message(specialization:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rsmp/alarm_state.rb', line 61

def to_message specialization:
  Alarm.new(
    'cId' => @component_id,
    'aSp' => specialization,
    'aCId' => @code,
    'aTs' => Clock.to_s(@timestamp),
    'ack' => (@acknowledged ? 'Acknowledged' : 'notAcknowledged'),
    'sS' => (@suspended ? 'suspended' : 'notSuspended'),
    'aS' => (@active ? 'Active' : 'inActive'),
    'cat' => @category,
    'pri' => @priority.to_s,
    'rvs' => @rvs
  )
end

#update_from_message(message) ⇒ Object

update from rsmp message component id, alarm code and specialization are not updated



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rsmp/alarm_state.rb', line 89

def update_from_message message
  unless differ_from_message? message
    raise RepeatedAlarmError.new("no changes from previous alarm #{message.m_id_short}")
  end
  if Time.parse(message.attribute('aTs')) < Time.parse(message.attribute('aTs'))
    raise TimestampError.new("timestamp is earlier than previous alarm #{message.m_id_short}")
  end
ensure
  @timestamp = message.attribute('aTs')
  @acknowledged = message.attribute('ack') == 'True'
  @suspended = message.attribute('sS') == 'True'
  @active = message.attribute('aS') == 'True'
  @category = message.attribute('cat')
  @priority = message.attribute('pri').to_i
  @rvs = message.attribute('rvs')
end

#update_timestampObject



57
58
59
# File 'lib/rsmp/alarm_state.rb', line 57

def update_timestamp
  @timestamp = @component.node.clock.now
end