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

- old
+ new

@@ -8,10 +8,11 @@ require "imperituroard/projects/iot/hua_oceanconnect_adapter" require "imperituroard/projects/iot/add_functions" require "imperituroard/projects/iot/internal_functions" require 'json' require 'ipaddr' +require 'date' module Imperituroard class Error < StandardError; end @@ -96,11 +97,12 @@ :key_path, :mongo_client, :add_functions_connector, :real_ip, #real ip address of procedure caller :remote_ip, #ip address of balancer - :hua_aceanconnect_connector + :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 @@ -113,10 +115,11 @@ @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) + @internal_func = InternalFunc.new end #error list #:code => 507, :result => "Unknown SDK error" @@ -175,14 +178,29 @@ if credentials[:code]==200 for aaa in for_insert begin dev_name = aaa["imei"].to_s - resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id], credentials[:body][:secret], aaa["imei"], dev_name, aaa["description"], aaa["device_type"], aaa["profile"]) + + #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 @@ -280,63 +298,73 @@ def imei_replace(login, im_list) input_json = {:login => login, :imei_list => im_list} 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 = [] resp_out = {} + #old_imei_list for query to iot platform for data request + step1_approved_dict_old=[] + + #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 p list1 begin thr3 = Thread.new do + #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 - p "list1[a]" - p list1 - p a - p list1[a] + + #step3 checking permission for writing for imei list prof_name1 = mongo_client.get_profile_name_from_imei(list1[a]) - p prof_name1 - if prof_name1[:code]==200 + internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug") - p "if prof_name1[:code]==200" - + if prof_name1[:code]==200 permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code] - p "permiss1" - p permiss1 + internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug") if permiss1==200 - ##Logic for IOT Platform connection### - - remove_one_device_from_iot(app_id, secret, dev_id) - - #########end iot platform logic####### - - mongo_client.device_modify_any_attr_mongo(list1[a], {:imei => a}) - 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"}) @@ -344,19 +372,71 @@ 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" + + 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) + + 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 => approved_list, :unapproved_list => not_processed_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 => approved_list, :unapproved_list => not_processed_list}} + 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 thr3.join @@ -440,11 +520,12 @@ #!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(login, newdevice_list) #add_functions_connector.telegram_message(newdevice_list.to_s) p newdevice_list p "gas" p MyJSON.valid?(newdevice_list[0].to_s) @@ -463,11 +544,26 @@ p prof_name permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"]) if permiss[:code]==200 ##Logic for IOT Platform connection### + credentials = mongo_client.get_iot_oceanconnect_credent(login) + 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") + + p ans + end + + end + + #########end iot platform logic####### resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address]) if resp[:code]==200 @@ -540,25 +636,20 @@ p new = mongo_client.get_profile_id_by_name(attr["profile"]) attr["profile"] = new["profile_id"] end 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}) 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 @@ -584,54 +675,59 @@ def logger_to_audit_database(proc_name, src_ip, input_json, output_json, real_ip) mongo_client.audit_logger(proc_name, src_ip, input_json, output_json, real_ip) end + #additional procedure for checking status on iot platform + def get_info_by_imeilist_from_iot(login, imei_list) + resp_out={} + begin + dev_id_list = [] + resss = {} + data_from_db = mongo_client.get_imei_info_from_db(imei_list) + p data_from_db + for g in data_from_db[:body] + dev_id_list.append(g["huadata"]["body"]["deviceId"]) + end + credentials = mongo_client.get_iot_oceanconnect_credent(login) + if credentials[:code]==200 + p apid = credentials[:body][:app_id] + p secre = credentials[:body][:secret] + resp_out = hua_aceanconnect_connector.quer_dev_query_list(apid, secre, dev_id_list) + end + 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 + + #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 test() 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_iot_oceanconnect_credent("test") + p ddd.get_device_type_info_by_model("BGT_PPMC11") end - def testhua() - cert_file = cert_path - key_file = key_path - ddd1 = HuaIot.new(iotplatform_ip, iotplatform_port, cert_file, key_file) - #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") - #ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "a8834c5e-4b4d-4f0f-ad87-14e916f3d0bb") - #ddd1.querying_device_activ_status("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") - #ddd1.querying_device_info("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "a8834c5e-4b4d-4f0f-ad87-14e916f3d0bb") - list = ["41c0ba82-d771-4669-b766-fcbfbedc17f4", "7521234165452", "feb9c4d1-4944-4b04-a717-df87dfde30f7", "9868e121-c309-4f4f-8ab3-0aa69072caff", "b3b82f35-0723-4a83-90af-d4ea40017194"] - #p ddd1.querying_device_direct_conn("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", list) - #p ddd1.querying_device_type_list("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa") - # ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452") - #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "45324523452345") - - p devtestid = ddd1.dev_register_passw_code_mode2("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "24235455", "test", "test", "phone", "0") - p ddd1.quer_dev_direct_conn_batches("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", [devtestid[:body]["deviceId"], "4c763ea1-be51-4eff-81db-863bc04791bb"]) - - #p ddd1.querying_device_info("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "c6e0dfb1-8042-472a-a35d-e984f118d2a6") - p "delete start" - p ddd1.dev_delete("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", devtestid[:body]["deviceId"]) - #p ddd1.dev_delete("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "2881a9cc-cb2a-4b59-8fac-de3aace2324d") - end - - - def testhua2() - tt = "{\"deviceId\":\"fad0a417-b6a3-4b0b-abfc-fa2b0af9691a\",\"verifyCode\":\"6cb6dcca\",\"timeout\":180,\"psk\":\"1d16b55d577bc1f2e5e75d416ce6b8a2\"}" - #tt = tt.gsub("\\","") - #p tt - ff = tt.to_s - p ff - gg = JSON.parse(ff) - p gg - end end