lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb in imperituroard-0.2.9 vs lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb in imperituroard-0.3.0

- old
+ new

@@ -46,29 +46,66 @@ {: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.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Post.new(uri.path) - data = { - :appId => app_id, - :secret => secret - } - request.content_type = 'application/x-www-form-urlencoded' - request.body = URI.encode_www_form(data) - res = https.request(request) - {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} + out_resp = {} + begin + 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.verify_mode = OpenSSL::SSL::VERIFY_NONE + request = Net::HTTP::Post.new(uri.path) + data = { + :appId => app_id, + :secret => secret + } + request.content_type = 'application/x-www-form-urlencoded' + request.body = URI.encode_www_form(data) + res = https.request(request) + out_resp = {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} + rescue + out_resp = {:code => 500, :message => "failed get token"} + end + p jjj = {:procedure => "get_token", :answ => out_resp} + out_resp end + + def token_logout(token) + + p "token_logout token: #{token}" + + out_resp = {} + begin + path = "/iocm/app/sec/v1.1.0/logout" + 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.verify_mode = OpenSSL::SSL::VERIFY_NONE + request = Net::HTTP::Post.new(uri.path) + data = { + :accessToken => token + } + request.content_type = 'application/json' + request.body = URI.encode_www_form(data) + res = https.request(request) + out_resp = {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} + rescue + out_resp = {:code => 500, :message => "failed logout token"} + end + p jjj = {:procedure => "token_logout", :answ => out_resp} + out_resp + end + #Registering a Directly Connected Device (Verification Code Mode) (V2) def dev_register_verif_code_mode(app_id, secret, node_id) token = get_token(app_id, secret)[:body]["accessToken"] path = "/iocm/app/reg/v1.1.0/deviceCredentials?appId=" + app_id url_string = "https://" + platformip + ":" + platformport + path @@ -86,11 +123,11 @@ res = https.request(request) p res.body.to_s {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} end -#2.2.4 Registering a Directly Connected Device (Password Mode) (V2) + #2.2.4 Registering a Directly Connected Device (Password Mode) (V2) def dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile) out_resp = {} token = get_token(app_id, secret)[:body]["accessToken"] path = "/iocm/app/reg/v2.0.0/deviceCredentials?appId=" + app_id url_string = "https://" + platformip + ":" + platformport + path @@ -111,36 +148,38 @@ p out_resp out_resp end - #2.2.12 Deleting a Directly Connected Device - def dev_delete(app_id, secret, dev_id) + def dev_delete(app_id, dev_id, token) out_resp = {} - token = get_token(app_id, secret)[:body]["accessToken"] - path = "/iocm/app/dm/v1.1.0/devices/" + dev_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.verify_mode = OpenSSL::SSL::VERIFY_NONE - 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) - p res.code - p res.body - if res.body != nil - out_resp = {:code => res.code, :message => res.message, :body => {:answ => JSON.parse(res.body.to_s)}} - else - out_resp = {:code => res.code, :message => res.message, :body => {:answ => "no data"}} - end + begin + path = "/iocm/app/dm/v1.1.0/devices/" + dev_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.verify_mode = OpenSSL::SSL::VERIFY_NONE + 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) + p res.code + p res.body + if res.body != nil + out_resp = {:code => res.code, :message => res.message, :body => {:answ => JSON.parse(res.body.to_s)}} + else + out_resp = {:code => res.code, :message => res.message, :body => {:answ => "no data"}} + end + rescue + out_resp = {:code => 500, :message => "dev_delete: Unknown IOT error"} + end p out_resp out_resp end @@ -271,24 +310,44 @@ res = https.request(request) {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)} end - ##2.10.7 Adding Members to a Device Group -##############################################################3 + ##############################################################3 ########final procedures############### def add_new_device_on_huawei(app_id, secret, node_id, name_p, description_p, device_type, profile) - dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile) + self.dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile) end def remove_one_device_from_iot(app_id, secret, dev_id) - dev_delete(app_id, secret, dev_id) + out_resp = {} + begin + token = self.get_token(app_id, secret) + if token[:code] != 500 && token[:body]["accessToken"]!=nil + out_resp = self.dev_delete(app_id, dev_id, token[:body]["accessToken"]) + if out_resp[:code].to_i == 200 || out_resp[:code].to_i == 204 + ###logout# + begin + self.token_logout(token[:body]["accessToken"]) + rescue + nil + end + ########## + end + else + out_resp = {:code => 500, :message => "remove_one_device_from_iot: Invalid IOT platform token"} + end + rescue + out_resp = {:code => 500, :message => "remove_one_device_from_iot: Unknown error"} + end + p jjj = {:procedure => "remove_one_device_from_iot", :answ => out_resp} + out_resp end ####################################### \ No newline at end of file