Class: RSMP::Message
- Inherits:
-
Object
show all
- Includes:
- Inspect
- Defined in:
- lib/rsmp/message.rb
Direct Known Subclasses
AggregatedStatus, AggregatedStatusRequest, Alarm, CommandRequest, CommandResponse, Malformed, MessageAcking, StatusRequest, StatusResponse, StatusSubscribe, StatusUnsubscribe, StatusUpdate, Unknown, Version, Watchdog
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Inspect
#inspect, #inspector
Constructor Details
#initialize(attributes = {}) ⇒ Message
Returns a new instance of Message.
134
135
136
137
138
139
140
141
|
# File 'lib/rsmp/message.rb', line 134
def initialize attributes = {}
@timestamp = Time.now
@attributes = { "mType"=> "rSMsg" }.merge attributes
ensure_message_id
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8
9
10
|
# File 'lib/rsmp/message.rb', line 8
def attributes
@attributes
end
|
#direction ⇒ Object
Returns the value of attribute direction.
10
11
12
|
# File 'lib/rsmp/message.rb', line 10
def direction
@direction
end
|
#json ⇒ Object
Returns the value of attribute json.
10
11
12
|
# File 'lib/rsmp/message.rb', line 10
def json
@json
end
|
#now ⇒ Object
Returns the value of attribute now.
8
9
10
|
# File 'lib/rsmp/message.rb', line 8
def now
@now
end
|
#out ⇒ Object
Returns the value of attribute out.
8
9
10
|
# File 'lib/rsmp/message.rb', line 8
def out
@out
end
|
#timestamp ⇒ Object
this is an internal timestamp recording when we receive/send
9
10
11
|
# File 'lib/rsmp/message.rb', line 9
def timestamp
@timestamp
end
|
Class Method Details
.bin_to_chars(s) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/rsmp/message.rb', line 113
def self.bin_to_chars(s)
out = s.gsub(/[^[:print:]]/i, '.')
max = 120
if out.size <= max
out
else
mid = " ... "
length = (max-mid.size)/2 - 1
"#{out[0..length]} ... #{out[-length-1..-1]}"
end
end
|
.build(attributes, json) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rsmp/message.rb', line 23
def self.build attributes, json
validate_message_type attributes
case attributes["type"]
when "MessageAck"
message = MessageAck.new attributes
when "MessageNotAck"
message = MessageNotAck.new attributes
when "Version"
message = Version.new attributes
when "AggregatedStatus"
message = AggregatedStatus.new attributes
when "AggregatedStatusRequest"
message = AggregatedStatusRequest.new attributes
when "Watchdog"
message = Watchdog.new attributes
when "Alarm"
message = self.build_alarm attributes
when "CommandRequest"
message = CommandRequest.new attributes
when "CommandResponse"
message = CommandResponse.new attributes
when "StatusRequest"
message = StatusRequest.new attributes
when "StatusResponse"
message = StatusResponse.new attributes
when "StatusSubscribe"
message = StatusSubscribe.new attributes
when "StatusUnsubscribe"
message = StatusUnsubscribe.new attributes
when "StatusUpdate"
message = StatusUpdate.new attributes
else
message = Unknown.new attributes
end
message.json = json
message.direction = :in
message
end
|
.build_alarm(attributes) ⇒ Object
.make_m_id ⇒ Object
12
13
14
|
# File 'lib/rsmp/message.rb', line 12
def self.make_m_id
SecureRandom.uuid()
end
|
.parse_attributes(json) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/rsmp/message.rb', line 16
def self.parse_attributes json
raise ArgumentError unless json
JSON.parse json
rescue JSON::ParserError
raise InvalidPacket, bin_to_chars(json)
end
|
.shorten_m_id(m_id, length = 4) ⇒ Object
93
94
95
|
# File 'lib/rsmp/message.rb', line 93
def self.shorten_m_id m_id, length=4
m_id[0..length-1]
end
|
.validate_message_type(attributes) ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/rsmp/message.rb', line 125
def self.validate_message_type attributes
raise MalformedMessage.new("JSON must be a Hash, got #{attributes.class} ") unless attributes.is_a?(Hash)
raise MalformedMessage.new("'mType' is missing") unless attributes["mType"]
raise MalformedMessage.new("'mType' must be a String, got #{attributes["mType"].class}") unless attributes["mType"].is_a? String
raise MalformedMessage.new("'mType' must be 'rSMsg', got '#{attributes["mType"]}'") unless attributes["mType"] == "rSMsg"
raise MalformedMessage.new("'type' is missing") unless attributes["type"]
raise MalformedMessage.new("'type' must be a String, got #{attributes["type"].class}") unless attributes["type"].is_a? String
end
|
Instance Method Details
#attribute(key) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/rsmp/message.rb', line 101
def attribute key
unless @attributes.key? key maybe = @attributes.find { |k,v| k.downcase == key.downcase }
if maybe
raise MissingAttribute.new "attribute '#{maybe.first}' should be named '#{key}'"
else
raise MissingAttribute.new "missing attribute '#{key}'"
end
end
@attributes[key]
end
|
#ensure_message_id ⇒ Object
143
144
145
146
|
# File 'lib/rsmp/message.rb', line 143
def ensure_message_id
@attributes["mId"] ||= Message.make_m_id
end
|
#generate_json ⇒ Object
168
169
170
|
# File 'lib/rsmp/message.rb', line 168
def generate_json
@json = JSON.generate @attributes
end
|
#m_id ⇒ Object
89
90
91
|
# File 'lib/rsmp/message.rb', line 89
def m_id
@attributes["mId"]
end
|
#m_id_short ⇒ Object
97
98
99
|
# File 'lib/rsmp/message.rb', line 97
def m_id_short
Message.shorten_m_id @attributes["mId"]
end
|
#type ⇒ Object
85
86
87
|
# File 'lib/rsmp/message.rb', line 85
def type
@attributes["type"]
end
|
#valid? ⇒ Boolean
164
165
166
|
# File 'lib/rsmp/message.rb', line 164
def valid?
true
end
|
#validate(schemas) ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/rsmp/message.rb', line 148
def validate schemas
errors = RSMP::Schema.validate attributes, schemas
if errors
error_string = errors.map {|item| item.reject {|e| e=='' } }.compact.join(', ').strip
raise SchemaError.new error_string
end
end
|
#validate_id ⇒ Object
160
161
162
|
# File 'lib/rsmp/message.rb', line 160
def validate_id
(@attributes["mId"] =~ /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/i) != nil
end
|
#validate_type ⇒ Object
156
157
158
|
# File 'lib/rsmp/message.rb', line 156
def validate_type
@attributes["mType"] == "rSMsg"
end
|