Module: TpLinkSmartplug::Message

Included in:
Device
Defined in:
lib/tp_link_smartplug/message.rb

Overview

Helper methods for plug communication messages

Instance Method Summary collapse

Instance Method Details

#decrypt(string) ⇒ Object

Decrypts a message received from the smart plug

Parameters:

  • string (String)

    the message to be decrypted



20
21
22
23
24
25
26
27
28
29
# File 'lib/tp_link_smartplug/message.rb', line 20

def decrypt(string)
  key = 171
  result = ''
  string.each_char do |char|
    a = key ^ char.ord
    key = char.ord
    result.concat(a.chr)
  end
  result
end

#encrypt(string) ⇒ Object

Encrypts a message to send to the smart plug

Parameters:

  • string (String)

    the message to be encrypted



7
8
9
10
11
12
13
14
15
# File 'lib/tp_link_smartplug/message.rb', line 7

def encrypt(string)
  key = 171
  result = [string.length].pack('N')
  string.each_char do |char|
    key = a = key ^ char.ord
    result.concat(a.chr)
  end
  result
end