Class: Forcast::Utils::Server::Thing

Inherits:
Object
  • Object
show all
Defined in:
lib/forcast/utils/thing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeThing

Returns a new instance of Thing.



11
12
13
# File 'lib/forcast/utils/thing.rb', line 11

def initialize
	self.headers = eval(File.read(Rails.root.join("tmp/cache/loginthings.txt")))
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/forcast/utils/thing.rb', line 7

def headers
  @headers
end

#json_sendObject

Returns the value of attribute json_send.



9
10
11
# File 'lib/forcast/utils/thing.rb', line 9

def json_send
  @json_send
end

#metodoObject

Returns the value of attribute metodo.



10
11
12
# File 'lib/forcast/utils/thing.rb', line 10

def metodo
  @metodo
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/forcast/utils/thing.rb', line 8

def url
  @url
end

Class Method Details

.sync_loginObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/forcast/utils/thing.rb', line 147

def self.
	p "LOGIN THINGS"
	app = Rails.application.class.parent.to_s
    servidorthings = Forcast::Utils::Server::Server.new do |config|		
		config.headers.merge!(eval(ENV[app+'_serverthings_headers']))
		config.dns = ENV[app+'_serverthings_dns']
		config.connect = ENV[app+'_serverthings_connect']
		config.read = ENV[app+'_serverthings_read']
		config.write = ENV[app+'_serverthings_write']
		config.data = ENV[app+'_serverthings_data']
	end
    	usuario  = ENV[app+'_serverthings_user']
 	password = ENV[app+'_serverthings_pass']
    url = "/auth/login"
    json_send = {:username => usuario, :password => password}
    headers_send = {}
    response = servidorthings.conexion(:post,{},url,json_send)[1]
    begin
    	json_recived = JSON.parse(response)
    rescue JSON::ParserError => e
    	json_recived = {}
    end
    if json_recived.include?("token")
		   headers = {"X-Authorization" => "Bearer "+ json_recived["token"]}.to_s
	else
		   headers = "PRUEBA HEADERS"
	end
	File.write(Rails.root.join("tmp/cache/loginthings.txt"),headers)
	p "DONE LOGIN"
end

Instance Method Details

#attributes_device_things(id, devices) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/forcast/utils/thing.rb', line 54

def attributes_device_things(id,devices)
     constructor do
      self.metodo = :get
      self.url = "/plugins/telemetry/DEVICE/"
      self.url += id.to_s
      self.url += "/values/attributes?keys="
      devices.each do |device|
       	self.url += device + ','
     	  end
     	  self.url = url[0...-1]
      self.json_send = {}
  end
end

#attributes_telemetry_device_things(id) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/forcast/utils/thing.rb', line 45

def attributes_telemetry_device_things(id)
  constructor do 
  	self.metodo = :get
  	self.url = "/plugins/telemetry/DEVICE/" 
   self.url += id
   self.url += "/keys/attributes"
   self.json_send = {}
  end
end

#auth_login_thingsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/forcast/utils/thing.rb', line 14

def 
       app = Rails.application.class.parent.to_s   	
       url = "/auth/login"
     	json_send = { :username => ENV[app+'_serverthings_user'], :password => ENV[app+'_serverthings_pass'] }
     	headers_send = {}
     	response = SERVIDORTHINGS.conexion(:post,{},url,json_send)[1]
     	#p response
     	json_recived = JSON.parse(response) if valid_json?(response)
     	if json_recived.include?("token")
	    	@headers_send = {"X-Authorization" => "Bearer "+ json_recived["token"]}
	else
		   return false
	end
end

#constructorObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/forcast/utils/thing.rb', line 126

def constructor
     yield
     if SERVIDORTHINGS
      response = SERVIDORTHINGS.conexion(self.metodo, self.headers, self.url, self.json_send)[1]
      json_recived = {}
      json_recived = JSON.parse(response) if self.valid_json?(response)
      return  json_recived
  else
	puts "Servidor SERVIDORTHINGS no definido"
	return
  end
end

#create_device_things(name, device_type: 'default') ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/forcast/utils/thing.rb', line 81

def create_device_things(name, device_type: 'default')
   json_received = constructor do
		self.metodo = :post
     self.url = '/device'
     self.json_send = { name: name, type: device_type }
					end
   p json_received
   if json_received.include?('id')
     data = {}
     data['token'] = get_token_device_things(json_received['id']['id'])
 	return false unless data['token'] 

     data['id'] = json_received['id']['id']
 	return data
 else
						return false
					end
end

#delete_device_things(id_things) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/forcast/utils/thing.rb', line 113

def delete_device_things(id_things)
  		json_recived = constructor do
   		self.metodo = :delete
   		self.url = "/device/"+id_things.to_s
   		self.json_send = {}
     	end
     	p json_recived
     	if json_recived.include?("errorCode")
		return false
	else
		return true
	end
end

#dorpc_device_things(id, metodo, parametros) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/forcast/utils/thing.rb', line 28

def dorpc_device_things(id,metodo,parametros)
  constructor do
    self.metodo = :post
    self.url = "/plugins/rpc/twoway/"
    self.url += id.to_s
    self.json_send = {"method" => metodo, "params" => parametros}
  end
end

#get_token_device_things(id) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/forcast/utils/thing.rb', line 100

def get_token_device_things(id)
  json_recived = constructor do 
   self.metodo = :get
   self.url = "/device/"+id.to_s+"/credentials"
   self.json_send = {}
 end
  if json_recived.include?("credentialsId")
   return json_recived["credentialsId"]
  else
   return false
  end
end

#keys_telemetry_device_things(id) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/forcast/utils/thing.rb', line 36

def keys_telemetry_device_things(id)
  constructor do
  	self.metodo = :get
  	self.url = "/plugins/telemetry/DEVICE/" 
  	self.url += id
  	self.url += "/keys/timeseries"
  	self.json_send = {}
  end
end

#last_telemetry_device_things(id, parametros) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/forcast/utils/thing.rb', line 67

def last_telemetry_device_things(id,parametros)
  constructor do
   self.metodo = :get
   self.url = "/plugins/telemetry/DEVICE/" 
   self.url += id
   self.url += "/values/timeseries?keys="
   parametros.each do |obis|
      self.url += obis + ','
   end
   self.url = url[0...-1]
   self.json_send = {}
  end
end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
# File 'lib/forcast/utils/thing.rb', line 138

def valid_json?(json)
					begin
JSON.parse(json)
return true
					rescue JSON::ParserError => e
puts e
return false
					end
end