$LOAD_PATH.unshift File.expand_path("../projects/iot", __dir__) require 'imperituroard/projects/iot/mongoconnector' require 'imperituroard/projects/iot/hua_oceanconnect_adapter' require 'imperituroard/projects/iot/add_functions' require 'imperituroard/projects/iot/internal_functions' require "imperituroard/projects/wttx/phpipamdb" require "imperituroard/projects/wttx/phpipamcps" require 'json' require 'ipaddr' require 'date' require 'net/http' require 'uri' class IotFunctions_2 attr_accessor :mongoip, :mongoport, #:iotip, #:mongo_database, #:iotplatform_ip, #:iotplatform_port, :cert_path, :key_path, :mongo_client, :add_functions_connector, #:real_ip, #real ip address of procedure caller #:remote_ip, #ip address of balancer :hua_aceanconnect_connector, :internal_func def initialize(mongoip, mongoport, iotip, mongo_database, 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, mongoip, mongoport, mongo_database) @internal_func = InternalFunc.new end #!!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, "device_type"=>"phone"}, #{"imei" => 56213126347645784, "description" => "dfdsf", "note"=>"second description", "profile"=>0}] #massive commands #+++ #iot logic added def add_device_to_prof_2(login, imei_list, real_ip, remote_ip) 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 = [] for ii in imei_list valid_resp = internal_func.imei_validate(ii["imei"]) if valid_resp[:checked] list1[ii["imei"]] = ii imei.append(ii["imei"]) else not_processed_list.append({:imei => ii["imei"], :error => valid_resp[:result]}) end end if imei != [] 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"}) p ss p "ss" p list_checked 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"}) end rescue not_processed_list.append({:imei => list1[jj], :error => "Unknown error"}) end end end begin added_on_iot_platf = [] if for_insert != [] ##Logic for IOT Platform connection### for aaa in for_insert #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) credentials = mongo_client.get_iot_oceanconn_credent_2(aaa["profile"]) ######### if aaa.key?("type") && !aaa.key?("device_type") aaa["device_type"] = aaa["type"] aaa = internal_func.delete_key_hash(aaa, "type") end aaa = internal_func.hash_val_to_string(aaa) if credentials[:code] == 200 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_universal(aaa["device_type"]) profile_data = mongo_client.get_profile_universal(aaa["profile"]) p profile_data p model_data p "model_data" 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" if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200 not_processed_list.append({:imei => aaa["imei"], :error => resss}) else s1 = aaa s1[:huadata] = resss s1[:created] = DateTime.now s1["device_type"] = model_data[:body]["model"] s1["profile"] = profile_data["profile_id"] added_on_iot_platf.append(s1) end 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 iot platform logic####### else resss_err = {:code => 400, :result => "IOT platform credentials not found"} not_processed_list.append({:imei => aaa["imei"], :error => resss_err}) end end if added_on_iot_platf.length != 0 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 => 202, :result => "Empty list. Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}} end 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 else resp_out = {:code => 509, :result => "Input data invalid"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr1.join resp_out[:body][:tosoapgw] = internal_func.iot_create_dev_soapgw_answer(input_json, resp_out) mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip, {}) resp_out end def add_device_to_prof2_2(request_full, real_ip, remote_ip) #{"autorization"=>{"login"=>"test", "token"=>"tE3aFvs27"}, "devlist"=>[{"devices"=>{"imei"=>123419520034999, "type"=>"1000002", "profile"=>0}}]} login = request_full["autorization"]["login"] im_li =[] for sd in request_full["devlist"] im_li.append(sd["devices"]) end imei_list = im_li input_json = {:request_full => request_full} input_json22 = {: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 = [] for ii in imei_list valid_resp = internal_func.imei_validate(ii["imei"]) if valid_resp[:checked] list1[ii["imei"]] = ii imei.append(ii["imei"]) else not_processed_list.append({:imei => ii["imei"], :error => valid_resp[:result]}) end end if imei != [] 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"}) p ss p "ss" p list_checked 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"}) end rescue not_processed_list.append({:imei => list1[jj], :error => "Unknown error"}) end end end begin added_on_iot_platf = [] if for_insert != [] ##Logic for IOT Platform connection### for aaa in for_insert #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) credentials = mongo_client.get_iot_oceanconn_credent_2(aaa["profile"]) ######### if aaa.key?("type") && !aaa.key?("device_type") aaa["device_type"] = aaa["type"] aaa = internal_func.delete_key_hash(aaa, "type") end aaa = internal_func.hash_val_to_string(aaa) if credentials[:code] == 200 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_universal(aaa["device_type"]) profile_data = mongo_client.get_profile_universal(aaa["profile"]) p profile_data p model_data p "model_data" 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" if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200 not_processed_list.append({:imei => aaa["imei"], :error => resss}) else s1 = aaa s1[:huadata] = resss s1[:created] = DateTime.now s1["device_type"] = model_data[:body]["model"] s1["profile"] = profile_data["profile_id"] added_on_iot_platf.append(s1) end 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 iot platform logic####### else resss_err = {:code => 400, :result => "IOT platform credentials not found"} not_processed_list.append({:imei => aaa["imei"], :error => resss_err}) end end if added_on_iot_platf.length != 0 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 => 202, :result => "Empty list. Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}} end 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 else resp_out = {:code => 509, :result => "Input data invalid"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr1.join resp_out[:body][:tosoapgw] = internal_func.iot_create_dev_soapgw_answer(input_json22, 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) # procedure for data selection from mongo database. # for this function IOT platform not need # login # imei # imei_list =[41234,23452345,132412] #++ def device_find_2(login, imei_list, real_ip, remote_ip) input_json = {:login => login, :imei_list => imei_list} ime_list_approved = [] ime_list_notapproved = [] resp_out = {} begin thr2 = Thread.new do 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 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) resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :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 else resp_out = {:code => 509, :result => "Input data invalid"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr2.join resp_out[:data][:tosoapgw] = add_functions_connector.iot_query_dev_soapgw_answer(input_json, resp_out) 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 #imei_new #massive commands #im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}] #++ def imei_replace_2(login, im_list, real_ip, remote_ip) input_json = {:login => login, :imei_list => im_list} resp_out = {} begin thr3 = Thread.new do if add_functions_connector.check_input_3(login, im_list)[:code]==200 li_new_imei = [] list1 = {} #dictionary for imeis which not processed. Final dictionary not_processed_list = [] #dictionary for devices which was processed correctly processed_list = [] #array for translations from old imei to new old_new_translation = {} approved_list = [] #old_imei_list for query to iot platform for data request step1_approved_dict_old=[] #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 #check if imei_new exists in database. If exists - not process this imei list_checked = mongo_client.check_imei_exists(li_new_imei) internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug") #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 #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]) internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug") 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 not exists in database"}) 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") ##Logic for IOT Platform connection### 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") #processing data. modifying data on iot platform and mongoDB if list_from_iot[:code]=="200" && list_from_iot[:body]["devices"] != nil 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 p list_from_iot p "list_from_iot" #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) info_from_mongo = mongo_client.get_imei_info_from_db([current_old_dev.to_i]) credentials = mongo_client.get_iot_oceanconn_credent_2(info_from_mongo[:body][0]["profile"]) ######### 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]}) 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 approved_list=[] end 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 resp_out = {:code => 509, :result => "Input data invalid"} end end rescue resp_out = {:code => 507, :result => "Unknown SDK error"} end thr3.join mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out, real_ip, {}) resp_out end #!!4 remove device #login #imei # not massive commands #imei=11341341234 #login="test" #+++ #IOT logic added def device_remove_2(login, imei, real_ip, remote_ip) input_json = {:login => login, :imei_list => imei} resp_out = {} begin thr4 = Thread.new do if add_functions_connector.check_input_4(login, imei)[: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### #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) credentials = mongo_client.get_iot_oceanconn_credent_2(prof_name[:body]["profile"]) ######### resp = {} if credentials[:code] == 200 imei_data = mongo_client.get_imei_info_from_db([imei]) if imei_data[:body] != [] ans = {} if imei_data[:body][0]["huadata"]["body"]["deviceId"] != nil ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"]) else ans = {:code => "204", :result => "Not processed by iot platform. deviceId not found in mongoDB"} end 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=permiss end else resp_out=prof_name end else resp_out = {:code => 509, :result => "Input data invalid"} 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, real_ip, {}) resp_out end #!5 add address to device #login #imei = newdevice_list #address = newdevice_list #newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}] #+++ #iot platform integration completed def device_add_address_2(login, newdevice_list, real_ip, remote_ip) #add_functions_connector.telegram_message(newdevice_list.to_s) p newdevice_list p "gas" p MyJSON.valid?(newdevice_list[0].to_s) p "sdfsdfgs" input_json = {:login => login, :devices => newdevice_list} resp_out = {} not_processed = [] processed = [] begin thr5 = Thread.new do if add_functions_connector.check_input_5(login, newdevice_list)[:code]==200 for p in newdevice_list prof_name = mongo_client.get_profile_name_from_imei(p[: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### #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) credentials = mongo_client.get_iot_oceanconn_credent_2(prof_name[:body]["profile"]) ######### resp = {} 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]) 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 iot platform logic####### 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 => prof_name}) 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 else resp_out = {:code => 509, :result => "Input data invalid"} 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, real_ip, {}) resp_out end #6 add service by SPA #imei #profile #imsi #msisdn #newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}] #+ def add_service_2(login, device_list, real_ip, remote_ip) resp_out = {} not_processed = [] processed = [] input_json = {:login => login, :devices => device_list} begin thr6 = Thread.new do if add_functions_connector.check_input_5(login, device_list)[:code]==200 for g in device_list prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"]) if prof_name1[:code]==200 permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"]) if permiss1[:code]==200 if g["attributes"].key?("profile") permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code] 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_universal(attr["profile"]) attr["profile"] = new["profile_id"] end end 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}) 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}) end else 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}} else resp_out = {:code => 509, :result => "Input data invalid"} end end 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 # save data to mongodb def autorize_subscriber_2(login, password, real_ip, remote_ip) 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 answ_dev_query_format_process(dev_list) add_functions_connector.answ_dev_query_format_process(dev_list) end #8 get available profiles by login def get_available_prof_2(login, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin data_processed = [] data_soapgw = [] login_info = mongo_client.get_login_inf2_select(login) if login_info[:code]==200 permit_profiles = login_info[:body]["permit_profiles"] permit_types = login_info[:body]["permit_types"] data_from_mongo = mongo_client.get_profile_list_by_id(permit_profiles) p data_from_mongo for ff in data_from_mongo data_processed.append({:profile_id => ff["profile_id"], :description => ff["description"], :note => ff["note"], :form => ff["form"], :profile => ff["profile"], :info => ff["info"] }) data_soapgw.append({:id => ff["profile_id"], :form => ff["form"], :description => ff["description"], :profile => ff["profile"]}) end tosoapgw = {:code => 200, :result => "Request completed successfully", :profiles => data_soapgw } resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}} else tosoapgw = {:code => login_info[:code], :result => login_info[:result], :profiles => data_soapgw } resp_out = {:code => login_info[:code], :result => login_info[:result], :body => {:tosoapgw => tosoapgw}} end rescue tosoapgw = {:code => 507, :result => "Unknown SDK error", :profiles => data_soapgw } resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}} end mongo_client.audit_logger("get_available_prof_2", remote_ip, input_params, resp_out, real_ip, {}) resp_out end #9 get available device types by login def get_available_types_2(login, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin data_processed = [] data_soapgw = [] login_info = mongo_client.get_login_inf2_select(login) if login_info[:code]==200 permit_profiles = login_info[:body]["permit_profiles"] permit_types = login_info[:body]["permit_types"] data_from_mongo = mongo_client.get_type_list_by_id(permit_types) for ff in data_from_mongo data_processed.append({:type_id => ff["type_id"], :description => ff["description"], :note => ff["note"], :form => ff["form"], :ManufacturerID => ff["ManufacturerID"], :ManufacturerNAME => ff["ManufacturerNAME"], :device_type => ff["device_type"], :model => ff["model"], :info => ff["info"] }) data_soapgw.append({:name => ff["model"], :form => ff["form"], :description => ff["description"], :type_id => ff["type_id"]}) end tosoapgw = {:code => 200, :result => "Request completed successfully", :types => data_soapgw } resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}} else tosoapgw = {:code => login_info[:code], :result => login_info[:result], :types => data_soapgw } resp_out = {:code => login_info[:code], :result => login_info[:result], :body => {:tosoapgw => tosoapgw}} end rescue tosoapgw = {:code => 507, :result => "Unknown SDK error", :types => data_soapgw } resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}} end mongo_client.audit_logger("get_available_types_2", remote_ip, input_params, resp_out, real_ip, {}) resp_out end #10 get available device types by login and profile. procedure update def get_available_types2_2(login, profile, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin data_processed = [] data_soapgw = [] login_info = mongo_client.get_login_inf2_select(login) prof_data = mongo_client.get_profile_universal(profile) if login_info[:code]==200 permit_profiles = login_info[:body]["permit_profiles"] permit_types = login_info[:body]["permit_types"] perm_tp_prof = prof_data["permit_types"] if permit_profiles.include?(prof_data["profile_id"]) res_types_permitted = internal_func.compare_dict(permit_types, perm_tp_prof) data_from_mongo = mongo_client.get_type_list_by_id(res_types_permitted) for ff in data_from_mongo data_processed.append({:type_id => ff["type_id"], :description => ff["description"], :note => ff["note"], :form => ff["form"], :ManufacturerID => ff["ManufacturerID"], :ManufacturerNAME => ff["ManufacturerNAME"], :device_type => ff["device_type"], :model => ff["model"], :info => ff["info"] }) data_soapgw.append({:name => ff["model"], :form => ff["form"], :description => ff["description"], :type_id => ff["type_id"]}) end tosoapgw = {:code => 200, :result => "Request completed successfully", :types => data_soapgw } resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}} else tosoapgw = {:code => 700, :result => "Profile not permitted", :types => data_soapgw } resp_out = {:code => 700, :result => "Profile not permitted", :body => {:tosoapgw => tosoapgw}} end else tosoapgw = {:code => login_info[:code], :result => login_info[:result], :types => data_soapgw } resp_out = {:code => login_info[:code], :result => login_info[:result], :body => {:tosoapgw => tosoapgw}} end rescue tosoapgw = {:code => 507, :result => "Unknown SDK error", :types => data_soapgw } resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}} end mongo_client.audit_logger("get_available_types_2", remote_ip, input_params, resp_out, real_ip, {}) resp_out end #11 get info for device type def get_info_data_type_2(type, real_ip, remote_ip) input_params = {:type => type} output_answ = {} mon_answer = {} begin #if type.is_a? String if internal_func.if_digit_or_string(type)[:body][:string] mon_answer = mongo_client.get_type_by_name(type) else mon_answer = mongo_client.get_type_by_id(type) end if mon_answer != {} && mon_answer != [] output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"], :model => mon_answer["model"], :type_id => mon_answer["type_id"] }}} else output_answ = {:code => 505, :result => "Unknown SDK error"} end rescue output_answ = {:code => 507, :result => "Unknown SDK error"} end mongo_client.audit_logger("get_info_data_type_2", remote_ip, input_params, output_answ, real_ip, {}) output_answ end #12 get info for profile def get_info_data_profile_2(profile, real_ip, remote_ip) input_params = {:profile => profile} output_answ = {} mon_answer = {} begin if internal_func.if_digit_or_string(profile)[:body][:string] mon_answer = mongo_client.get_profile_id_by_name(profile) else mon_answer = mongo_client.get_profile_name_by_id(profile) end p mon_answer if mon_answer != {} && mon_answer != [] output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"], :profile => mon_answer["profile"], :profile_id => mon_answer["profile_id"] }}} else output_answ = {:code => 505, :result => "Unknown SDK error"} end rescue output_answ = {:code => 507, :result => "Unknown SDK error"} end mongo_client.audit_logger("get_info_data_profile_2", remote_ip, input_params, output_answ, real_ip, {}) output_answ end #set_data_replace #13 # {"autorization"=>{"login"=>"test", "token"=>"token"}, "datalist"=>[{"imei"=>"524523654366", "new_profile"=>"3", "new_type"=>"1000001"}, {"imei"=>"53623413423", "new_profile"=>"1", "new_type"=>"1000003"}]} # answer = {:code=>200, :result=>"Success", :body=>{:processednum=>1, :failednum=>1, :deviceserr=>[{:failedimei=>524523654366}]}} def set_data_replace_2(soapgw_params, real_ip, remote_ip) input_params = {:soapgw_params => soapgw_params, :real_ip => real_ip, :remote_ip => remote_ip} output_answ = {} failes_list = [] answ_to_soapgw = {} not_processed_list = [] processed_list = [] inp_login = soapgw_params["autorization"]["login"] inp_dev_list = soapgw_params["datalist"] thr13set = Thread.new do inp_dev_list.each do |curr_dev| begin if curr_dev["imei"] != [] && curr_dev["imei"] != nil && curr_dev["imei"] != "" list_checked = mongo_client.check_imei_exists([curr_dev["imei"].to_i]) if list_checked[:code] == 200 && list_checked[:body][:exists] != [] && list_checked[:body][:not_exists] == [] #step1 checking permission for writing for imei list prof_name1 = mongo_client.get_profile_name_from_imei(curr_dev["imei"].to_i) p prof_name1 p "prof_name1" if prof_name1[:code] == 200 if curr_dev["new_profile"] != "nil" && curr_dev["new_profile"] != "null" && curr_dev["new_profile"] != "empty" && curr_dev["new_profile"] != "nill" prof_new_id = mongo_client.get_profile_universal(curr_dev["new_profile"])["profile_id"] else prof_new_id = prof_name1[:body]["profile_id"] end if curr_dev["new_type"] != "nil" && curr_dev["new_type"] != "null" && curr_dev["new_type"] != "empty" && curr_dev["new_type"] != "nill" type_new_pr = curr_dev["new_type"] else type_new_pr = prof_name1[:imei_info][:body][0]["device_type"] end p type_new_pr p prof_new_id p "st111111" get_login_info = mongo_client.check_login_prof_perm_id_one(inp_login, prof_name1[:body]["profile_id"]) p get_login_info p "get_login_info" if get_login_info[:code] == 200 get_login_info_new = mongo_client.check_login_prof_perm_id_one(inp_login, prof_new_id) p get_login_info_new p "get_login_info_new" if get_login_info_new[:code] == 200 cur_comp1 = mongo_client.compare_profiles(prof_name1[:body]["profile_id"], prof_new_id) p cur_comp1 p "cur_comp1" cur_comp2 = mongo_client.compare_device_types(prof_name1[:imei_info][:body][0]["device_type"], type_new_pr) p cur_comp2 p "cur_comp2" if cur_comp1[:is_the_same] && cur_comp2[:is_the_same] #the same. Nothing to do processed_list.append(curr_dev) ######## else credentials_old = mongo_client.get_iot_oceanconn_credent_2(prof_name1[:body]["profile"]) credentials_new = mongo_client.get_iot_oceanconn_credent_2(prof_new_id) p credentials_new p "credentials_new" ######### if credentials_old[:code] == 200 && credentials_new[:code] == 200 imei_data = mongo_client.get_imei_info_from_db([curr_dev["imei"].to_i]) p imei_data p "imei_data" if imei_data[:body] != [] ans = {} if imei_data[:body][0]["huadata"]["body"]["deviceId"] != nil model_data = mongo_client.get_device_type_info_universal(type_new_pr) p model_data p "model_data" #####all checks completed. then process data##### ans_old = hua_aceanconnect_connector.remove_one_device_from_iot(credentials_old[:body][:app_id], credentials_old[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"]) if ans_old[:code].to_i == 200 || ans_old[:code].to_i == 202 || ans_old[:code].to_i == 204 begin resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials_new[:body][:app_id], credentials_new[:body][:secret], curr_dev["imei"].to_i, curr_dev["imei"].to_s, prof_name1[:body]["description"], model_data[:body]["device_type"], curr_dev["new_profile"], model_data[:body]["ManufacturerID"], model_data[:body]["ManufacturerNAME"], model_data[:body]["model"] ) if resss[:code] == "200" if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200 not_processed_list.append({:imei => curr_dev["imei"], :error => resss}) else p resss new_id_iot = resss[:body] new_profile_id = prof_new_id now_date = DateTime.now #finished success. update database attribute = {profile: new_profile_id, device_type: model_data[:body]["model"], huadata: resss, updated: now_date} answ_mongo = mongo_client.modify_attr_mongo_universal(curr_dev["imei"].to_i, attribute) if answ_mongo[:code] != 200 not_processed_list.append({:imei => curr_dev["imei"], :error => "Critical error. Mongo not updated", :mongo_err => answ_mongo}) else processed_list.append(curr_dev) end end else not_processed_list.append({:imei => curr_dev["imei"], :error => resss}) end rescue not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown error with insertion imei on IOT platform"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Something wrong with IOT platform"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Not processed by iot platform. deviceId not found in mongoDB"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Failed to get info from database"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Failed to get credentials from database"}) end end else not_processed_list.append({:imei => curr_dev["imei"], :error => "New profile modification not permitted"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Old profile modification not permitted"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown current profile"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "Device not exists"}) end else not_processed_list.append({:imei => curr_dev["imei"], :error => "IMEI can't be nil"}) end rescue not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown SDK error"}) end end err_li = [] if not_processed_list.length > 0 not_processed_list.each do |err_l| err_li.append({:failedimei => err_l[:imei]}) end end if processed_list.length > 0 answ_to_soapgw = {:code => 200, :result => "Success", :body => {:processednum => processed_list.length, :failednum => not_processed_list.length, :deviceserr => err_li}} else answ_to_soapgw = {:code => 202, :result => "Nothing done", :body => {:processednum => 0, :failednum => not_processed_list.length, :deviceserr => err_li}} end end thr13set.join output_answ = {:code => 200, :result => "Success", :body => {:to_soap_gw => answ_to_soapgw, :not_processed_list => not_processed_list, :processed_list => processed_list}} mongo_client.audit_logger("set_data_replace_2", remote_ip, input_params, output_answ, real_ip, {}) output_answ end #14 #additional procedure for checking status on iot platform def get_info_by_imeilist_from_iot(login, imei_list) resp_out={} begin dev_id_list = [] for_process_list = {} resss = {} data_from_db = mongo_client.get_imei_info_from_db(imei_list) p data_from_db profile = 0 for g in data_from_db[:body] p g p "ggggg" dev_id_list.append(g["huadata"]["body"]["deviceId"]) if for_process_list[g["profile"]] == nil for_process_list[g["profile"]] = [g["huadata"]["body"]["deviceId"]] else for_process_list[g["profile"]].append(g["huadata"]["body"]["deviceId"]) end if g["profile"] != nil && g["profile"] != "" profile = g["profile"] end end p dev_id_list p "dev_id_list" p for_process_list for ard in for_process_list ##Logic for IOT Platform connection### #########change 2020.05.11. imperituroard #########replace process for get iot app_id and password from users to profile #credentials = mongo_client.get_iot_oceanconnect_credent(login) credentials = mongo_client.get_iot_oceanconn_credent_2(ard[0]) ######### if credentials[:code] == 200 resp_out[:code] = "200" resp_out[:result] = "Processed" p apid = credentials[:body][:app_id] p secre = credentials[:body][:secret] resp_out_1 = hua_aceanconnect_connector.quer_dev_query_list(apid, secre, ard[1]) p resp_out_1 p "resp_out_1" p resp_out[:body] p "resp_out[:body][\"devices\"]" if resp_out[:body] == nil || resp_out[:body] == {} || resp_out[:body] == {"devices" => nil} p "step1111" p resp_out_1[:body]["devices"] resp_out[:body] = {'devices' => resp_out_1[:body]["devices"]} p "step22222" else if resp_out_1[:body]["devices"] != nil resp_out[:body]["devices"] = resp_out[:body]["devices"] + resp_out_1[:body]["devices"] end end end end p resp_out p "resp_out" rescue resp_out = {:code => "500", :message => "get_info_by_imeilist_from_iot: Something wrong", :body => {"devices" => []}} end internal_func.printer_texter(resp_out, "debug") resp_out end ################################################ ############technology procedures############### ################################################ def delete_all_devices_in_database imei_list = [] all_imei = mongo_client.get_all_imei_from_db all_imei[:body].each do |immm| imei_list.append(immm["imei"]) end imei_list.each do |del_imei| p del_imei self.device_remove_2("test", del_imei, "localhost", "localhost") end end #for internal use. Add new device model def add_model_to_mongo(model, manufacture_id, manufacture_name, device_type, description, note) model = { model: model, ManufacturerID: manufacture_id, ManufacturerNAME: manufacture_name, device_type: device_type, description: description, note: note, created: DateTime.now } mongo_client.imei_insert_model(model) end def test1278493 #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 "yyyyy" #fff = HuaIot.new("134.17.93.4", "443", "/Users/imperituroard/Desktop/cert.crt", "/Users/imperituroard/Desktop/key.pem") p hua_aceanconnect_connector.querying_device_id("Jy5KpoBUOScZEcrO2bRZhzfaIOUa", "VFFWTOb9k3_cQhzrGsRWT4HE8Z0a", "123456789876543") end end