lib/imperituroard.rb in imperituroard-0.2.2 vs lib/imperituroard.rb in imperituroard-0.2.3
- old
+ new
@@ -21,11 +21,11 @@
p jjj
end
# Your code goes here...
end
-module Phpipam
+module PhpipamModule
def test(ggg)
p ggg
end
end
@@ -79,11 +79,11 @@
else
"failed"
end
end
- include Phpipam
+ include PhpipamModule
end
class Iot
attr_accessor :mongoip,
:mongoport,
@@ -129,49 +129,55 @@
input_json = {:login => login, :imei_list => imei_list}
resp_out = {}
begin
thr1 = Thread.new do
- imei = []
- list1 = {}
- for_insert = []
- not_processed_list = []
- processed_list = []
+ imei = []
+ list1 = {}
+ for_insert = []
+ not_processed_list = []
+ processed_list = []
- for ii in imei_list
- list1[ii["imei"]] = ii
- imei.append(ii["imei"])
- end
- 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"})
- 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"})
+ for ii in imei_list
+ list1[ii["imei"]] = ii
+ imei.append(ii["imei"])
end
- rescue
- not_processed_list.append({:imei=>list1[jj], :error => "Unknown error"})
+ 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"})
+ 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
- if for_insert!=[]
- mongo_client.imei_insert_list(for_insert)
- resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
- else
- resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
+ begin
+ if for_insert!=[]
+ ##Logic for IOT Platform connection###
+ #########end iot platform logic#######
+
+ mongo_client.imei_insert_list(for_insert)
+ resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
+ else
+ resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
+
+ end
+ rescue
+ resp_out = {:code => 505, :result => "Error with database communication"}
end
- rescue
- resp_out = {:code => 505, :result => "Error with database communication"}
- end
end
rescue
resp_out = {:code => 507, :result => "Unknown SDK error"}
end
thr1.join
@@ -230,140 +236,293 @@
thr2.join
mongo_client.audit_logger("device_find", remote_ip, input_json, resp)
resp
end
- #3 device modify, change imei
+ #!3 device modify, change imei
#login
#imei_old
#imei_new
#massive commands
- #im_list = [{:imei_old=>7967843245667, :imei_new=>7967843245665}]
- #+
- def imei_replace(login, im_list)
+ #im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}]
+ #++
+ def imei_replace(login, im_list, remote_ip)
input_json = {:login => login, :imei_list => im_list}
+
+ li_new_imei = []
+ list1 = {}
+ not_processed_list = []
+ approved_list = []
+ resp_out = {}
+
+ for pr1 in im_list
+ p "pr1"
+ p pr1
+ li_new_imei.append(pr1["imei_new"])
+ list1[pr1["imei_new"]]=pr1["imei_old"]
+ end
+
+ p list1
+
begin
- for a in im_list
- p a
- prof_name1 = mongo_client.get_profile_name_from_imei(a[:imei_old])
- p prof_name1
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
- if permiss1==200
- mongo_client.device_modify_any_attr_mongo(a[:imei_old], {:imei => a[:imei_new]})
+
+ thr3 = Thread.new do
+
+ list_checked = mongo_client.check_imei_exists(li_new_imei)
+ 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
+
+ step2_list = list_checked[:body][:not_exists]
+
+ for a in step2_list
+ begin
+ p "list1[a]"
+ p list1
+ p a
+ p list1[a]
+ prof_name1 = mongo_client.get_profile_name_from_imei(list1[a])
+
+ p prof_name1
+ if prof_name1[:code]==200
+
+ p "if prof_name1[:code]==200"
+
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code]
+ p "permiss1"
+ p permiss1
+ if permiss1==200
+
+ ##Logic for IOT Platform connection###
+
+ #########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})
+
+
+ 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
+
+ if approved_list!=[]
+ resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => approved_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}}
+ end
+
end
+
+
rescue
- {:code => 507, :result => "Unknown SDK error"}
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
end
- mongo_client.audit_logger("add_device_to_profile", "127.0.0.1", input_json, "resp")
+ thr3.join
+ mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out)
+ resp_out
+
end
- #4 remove device
+ #!4 remove device
#login
#imei
# not massive commands
#imei=11341341234
#login="test"
- #+
- def device_remove(login, imei)
+ #++
+ def device_remove(login, imei, remote_ip)
input_json = {:login => login, :imei_list => imei}
- resp = {}
+ resp_out = {}
begin
-
+ thr4 = Thread.new do
prof_name = mongo_client.get_profile_name_from_imei(imei)
- permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
- if permiss==200
- resp = mongo_client.device_remove_single_mongo(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###
+
+ #########end iot platform logic#######
+
+ resp = mongo_client.device_remove_single_mongo(imei)
+
+ if resp[:code]==200
+ resp_out = {:code => 200, :result => "Request completed successfully"}
+ else
+ resp_out=resp
+ end
+ else
+ resp_out=permiss
+ end
+ else
+ resp_out=prof_name
end
+ end
+
rescue
- {:code => 507, :result => "Unknown SDK error"}
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
end
- mongo_client.audit_logger("device_remove", "127.0.0.1", input_json, "resp")
- resp
+ thr4.join
+ mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out)
+ resp_out
end
- #5 add address to device
+ #!5 add address to device
#login
#imei = newdevice_list
#address = newdevice_list
#newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}]
- #+
- def device_add_address(login, newdevice_list)
- add_functions_connector.telegram_message(newdevice_list.to_s)
+ #++
+ def device_add_address(login, newdevice_list, 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
- resp = {}
+ thr5 = Thread.new do
for p in newdevice_list
prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
- p "prof_name"
- p prof_name
- permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
- if permiss==200
- resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
+ if prof_name[:code]==200
+ p "prof_name"
+ p prof_name
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
+ if permiss[:code]==200
+
+ ##Logic for IOT Platform connection###
+
+ #########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
- return resp
+
+ 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
+ end
rescue
- {:code => 507, :result => "Unknown SDK error"}
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
end
- mongo_client.audit_logger("device_remove", "127.0.0.1", input_json, "resp")
+ thr5.join
+ mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out)
+ 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(login, device_list)
+ def add_service(login, device_list, remote_ip)
+ resp_out = {}
+ not_processed = []
+ processed = []
+
+
input_json = {:login => login, :devices => device_list}
begin
for g in device_list
+ p g
+
prof_name1 = mongo_client.get_profile_name_from_imei(g[:imei])
p prof_name1
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
- p permiss1
- permiss2 = mongo_client.check_login_profile_permiss(login, g[:attributes][:profile])[:code]
- if permiss1==200 && permiss2==200
- attr = g[:attributes]
- #mod_attr = {}
+ if prof_name1[:code]==200
+ p prof_name1
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
+ p "permiss1"
+ p permiss1
+ if permiss1[:code]==200
- if attr.key?(:profile)
- if attr[:profile].is_a? Integer
- p "Ok"
+ 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_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
- p new = mongo_client.get_profile_id_by_name(attr[:profile])
- attr[:profile] = new["profile_id"]
+ 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
- p attr
- mongo_client.device_modify_any_attr_mongo(g[:imei], attr)
+ 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}}
+
rescue
- {:code => 507, :result => "Unknown SDK error"}
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
end
-
- mongo_client.audit_logger("device_remove", "127.0.0.1", input_json, "resp")
-
+ mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out)
+ resp_out
end
def test()
ddd = MongoIot.new(mongoip, mongoport, mongo_database)