-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplug.rb
More file actions
78 lines (66 loc) · 2.13 KB
/
plug.rb
File metadata and controls
78 lines (66 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'socket'
module Plug
extend self
def encrypt(string)
key = 171
result = ''
string.each_byte do |byte|
a = key ^ byte
key = a
result += a.chr
end
return "\x00\x00\x00*" + result
end
def decrypt(string)
#string.dup.force_encoding('BINARY')
# p string.bytes.to_a
# p string.inspect
string = string[4..-1]
key = 171
result = ''
#p "here "
string.each_byte do |byte|
#p "key: #{key} char: #{byte}"
a = key ^ byte
#p "key: #{key} char: #{a}"
key = byte
result += a.chr
#p "----"
end
return result
end
def sendOrderToPlug(order)
ip = '192.168.1.30'
port = 9999
begin
socket = TCPSocket.new(ip, port)
rescue StandardErrpr => e
puts "Error occurred: #{e}"
puts "Failed to establish connection #{socket}."
log_file = File.open('pv_system_log.txt', 'a')
log_file.puts "Failed to establish connection #{socket}."
log_file.puts "Error occurred: #{e}\n"
log_file.close
retry
end
#p Socket.getnameinfo(Socket.sockaddr_in(9999, "192.168.1.23"))
off = "{\"system\":{\"set_relay_state\":{\"state\":0}}}"
on = "{\"system\":{\"set_relay_state\":{\"state\":1}}}"
orderMessage = ""
order == "on" ? orderMessage = on : orderMessage = off
# on = "\x00\x00\x00*\xd0\xf2\x81\xf8\x8b\xff\x9a\xf7\xd5\xef\x94\xb6\xc5\xa0\xd4\x8b\xf9\x9c\xf0\x91\xe8\xb7\xc4\xb0\xd1\xa5\xc0\xe2\xd8\xa3\x81\xf2\x86\xe7\x93\xf6\xd4\xee\xdf\xa2\xdf\xa2"
#off = "\x00\x00\x00*\xd0\xf2\x81\xf8\x8b\xff\x9a\xf7\xd5\xef\x94\xb6\xc5\xa0\xd4\x8b\xf9\x9c\xf0\x91\xe8\xb7\xc4\xb0\xd1\xa5\xc0\xe2\xd8\xa3\x81\xf2\x86\xe7\x93\xf6\xd4\xee\xde\xa3\xde\xa3"
#p encrypt(orderMessage)
log_file = File.open('pv_system_log.txt', 'a')
log_file.puts "Message sent: \"#{order}\" -> #{orderMessage}."
puts "Message sent: \"#{order}\" -> #{orderMessage}."
socket.write(encrypt(orderMessage))
message = socket.recv(2048)
log_file.puts "Message recived: #{decrypt(message)}."
puts "Message recived: #{decrypt(message)}."
log_file.puts "\n"
puts "\n"
log_file.close
socket.close
end
end