Class: RSMP::AlarmState

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

Overview

The state of an alarm on a component. The alarm state is for a particular alarm code, a component typically have an alarm state for each alarm code that is defined for the component type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component:, code:, suspended: false, acknowledged: false, active: false, timestamp: nil, category: 'D', priority: 2, rvs: []) ⇒ AlarmState

Returns a new instance of AlarmState.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rsmp/alarm_state.rb', line 25

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

Instance Attribute Details

#acknowledgedObject (readonly)

Returns the value of attribute acknowledged.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def acknowledged
  @acknowledged
end

#activeObject (readonly)

Returns the value of attribute active.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def active
  @active
end

#categoryObject (readonly)

Returns the value of attribute category.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def category
  @category
end

#codeObject (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def code
  @code
end

#component_idObject (readonly)

Returns the value of attribute component_id.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def component_id
  @component_id
end

#priorityObject (readonly)

Returns the value of attribute priority.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def priority
  @priority
end

#rvsObject (readonly)

Returns the value of attribute rvs.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def rvs
  @rvs
end

#suspendedObject (readonly)

Returns the value of attribute suspended.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def suspended
  @suspended
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



9
10
11
# File 'lib/rsmp/alarm_state.rb', line 9

def timestamp
  @timestamp
end

Class Method Details

.create_from_message(component, message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rsmp/alarm_state.rb', line 11

def self.create_from_message component, message
  self.new(
    component: component,
    code: message.attribute("aCId"),
    timestamp: RSMP::Clock.parse(message.attribute('aTs')),
    acknowledged: message.attribute('ack') == 'Acknowledged',
    suspended: message.attribute('aS') == 'suspended',
    active: message.attribute('sS') == 'Active',
    category: message.attribute('cat'),
    priority: message.attribute('pri').to_i,
    rvs: message.attribute('rvs')
  )
end

Instance Method Details

#acknowledgeObject



54
55
56
57
58
# File 'lib/rsmp/alarm_state.rb', line 54

def acknowledge
  change, @acknowledged = !@acknowledged, true
  update_timestamp if change
  change
end

#activateObject

according to the rsmp core spec, the only time an alarm changes to unanknowledged, is when it’s activated. See: rsmp-nordic.org/rsmp_specifications/core/3.2/applicability/basic_structure.html#alarm-status



75
76
77
78
79
# File 'lib/rsmp/alarm_state.rb', line 75

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

#deactivateObject



81
82
83
84
85
# File 'lib/rsmp/alarm_state.rb', line 81

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

#differ_from_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
# File 'lib/rsmp/alarm_state.rb', line 91

def differ_from_message? message
  return true if RSMP::Clock.to_s(@timestamp) != message.attribute('aTs')
  return true if message.attribute('ack') && @acknowledged != (message.attribute('ack') == 'Acknowledged')
  return true if message.attribute('sS') && @suspended != (message.attribute('sS') == 'suspended')
  return true if message.attribute('aS') && @active != (message.attribute('aS') == 'Active')
  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

#older_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/rsmp/alarm_state.rb', line 102

def older_message? message
  return false if @timestamp == nil
  RSMP::Clock.parse(message.attribute('aTs')) < @timestamp
end

#resumeObject



66
67
68
69
70
# File 'lib/rsmp/alarm_state.rb', line 66

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

#suspendObject



60
61
62
63
64
# File 'lib/rsmp/alarm_state.rb', line 60

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

#to_hashObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rsmp/alarm_state.rb', line 40

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

#update_from_message(message) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rsmp/alarm_state.rb', line 109

def update_from_message message
  unless differ_from_message? message
    raise RepeatedAlarmError.new("no changes from previous alarm #{message.m_id_short}")
  end
  if older_message? message
    raise TimestampError.new("timestamp is earlier than previous alarm #{message.m_id_short}")
  end
ensure
  @timestamp = RSMP::Clock.parse 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



87
88
89
# File 'lib/rsmp/alarm_state.rb', line 87

def update_timestamp
  @timestamp = @component.now
end