forked from toddq/sinope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinope.rb
159 lines (134 loc) · 4.17 KB
/
sinope.rb
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'json'
require 'logger'
require 'net/http'
require 'net/https'
require 'uri'
class Sinope
HOST = 'neviweb.com'
# gateway modes
HOME = 0
AWAY = 2
# device modes
# auto => 3
# away => 5
# bypass => 131, temporary device setpoint override
def initialize(username, password)
@username = username
@password = password
@http = Net::HTTP.new(HOST, 443)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
end
def status()
login() if !logged_in?
get_gateway() if @gateway.nil?
status = {:gateway => @gateway}
get_devices() if @devices.nil?
status[:devices] = @devices
@devices.each do |device|
device[:status] = device_status(device)
end
return status
end
def device_status(device)
@logger.debug "Requesting status of #{device['name']}"
request = get("/api/device/#{device['id']}/data")
response = request(request)
return format(response)
end
def setpoint(device_id, value, format='f')
@logger.warn "Not yet implemented"
end
def home_away_status()
login if !logged_in?
get_gateway() if @gateway.nil?
request = get("/api/gateway/#{@gateway['id']}/mode")
response = request( request )
return response['mode']
end
def home()
@logger.debug "Setting home/away state to Home"
return set_home_away(HOME)
end
def away()
@logger.debug "Setting home/away state to Away"
return set_home_away(AWAY)
end
def set_home_away(mode)
login if !logged_in?
get_gateway() if @gateway.nil?
request = post("/api/gateway/#{@gateway['id']}/mode")
request.set_form_data({'mode' => mode})
return request(request)
end
def logged_in?()
return !@session_id.nil? && !@session_id.empty?
end
def login()
@logger.debug "Logging in"
request = post('/api/login')
request.content_type = 'application/x-www-form-urlencoded; charset=UTF-8'
request.set_form_data({'email' => @username, 'password' => @password, 'stayConnected' => 0})
begin
response = request(request)
rescue Exception => e
raise "Authentication error: #{e}"
end
@logger.debug response
@session_id = response['session']
@temperature_format = response['user']['format']['temperature']
@logger.debug "Logged in"
end
def get_gateway()
@logger.debug "Getting Gateway"
request = get('/api/gateway')
response = request(request)
@gateway = response[0]
end
def get_devices()
@logger.debug "Getting Devices"
request = get("/api/device?gatewayId=#{@gateway['id']}")
@devices = request(request)
end
def get(path)
request = Net::HTTP::Get.new(path)
request.content_type = 'application/json'
request['Session-Id'] = @session_id
return request
end
def put(path)
request = Net::HTTP::Put.new(path)
request['Session-Id'] = @session_id
return request
end
def post(path)
request = Net::HTTP::Post.new(path)
request['Session-Id'] = @session_id
return request
end
def request(request)
response = @http.request(request)
if response.message == 'OK'
body = JSON.parse(response.body)
raise "Request error: #{body}" if body.is_a?(Hash) && !body['error'].nil?
return body
else
raise "Failed request for #{request.path}: #{response.message}"
end
end
def format(response)
if @temperature_format == 'f'
response['temperature'] = cToF(response['temperature'].to_f).to_s if response['temperature']
response['setpoint'] = cToF(response['setpoint'].to_f).to_s if response['setpoint']
end
return response
end
def cToF(temp)
return (temp * 1.8 + 32).to_f
end
def fToC(temp)
return ((temp - 32) / 1.8).to_f
end
end