Class: HuaIot
- Inherits:
-
Object
- Object
- HuaIot
- Defined in:
- lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb
Instance Attribute Summary collapse
-
#cert_file ⇒ Object
Returns the value of attribute cert_file.
-
#client ⇒ Object
Returns the value of attribute client.
-
#client_iot_mongo ⇒ Object
Returns the value of attribute client_iot_mongo.
-
#database ⇒ Object
Returns the value of attribute database.
-
#internal_func ⇒ Object
Returns the value of attribute internal_func.
-
#key_file ⇒ Object
Returns the value of attribute key_file.
-
#platformip ⇒ Object
Returns the value of attribute platformip.
-
#platformport ⇒ Object
Returns the value of attribute platformport.
Instance Method Summary collapse
- #add_new_device_on_huawei(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) ⇒ Object
- #add_new_device_on_huawei2(app_id, secret, attr_list) ⇒ Object
-
#dev_delete(app_id, dev_id, token) ⇒ Object
2.2.12 Deleting a Directly Connected Device.
-
#dev_modify_location_v2(app_id, dev_id, token, address) ⇒ Object
2.2.11 Modifying Device Information (V2).
-
#dev_reg_passw_code_mode2_2(app_id, secret, attr_list) ⇒ Object
2.2.4 Registering a Directly Connected Device (Password Mode) (V2).
-
#dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) ⇒ Object
2.2.4 Registering a Directly Connected Device (Password Mode) (V2).
-
#dev_register_verif_code_mode(app_id, secret, node_id) ⇒ Object
Registering a Directly Connected Device (Verification Code Mode) (V2).
- #get_token(app_id, secret) ⇒ Object
-
#initialize(platformip, platformport, cert_file, key_file, mongo_ip, mongo_port, mongo_database) ⇒ HuaIot
constructor
A new instance of HuaIot.
-
#modify_location_iot(app_id, secret, dev_id, address) ⇒ Object
final procedures###############.
- #parse_token(str) ⇒ Object
-
#quer_dev_direct_conn_batches(app_id, dev_list, token) ⇒ Object
2.9.6 Querying Directly Connected Devices and Their Mounted Devices in Batches.
- #quer_dev_query_list(app_id, secret, dev_list) ⇒ Object
-
#querying_device_activ_status(app_id, secret, device_id) ⇒ Object
2.2.14 Querying Device Activation Status.
-
#querying_device_direct_conn(app_id, secret, dev_list) ⇒ Object
2.9.6 Querying Directly Connected Devices and Their Mounted Devices in Batches.
-
#querying_device_id(app_id, secret, node_id) ⇒ Object
2.2.44 Querying the Device ID.
-
#querying_device_info(app_id, secret, device_id) ⇒ Object
2.9.1 Querying Information About a Device.
-
#querying_device_type_list(app_id, secret) ⇒ Object
2.9.19 Querying the Complete Device Type List of All Device Capabilities.
- #remove_one_device_from_iot(app_id, secret, dev_id) ⇒ Object
- #test ⇒ Object
- #token_logout(token) ⇒ Object
- #valid_json?(json) ⇒ Boolean
Constructor Details
#initialize(platformip, platformport, cert_file, key_file, mongo_ip, mongo_port, mongo_database) ⇒ HuaIot
Returns a new instance of HuaIot.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 16 def initialize(platformip, platformport, cert_file, key_file, mongo_ip, mongo_port, mongo_database) @database = database @platformip = platformip @platformport = platformport @cert_file = cert_file @key_file = key_file #client_host = [mongoip + ":" + mongoport] #@client = Mongo::Client.new(client_host, :database => database) @internal_func = InternalFunc.new @client_iot_mongo = MongoIot.new(mongo_ip, mongo_port, mongo_database) end |
Instance Attribute Details
#cert_file ⇒ Object
Returns the value of attribute cert_file.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def cert_file @cert_file end |
#client ⇒ Object
Returns the value of attribute client.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def client @client end |
#client_iot_mongo ⇒ Object
Returns the value of attribute client_iot_mongo.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def client_iot_mongo @client_iot_mongo end |
#database ⇒ Object
Returns the value of attribute database.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def database @database end |
#internal_func ⇒ Object
Returns the value of attribute internal_func.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def internal_func @internal_func end |
#key_file ⇒ Object
Returns the value of attribute key_file.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def key_file @key_file end |
#platformip ⇒ Object
Returns the value of attribute platformip.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def platformip @platformip end |
#platformport ⇒ Object
Returns the value of attribute platformport.
14 15 16 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 14 def platformport @platformport end |
Instance Method Details
#add_new_device_on_huawei(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) ⇒ Object
557 558 559 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 557 def add_new_device_on_huawei(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) self.dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) end |
#add_new_device_on_huawei2(app_id, secret, attr_list) ⇒ Object
561 562 563 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 561 def add_new_device_on_huawei2(app_id, secret, attr_list) self.dev_reg_passw_code_mode2_2(app_id, secret, attr_list) end |
#dev_delete(app_id, dev_id, token) ⇒ Object
2.2.12 Deleting a Directly Connected Device
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 258 def dev_delete(app_id, dev_id, token) out_resp = {} 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) if res.body != nil out_resp = {:code => res.code, :message => res., :body => {:answ => valid_json?(res.body.to_s)}} else out_resp = {:code => res.code, :message => res., :body => {:answ => "no data"}} end fff = {:func => "dev_delete", :iot_fun => "2.2.12", :req_header => {:authorization => 'Bearer ' + token, :content_type => 'application/json', :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, "Delete", out_resp) rescue out_resp = {:code => 500, :message => "dev_delete: Unknown IOT error"} end out_resp end |
#dev_modify_location_v2(app_id, dev_id, token, address) ⇒ Object
2.2.11 Modifying Device Information (V2)
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 479 def dev_modify_location_v2(app_id, dev_id, token, address) out_resp = {} begin path = "/iocm/app/dm/v1.4.0/devices/" + dev_id + "?app_Id=" + app_id url_string = "https://" + platformip + ":" + platformport + path internal_func.printer_texter({:url_string => url_string, :procedure => "dev_modify_location_v2"}, "debug") 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::Put.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id req_b = {location: address} request.body = req_b.to_json res = https.request(request) p res.code p res.body if res.body != nil out_resp = {:code => res.code, :message => res., :body => {:answ => valid_json?(res.body.to_s)}} else out_resp = {:code => res.code, :message => res., :body => {:answ => "no data"}} end fff = {:func => "dev_modify_location_v2", :iot_fun => "2.2.11", :req_header => {:authorization => 'Bearer ' + token, :content_type => 'application/json', :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, req_b, out_resp) rescue out_resp = {:code => 500, :message => "dev_modify_location_v2: Unknown IOT error"} end p out_resp out_resp end |
#dev_reg_passw_code_mode2_2(app_id, secret, attr_list) ⇒ Object
2.2.4 Registering a Directly Connected Device (Password Mode) (V2)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 223 def dev_reg_passw_code_mode2_2(app_id, secret, attr_list) 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 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) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id data = {deviceInfo: attr_list} data_out = data.to_json internal_func.printer_texter({:procedure => "dev_register_passw_code_mode2", :data => {:body => data_out, :url => url_string}}, "debug") request.body = data_out res = https.request(request) p res.body.to_s out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "dev_reg_passw_code_mode2_2", :iot_fun => "2.2.4", :req_header => {:content_type => 'application/json', :authorization => 'Bearer ' + token, :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, data, out_resp) out_resp end |
#dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) ⇒ Object
2.2.4 Registering a Directly Connected Device (Password Mode) (V2)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 182 def dev_register_passw_code_mode2(app_id, secret, node_id, name_p, description_p, device_type, profile, manufacturer_id, manufacturer_name, model) 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 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) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id data_out = {deviceInfo: {nodeId: node_id, name: name_p, description: description_p, deviceType: device_type, manufacturerId: manufacturer_id, manufacturerName: manufacturer_name, model: model, isSecurity: "FALSE", supportedSecurity: "FALSE"}} internal_func.printer_texter({:procedure => "dev_register_passw_code_mode2", :data => {:body => data_out, :url => url_string}}, "debug") request.body = data_out.to_json res = https.request(request) out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "dev_register_passw_code_mode2", :iot_fun => "2.2.4", :req_header => {:authorization => 'Bearer ' + token, :content_type => 'application/json', :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, data_out, out_resp) out_resp end |
#dev_register_verif_code_mode(app_id, secret, node_id) ⇒ Object
Registering a Directly Connected Device (Verification Code Mode) (V2)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 153 def dev_register_verif_code_mode(app_id, secret, node_id) out_resp = {} 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 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) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id data = {nodeId: node_id} request.body = data.to_json res = https.request(request) out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "dev_register_verif_code_mode", :iot_fun => "Registering a Directly Connected Device (Verification Code Mode) (V2)", :req_header => {:content_type => 'application/json', :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, data, out_resp) out_resp end |
#get_token(app_id, secret) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 60 def get_token(app_id, secret) internal_func.printer_texter("get_token: start. Step1, iput: app_id: #{app_id.to_s}, secret: #{secret.to_s}", "debug") out_resp = {} fff = {} data = {} url_string = "" begin path = '/iocm/app/sec/v1.1.0/login' url_string = "https://" + platformip + ":" + platformport + path internal_func.printer_texter("get_token: start. Step2, url_string: #{url_string}", "debug") 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 } internal_func.printer_texter("get_token: start. Step3, data: #{data.to_s}", "debug") request.content_type = 'application/x-www-form-urlencoded' request.body = URI.encode_www_form(data) res = https.request(request) p "get_token" p res.code p res. p res.body.to_s out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "get_token", :iot_fun => "", :req_header => {:content_type => 'application/x-www-form-urlencoded', :platform => "Huawei oceanconnect" }} rescue out_resp = {:code => 500, :message => "failed get token"} end client_iot_mongo.audit_iot_logger(fff, url_string, data, out_resp) jjj = {:procedure => "get_token", :answ => out_resp} internal_func.printer_texter(jjj, "debug") out_resp end |
#modify_location_iot(app_id, secret, dev_id, address) ⇒ Object
final procedures###############
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 530 def modify_location_iot(app_id, secret, dev_id, address) out_resp = {} begin token = self.get_token(app_id, secret) if token[:code] != 500 && token[:body]["accessToken"]!=nil out_resp = self.dev_modify_location_v2(app_id, dev_id, token[:body]["accessToken"], address) 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 => "modify_location_iot: Invalid IOT platform token"} end rescue out_resp = {:code => 500, :message => "modify_location_iot: Unknown error"} end jjj = {:procedure => "modify_location_iot", :answ => out_resp} internal_func.printer_texter(jjj, "debug") out_resp end |
#parse_token(str) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 34 def parse_token(str) begin dd = str.split(",") acc_token = '' refr_token = '' exp_in = '' 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) end end {:status => 200, :result => "OK", :accessToken => acc_token[1], :refreshToken => refr_token[1], :expiresIn => exp_in[1]} rescue {:status => 500, :result => "failed"} end end |
#quer_dev_direct_conn_batches(app_id, dev_list, token) ⇒ Object
2.9.6 Querying Directly Connected Devices and Their Mounted Devices in Batches
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 447 def quer_dev_direct_conn_batches(app_id, dev_list, token) output_ans = {} path = "/iocm/app/dm/v1.1.0/queryDevicesByIds" 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.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id data = {deviceIds: dev_list} request.body = data.to_json res = https.request(request) output_ans = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "quer_dev_direct_conn_batches", :iot_fun => "2.9.6", :req_header => {:content_type => 'application/json', :authorization => 'Bearer ' + token, :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, data, output_ans) output_ans end |
#quer_dev_query_list(app_id, secret, dev_list) ⇒ Object
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 592 def quer_dev_query_list(app_id, secret, dev_list) out_resp = {} begin token = self.get_token(app_id, secret) if token[:code] != 500 && token[:body]["accessToken"]!=nil out_resp = self.quer_dev_direct_conn_batches(app_id, dev_list, 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 => "quer_dev_query_list: Invalid IOT platform token"} end rescue out_resp = {:code => 500, :message => "quer_dev_query_list: Unknown error"} end jjj = {:procedure => "quer_dev_query_list", :answ => out_resp} internal_func.printer_texter(jjj, "debug") out_resp end |
#querying_device_activ_status(app_id, secret, device_id) ⇒ Object
2.2.14 Querying Device Activation Status
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 324 def (app_id, secret, device_id) output_ans = {} token = get_token(app_id, secret)[:body]["accessToken"] 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.verify_mode = OpenSSL::SSL::VERIFY_NONE 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) output_ans = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "querying_device_activ_status", :iot_fun => "2.2.14", :req_header => {:content_type => 'application/json', :authorization => 'Bearer ' + token, :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, data, output_ans) output_ans end |
#querying_device_direct_conn(app_id, secret, dev_list) ⇒ Object
2.9.6 Querying Directly Connected Devices and Their Mounted Devices in Batches
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 383 def (app_id, secret, dev_list) output_str = {} token = get_token(app_id, secret)[:body]["accessToken"] 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.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.path) request.content_type = 'application/json' request['Authorization'] = 'Bearer ' + token request['app_key'] = app_id req_body = {deviceIds: dev_list} request.body = req_body.to_json res = https.request(request) output_str = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "querying_device_direct_conn", :iot_fun => "2.9.6", :req_header => {:authorization => 'Bearer ' + token, :content_type => 'application/json', :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, req_body, output_str) output_str end |
#querying_device_id(app_id, secret, node_id) ⇒ Object
2.2.44 Querying the Device ID
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 296 def (app_id, secret, node_id) get_token_data = get_token(app_id, secret) if get_token_data[:code]=="200" token = get_token_data[:body]["accessToken"] uri1 = URI("https://134.17.93.4:8743/iocm/app/dm/v1.1.0/queryDeviceIdByNodeId?nodeId=" + node_id + "&" + app_id) https = Net::HTTP.new(uri1.host, uri1.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::Get.new(uri1) request.content_type = 'application/json' request['app_key'] = app_id request['Authorization'] = 'Bearer ' + token #request.set_form_data( params ) #request['node-id'] = node_id bbb = {"nodeId": node_id, "appId": "Jy5KpoBUOScZEcrO2bRZhzfaIOUa"}.to_json #request.body = bbb res = https.request(request) #p res.body.to_s #p res.code {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} else {:code => "500", :message => "Something wrong", :body => {}} end end |
#querying_device_info(app_id, secret, device_id) ⇒ Object
2.9.1 Querying Information About a Device
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 354 def (app_id, secret, device_id) out_resp = {} token = get_token(app_id, secret)[:body]["accessToken"] 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.verify_mode = OpenSSL::SSL::VERIFY_NONE 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) out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "querying_device_info", :iot_fun => "2.9.1", :req_header => {:authorization => 'Bearer ' + token, :content_type => 'application/json', :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, "Get", out_resp) out_resp end |
#querying_device_type_list(app_id, secret) ⇒ Object
2.9.19 Querying the Complete Device Type List of All Device Capabilities
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 415 def (app_id, secret) output_ans = {} token = get_token(app_id, secret)[:body]["accessToken"] 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.verify_mode = OpenSSL::SSL::VERIFY_NONE 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) output_ans = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s)} fff = {:func => "querying_device_type_list", :iot_fun => "2.9.19", :req_header => {:content_type => 'application/json', :authorization => 'Bearer ' + token, :app_key => app_id, :platform => "Huawei oceanconnect" }} client_iot_mongo.audit_iot_logger(fff, url_string, "Get", output_ans) output_ans end |
#remove_one_device_from_iot(app_id, secret, dev_id) ⇒ Object
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 566 def remove_one_device_from_iot(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 jjj = {:procedure => "remove_one_device_from_iot", :answ => out_resp} internal_func.printer_texter(jjj, "debug") out_resp end |
#test ⇒ Object
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 621 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' } req = {"dstAppId": "bCRahH5zSi9SNmyfqv3BkJABAq8a"} post_data = URI.encode_www_form(req) uri = URI.parse url_string p uri.host p uri.port p uri.path cert_file = "/Users/imperituroard/Desktop/cert.crt" key_file = "/Users/imperituroard/Desktop/key.pem" 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.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.path) data = { :appId => "bCRahH5zSi9SNmyfqv3BkJABAq8a", :secret => "ppeMsOq6zdb2fSUH4GoRooS_FgEa" } #request['app_key'] = 'ppeMsOq6zdb2fSUH4GoRooS_FgEa' #request['Authorization'] = 'Bearer ppeMsOq6zdb2fSUH4GoRooS_FgEa' request.content_type = 'application/x-www-form-urlencoded' #p request.body = req request.body = URI.encode_www_form(data) res = https.request(request) p res.code p res. p parse_token(res.body) end |
#token_logout(token) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 107 def token_logout(token) internal_func.printer_texter("token_logout Step1 token: #{token}", "debug") out_resp = {} fff = {} data = {} url_string = "" begin path = "/iocm/app/sec/v1.1.0/logout" url_string = "https://" + platformip + ":" + platformport + path internal_func.printer_texter("token_logout Step2 url_string: #{url_string}", "debug") 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 } internal_func.printer_texter("token_logout Step3 data: #{data.to_s}", "debug") request.content_type = 'application/json' request.body = URI.encode_www_form(data) res = https.request(request) #JSON.parse(res.body.to_s) out_resp = {:code => res.code, :message => res., :body => valid_json?(res.body.to_s) } fff = {:func => "token_logout", :iot_fun => "logout", :platform => "Huawei oceanconnect" } p res.code p res. p res.body.to_s rescue out_resp = {:code => 500, :message => "failed logout token"} end client_iot_mongo.audit_iot_logger(fff, url_string, data, out_resp) jjj = {:procedure => "token_logout", :answ => out_resp} internal_func.printer_texter(jjj, "debug") out_resp end |
#valid_json?(json) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb', line 28 def valid_json?(json) JSON.parse(json) rescue JSON::ParserError => e json end |