lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb in imperituroard-0.2.1 vs lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb in imperituroard-0.2.2

- old
+ new

@@ -22,44 +22,44 @@ #@client = Mongo::Client.new(client_host, :database => database) end def parse_token(str) begin - dd = str.split(",") - acc_token = "" - refr_token = "" - exp_in = "" + dd = str.split(",") + acc_token = "" + refr_token = "" + exp_in = "" - access_token = /\"accessToken\":\"(\S+)\"/ - refresh_token = /\"refreshToken\":\"(.+)\"/ - expires_in = /\"expiresIn\":(\d+)/ + access_token = /\"accessToken\":\"(\S+)\"/ + refresh_token = /\"refreshToken\":\"(.+)\"/ + expires_in = /\"expiresIn\":(\d+)/ - for i in dd - if i.to_s.include?("accessToken") - acc_token = access_token.match(i) - elsif i.to_s.include?("refreshToken") - refr_token = refresh_token.match(i) - elsif i.to_s.include?("expiresIn") - exp_in = expires_in.match(i) + for i in dd + if i.to_s.include?("accessToken") + acc_token = access_token.match(i) + elsif i.to_s.include?("refreshToken") + refr_token = refresh_token.match(i) + elsif i.to_s.include?("expiresIn") + exp_in = expires_in.match(i) + end end - end - {:status=> 200, :result=>"OK", :accessToken=>acc_token[1], :refreshToken=>refr_token[1], :expiresIn => exp_in[1] } + {:status => 200, :result => "OK", :accessToken => acc_token[1], :refreshToken => refr_token[1], :expiresIn => exp_in[1]} rescue - {:status=> 500, :result=>"failed"} + {:status => 500, :result => "failed"} end end def get_token(app_id, secret) path = "/iocm/app/sec/v1.1.0/login" url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file) ) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Post.new( uri.path) + request = Net::HTTP::Post.new(uri.path) data = { :appId => app_id, :secret => secret } request.content_type = 'application/x-www-form-urlencoded' @@ -74,14 +74,14 @@ path = "/iocm/app/reg/v1.1.0/deviceCredentials?appId=" + app_id url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file) ) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Post.new( uri.path) + request = Net::HTTP::Post.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id request.body = {nodeId: node_id}.to_json res = https.request(request) @@ -90,19 +90,19 @@ end def dev_delete(app_id, secret, node_id) token = get_token(app_id, secret)[:body]["accessToken"] - path = "/iocm/app/dm/v1.1.0/devices/" + node_id + "?app_Id=" + app_id + "&cascade=true" + path = "/iocm/app/dm/v1.1.0/devices/" + node_id + "?app_Id=" + app_id + "&cascade=true" url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Delete.new( uri.path) + request = Net::HTTP::Delete.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id res = https.request(request) {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} @@ -118,14 +118,14 @@ url_string = "https://" + platformip + ":" + platformport + path p url_string uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new( uri.path) + request = Net::HTTP::Get.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id p request.body res = https.request(request) @@ -134,19 +134,19 @@ end #2.2.14 Querying Device Activation Status def querying_device_activ_status(app_id, secret, device_id) token = get_token(app_id, secret)[:body]["accessToken"] - path = "/iocm/app/reg/v1.1.0/devices/" + device_id + "?app_Id=" + app_id + path = "/iocm/app/reg/v1.1.0/devices/" + device_id + "?app_Id=" + app_id url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new( uri.path) + request = Net::HTTP::Get.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id res = https.request(request) {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} @@ -154,19 +154,19 @@ #2.9.1 Querying Information About a Device def querying_device_info(app_id, secret, device_id) token = get_token(app_id, secret)[:body]["accessToken"] - path = "/iocm/app/dm/v1.1.0/devices/" + device_id + "?app_Id=" + app_id + path = "/iocm/app/dm/v1.1.0/devices/" + device_id + "?app_Id=" + app_id url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new( uri.path) + request = Net::HTTP::Get.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id res = https.request(request) {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} @@ -178,14 +178,14 @@ path = "/iocm/app/dm/v1.1.0/queryDevicesByIds" url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Post.new( uri.path) + request = Net::HTTP::Post.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id request.body = {deviceIds: dev_list}.to_json res = https.request(request) @@ -199,14 +199,14 @@ path = "/iocm/app/profile/v1.1.0/allDeviceTypes" url_string = "https://" + platformip + ":" + platformport + path uri = URI.parse url_string https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file)) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new( uri.path) + request = Net::HTTP::Get.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id res = https.request(request) {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} @@ -214,16 +214,16 @@ def test() url_string = "https://134.17.93.4:8743/iocm/app/sec/v1.1.0/login" headers = { - 'Authorization'=>'Bearer ppeMsOq6zdb2fSUH4GoRooS_FgEa', - 'Content-Type' =>'application/json', - 'Accept'=>'application/json' + 'Authorization' => 'Bearer ppeMsOq6zdb2fSUH4GoRooS_FgEa', + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' } - req = {"dstAppId": "bCRahH5zSi9SNmyfqv3BkJABAq8a" } + req = {"dstAppId": "bCRahH5zSi9SNmyfqv3BkJABAq8a"} post_data = URI.encode_www_form(req) uri = URI.parse url_string p uri.host @@ -232,17 +232,17 @@ cert_file = "/Users/imperituroard/Desktop/cert.crt" key_file = "/Users/imperituroard/Desktop/key.pem" - p https = Net::HTTP.new(uri.host, uri.port) + p https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true - https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) ) - https.key = OpenSSL::PKey::RSA.new( File.read(key_file) ) + https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file)) + https.key = OpenSSL::PKey::RSA.new(File.read(key_file)) https.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Post.new( uri.path) + request = Net::HTTP::Post.new(uri.path) data = { :appId => "bCRahH5zSi9SNmyfqv3BkJABAq8a", :secret => "ppeMsOq6zdb2fSUH4GoRooS_FgEa" } @@ -259,11 +259,10 @@ p parse_token(res.body) end - # App ID - # password O2k2aMStOweZOeSoVDYjI3c6uaMa - + # App ID + # password O2k2aMStOweZOeSoVDYjI3c6uaMa end \ No newline at end of file