Module: MaxCube::Messages

Included in:
Handler
Defined in:
lib/maxcube/messages.rb,
lib/maxcube/messages/tcp.rb,
lib/maxcube/messages/udp.rb,
lib/maxcube/messages/parser.rb,
lib/maxcube/messages/handler.rb,
lib/maxcube/messages/serializer.rb,
lib/maxcube/messages/tcp/parser.rb,
lib/maxcube/messages/tcp/type/a.rb,
lib/maxcube/messages/tcp/type/c.rb,
lib/maxcube/messages/tcp/type/f.rb,
lib/maxcube/messages/tcp/type/h.rb,
lib/maxcube/messages/tcp/type/l.rb,
lib/maxcube/messages/tcp/type/m.rb,
lib/maxcube/messages/tcp/type/n.rb,
lib/maxcube/messages/tcp/type/q.rb,
lib/maxcube/messages/tcp/type/s.rb,
lib/maxcube/messages/tcp/type/t.rb,
lib/maxcube/messages/tcp/type/u.rb,
lib/maxcube/messages/tcp/type/z.rb,
lib/maxcube/messages/udp/parser.rb,
lib/maxcube/messages/udp/type/h.rb,
lib/maxcube/messages/udp/type/i.rb,
lib/maxcube/messages/udp/type/n.rb,
lib/maxcube/messages/tcp/handler.rb,
lib/maxcube/messages/udp/handler.rb,
lib/maxcube/messages/tcp/serializer.rb,
lib/maxcube/messages/udp/serializer.rb

Overview

