lib/imperituroard/projects/iot.rb in imperituroard-0.5.9 vs lib/imperituroard/projects/iot.rb in imperituroard-1.0.0

- old
+ new

@@ -15,38 +15,48 @@ class IotFunctions_2 attr_accessor :mongoip, :mongoport, - :iotip, - :mongo_database, - :iotplatform_ip, - :iotplatform_port, + #: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 + #: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) + 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 + #@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) + #@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 @@ -55,11 +65,11 @@ #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) + 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 @@ -209,11 +219,11 @@ # for this function IOT platform not need # login # imei # imei_list =[41234,23452345,132412] #++ - def device_find_2(login, imei_list) + 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 @@ -272,11 +282,11 @@ #imei_old #imei_new #massive commands #im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}] #++ - def imei_replace_2(login, im_list) + def imei_replace_2(login, im_list, real_ip, remote_ip) input_json = {:login => login, :imei_list => im_list} resp_out = {} begin @@ -447,11 +457,11 @@ # not massive commands #imei=11341341234 #login="test" #+++ #IOT logic added - def device_remove_2(login, imei) + def device_remove_2(login, imei, real_ip, remote_ip) input_json = {:login => login, :imei_list => imei} resp_out = {} begin @@ -531,11 +541,11 @@ #imei = newdevice_list #address = newdevice_list #newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}] #+++ #iot platform integration completed - def device_add_address_2(login, newdevice_list) + 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" @@ -613,11 +623,11 @@ #profile #imsi #msisdn #newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}] #+ - def add_service_2(login, device_list) + def add_service_2(login, device_list, real_ip, remote_ip) resp_out = {} not_processed = [] processed = [] input_json = {:login => login, :devices => device_list} @@ -688,11 +698,11 @@ end #7 procedure for subscriber autorization # save data to mongodb - def autorize_subscriber_2(login, password) + 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) @@ -720,11 +730,11 @@ add_functions_connector.answ_dev_query_format_process(dev_list) end #8 get available profiles by login - def get_available_prof_2(login) + def get_available_prof_2(login, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin @@ -771,11 +781,11 @@ 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) + def get_available_types_2(login, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin @@ -828,11 +838,11 @@ resp_out end #10 get available device types by login and profile. procedure update - def get_available_types2_2(login, profile) + def get_available_types2_2(login, profile, real_ip, remote_ip) input_params = {:login => login} resp_out = {} begin @@ -885,11 +895,11 @@ resp_out end #11 get info for device type - def get_info_data_type_2(type) + def get_info_data_type_2(type, real_ip, remote_ip) input_params = {:type => type} output_answ = {} mon_answer = {} begin @@ -915,11 +925,11 @@ output_answ end #12 get info for profile - def get_info_data_profile_2(profile) + def get_info_data_profile_2(profile, real_ip, remote_ip) input_params = {:profile => profile} output_answ = {} mon_answer = {} begin @@ -944,10 +954,159 @@ 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"] + + 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) + if prof_name1[:code] == 200 + prof_new_id = mongo_client.get_profile_universal(curr_dev["new_profile"])["profile_id"] + + get_login_info = mongo_client.check_login_prof_perm_id_one(inp_login, prof_name1[:body]["profile_id"]) + if get_login_info[:code] == 200 + get_login_info_new = mongo_client.check_login_prof_perm_id_one(inp_login, prof_new_id) + if get_login_info_new[:code] == 200 + + cur_comp1 = mongo_client.compare_profiles(prof_name1[:body]["profile_id"], prof_new_id) + cur_comp2 = mongo_client.compare_device_types(prof_name1[:imei_info][:body][0]["device_type"], curr_dev["new_type"]) + + p cur_comp1 + 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) + ######### + if credentials_old[:code] == 200 && credentials_new[:code] == 200 + imei_data = mongo_client.get_imei_info_from_db([curr_dev["imei"].to_i]) + if imei_data[:body] != [] + ans = {} + if imei_data[:body][0]["huadata"]["body"]["deviceId"] != nil + + model_data = mongo_client.get_device_type_info_universal(curr_dev["new_type"]) + + #####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 + 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: {body: new_id_iot}, 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 + + #added_on_iot_platf.append(s1) + 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 + 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 + soap_answ = {:code=>200, :result=>"Success", :body=>{:processednum => processed_list.length, :failednum => not_processed_list.length, :deviceserr => err_li}} + else + soap_answ = {:code=>202, :result=>"Nothing done", :body=>{:processednum => 0, :failednum => not_processed_list.length, :deviceserr => err_li}} + end + + {:code => 200, :result => "Success", :body => {:to_soap_gw => soap_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 = [] @@ -996,13 +1155,13 @@ 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} + 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"]} + 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