lib/imperituroard/projects/iot.rb in imperituroard-1.0.4 vs lib/imperituroard/projects/iot.rb in imperituroard-1.0.5

- old
+ new

@@ -113,14 +113,13 @@ not_processed_list.append({:imei => list1[jj], :error => "Unknown error"}) end end end - begin added_on_iot_platf = [] - if for_insert!=[] + if for_insert != [] ##Logic for IOT Platform connection### for aaa in for_insert @@ -139,11 +138,11 @@ 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"]) + model_data = mongo_client.get_device_type_info_universal(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"], @@ -158,11 +157,10 @@ not_processed_list.append({:imei => aaa["imei"], :error => resss}) else s1 = aaa s1[:huadata] = resss s1[:created] = DateTime.now - s1[:type] = s1["device_type"] added_on_iot_platf.append(s1) end else not_processed_list.append({:imei => aaa["imei"], :error => resss}) end @@ -197,13 +195,11 @@ 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 @@ -219,20 +215,20 @@ 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 + 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 + 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 + if permiss1[:code] == 200 ime_list_approved.append(t) else ime_list_notapproved.append({:imei => t, :error => permiss1}) end rescue @@ -458,16 +454,16 @@ resp_out = {} begin thr4 = Thread.new do - if add_functions_connector.check_input_4(login, imei)[:code]==200 + 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 + if prof_name[:code] == 200 permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) - if permiss[:code]==200 + 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 @@ -477,21 +473,21 @@ ######### resp = {} - if credentials[:code]==200 + if credentials[:code] == 200 imei_data = mongo_client.get_imei_info_from_db([imei]) - if imei_data[:body]!=[] + 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" + 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 @@ -571,14 +567,12 @@ 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]) @@ -649,11 +643,11 @@ if attr.key?("profile") if attr["profile"].is_a? Integer p "Ok" else - p new = mongo_client.get_profile_id_by_name(attr["profile"]) + 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"]) @@ -734,10 +728,11 @@ 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"], @@ -838,37 +833,51 @@ 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"] - data_from_mongo = mongo_client.get_type_list_by_id(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"]}) + 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 - 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 } @@ -928,10 +937,12 @@ 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"] }}} @@ -941,11 +952,10 @@ 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 @@ -1000,11 +1010,11 @@ 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]["type"], type_new_pr) + cur_comp2 = mongo_client.compare_device_types(prof_name1[:imei_info][:body][0]["device_type"], type_new_pr) p cur_comp2 p "cur_comp2" @@ -1051,10 +1061,10 @@ 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, type: model_data[:body]["model"], huadata: {body: new_id_iot}, updated: now_date} + 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)