lib/imperituroard.rb in imperituroard-0.2.5 vs lib/imperituroard.rb in imperituroard-0.2.7

- old
+ new

@@ -86,48 +86,54 @@ class Iot attr_accessor :mongoip, :mongoport, :iotip, - :iottoken, :mongo_database, :iotplatform_ip, :iotplatform_port, :cert_path, :key_path, :mongo_client, - :add_functions_connector + :add_functions_connector, + :real_ip, #real ip address of procedure caller + :remote_ip, #ip address of balancer + :hua_aceanconnect_connector def initialize(mongoip, mongoport, iotip, mongo_database, - iotplatform_ip, iotplatform_port, cert_path, key_path, telegram_api_url, telegram_chat_id) + iotplatform_ip, iotplatform_port, cert_path, key_path, telegram_api_url, telegram_chat_id, real_ip, remote_ip) @mongoip = mongoip @mongoport = mongoport @iotip = iotip @mongo_database = mongo_database @iotplatform_ip = iotplatform_ip @iotplatform_port = iotplatform_port @cert_path = cert_path @key_path = key_path @mongo_client = MongoIot.new(mongoip, mongoport, mongo_database) @add_functions_connector = AdditionalFunc.new(telegram_api_url, telegram_chat_id) + @real_ip = real_ip + @remote_ip = remote_ip + @hua_aceanconnect_connector = HuaIot.new(iotplatform_ip, iotplatform_port, cert_path, key_path) end #error list #:code => 507, :result => "Unknown SDK error" #{:code => 200, :result => "Request completed successfully", :body => result_ps} - #!1. Add device to profile (only for new device) + #!!1. Add device to profile (only for new device) #login - login for client identification #profile - profile for device #imei_list - device identificator - #imei_list = [{"imei" => 131234123412341233, "description" => "dfdsf", "note"=>"second description", "profile"=>0}, + #imei_list = [{"imei" => 131234123412341233, "description" => "dfdsf", "note"=>"second description", "profile"=>0, "device_type"=>"phone"}, #{"imei" => 56213126347645784, "description" => "dfdsf", "note"=>"second description", "profile"=>0}] #massive commands - #++ - def add_device_to_profile(login, imei_list, remote_ip) + #+++ + #iot logic added + def add_device_to_profile(login, imei_list) input_json = {:login => login, :imei_list => imei_list} resp_out = {} begin thr1 = Thread.new do @@ -148,54 +154,77 @@ for jj in list_checked[:body][:not_exists] begin get_login_info = mongo_client.check_login_prof_perm_id_one(login, list1[jj]["profile"])[:code] if get_login_info==200 - - for_insert.append(list1[jj]) else not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"}) end rescue not_processed_list.append({:imei => list1[jj], :error => "Unknown error"}) end end begin + added_on_iot_platf = [] if for_insert!=[] ##Logic for IOT Platform connection### - #########end iot platform logic####### + credentials = mongo_client.get_iot_oceanconnect_credent(login) - mongo_client.imei_insert_list(for_insert) - resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => for_insert, :error_list => not_processed_list}} + if credentials[:code]==200 + for aaa in for_insert + begin + dev_name = aaa["imei"].to_s + resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id], credentials[:body][:secret], aaa["imei"], dev_name, aaa["description"], aaa["device_type"], aaa["profile"]) + if resss[:code]=="200" + s1 = aaa + s1[:huadata] = resss + added_on_iot_platf.append(s1) + else + not_processed_list.append({:imei => aaa["imei"], :error => resss}) + end + rescue + not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"}) + end + end + + #########end iot platform logic####### + + mongo_client.imei_insert_list(added_on_iot_platf) + resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}} + else + resp_out = {:code => 400, :result => "IOT platform credentials not found"} + end + + else - resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => for_insert, :error_list => not_processed_list}} + resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}} end rescue resp_out = {:code => 505, :result => "Error with database communication"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr1.join - mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out) + mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip) resp_out end - #!2 Find device (only mongo datebase. IOT platform not need) + #!!2 Find device (only mongo datebase. IOT platform not need) # procedure for data selection from mongo database. # for this function IOT platform not need # login # imei # imei_list =[41234,23452345,132412] #++ - def device_find(login, imei_list, remote_ip) + def device_find(login, imei_list) input_json = {:login => login, :imei_list => imei_list} ime_list_approved = [] ime_list_notapproved = [] resp = {} begin @@ -234,22 +263,22 @@ end rescue resp = {:code => 507, :result => "Unknown SDK error"} end thr2.join - mongo_client.audit_logger("device_find", remote_ip, input_json, resp) + mongo_client.audit_logger("device_find", remote_ip, input_json, resp, real_ip) resp end #!3 device modify, change imei #login #imei_old #imei_new #massive commands #im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}] #++ - def imei_replace(login, im_list, remote_ip) + def imei_replace(login, im_list) input_json = {:login => login, :imei_list => im_list} li_new_imei = [] list1 = {} not_processed_list = [] @@ -294,10 +323,12 @@ p permiss1 if permiss1==200 ##Logic for IOT Platform connection### + remove_one_device_from_iot(app_id, secret, dev_id) + #########end iot platform logic####### mongo_client.device_modify_any_attr_mongo(list1[a], {:imei => a}) approved_list.append({:imei_old => list1[a], :imei_new => a}) @@ -326,71 +357,94 @@ rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr3.join - mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out) + mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out, real_ip) resp_out end - #!4 remove device + #!!4 remove device #login #imei # not massive commands #imei=11341341234 #login="test" - #++ - def device_remove(login, imei, remote_ip) + #+++ + #IOT logic added + def device_remove(login, imei) input_json = {:login => login, :imei_list => imei} resp_out = {} begin thr4 = Thread.new do - prof_name = mongo_client.get_profile_name_from_imei(imei) - if prof_name[:code]==200 - permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) - if permiss[:code]==200 + prof_name = mongo_client.get_profile_name_from_imei(imei) + if prof_name[:code]==200 + permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) + if permiss[:code]==200 - ##Logic for IOT Platform connection### + ##Logic for IOT Platform connection### - #########end iot platform logic####### - resp = mongo_client.device_remove_single_mongo(imei) + credentials = mongo_client.get_iot_oceanconnect_credent(login) + resp = {} - if resp[:code]==200 - resp_out = {:code => 200, :result => "Request completed successfully"} + if credentials[:code]==200 + + imei_data = mongo_client.get_imei_info_from_db([imei]) + if imei_data[:body]!=[] + ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"]) + p ans + if ans[:code]=="204" or ans[:code]=="200" + resp = mongo_client.device_remove_single_mongo(imei) + else + resp = {:code => 500, :result => "Unknown IOT platform error", :body => ans} + end + else + resp_out = {:code => 404, :result => "Data not found"} + end + + #########end iot platform logic####### + + if resp[:code]==200 + resp_out = {:code => 200, :result => "Request completed successfully"} + else + resp_out=resp + end + + else + resp_out = {:code => 400, :result => "IOT platform credentials not found"} + end + else - resp_out=resp + resp_out=permiss end else - resp_out=permiss + resp_out=prof_name end - else - resp_out=prof_name end - end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr4.join - mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out) + mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip) resp_out end #!5 add address to device #login #imei = newdevice_list #address = newdevice_list #newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}] #++ - def device_add_address(login, newdevice_list, remote_ip) + def device_add_address(login, newdevice_list) #add_functions_connector.telegram_message(newdevice_list.to_s) p newdevice_list p "gas" p MyJSON.valid?(newdevice_list[0].to_s) p "sdfsdfgs" @@ -398,47 +452,47 @@ resp_out = {} not_processed = [] processed = [] begin thr5 = Thread.new do - for p in newdevice_list - prof_name = mongo_client.get_profile_name_from_imei(p[:imei]) + for p in newdevice_list + prof_name = mongo_client.get_profile_name_from_imei(p[:imei]) - if prof_name[:code]==200 - p "prof_name" - p prof_name - permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) - if permiss[:code]==200 + if prof_name[:code]==200 + p "prof_name" + p prof_name + permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) + if permiss[:code]==200 - ##Logic for IOT Platform connection### + ##Logic for IOT Platform connection### - #########end iot platform logic####### + #########end iot platform logic####### - resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address]) - if resp[:code]==200 - processed.append({:imei=>p[:imei]}) + resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address]) + if resp[:code]==200 + processed.append({:imei => p[:imei]}) + end + else + not_processed.append({:imei => p[:imei], :address => p[:address], :error => permiss}) end else - not_processed.append({:imei=>p[:imei], :address=>p[:address], :error=>permiss}) + not_processed.append({:imei => p[:imei], :address => p[:address], :error => prof_name}) end + end + + if processed!=[] + resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}} else - not_processed.append({:imei=>p[:imei], :address=>p[:address], :error=>prof_name}) + resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}} end end - - if processed!=[] - resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}} - else - resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}} - end - end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr5.join - mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out) + mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out, real_ip) resp_out end #6 add service by SPA @@ -446,11 +500,11 @@ #profile #imsi #msisdn #newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}] #+ - def add_service(login, device_list, remote_ip) + def add_service(login, device_list) resp_out = {} not_processed = [] processed = [] input_json = {:login => login, :devices => device_list} @@ -491,80 +545,81 @@ mongo_client.device_modify_any_attr_mongo(g["imei"], attr) processed.append(g["imei"]) else - not_processed.append({:imei=>g["imei"], :description=> "New profile permission error", :error=>permiss2 }) + not_processed.append({:imei => g["imei"], :description => "New profile permission error", :error => permiss2}) end else attr = g["attributes"] mongo_client.device_modify_any_attr_mongo(g["imei"], attr) processed.append(g["imei"]) end - - else - not_processed.append({:imei=>g["imei"], :description=> "Old profile permission error", :error=>permiss1 }) + not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1}) end else - not_processed.append({:imei=>g["imei"],:error=>prof_name1}) + not_processed.append({:imei => g["imei"], :error => prof_name1}) end end resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}} rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end - mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out) + mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip) resp_out end def answ_dev_query_format_process(dev_list) add_functions_connector.answ_dev_query_format_process(dev_list) end - def logger_to_audit_database(proc_name, src_ip, input_json, output_json) - mongo_client.audit_logger(proc_name, src_ip, input_json, output_json) + def logger_to_audit_database(proc_name, src_ip, input_json, output_json, real_ip) + mongo_client.audit_logger(proc_name, src_ip, input_json, output_json, real_ip) end def test() ddd = MongoIot.new(mongoip, mongoport, mongo_database) #ddd.get_profiles_by_login("test") ff = [131234123412341233, 131234123127341233] #ddd.get_imsi_info_from_db(ff) - p ddd.get_profile_id_by_name("1341241") + #p ddd.get_profile_id_by_name("1341241") + p ddd.get_iot_oceanconnect_credent("test") end def testhua() cert_file = cert_path key_file = key_path - ddd1 = HuaIot.new(iotplatform_ip, iotplatform_port, "", "", cert_file, key_file) + ddd1 = HuaIot.new(iotplatform_ip, iotplatform_port, cert_file, key_file) #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") #ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "a8834c5e-4b4d-4f0f-ad87-14e916f3d0bb") #ddd1.querying_device_activ_status("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") #ddd1.querying_device_info("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "a8834c5e-4b4d-4f0f-ad87-14e916f3d0bb") list = ["41c0ba82-d771-4669-b766-fcbfbedc17f4", "7521234165452", "feb9c4d1-4944-4b04-a717-df87dfde30f7", "9868e121-c309-4f4f-8ab3-0aa69072caff", "b3b82f35-0723-4a83-90af-d4ea40017194"] #p ddd1.querying_device_direct_conn("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", list) #p ddd1.querying_device_type_list("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa") - #p ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") - #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "12321234545345") + # ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") + #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "45324523452345") - p ddd1.quer_dev_direct_conn_batches("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", ["62c5f5ef-dead-48bf-9f09-292ca5e41577", "4c763ea1-be51-4eff-81db-863bc04791bb"]) + p devtestid = ddd1.dev_register_passw_code_mode2("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "24235455", "test", "test", "phone", "0") + p ddd1.quer_dev_direct_conn_batches("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", [devtestid[:body]["deviceId"], "4c763ea1-be51-4eff-81db-863bc04791bb"]) - #ddd1.querying_device_info("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "c6e0dfb1-8042-472a-a35d-e984f118d2a6") - - #ddd1.dev_delete("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "484114f6-a49c-4bab-88e6-4ddaf1cc1c8f") + #p ddd1.querying_device_info("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "c6e0dfb1-8042-472a-a35d-e984f118d2a6") + p "delete start" + p ddd1.dev_delete("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", devtestid[:body]["deviceId"]) + #p ddd1.dev_delete("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "2881a9cc-cb2a-4b59-8fac-de3aace2324d") end def testhua2() tt = "{\"deviceId\":\"fad0a417-b6a3-4b0b-abfc-fa2b0af9691a\",\"verifyCode\":\"6cb6dcca\",\"timeout\":180,\"psk\":\"1d16b55d577bc1f2e5e75d416ce6b8a2\"}"