Encapsulates methods related to Cube messages, i.e. parsing and serializing of TCP/UDP messages. It does not provide any network features (this is responsibility of Network.

Defined Under Namespace

Modules: Handler, Parser, Serializer, TCP, UDP Classes: InvalidMessage, InvalidMessageBody, InvalidMessageFormat, InvalidMessageLength, InvalidMessageType

Constant Summary

DEVICE_MODE =

Device modes that determines geating scheduling.

%i[auto manual vacation boost].freeze
DEVICE_TYPE =

Device types identified in Cube protocol.

%i[cube
radiator_thermostat radiator_thermostat_plus
wall_thermostat
shutter_contact eco_switch].freeze
DAYS_OF_WEEK =

Names of days of week in order Cube protocol uses.

%w[Saturday Sunday Monday
Tuesday Wednesday Thursday Friday].freeze

Instance Method Summary collapse

Instance Method Details

#ary_elem(ary, id, info) ⇒ Object (private)

Helper method that checks presence of index in array (if not, exception is raised).

Parameters:

  • ary (#[])

    input container (usually constant).

  • id

    index of element in container.

  • info (#to_s)

    context information to pass to raised error.

Returns:

  • element of container if found.

Raises:



176
177
178
179
180
181
# File 'lib/maxcube/messages.rb', line 176

def ary_elem(ary, id, info)
  elem = ary[id]
  return elem if elem
  raise InvalidMessageBody
    .new(@msg_type, "unrecognized #{info} id: #{id}")
end

#ary_elem_id(ary, elem, info) ⇒ Object (private)

Reverse method to #ary_elem.

Raises:



184
185
186
187
188
189
# File 'lib/maxcube/messages.rb', line 184

def ary_elem_id(ary, elem, info)
  id = ary.index(elem)
  return id if id
  raise InvalidMessageBody
    .new(@msg_type, "unrecognized #{info}: #{elem}")
end

#conv_args(type, info, *args) { ... } ⇒ Array (private)

Applies a block to given arguments in order to perform conversion to certain type. If conversion fails, InvalidMessageBody is raised. Thus, this method can be used also for type checking purposes only.

Parameters:

  • type (#to_s)

    name of the type to convert to.

  • info (#to_s)

    context information to pass to raised error.

  • args (Array)

    arguments to be converted into the same type.

Yields:

  • a rule to provide certain type check and conversion of arguments.

Returns:

  • (Array)

    converted elements.

Raises:



74
75
76
77
78
79
80
81
# File 'lib/maxcube/messages.rb', line 74

def conv_args(type, info, *args, &block)
  info = info.to_s.tr('_', ' ')
  args.map(&block)
rescue ArgumentError, TypeError
  raise InvalidMessageBody
    .new(@msg_type,
         "invalid #{type} format of arguments #{args} (#{info})")
end

#day_of_week(day_id) ⇒ Object (private)



212
213
214
# File 'lib/maxcube/messages.rb', line 212

def day_of_week(day_id)
  ary_elem(DAYS_OF_WEEK, day_id, 'day of week')
end

#day_of_week_id(day) ⇒ Object (private)



217
218
219
220
221
222
# File 'lib/maxcube/messages.rb', line 217

def day_of_week_id(day)
  if day.respond_to?('to_i') && day.to_i.between?(1, 7)
    return (day.to_i + 1) % 7
  end
  ary_elem_id(DAYS_OF_WEEK, day.capitalize, 'day of week')
end

#device_mode(device_mode_id) ⇒ Object (private)



202
203
204
# File 'lib/maxcube/messages.rb', line 202

def device_mode(device_mode_id)
  ary_elem(DEVICE_MODE, device_mode_id, 'device mode')
end

#device_mode_id(device_mode) ⇒ Object (private)



207
208
209
# File 'lib/maxcube/messages.rb', line 207

def device_mode_id(device_mode)
  ary_elem_id(DEVICE_MODE, device_mode.to_sym, 'device mode')
end

#device_type(device_type_id) ⇒ Object (private)



192
193
194
# File 'lib/maxcube/messages.rb', line 192

def device_type(device_type_id)
  ary_elem(DEVICE_TYPE, device_type_id, 'device type')
end

#device_type_id(device_type) ⇒ Object (private)



197
198
199
# File 'lib/maxcube/messages.rb', line 197

def device_type_id(device_type)
  ary_elem_id(DEVICE_TYPE, device_type.to_sym, 'device type')
end

#to_bool(info, arg) ⇒ Boolean (private)

Uses #to_bools, but operates with single argument.

Parameters:

  • arg

    argument to convert to bool.

Returns:

  • (Boolean)

    converted element to TrueClass or FalseClass.



139
140
141
# File 'lib/maxcube/messages.rb', line 139

def to_bool(info, arg)
  to_bools(info, arg).first
end

#to_bools(info, *args) ⇒ Array<Boolean> (private)

Uses #conv_args to convert objects to bools.

Parameters:

  • args (Array)

    arguments to convert to bools.

Returns:

  • (Array<Boolean>)

    converted elements to TrueClass or FalseClass.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/maxcube/messages.rb', line 121

def to_bools(info, *args)
  conv_args('boolean', info, *args) do |arg|
    if arg == !!arg
      arg
    elsif arg.nil?
      false
    elsif %w[true false].include?(arg)
      arg == 'true'
    else
      !Integer(arg).zero?
    end
  end
end

#to_datetime(info, arg) ⇒ Time (private)

Uses #to_datetime, but operates with single argument.

Parameters:

  • arg

    argument to convert to Time.

Returns:

  • (Time)

    converted element.



165
166
167
# File 'lib/maxcube/messages.rb', line 165

def to_datetime(info, arg)
  to_datetimes(info, arg).first
end

#to_datetimes(info, *args) ⇒ Array<Time> (private)

Uses #conv_args to convert objects to Time.

Parameters:

  • args (Array)

    arguments to convert to Time.

Returns:

  • (Array<Time>)

    converted elements.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/maxcube/messages.rb', line 146

def to_datetimes(info, *args)
  conv_args('datetime', info, *args) do |arg|
    if arg.is_a?(Time)
      arg
    elsif arg.is_a?(String)
      Time.parse(arg)
    elsif arg.respond_to?('to_time')
      arg.to_time
    elsif arg.respond_to?('to_date')
      arg.to_date.to_time
    else
      raise ArgumentError
    end
  end
end

#to_float(info, arg) ⇒ Float (private)

Uses #to_floats, but operates with single argument.

Parameters:

  • arg (#Float)

    argument to convert to float.

Returns:

  • (Float)

    converted element.



113
114
115
# File 'lib/maxcube/messages.rb', line 113

def to_float(info, arg)
  to_floats(info, arg).first
end

#to_floats(info, *args) ⇒ Array<Float> (private)

Uses #conv_args to convert numbers or string of characters (not binary data!) to floats.

Parameters:

  • args (Array<#Float>)

    arguments to convert to floats.

Returns:

  • (Array<Float>)

    converted elements.



106
107
108
# File 'lib/maxcube/messages.rb', line 106

def to_floats(info, *args)
  conv_args('float', info, *args) { |x| Float(x) }
end

#to_int(base, info, arg) ⇒ Integer (private)

Uses #to_ints, but operates with single argument.

Parameters:

  • arg (#Integer)

    argument to convert to integer.

Returns:

  • (Integer)

    converted element.



98
99
100
# File 'lib/maxcube/messages.rb', line 98

def to_int(base, info, arg)
  to_ints(base, info, arg).first
end

#to_ints(base, info, *args) ⇒ Array<Integer> (private)

Uses #conv_args to convert numbers or string of characters (not binary data!) to integers in given base (radix). For binary data use MaxCube::Messages::Parser#read.

Parameters:

  • base (Integer)

    integers base (radix), 0 means auto-recognition.

  • args (Array<#Integer>)

    arguments to convert to integers.

Returns:

  • (Array<Integer>)

    converted elements.



90
91
92
93
# File 'lib/maxcube/messages.rb', line 90

def to_ints(base, info, *args)
  base_str = base.zero? ? '' : "(#{base})"
  conv_args("integer#{base_str}", info, *args) { |x| Integer(x, base) }
end