lib/imperituroard.rb in imperituroard-0.3.3 vs lib/imperituroard.rb in imperituroard-0.3.4

- old
+ new

@@ -137,100 +137,107 @@ #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 + if add_functions_connector.check_input_1(login, imei_list)[:code]==200 - imei = [] - list1 = {} - for_insert = [] - not_processed_list = [] - processed_list = [] + imei = [] + list1 = {} + for_insert = [] + not_processed_list = [] + processed_list = [] - for ii in imei_list - list1[ii["imei"]] = ii - imei.append(ii["imei"]) - end - list_checked = mongo_client.check_imei_exists(imei) - for ss in list_checked[:body][:exists] - not_processed_list.append({:imei => ss, :error => "Device exists in database"}) - end + for ii in imei_list + list1[ii["imei"]] = ii + imei.append(ii["imei"]) + end + list_checked = mongo_client.check_imei_exists(imei) + for ss in list_checked[:body][:exists] + not_processed_list.append({:imei => ss, :error => "Device exists in database"}) + end - 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"}) + 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 - 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### + begin + added_on_iot_platf = [] + if for_insert!=[] + ##Logic for IOT Platform connection### - credentials = mongo_client.get_iot_oceanconnect_credent(login) + credentials = mongo_client.get_iot_oceanconnect_credent(login) - if credentials[:code]==200 - for aaa in for_insert - begin - dev_name = aaa["imei"].to_s + if credentials[:code]==200 + for aaa in for_insert + begin + dev_name = aaa["imei"].to_s - #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"} - #from database - model_data = mongo_client.get_device_type_info_by_model(aaa["device_type"]) - resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id], - credentials[:body][:secret], - aaa["imei"], - dev_name, - aaa["description"], - model_data[:body]["device_type"], - aaa["profile"], - model_data[:body]["ManufacturerID"], - model_data[:body]["ManufacturerNAME"], - model_data[:body]["model"] - ) - if resss[:code]=="200" - s1 = aaa - s1[:huadata] = resss - s1[:created] = DateTime.now - added_on_iot_platf.append(s1) - else - not_processed_list.append({:imei => aaa["imei"], :error => resss}) + #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"} + #from database + model_data = mongo_client.get_device_type_info_by_model(aaa["device_type"]) + resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id], + credentials[:body][:secret], + aaa["imei"], + dev_name, + aaa["description"], + model_data[:body]["device_type"], + aaa["profile"], + model_data[:body]["ManufacturerID"], + model_data[:body]["ManufacturerNAME"], + model_data[:body]["model"] + ) + if resss[:code]=="200" + s1 = aaa + s1[:huadata] = resss + s1[:created] = DateTime.now + 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 - rescue - not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"}) 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 - #########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 + resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}} - - else - 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 - rescue - resp_out = {:code => 505, :result => "Error with database communication"} + + else + resp_out = {:code => 509, :result => "Input data invalid"} 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, real_ip) resp_out end @@ -243,51 +250,56 @@ #++ def device_find(login, imei_list) input_json = {:login => login, :imei_list => imei_list} ime_list_approved = [] ime_list_notapproved = [] - resp = {} + resp_out = {} begin thr2 = Thread.new do - for t in imei_list - prof_name1 = mongo_client.get_profile_name_from_imei(t) - if prof_name1[:code]==200 - begin - permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"]) - p "permiss1" - p permiss1 - if permiss1[:code]==200 - ime_list_approved.append(t) - else - ime_list_notapproved.append({:imei => t, :error => permiss1}) + if add_functions_connector.check_input_2(login, imei_list)[:code]==200 + + for t in imei_list + prof_name1 = mongo_client.get_profile_name_from_imei(t) + if prof_name1[:code]==200 + begin + permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"]) + p "permiss1" + p permiss1 + if permiss1[:code]==200 + ime_list_approved.append(t) + else + ime_list_notapproved.append({:imei => t, :error => permiss1}) + end + rescue + ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}}) end - rescue - ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}}) + else + ime_list_notapproved.append({:imei => t, :error => prof_name1}) end - else - ime_list_notapproved.append({:imei => t, :error => prof_name1}) end - end - begin - if ime_list_approved != [] - data = mongo_client.get_imei_info_from_db(ime_list_approved) + begin + if ime_list_approved != [] + data = mongo_client.get_imei_info_from_db(ime_list_approved) - resp = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}} + resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}} - else - resp = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}} + else + resp_out = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}} + end + rescue + resp_out = {:code => 504, :result => "Unsuccessfully data transfer"} end - rescue - resp = {:code => 504, :result => "Unsuccessfully data transfer"} + else + resp_out = {:code => 509, :result => "Input data invalid"} end end rescue - resp = {:code => 507, :result => "Unknown SDK error"} + resp_out = {:code => 507, :result => "Unknown SDK error"} end thr2.join - mongo_client.audit_logger("device_find", remote_ip, input_json, resp, real_ip) - resp + mongo_client.audit_logger("device_find", remote_ip, input_json, resp_out, real_ip) + resp_out end #!3 device modify, change imei #login #imei_old @@ -295,146 +307,149 @@ #massive commands #im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}] #++ def imei_replace(login, im_list) input_json = {:login => login, :imei_list => im_list} + resp_out = {} - li_new_imei = [] - list1 = {} + begin - #dictionary for imeis which not processed. Final dictionary - not_processed_list = [] + thr3 = Thread.new do - #dictionary for devices which was processed correctly - processed_list = [] + if add_functions_connector.check_input_3(login, imei_list)[:code]==200 - #array for translations from old imei to new - old_new_translation = {} + li_new_imei = [] + list1 = {} - approved_list = [] - resp_out = {} + #dictionary for imeis which not processed. Final dictionary + not_processed_list = [] - #old_imei_list for query to iot platform for data request - step1_approved_dict_old=[] + #dictionary for devices which was processed correctly + processed_list = [] - #form dictionary for processing - for pr1 in im_list - p "pr1" - p pr1 - li_new_imei.append(pr1["imei_new"]) - list1[pr1["imei_new"]]=pr1["imei_old"] - old_new_translation[pr1["imei_old"]]=pr1["imei_new"] - end + #array for translations from old imei to new + old_new_translation = {} - p list1 + approved_list = [] - begin - thr3 = Thread.new do + #old_imei_list for query to iot platform for data request + step1_approved_dict_old=[] - #check if imei_new exists in database. If exists - not process this imei - list_checked = mongo_client.check_imei_exists(li_new_imei) + #form dictionary for processing + for pr1 in im_list + li_new_imei.append(pr1["imei_new"]) + list1[pr1["imei_new"]]=pr1["imei_old"] + old_new_translation[pr1["imei_old"]]=pr1["imei_new"] + end - internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug") + #check if imei_new exists in database. If exists - not process this imei + list_checked = mongo_client.check_imei_exists(li_new_imei) - #add already exists new IMEI in error dictionary - for ss in list_checked[:body][:exists] - not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"}) - end + internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug") - #new_imei list which processed step1 - step2_list = list_checked[:body][:not_exists] + #add already exists new IMEI in error dictionary + for ss in list_checked[:body][:exists] + not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"}) + end - internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug") + #new_imei list which processed step1 + step2_list = list_checked[:body][:not_exists] + internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug") - for a in step2_list - begin - #step3 checking permission for writing for imei list - prof_name1 = mongo_client.get_profile_name_from_imei(list1[a]) + for a in step2_list + begin - internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug") + #step3 checking permission for writing for imei list + prof_name1 = mongo_client.get_profile_name_from_imei(list1[a]) - if prof_name1[:code]==200 - permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code] - internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug") - if permiss1==200 + internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug") - approved_list.append({:imei_old => list1[a], :imei_new => a}) - step1_approved_dict_old.append(list1[a]) + if prof_name1[:code]==200 + permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code] + internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug") + if permiss1==200 + approved_list.append({:imei_old => list1[a], :imei_new => a}) + step1_approved_dict_old.append(list1[a]) + + else + not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"}) + end else - not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"}) + not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"}) end - else - not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"}) + rescue + not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"}) end - rescue - not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"}) end - end - internal_func.printer_texter({:function => "imei_replace Step5", :not_processed_list => not_processed_list, :input => list1, :approved_list => approved_list, :step1_approved_dict_old => step1_approved_dict_old}, "debug") + internal_func.printer_texter({:function => "imei_replace Step5", :not_processed_list => not_processed_list, :input => list1, :approved_list => approved_list, :step1_approved_dict_old => step1_approved_dict_old}, "debug") - ##Logic for IOT Platform connection### + ##Logic for IOT Platform connection### - list_from_iot = self.get_info_by_imeilist_from_iot(login, step1_approved_dict_old) + list_from_iot = self.get_info_by_imeilist_from_iot(login, step1_approved_dict_old) - internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug") + internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug") - #processing data. modifying data on iot platform and mongoDB - if list_from_iot[:code]=="200" + #processing data. modifying data on iot platform and mongoDB + if list_from_iot[:code]=="200" - for ard in list_from_iot[:body]["devices"] - p ard - new_data_cur_dev = {} - mongo_answer = {} - current_old_dev = ard["deviceInfo"]["nodeId"] - current_device_id = ard["deviceId"] - new_data_cur_dev = ard["deviceInfo"] - new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s + for ard in list_from_iot[:body]["devices"] + p ard + new_data_cur_dev = {} + mongo_answer = {} + current_old_dev = ard["deviceInfo"]["nodeId"] + current_device_id = ard["deviceId"] + new_data_cur_dev = ard["deviceInfo"] + new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s - credentials = mongo_client.get_iot_oceanconnect_credent(login) + credentials = mongo_client.get_iot_oceanconnect_credent(login) - if credentials[:code]==200 - flag_remove=0 - flag_create=0 - remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id) - create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev) + if credentials[:code]==200 + flag_remove=0 + flag_create=0 + remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id) + create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev) - if remove_answer[:code]=="204" || remove_answer[:code]=="200" - flag_remove=1 - end - if create_answer[:code]=="200" - flag_create=1 - end - if flag_remove==1 && flag_create==1 - mongo_answer = mongo_client.device_modify_any_attr_mongo(current_old_dev.to_i, {:imei => old_new_translation[current_old_dev.to_i], :huadata => {:body => create_answer[:body]}, :updated => DateTime.now}) - processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]}) + if remove_answer[:code]=="204" || remove_answer[:code]=="200" + flag_remove=1 + end + if create_answer[:code]=="200" + flag_create=1 + end + if flag_remove==1 && flag_create==1 + mongo_answer = mongo_client.device_modify_any_attr_mongo(current_old_dev.to_i, {:imei => old_new_translation[current_old_dev.to_i], :huadata => {:body => create_answer[:body]}, :updated => DateTime.now}) + processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]}) + else + not_processed_list.append({:record => {:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]}, :error => "Failed for provisioning to IOT platform"}) + end + + internal_func.printer_texter({:function => "imei_replace Step7", :remove_answer => remove_answer, :create_answer => create_answer, :mongo_answer => mongo_answer, :description => "processing imei #{current_old_dev.to_s}"}, "debug") + else - not_processed_list.append({:record => {:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]}, :error => "Failed for provisioning to IOT platform"}) + approved_list=[] end + end - internal_func.printer_texter({:function => "imei_replace Step7", :remove_answer => remove_answer, :create_answer => create_answer, :mongo_answer => mongo_answer, :description => "processing imei #{current_old_dev.to_s}"}, "debug") + else + approved_list=[] + end - else - approved_list=[] - end + if approved_list!=[] + resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}} + else + resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}} end else - approved_list=[] + resp_out = {:code => 509, :result => "Input data invalid"} end - if approved_list!=[] - resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}} - else - resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}} - end - end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end @@ -460,54 +475,58 @@ 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 - ##Logic for IOT Platform connection### + if add_functions_connector.check_input_4(login, imei_list)[: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 - credentials = mongo_client.get_iot_oceanconnect_credent(login) - resp = {} + ##Logic for IOT Platform connection### - if credentials[:code]==200 + credentials = mongo_client.get_iot_oceanconnect_credent(login) + resp = {} - 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) + 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 = {:code => 500, :result => "Unknown IOT platform error", :body => ans} + resp_out = {:code => 404, :result => "Data not found"} end - else - resp_out = {:code => 404, :result => "Data not found"} - end - #########end iot platform logic####### + #########end iot platform logic####### - if resp[:code]==200 - resp_out = {:code => 200, :result => "Request completed successfully"} + if resp[:code]==200 + resp_out = {:code => 200, :result => "Request completed successfully"} + else + resp_out=resp + end else - resp_out=resp + resp_out = {:code => 400, :result => "IOT platform credentials not found"} end - else - resp_out = {:code => 400, :result => "IOT platform credentials not found"} + resp_out=permiss end - else - resp_out=permiss + resp_out=prof_name end else - resp_out=prof_name + resp_out = {:code => 509, :result => "Input data invalid"} end + end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end @@ -534,55 +553,54 @@ 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]) + if add_functions_connector.check_input_5(login, imei_list)[: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 + for p in newdevice_list + prof_name = mongo_client.get_profile_name_from_imei(p[:imei]) - ##Logic for IOT Platform connection### - credentials = mongo_client.get_iot_oceanconnect_credent(login) - resp = {} + if prof_name[:code]==200 + permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) + if permiss[:code]==200 - if credentials[:code]==200 - imei_data = mongo_client.get_imei_info_from_db([p[:imei]]) - if imei_data[:body]!=[] - ans =hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address]) + ##Logic for IOT Platform connection### + credentials = mongo_client.get_iot_oceanconnect_credent(login) + resp = {} - internal_func.printer_texter({:function => "device_add_address Step2", :ans => ans, :descrition=>"answer from hua IOT", :input=>{:did=>imei_data[:body][0]["huadata"]["body"]["deviceId"], :appid=>credentials[:body][:app_id], :secret=>credentials[:body][:secret], :address=>p[:address]}}, "debug") + if credentials[:code]==200 + imei_data = mongo_client.get_imei_info_from_db([p[:imei]]) + if imei_data[:body]!=[] + ans =hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address]) - p ans + internal_func.printer_texter({:function => "device_add_address Step2", :ans => ans, :descrition => "answer from hua IOT", :input => {:did => imei_data[:body][0]["huadata"]["body"]["deviceId"], :appid => credentials[:body][:app_id], :secret => credentials[:body][:secret], :address => p[:address]}}, "debug") + end + end - end + #########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 - else - 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}} + 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 else - resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}} + resp_out = {:code => 509, :result => "Input data invalid"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end @@ -606,70 +624,109 @@ input_json = {:login => login, :devices => device_list} begin - for g in device_list - p g + thr6 = Thread.new do - prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"]) - p prof_name1 + if add_functions_connector.check_input_5(login, imei_list)[:code]==200 - if prof_name1[:code]==200 - p prof_name1 - permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"]) - p "permiss1" - p permiss1 - if permiss1[:code]==200 + for g in device_list - if g["attributes"].key?("profile") - permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code] + prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"]) - if permiss2==200 + if prof_name1[:code]==200 + permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"]) + if permiss1[:code]==200 - attr = g["attributes"] - #mod_attr = {} + if g["attributes"].key?("profile") + permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code] - if attr.key?("profile") - if attr["profile"].is_a? Integer - p "Ok" + if permiss2==200 + + attr = g["attributes"] + #mod_attr = {} + + if attr.key?("profile") + if attr["profile"].is_a? Integer + p "Ok" + else + p new = mongo_client.get_profile_id_by_name(attr["profile"]) + attr["profile"] = new["profile_id"] + end + end + mongo_client.device_modify_any_attr_mongo(g["imei"], attr) + processed.append(g["imei"]) else - p new = mongo_client.get_profile_id_by_name(attr["profile"]) - attr["profile"] = new["profile_id"] + 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 - p attr - 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 => "Old profile permission error", :error => permiss1}) end else - attr = g["attributes"] - mongo_client.device_modify_any_attr_mongo(g["imei"], attr) - processed.append(g["imei"]) + not_processed.append({:imei => g["imei"], :error => prof_name1}) end - else - not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1}) - end + end + resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}} else - not_processed.append({:imei => g["imei"], :error => prof_name1}) + resp_out = {:code => 509, :result => "Input data invalid"} 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 + thr6.join mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip) resp_out end + #7 procedure for subscriber autorization + def autorize_subscriber(login, password) + input_json = {:login => login, :password => password} + resp_out = {} + thr7 = Thread.new do + begin + login_inform = mongo_client.get_login_info(login) + if login_inform[:code]==200 + pass_mongo = login_inform[:body]["password"] + if pass_mongo == password + resp_out = {:code => 200, :result => "Access granted"} + else + resp_out = {:code => 400, :result => "Access denied"} + end + else + resp_out = {:code => 401, :result => "Access denied. Incorrect login"} + end + rescue + resp_out = {:code => 507, :result => "Unknown SDK error"} + end + end + thr7.join + mongo_client.audit_logger("autorize_subscriber", remote_ip, input_json, resp_out, real_ip) + resp_out + end + + def get_available_profiles(login) + + end + + def get_available_types(login) + + end + + def answ_dev_query_format_process(dev_list) add_functions_connector.answ_dev_query_format_process(dev_list) end @@ -715,19 +772,19 @@ } mongo_client.imei_insert_model(model) end def test() - ddd = MongoIot.new(mongoip, mongoport, mongo_database) + #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_device_type_info_by_model("BGT_PPMC11") + #p ddd.get_device_type_info_by_model("BGT_PPMC11") + + #fff = HuaIot.new("134.17.93.4", "443", "/Users/imperituroard/Desktop/cert.crt", "/Users/imperituroard/Desktop/key.pem") + #p fff.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "eWllZpHgXgvgDAQlJjWs4QXaR94a", [91234]) end - - end -