Class: Pebble::Watch::AppMessageEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/pebble/watch/app_message_event.rb

Defined Under Namespace

Modules: TupleType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/pebble/watch/app_message_event.rb', line 13

def data
  @data
end

#transaction_idObject

Returns the value of attribute transaction_id.



11
12
13
# File 'lib/pebble/watch/app_message_event.rb', line 11

def transaction_id
  @transaction_id
end

#uuidObject

Returns the value of attribute uuid.



12
13
14
# File 'lib/pebble/watch/app_message_event.rb', line 12

def uuid
  @uuid
end

Class Method Details

.parse(raw_message) ⇒ Object



15
16
17
18
19
20
21
22
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
# File 'lib/pebble/watch/app_message_event.rb', line 15

def self.parse(raw_message)
  return nil if raw_message.length < 19

  message_type, transaction_id, uuid, dictionary_length = raw_message[0, 19].unpack("CCA16C")

  data = {}

  offset = 19
  dictionary_length.times do |i|
    key, tuple_type, item_length = raw_message[offset, 7].unpack("CL>S<")
    offset += 7

    format = case tuple_type
    when TupleType::ByteArray;      "a*"
    when TupleType::String;         "A*"
    when TupleType::UnsignedInteger
      case item_length
      when 1; "C"
      when 2; "S<"
      when 4; "L<"
      end
    when TupleType::Integer
      case item_length
      when 1; "c"
      when 2; "s<"
      when 4; "l<"
      end
    end

    item_data = raw_message[offset, item_length].unpack(format).first
    offset += item_length

    data[key] = item_data
  end

  event = new

  event.transaction_id  = transaction_id
  event.uuid            = uuid
  event.data            = data

  event
end