Class: Neohub::Neohub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Neohub

Returns a new instance of Neohub.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/neohub/neohub.rb', line 12

def initialize(params={})
	@url=params.fetch(:url,'https://neohub.co.uk')
	@uri=URI(@url)
	@http = Net::HTTP.new(@uri.host, @uri.port)
	@ssl=params.fetch(:ssl, true)
	if @ssl then
		@http.use_ssl = true
	end
	@debug=params.fetch(:debug, false)
	setupdebug()
	@devicetypeid=params.fetch(:devicetypeid,2)
	@devkey=params.fetch(:devkey, nil) # holderid
	@devid=params.fetch(:devid, nil)
	@vendorid=params.fetch(:vendorid, 1)
	@max_tries=params.fetch(:max_tries, 10)
	@user=params.fetch(:user, nil)
	@pass=params.fetch(:pass, nil)
	@autologin=params.fetch(:autologin, true)
	@token=nil
	if not @pass.nil? and @autologin then
		(@user,@pass) 
	end
end

Instance Attribute Details

#autologinObject

Returns the value of attribute autologin.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def autologin
  @autologin
end

#devicesObject

Returns the value of attribute devices.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def devices
  @devices
end

#devicetypeidObject

Returns the value of attribute devicetypeid.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def devicetypeid
  @devicetypeid
end

#devidObject

Returns the value of attribute devid.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def devid
  @devid
end

#devkeyObject

Returns the value of attribute devkey.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def devkey
  @devkey
end

#max_triesObject

Returns the value of attribute max_tries.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def max_tries
  @max_tries
end

#passObject

Returns the value of attribute pass.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def pass
  @pass
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def token
  @token
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def url
  @url
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def user
  @user
end

#vendoridObject

Returns the value of attribute vendorid.



10
11
12
# File 'lib/neohub/neohub.rb', line 10

def vendorid
  @vendorid
end

Instance Method Details

#away_off(dev_id) ⇒ Object



150
151
152
153
# File 'lib/neohub/neohub.rb', line 150

def away_off(dev_id)
	cmdstr="{'AWAY_OFF': [#{dev_id}]}"
	return sendaddcommand(dev_id,cmdstr)
end

#away_on(dev_id) ⇒ Object



145
146
147
148
# File 'lib/neohub/neohub.rb', line 145

def away_on(dev_id)
	cmdstr="{'AWAY_ON': [#{dev_id}]}"
	return sendaddcommand(dev_id,cmdstr)
end

#cmd_resp(dev_id, cmdstr) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/neohub/neohub.rb', line 176

def cmd_resp(dev_id, cmdstr)
	@logger.debug(cmdstr)
	params={
		'device_id'=>dev_id,
		'command'=>cmdstr
	}
	respadd=jsonreq('/hm_add_command',params)
	@logger.debug(respadd)
	command_id=respadd["COMMANDID"]
	params={
		'device_id'=>dev_id,
		'command_id'=>command_id
	}
	respread='None'
	try=0
	while (respread=='None' and try<@max_tries) do
		respresp=httpauthreq('/hm_get_response',params)
		respread=respresp.body
		sleep 5
		try=try+1
	end
	@logger.debug(respread)
	return respread
end

#device_status(device_id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/neohub/neohub.rb', line 103

def device_status(device_id)
	params={
		'device_id'=>device_id,
	}
	jsonresp=jsonreq('/hm_device_status',params)
	if jsonresp['devices'][0].has_key?('deviceid') then
		@devid=jsonresp['devices'][0]['deviceid']
	end
	return jsonresp
end

#frost_off(dev_id) ⇒ Object



160
161
162
163
# File 'lib/neohub/neohub.rb', line 160

def frost_off(dev_id)
	cmdstr="{'FROST_OFF':[#{dev_id}]}"
	return sendaddcommand(dev_id,cmdstr)
end

#frost_on(dev_id) ⇒ Object



155
156
157
158
# File 'lib/neohub/neohub.rb', line 155

def frost_on(dev_id)
	cmdstr="{'FROST_ON':[#{dev_id}]}"
	return sendaddcommand(dev_id,cmdstr)
end

#get_geo_state(device_id) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/neohub/neohub.rb', line 137

def get_geo_state(device_id)
	params={
		'device_id'=>device_id,
		'username'=>@user,
	}
	return jsonreq('/hm_geo_state',params)
end

#getdevicesObject



96
97
98
99
100
101
# File 'lib/neohub/neohub.rb', line 96

def getdevices()
	params={
		'USERNAME'=>@user,
	}
	return jsonreq('/hm_get_devices',params)
