lib/imperituroard/projects/iot.rb in imperituroard-1.1.0 vs lib/imperituroard/projects/iot.rb in imperituroard-1.1.1
- old
+ new
@@ -207,15 +207,175 @@
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)
+ 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}
+ 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
+
+
#!!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
@@ -270,11 +430,11 @@
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)
+ mongo_client.audit_logger("device_find", remote_ip, input_json, resp_out, real_ip, {})
resp_out
end
#!3 device modify, change imei
@@ -442,11 +602,11 @@
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)
+ mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out, real_ip, {})
resp_out
end
@@ -529,11 +689,11 @@
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)
+ mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip, {})
resp_out
end
#!5 add address to device
@@ -609,11 +769,11 @@
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)
+ mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out, real_ip, {})
resp_out
end
#6 add service by SPA
@@ -687,11 +847,11 @@
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)
+ mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip, {})
resp_out
end
#7 procedure for subscriber autorization
@@ -715,11 +875,11 @@
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)
+ 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)
@@ -773,11 +933,11 @@
: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)
+ 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)
@@ -829,11 +989,11 @@
: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)
+ 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
@@ -900,11 +1060,11 @@
: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)
+ 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
@@ -930,11 +1090,11 @@
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)
+ 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
@@ -961,11 +1121,11 @@
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)
+ mongo_client.audit_logger("get_info_data_profile_2", remote_ip, input_params, output_answ, real_ip, {})
output_answ
end
#set_data_replace
@@ -1137,11 +1297,11 @@
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)
+ mongo_client.audit_logger("set_data_replace_2", remote_ip, input_params, output_answ, real_ip, {})
output_answ
end
#14
@@ -1217,9 +1377,34 @@
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 = {