require "forcast/utils/server" module Forcast module Utils module Server class Thing attr_accessor :headers attr_accessor :url attr_accessor :json_send attr_accessor :metodo def initialize self.headers = eval(File.read(Rails.root.join("tmp/cache/loginthings.txt"))) end def auth_login_things 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 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 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 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 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 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 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 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 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 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 def valid_json?(json) begin JSON.parse(json) return true rescue JSON::ParserError => e puts e return false end end def self.sync_login 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 end end end end