end

#hold_cancel_all(dev_id) ⇒ Object



171
172
173
174
# File 'lib/neohub/neohub.rb', line 171

def hold_cancel_all(dev_id)
	cmdstr="{'CANCEL_HOLD_ALL':0}"
	return sendaddcommand(dev_id,cmdstr)
end

#hold_temp(dev_id, temp, hour, min) ⇒ Object



165
166
167
168
169
# File 'lib/neohub/neohub.rb', line 165

def hold_temp(dev_id,temp,hour,min)
	# {'HOLD': [{'temp' : 21,'id':'X','hours':0,'minutes':0},'X']}
	cmdstr="{'HOLD': [{'temp' : #{temp},'id':'#{dev_id}','hours':#{hour},'minutes':#{min}},'#{dev_id}']}"
	return sendaddcommand(dev_id,cmdstr)
end

#httpauthreq(uri, params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/neohub/neohub.rb', line 52

def httpauthreq(uri,params) 
	authparams={
		'token'=>@token,
		'devkey'=>@devkey,
		'vendorid'=>@vendorid,
		'devicetypeid' => @devicetypeid
	}
	merged=authparams.merge(params)
	return httpreq(uri,merged)
end

#httpreq(uri, params) ⇒ Object



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

def httpreq(uri,params) 
	req = Net::HTTP::Post.new(uri, {})
	req.delete("user-agent")
	req.set_form_data(params)
	response = @http.request(req)
	return response
end

#jsonreq(uri, params) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/neohub/neohub.rb', line 63

def jsonreq(uri,params)
	resp=httpauthreq(uri,params)
	jresp=JSON.parse(resp.body) 
	if jresp['STATUS']==401 then
		(@user,@pass)
		resp=httpauthreq(uri,params)
		jresp=JSON.parse(resp.body)
	end
	return jresp
end

#login(user, pass) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/neohub/neohub.rb', line 74

def (user,pass)
	@user=user
	params={
		'USERNAME'=>@user,
		'PASSWORD'=>pass,
		'devkey'=>@devkey,
		'vendorid'=>@vendorid,
		'devicetypeid' => @devicetypeid
	}
	resp=httpreq('/hm_user_login',params)
	jresp=JSON.parse(resp.body)
	if jresp.has_key?('STATUS')
		if jresp['STATUS']==1 then
			@token=jresp['TOKEN']
			@devices=jresp['devices']	
			@pass=pass
			return jresp
		end
	end	
	return nil
end

#read_comfort_levels(dev_id) ⇒ Object



201
202
203
204
205
# File 'lib/neohub/neohub.rb', line 201

def read_comfort_levels(dev_id)
	# {'READ_COMFORT_LEVELS':['X']}
	cmdstr="{'READ_COMFORT_LEVELS':['#{dev_id}']}"
	return cmd_resp(dev_id, cmdstr)
end

#read_dcb(dev_id) ⇒ Object



212
213
214
215
# File 'lib/neohub/neohub.rb', line 212

def read_dcb(dev_id)
	cmdstr="{'READ_DCB':100}"
	return cmd_resp(dev_id, cmdstr)
end

#read_dcb2(dev_id) ⇒ Object



207
208
209
210
# File 'lib/neohub/neohub.rb', line 207

def read_dcb2(dev_id)
	cmdstr="{'READ_DCB':['#{dev_id}']}"
	return cmd_resp(dev_id, cmdstr)
end

#sendaddcommand(dev_id, command) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/neohub/neohub.rb', line 123

def sendaddcommand(dev_id,command)
	@logger.debug(command)
	params={
		'device_id'=>dev_id,
		'command'=>command
	}
	return jsonreq('/hm_add_command',params)
end

#sendsscommand(device_id, command) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/neohub/neohub.rb', line 114

def sendsscommand(device_id,command)
	params={
		# 'location_id'=>location_id,
		'devices'=>device_id,
		'command'=>command
	}
	return jsonreq('/hm_ss_multicommand',params)
end

#set_temp(dev_id, temp) ⇒ Object



132
133
134
135
# File 'lib/neohub/neohub.rb', line 132

def set_temp(dev_id,temp)
	cmdstr="{'SET_TEMP':[#{temp},'#{dev_id}']}"
	return sendaddcommand(dev_id,cmdstr)
end

#setupdebugObject



36
37
38
39
40
41
42
# File 'lib/neohub/neohub.rb', line 36

def setupdebug
	if @debug then
		@http.set_debug_output $stderr
		@logger = Logger.new(STDERR)
		@logger.level = Logger::DEBUG
	end
end