Class: GizwitsSac::SnotiSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/gizwits_sac/snoti_socket.rb

Overview

options = { host: “xxx”, port: “xxx”, connect_timeout: xx, read_timeout: xx, write_timeout: xx, prefetch_count: xx, auth_data: [ { product_key: “xxxx”, auth_id: “xxxx”, auth_secret: “xxxx”, subkey: “xxxx”, events: [‘xxx’, ‘xxx’] } ] }

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SnotiSocket

Returns a new instance of SnotiSocket.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gizwits_sac/snoti_socket.rb', line 25

def initialize(options)
	@host = options[:host]
	@port = options[:port]
	@timeout = options[:connect_timeout] || 3
	@read_timeout = options[:read_timeout] || @timeout
	@write_timeout = options[:write_timeout] || @timeout
	@prefetch_count = options[:prefetch_count] || 50
	@auth_data = options[:auth_data]
	@socket = nil
	@ssl_socket = nil
	@lf = "\n"
	@buffer = "".dup
end

Instance Method Details

#ack(delivery_id) ⇒ Object



80
81
82
# File 'lib/gizwits_sac/snoti_socket.rb', line 80

def ack(delivery_id)
	write("{\"cmd\": \"event_ack\",\"delivery_id\": #{delivery_id}}")
end

#closeObject



84
85
86
87
88
89
# File 'lib/gizwits_sac/snoti_socket.rb', line 84

def close
	@ssl_socket.close if (!@ssl_socket.nil? && !@ssl_socket.closed?)
	@ssl_socket = nil
	@socket.close if (!@socket.nil? && !@socket.closed?)
	@socket = nil
end

#closed?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gizwits_sac/snoti_socket.rb', line 56

def closed?
	@socket.nil? && @ssl_socket.nil?
end

#connectObject



39
40
41
42
# File 'lib/gizwits_sac/snoti_socket.rb', line 39

def connect
	socket_connect
	ssl_connect
end

#login_ok?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File 'lib/gizwits_sac/snoti_socket.rb', line 60

def 
	req_msg = "{\"cmd\":\"login_req\",\"prefetch_count\": #{@prefetch_count},\"data\": #{@auth_data.to_json}}"
	write(req_msg)
	resp =JSON.parse(read)
	succeed = resp["data"]["result"]
	# close connection if login failed
	close if !succeed
	return succeed
end

#pingObject



76
77
78
# File 'lib/gizwits_sac/snoti_socket.rb', line 76

def ping
	write("{\"cmd\": \"ping\"}")
end

#read(n_bytes = 1) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/gizwits_sac/snoti_socket.rb', line 44

def read(n_bytes = 1)
	index = nil
	while (index = @buffer.index(@lf)) == nil
		@buffer << read_data(n_bytes)
	end
	@buffer.slice!(0, index + @lf.bytesize)
end

#remote_control(data_arr, msg_id = nil) ⇒ Object



70
71
72
73
74
# File 'lib/gizwits_sac/snoti_socket.rb', line 70

def remote_control(data_arr, msg_id = nil)
	req = { cmd: "remote_control_req", data: data_arr }
	req[:msg_id] = msg_id if !msg_id.nil?
	write(req.to_json)
end

#write(data) ⇒ Object



52
53
54
# File 'lib/gizwits_sac/snoti_socket.rb', line 52

def write(data)
	write_data(data)
end