imperituroard 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9145a7e26b0478b84af6d549588cd403cf52080d
4
- data.tar.gz: fbdc0528f7ff7e535eaa29a2ad3eba67216163be
3
+ metadata.gz: 1ad44807fb4cff12695580d243f54083f75abda4
4
+ data.tar.gz: d075e7afb11370fe50d8837282016313da1713df
5
5
  SHA512:
6
- metadata.gz: 2db737b82e9d6127b475ea6c6e472d86c82857a253fb8e16e5de2c804fdded8681a4764d1c6735994c783e81da413370d95b300a498094fc98276792a623544e
7
- data.tar.gz: 8e93980bda5a7d66317611c4291c62b4f094d318dd45f404196f7c5219c6fec88ae2637f00f59f0c5cc62747a56543a67d355b977209ba68ab80352b2b853e83
6
+ metadata.gz: b5a77c1263b5f9058143127c5daa69cf93c2c83c38c8d9c5ad5ef3d2f7bb8ea6ed12cb2e01651c5531068de6519f6fa775a8486ea6dbef4806534062576aa8aa
7
+ data.tar.gz: 6d8fcb8c02ece03e5e73ae3564fe81874e9da0f322622aa2a8aef7776b2d6800f964314b1a9e1ecd381e73934e8535b6b377c1f2d43ed7e970c1480b08ab6b5a
Binary file
@@ -10,7 +10,6 @@ class AdditionalFunc
10
10
  def initialize(telegram_api_url, telegram_chat_id)
11
11
  @telegram_api_url = telegram_api_url
12
12
  @telegram_chat_id = telegram_chat_id
13
-
14
13
  end
15
14
 
16
15
  #procedure for send log to telegram chat
@@ -33,7 +32,6 @@ class AdditionalFunc
33
32
  end
34
33
  end
35
34
 
36
-
37
35
  def answ_dev_query_format_process(dev_list)
38
36
  dataaa_ok = []
39
37
  dataaa_failed = []
@@ -115,5 +113,28 @@ class AdditionalFunc
115
113
 
116
114
  end
117
115
 
116
+ def check_input_1(login, imei_list)
117
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
118
+ end
119
+
120
+ def check_input_2(login, imei_list)
121
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
122
+ end
123
+
124
+ def check_input_3(login, imei_list)
125
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
126
+ end
127
+
128
+ def check_input_4(login, imei_list)
129
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
130
+ end
131
+
132
+ def check_input_5(login, imei_list)
133
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
134
+ end
135
+
136
+ def check_input_6(login, imei_list)
137
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
138
+ end
118
139
 
119
140
  end
@@ -4,9 +4,24 @@ require 'date'
4
4
 
5
5
  class InternalFunc
6
6
 
7
+ def datetimenow()
8
+ d_curr = DateTime.now
9
+ time_zone = 'Europe/Minsk'
10
+ DateTime.new
11
+ .in_time_zone(time_zone)
12
+ .change(year: d_curr.year, month: d_curr.month, day: d_curr.day, hour: d_curr.hour, min: d_curr.min, sec: d_curr.sec)
13
+ end
14
+
15
+ def check_pass_format(passw)
16
+ if passw!=nil && passw!=""
17
+ {:code=>200, :result=>"Request completed successfully", :body=>"Data checked"}
18
+ else
19
+ {:code=>500, :result=>"Request completed", :body=>"invalid password"}
20
+ end
21
+ end
7
22
 
8
23
  def printer_texter(text, log_level)
9
- mess = {:datetime => DateTime.now, :sdk=> "imperituroard", :sdk_version=> "0.3.3", :message => text}
24
+ mess = {:datetime => datetimenow, :sdk=> "imperituroard", :sdk_version=> "0.3.3", :message => text}
10
25
  p mess
11
26
  end
12
27
 
@@ -23,7 +23,7 @@ class MongoIot
23
23
  def audit_logger(proc_name, src_ip, input_json, output_json, real_ip)
24
24
  out_resp = {}
25
25
  begin
26
- current = DateTime.now
26
+ current = internal_func.datetimenow
27
27
  collection = client[:audit]
28
28
  doc = {
29
29
  :proc_name => proc_name,
@@ -67,7 +67,6 @@ class MongoIot
67
67
  else
68
68
  out_resp = {:code => 404, :result => "get_profiles_by_login: Access denied. Incorrect login"}
69
69
  end
70
-
71
70
  rescue
72
71
  out_resp = {:code => 507, :result => "get_profiles_by_login: Unknown SDK error"}
73
72
  end
@@ -75,6 +74,38 @@ class MongoIot
75
74
  out_resp
76
75
  end
77
76
 
77
+
78
+ def get_login_info(login)
79
+ out_resp = {}
80
+ begin
81
+ login_profiles = []
82
+ req2 = []
83
+ result_ps = []
84
+ collection = client[:users]
85
+ collection2 = client[:device_profiles]
86
+ collection.find({:login => login}).each {|row|
87
+ login_profiles = row
88
+ }
89
+ p "login_profiles"
90
+ p login_profiles
91
+ if login_profiles != [] && login_profiles["login"] != nil && login_profiles["login"] != ""
92
+ passw_check = internal_func.check_pass_format(login_profiles["password"])
93
+ if passw_check[:code]==200
94
+ out_resp = {:code => 200, :result => "get_login_info: Request completed successfully", :body => login_profiles}
95
+ else
96
+ out_resp = {:code => 405, :result => "get_login_info: incorrect password"}
97
+ end
98
+ else
99
+ out_resp = {:code => 404, :result => "get_login_info: login not found in database"}
100
+ end
101
+ rescue
102
+ out_resp = {:code => 507, :result => "get_login_info: Unknown SDK error"}
103
+ end
104
+ p out_resp
105
+ internal_func.printer_texter(out_resp, "debug")
106
+ out_resp
107
+ end
108
+
78
109
  def get_imei_info_from_db(imeilist)
79
110
  out_resp = {}
80
111
  p imeilist
@@ -125,24 +156,13 @@ class MongoIot
125
156
 
126
157
  def check_login_profile_permiss(login, profile)
127
158
  out_resp = {}
128
- p "profile"
129
- p profile
130
159
  get_login_info = get_profiles_by_login(login)
131
- p "get_login_info"
132
- p get_login_info
133
-
134
160
  if get_login_info[:code]==200
135
-
136
-
137
161
  dst_profile = get_profile_id_by_name(profile)
138
- p "dst_profile"
139
- p dst_profile
140
162
  access=1
141
163
  if get_login_info[:body]!=[]
142
164
  if dst_profile!=[]
143
- p "sgsgsd"
144
165
  for j in get_login_info[:body]
145
- p j
146
166
  if j["profile_id"].to_i==dst_profile["profile_id"].to_i
147
167
  access=0
148
168
  end
@@ -168,16 +188,10 @@ class MongoIot
168
188
 
169
189
  def check_login_prof_perm_id_one(login, profile_id)
170
190
  out_resp = {}
171
- p "profile"
172
- p profile_id
173
191
  get_login_info = get_profiles_by_login(login)
174
- p "get_login_info"
175
- p get_login_info
176
192
  access=1
177
193
  if get_login_info[:body]!=[]
178
- p "sgsgsd"
179
194
  for j in get_login_info[:body]
180
- p j
181
195
  if j["profile_id"].to_i==profile_id.to_i
182
196
  access=0
183
197
  end
@@ -198,19 +212,10 @@ class MongoIot
198
212
  out_resp = {}
199
213
  res_exists = []
200
214
  imei_list_res = get_imei_info_from_db(imei_list)
201
- p imei_list_res
202
- p "imei_list"
203
215
  for k in imei_list_res[:body]
204
- p k
205
216
  res_exists.append(k["imei"])
206
217
  end
207
- p "aaaa"
208
- p imei_list
209
- p res_exists
210
218
  not_ex = imei_list - res_exists
211
- p "not_ex"
212
- p not_ex
213
- p res_exists
214
219
  out_resp = {:code => 200, :result => "check_imei_exists: Request completed successfully",
215
220
  :body => {:exists => res_exists, :not_exists => not_ex}}
216
221
  internal_func.printer_texter(out_resp, "debug")
@@ -221,7 +226,6 @@ class MongoIot
221
226
  def imei_insert_list(imei_list)
222
227
  begin
223
228
  collection = client[:device_imei]
224
- p imei_list
225
229
  for l in imei_list
226
230
  doc = {
227
231
  imei: l,
@@ -245,7 +249,6 @@ class MongoIot
245
249
  def imei_insert_list2(imei_list)
246
250
  begin
247
251
  collection = client[:device_imei]
248
- p imei_list
249
252
  for l in imei_list
250
253
  doc = {
251
254
  imei: l,
@@ -283,17 +286,10 @@ class MongoIot
283
286
  id = ""
284
287
  begin
285
288
  info = get_imei_info_from_db([imei])
286
- p info
287
- p "info"
288
289
  if info[:body]==[]
289
290
  out_resp = {:code => 505, :result => "get_profile_name_from_imei: get_imei_info_from_db returned empty list from database. IMEIS not found"}
290
291
  else
291
- p "fshhsdf"
292
- p info
293
292
  id = info[:body][0]["profile"]
294
- p id
295
- p "id"
296
-
297
293
  begin
298
294
  p id
299
295
  res = get_profile_name_by_id(id)
@@ -1,3 +1,3 @@
1
1
  module Imperituroard
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
data/lib/imperituroard.rb CHANGED
@@ -139,96 +139,103 @@ class Iot
139
139
  input_json = {:login => login, :imei_list => imei_list}
140
140
  resp_out = {}
141
141
  begin
142
+
142
143
  thr1 = Thread.new do
144
+ if add_functions_connector.check_input_1(login, imei_list)[:code]==200
143
145
 
144
- imei = []
145
- list1 = {}
146
- for_insert = []
147
- not_processed_list = []
148
- processed_list = []
146
+ imei = []
147
+ list1 = {}
148
+ for_insert = []
149
+ not_processed_list = []
150
+ processed_list = []
149
151
 
150
- for ii in imei_list
151
- list1[ii["imei"]] = ii
152
- imei.append(ii["imei"])
153
- end
154
- list_checked = mongo_client.check_imei_exists(imei)
155
- for ss in list_checked[:body][:exists]
156
- not_processed_list.append({:imei => ss, :error => "Device exists in database"})
157
- end
152
+ for ii in imei_list
153
+ list1[ii["imei"]] = ii
154
+ imei.append(ii["imei"])
155
+ end
156
+ list_checked = mongo_client.check_imei_exists(imei)
157
+ for ss in list_checked[:body][:exists]
158
+ not_processed_list.append({:imei => ss, :error => "Device exists in database"})
159
+ end
158
160
 
159
- for jj in list_checked[:body][:not_exists]
160
- begin
161
- get_login_info = mongo_client.check_login_prof_perm_id_one(login, list1[jj]["profile"])[:code]
162
- if get_login_info==200
163
- for_insert.append(list1[jj])
164
- else
165
- not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"})
161
+ for jj in list_checked[:body][:not_exists]
162
+ begin
163
+ get_login_info = mongo_client.check_login_prof_perm_id_one(login, list1[jj]["profile"])[:code]
164
+ if get_login_info==200
165
+ for_insert.append(list1[jj])
166
+ else
167
+ not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"})
168
+ end
169
+ rescue
170
+ not_processed_list.append({:imei => list1[jj], :error => "Unknown error"})
166
171
  end
167
- rescue
168
- not_processed_list.append({:imei => list1[jj], :error => "Unknown error"})
169
172
  end
170
- end
171
173
 
172
- begin
173
- added_on_iot_platf = []
174
- if for_insert!=[]
175
- ##Logic for IOT Platform connection###
176
-
177
- credentials = mongo_client.get_iot_oceanconnect_credent(login)
178
-
179
- if credentials[:code]==200
180
- for aaa in for_insert
181
- begin
182
- dev_name = aaa["imei"].to_s
183
-
184
- #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"}
185
- #from database
186
- model_data = mongo_client.get_device_type_info_by_model(aaa["device_type"])
187
- resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id],
188
- credentials[:body][:secret],
189
- aaa["imei"],
190
- dev_name,
191
- aaa["description"],
192
- model_data[:body]["device_type"],
193
- aaa["profile"],
194
- model_data[:body]["ManufacturerID"],
195
- model_data[:body]["ManufacturerNAME"],
196
- model_data[:body]["model"]
197
- )
198
- if resss[:code]=="200"
199
- s1 = aaa
200
- s1[:huadata] = resss
201
- s1[:created] = DateTime.now
202
- added_on_iot_platf.append(s1)
203
- else
204
- not_processed_list.append({:imei => aaa["imei"], :error => resss})
174
+ begin
175
+ added_on_iot_platf = []
176
+ if for_insert!=[]
177
+ ##Logic for IOT Platform connection###
178
+
179
+ credentials = mongo_client.get_iot_oceanconnect_credent(login)
180
+
181
+ if credentials[:code]==200
182
+ for aaa in for_insert
183
+ begin
184
+ dev_name = aaa["imei"].to_s
185
+
186
+ #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"}
187
+ #from database
188
+ model_data = mongo_client.get_device_type_info_by_model(aaa["device_type"])
189
+ resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id],
190
+ credentials[:body][:secret],
191
+ aaa["imei"],
192
+ dev_name,
193
+ aaa["description"],
194
+ model_data[:body]["device_type"],
195
+ aaa["profile"],
196
+ model_data[:body]["ManufacturerID"],
197
+ model_data[:body]["ManufacturerNAME"],
198
+ model_data[:body]["model"]
199
+ )
200
+ if resss[:code]=="200"
201
+ s1 = aaa
202
+ s1[:huadata] = resss
203
+ s1[:created] = DateTime.now
204
+ added_on_iot_platf.append(s1)
205
+ else
206
+ not_processed_list.append({:imei => aaa["imei"], :error => resss})
207
+ end
208
+ rescue
209
+ not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"})
205
210
  end
206
- rescue
207
- not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"})
208
211
  end
209
- end
210
212
 
211
- #########end iot platform logic#######
213
+ #########end iot platform logic#######
212
214
 
213
- mongo_client.imei_insert_list(added_on_iot_platf)
214
- resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
215
- else
216
- resp_out = {:code => 400, :result => "IOT platform credentials not found"}
217
- end
215
+ mongo_client.imei_insert_list(added_on_iot_platf)
216
+ resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
217
+ else
218
+ resp_out = {:code => 400, :result => "IOT platform credentials not found"}
219
+ end
218
220
 
219
221
 
220
- else
221
- resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
222
+ else
223
+ resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
222
224
 
225
+ end
226
+ rescue
227
+ resp_out = {:code => 505, :result => "Error with database communication"}
223
228
  end
224
- rescue
225
- resp_out = {:code => 505, :result => "Error with database communication"}
229
+
230
+ else
231
+ resp_out = {:code => 509, :result => "Input data invalid"}
226
232
  end
227
233
  end
228
234
  rescue
229
235
  resp_out = {:code => 507, :result => "Unknown SDK error"}
230
236
  end
231
237
  thr1.join
238
+
232
239
  mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip)
233
240
  resp_out
234
241
  end
@@ -245,47 +252,52 @@ class Iot
245
252
  input_json = {:login => login, :imei_list => imei_list}
246
253
  ime_list_approved = []
247
254
  ime_list_notapproved = []
248
- resp = {}
255
+ resp_out = {}
249
256
  begin
250
257
  thr2 = Thread.new do
251
- for t in imei_list
252
- prof_name1 = mongo_client.get_profile_name_from_imei(t)
253
- if prof_name1[:code]==200
254
- begin
255
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
256
- p "permiss1"
257
- p permiss1
258
- if permiss1[:code]==200
259
- ime_list_approved.append(t)
260
- else
261
- ime_list_notapproved.append({:imei => t, :error => permiss1})
258
+ if add_functions_connector.check_input_2(login, imei_list)[:code]==200
259
+
260
+ for t in imei_list
261
+ prof_name1 = mongo_client.get_profile_name_from_imei(t)
262
+ if prof_name1[:code]==200
263
+ begin
264
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
265
+ p "permiss1"
266
+ p permiss1
267
+ if permiss1[:code]==200
268
+ ime_list_approved.append(t)
269
+ else
270
+ ime_list_notapproved.append({:imei => t, :error => permiss1})
271
+ end
272
+ rescue
273
+ ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}})
262
274
  end
263
- rescue
264
- ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}})
275
+ else
276
+ ime_list_notapproved.append({:imei => t, :error => prof_name1})
265
277
  end
266
- else
267
- ime_list_notapproved.append({:imei => t, :error => prof_name1})
268
278
  end
269
- end
270
- begin
271
- if ime_list_approved != []
272
- data = mongo_client.get_imei_info_from_db(ime_list_approved)
279
+ begin
280
+ if ime_list_approved != []
281
+ data = mongo_client.get_imei_info_from_db(ime_list_approved)
273
282
 
274
- resp = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}}
283
+ resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}}
275
284
 
276
- else
277
- resp = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}}
285
+ else
286
+ resp_out = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}}
287
+ end
288
+ rescue
289
+ resp_out = {:code => 504, :result => "Unsuccessfully data transfer"}
278
290
  end
279
- rescue
280
- resp = {:code => 504, :result => "Unsuccessfully data transfer"}
291
+ else
292
+ resp_out = {:code => 509, :result => "Input data invalid"}
281
293
  end
282
294
  end
283
295
  rescue
284
- resp = {:code => 507, :result => "Unknown SDK error"}
296
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
285
297
  end
286
298
  thr2.join
287
- mongo_client.audit_logger("device_find", remote_ip, input_json, resp, real_ip)
288
- resp
299
+ mongo_client.audit_logger("device_find", remote_ip, input_json, resp_out, real_ip)
300
+ resp_out
289
301
  end
290
302
 
291
303
  #!3 device modify, change imei
@@ -297,140 +309,143 @@ class Iot
297
309
  #++
298
310
  def imei_replace(login, im_list)
299
311
  input_json = {:login => login, :imei_list => im_list}
312
+ resp_out = {}
300
313
 
301
- li_new_imei = []
302
- list1 = {}
314
+ begin
303
315
 
304
- #dictionary for imeis which not processed. Final dictionary
305
- not_processed_list = []
316
+ thr3 = Thread.new do
306
317
 
307
- #dictionary for devices which was processed correctly
308
- processed_list = []
318
+ if add_functions_connector.check_input_3(login, imei_list)[:code]==200
309
319
 
310
- #array for translations from old imei to new
311
- old_new_translation = {}
320
+ li_new_imei = []
321
+ list1 = {}
312
322
 
313
- approved_list = []
314
- resp_out = {}
323
+ #dictionary for imeis which not processed. Final dictionary
324
+ not_processed_list = []
315
325
 
316
- #old_imei_list for query to iot platform for data request
317
- step1_approved_dict_old=[]
326
+ #dictionary for devices which was processed correctly
327
+ processed_list = []
318
328
 
319
- #form dictionary for processing
320
- for pr1 in im_list
321
- p "pr1"
322
- p pr1
323
- li_new_imei.append(pr1["imei_new"])
324
- list1[pr1["imei_new"]]=pr1["imei_old"]
325
- old_new_translation[pr1["imei_old"]]=pr1["imei_new"]
326
- end
329
+ #array for translations from old imei to new
330
+ old_new_translation = {}
327
331
 
328
- p list1
332
+ approved_list = []
329
333
 
330
- begin
331
334
 
332
- thr3 = Thread.new do
335
+ #old_imei_list for query to iot platform for data request
336
+ step1_approved_dict_old=[]
333
337
 
334
- #check if imei_new exists in database. If exists - not process this imei
335
- list_checked = mongo_client.check_imei_exists(li_new_imei)
338
+ #form dictionary for processing
339
+ for pr1 in im_list
340
+ li_new_imei.append(pr1["imei_new"])
341
+ list1[pr1["imei_new"]]=pr1["imei_old"]
342
+ old_new_translation[pr1["imei_old"]]=pr1["imei_new"]
343
+ end
336
344
 
337
- internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug")
345
+ #check if imei_new exists in database. If exists - not process this imei
346
+ list_checked = mongo_client.check_imei_exists(li_new_imei)
338
347
 
339
- #add already exists new IMEI in error dictionary
340
- for ss in list_checked[:body][:exists]
341
- not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"})
342
- end
348
+ internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug")
343
349
 
344
- #new_imei list which processed step1
345
- step2_list = list_checked[:body][:not_exists]
350
+ #add already exists new IMEI in error dictionary
351
+ for ss in list_checked[:body][:exists]
352
+ not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"})
353
+ end
346
354
 
347
- internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug")
355
+ #new_imei list which processed step1
356
+ step2_list = list_checked[:body][:not_exists]
348
357
 
358
+ internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug")
349
359
 
350
- for a in step2_list
351
- begin
352
360
 
353
- #step3 checking permission for writing for imei list
354
- prof_name1 = mongo_client.get_profile_name_from_imei(list1[a])
361
+ for a in step2_list
362
+ begin
355
363
 
356
- internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug")
364
+ #step3 checking permission for writing for imei list
365
+ prof_name1 = mongo_client.get_profile_name_from_imei(list1[a])
357
366
 
358
- if prof_name1[:code]==200
359
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code]
360
- internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug")
361
- if permiss1==200
367
+ internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug")
362
368
 
363
- approved_list.append({:imei_old => list1[a], :imei_new => a})
364
- step1_approved_dict_old.append(list1[a])
369
+ if prof_name1[:code]==200
370
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code]
371
+ internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug")
372
+ if permiss1==200
365
373
 
374
+ approved_list.append({:imei_old => list1[a], :imei_new => a})
375
+ step1_approved_dict_old.append(list1[a])
376
+
377
+ else
378
+ not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"})
379
+ end
366
380
  else
367
- not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"})
381
+ not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"})
368
382
  end
369
- else
370
- not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"})
383
+ rescue
384
+ not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"})
371
385
  end
372
- rescue
373
- not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"})
374
386
  end
375
- end
376
387
 
377
- 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")
388
+ 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")
378
389
 
379
390
 
380
- ##Logic for IOT Platform connection###
391
+ ##Logic for IOT Platform connection###
381
392
 
382
- list_from_iot = self.get_info_by_imeilist_from_iot(login, step1_approved_dict_old)
393
+ list_from_iot = self.get_info_by_imeilist_from_iot(login, step1_approved_dict_old)
383
394
 
384
- internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug")
395
+ internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug")
385
396
 
386
- #processing data. modifying data on iot platform and mongoDB
387
- if list_from_iot[:code]=="200"
397
+ #processing data. modifying data on iot platform and mongoDB
398
+ if list_from_iot[:code]=="200"
388
399
 
389
- for ard in list_from_iot[:body]["devices"]
390
- p ard
391
- new_data_cur_dev = {}
392
- mongo_answer = {}
393
- current_old_dev = ard["deviceInfo"]["nodeId"]
394
- current_device_id = ard["deviceId"]
395
- new_data_cur_dev = ard["deviceInfo"]
396
- new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s
400
+ for ard in list_from_iot[:body]["devices"]
401
+ p ard
402
+ new_data_cur_dev = {}
403
+ mongo_answer = {}
404
+ current_old_dev = ard["deviceInfo"]["nodeId"]
405
+ current_device_id = ard["deviceId"]
406
+ new_data_cur_dev = ard["deviceInfo"]
407
+ new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s
397
408
 
398
- credentials = mongo_client.get_iot_oceanconnect_credent(login)
409
+ credentials = mongo_client.get_iot_oceanconnect_credent(login)
399
410
 
400
- if credentials[:code]==200
401
- flag_remove=0
402
- flag_create=0
403
- remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id)
404
- create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev)
411
+ if credentials[:code]==200
412
+ flag_remove=0
413
+ flag_create=0
414
+ remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id)
415
+ create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev)
405
416
 
406
- if remove_answer[:code]=="204" || remove_answer[:code]=="200"
407
- flag_remove=1
408
- end
409
- if create_answer[:code]=="200"
410
- flag_create=1
411
- end
412
- if flag_remove==1 && flag_create==1
413
- 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})
414
- processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]})
415
- else
416
- 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"})
417
- end
417
+ if remove_answer[:code]=="204" || remove_answer[:code]=="200"
418
+ flag_remove=1
419
+ end
420
+ if create_answer[:code]=="200"
421
+ flag_create=1
422
+ end
423
+ if flag_remove==1 && flag_create==1
424
+ 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})
425
+ processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]})
426
+ else
427
+ 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"})
428
+ end
418
429
 
419
- 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")
430
+ 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")
420
431
 
421
- else
422
- approved_list=[]
432
+ else
433
+ approved_list=[]
434
+ end
423
435
  end
436
+
437
+ else
438
+ approved_list=[]
424
439
  end
425
440
 
426
- else
427
- approved_list=[]
428
- end
441
+ if approved_list!=[]
442
+ resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
443
+ else
444
+ resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
445
+ end
429
446
 
430
- if approved_list!=[]
431
- resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
432
447
  else
433
- resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
448
+ resp_out = {:code => 509, :result => "Input data invalid"}
434
449
  end
435
450
 
436
451
  end
@@ -462,50 +477,54 @@ class Iot
462
477
 
463
478
  begin
464
479
  thr4 = Thread.new do
465
- prof_name = mongo_client.get_profile_name_from_imei(imei)
466
- if prof_name[:code]==200
467
- permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
468
- if permiss[:code]==200
469
480
 
470
- ##Logic for IOT Platform connection###
481
+ if add_functions_connector.check_input_4(login, imei_list)[:code]==200
471
482
 
483
+ prof_name = mongo_client.get_profile_name_from_imei(imei)
484
+ if prof_name[:code]==200
485
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
486
+ if permiss[:code]==200
487
+
488
+ ##Logic for IOT Platform connection###
472
489
 
473
- credentials = mongo_client.get_iot_oceanconnect_credent(login)
474
- resp = {}
490
+ credentials = mongo_client.get_iot_oceanconnect_credent(login)
491
+ resp = {}
475
492
 
476
- if credentials[:code]==200
493
+ if credentials[:code]==200
477
494
 
478
- imei_data = mongo_client.get_imei_info_from_db([imei])
479
- if imei_data[:body]!=[]
480
- ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"])
481
- p ans
482
- if ans[:code]=="204" or ans[:code]=="200"
483
- resp = mongo_client.device_remove_single_mongo(imei)
495
+ imei_data = mongo_client.get_imei_info_from_db([imei])
496
+ if imei_data[:body]!=[]
497
+ ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"])
498
+ p ans
499
+ if ans[:code]=="204" or ans[:code]=="200"
500
+ resp = mongo_client.device_remove_single_mongo(imei)
501
+ else
502
+ resp = {:code => 500, :result => "Unknown IOT platform error", :body => ans}
503
+ end
484
504
  else
485
- resp = {:code => 500, :result => "Unknown IOT platform error", :body => ans}
505
+ resp_out = {:code => 404, :result => "Data not found"}
486
506
  end
487
- else
488
- resp_out = {:code => 404, :result => "Data not found"}
489
- end
490
507
 
491
- #########end iot platform logic#######
508
+ #########end iot platform logic#######
492
509
 
493
- if resp[:code]==200
494
- resp_out = {:code => 200, :result => "Request completed successfully"}
510
+ if resp[:code]==200
511
+ resp_out = {:code => 200, :result => "Request completed successfully"}
512
+ else
513
+ resp_out=resp
514
+ end
495
515
  else
496
- resp_out=resp
516
+ resp_out = {:code => 400, :result => "IOT platform credentials not found"}
497
517
  end
498
-
499
518
  else
500
- resp_out = {:code => 400, :result => "IOT platform credentials not found"}
519
+ resp_out=permiss
501
520
  end
502
-
503
521
  else
504
- resp_out=permiss
522
+ resp_out=prof_name
505
523
  end
506
524
  else
507
- resp_out=prof_name
525
+ resp_out = {:code => 509, :result => "Input data invalid"}
508
526
  end
527
+
509
528
  end
510
529
 
511
530
  rescue
@@ -536,51 +555,50 @@ class Iot
536
555
  processed = []
537
556
  begin
538
557
  thr5 = Thread.new do
539
- for p in newdevice_list
540
- prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
541
-
542
- if prof_name[:code]==200
543
- p "prof_name"
544
- p prof_name
545
- permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
546
- if permiss[:code]==200
547
-
548
- ##Logic for IOT Platform connection###
549
- credentials = mongo_client.get_iot_oceanconnect_credent(login)
550
- resp = {}
558
+ if add_functions_connector.check_input_5(login, imei_list)[:code]==200
551
559
 
552
- if credentials[:code]==200
553
- imei_data = mongo_client.get_imei_info_from_db([p[:imei]])
554
- if imei_data[:body]!=[]
555
- ans =hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address])
560
+ for p in newdevice_list
561
+ prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
556
562
 
557
- 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")
563
+ if prof_name[:code]==200
564
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
565
+ if permiss[:code]==200
558
566
 
559
- p ans
560
- end
567
+ ##Logic for IOT Platform connection###
568
+ credentials = mongo_client.get_iot_oceanconnect_credent(login)
569
+ resp = {}
561
570
 
562
- end
571
+ if credentials[:code]==200
572
+ imei_data = mongo_client.get_imei_info_from_db([p[:imei]])
573
+ if imei_data[:body]!=[]
574
+ ans =hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address])
563
575
 
576
+ 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")
577
+ end
564
578
 
565
- #########end iot platform logic#######
579
+ end
566
580
 
581
+ #########end iot platform logic#######
567
582
 
568
- resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
569
- if resp[:code]==200
570
- processed.append({:imei => p[:imei]})
583
+ resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
584
+ if resp[:code]==200
585
+ processed.append({:imei => p[:imei]})
586
+ end
587
+ else
588
+ not_processed.append({:imei => p[:imei], :address => p[:address], :error => permiss})
571
589
  end
572
590
  else
573
- not_processed.append({:imei => p[:imei], :address => p[:address], :error => permiss})
591
+ not_processed.append({:imei => p[:imei], :address => p[:address], :error => prof_name})
574
592
  end
575
- else
576
- not_processed.append({:imei => p[:imei], :address => p[:address], :error => prof_name})
577
593
  end
578
- end
579
594
 
580
- if processed!=[]
581
- resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
595
+ if processed!=[]
596
+ resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
597
+ else
598
+ resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}}
599
+ end
582
600
  else
583
- resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}}
601
+ resp_out = {:code => 509, :result => "Input data invalid"}
584
602
  end
585
603
  end
586
604
  rescue
@@ -608,66 +626,105 @@ class Iot
608
626
 
609
627
  begin
610
628
 
611
- for g in device_list
612
- p g
629
+ thr6 = Thread.new do
613
630
 
614
- prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"])
615
- p prof_name1
631
+ if add_functions_connector.check_input_5(login, imei_list)[:code]==200
616
632
 
617
- if prof_name1[:code]==200
618
- p prof_name1
619
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
620
- p "permiss1"
621
- p permiss1
622
- if permiss1[:code]==200
633
+ for g in device_list
623
634
 
624
- if g["attributes"].key?("profile")
625
- permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code]
635
+ prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"])
626
636
 
627
- if permiss2==200
637
+ if prof_name1[:code]==200
638
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
639
+ if permiss1[:code]==200
640
+
641
+ if g["attributes"].key?("profile")
642
+ permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code]
643
+
644
+ if permiss2==200
628
645
 
629
- attr = g["attributes"]
630
- #mod_attr = {}
646
+ attr = g["attributes"]
647
+ #mod_attr = {}
631
648
 
632
- if attr.key?("profile")
633
- if attr["profile"].is_a? Integer
634
- p "Ok"
649
+ if attr.key?("profile")
650
+ if attr["profile"].is_a? Integer
651
+ p "Ok"
652
+ else
653
+ p new = mongo_client.get_profile_id_by_name(attr["profile"])
654
+ attr["profile"] = new["profile_id"]
655
+ end
656
+ end
657
+ mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
658
+ processed.append(g["imei"])
635
659
  else
636
- p new = mongo_client.get_profile_id_by_name(attr["profile"])
637
- attr["profile"] = new["profile_id"]
660
+ not_processed.append({:imei => g["imei"], :description => "New profile permission error", :error => permiss2})
638
661
  end
662
+
663
+ else
664
+ attr = g["attributes"]
665
+ mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
666
+ processed.append(g["imei"])
639
667
  end
640
- p attr
641
- mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
642
- processed.append(g["imei"])
643
668
  else
644
- not_processed.append({:imei => g["imei"], :description => "New profile permission error", :error => permiss2})
669
+ not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1})
645
670
  end
646
671
 
647
672
  else
648
- attr = g["attributes"]
649
- mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
650
- processed.append(g["imei"])
673
+ not_processed.append({:imei => g["imei"], :error => prof_name1})
651
674
  end
652
- else
653
- not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1})
654
- end
655
675
 
676
+ end
677
+ resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
656
678
  else
657
- not_processed.append({:imei => g["imei"], :error => prof_name1})
679
+ resp_out = {:code => 509, :result => "Input data invalid"}
658
680
  end
659
681
 
660
682
  end
661
- resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
662
683
 
663
684
  rescue
664
685
  resp_out = {:code => 507, :result => "Unknown SDK error"}
665
686
  end
687
+ thr6.join
666
688
  mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip)
667
689
  resp_out
668
690
  end
669
691
 
670
692
 
693
+ #7 procedure for subscriber autorization
694
+ def autorize_subscriber(login, password)
695
+ input_json = {:login => login, :password => password}
696
+ resp_out = {}
697
+ thr7 = Thread.new do
698
+ begin
699
+ login_inform = mongo_client.get_login_info(login)
700
+ if login_inform[:code]==200
701
+ pass_mongo = login_inform[:body]["password"]
702
+ if pass_mongo == password
703
+ resp_out = {:code => 200, :result => "Access granted"}
704
+ else
705
+ resp_out = {:code => 400, :result => "Access denied"}
706
+ end
707
+ else
708
+ resp_out = {:code => 401, :result => "Access denied. Incorrect login"}
709
+ end
710
+ rescue
711
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
712
+ end
713
+ end
714
+ thr7.join
715
+ mongo_client.audit_logger("autorize_subscriber", remote_ip, input_json, resp_out, real_ip)
716
+ resp_out
717
+ end
718
+
719
+ def get_available_profiles(login)
720
+
721
+ end
722
+
723
+ def get_available_types(login)
724
+
725
+ end
726
+
727
+
671
728
  def answ_dev_query_format_process(dev_list)
672
729
  add_functions_connector.answ_dev_query_format_process(dev_list)
673
730
  end
@@ -717,17 +774,17 @@ class Iot
717
774
  end
718
775
 
719
776
  def test()
720
- ddd = MongoIot.new(mongoip, mongoport, mongo_database)
777
+ #ddd = MongoIot.new(mongoip, mongoport, mongo_database)
721
778
  #ddd.get_profiles_by_login("test")
722
779
 
723
780
  ff = [131234123412341233, 131234123127341233]
724
781
  #ddd.get_imsi_info_from_db(ff)
725
782
 
726
783
  #p ddd.get_profile_id_by_name("1341241")
727
- p ddd.get_device_type_info_by_model("BGT_PPMC11")
728
- end
729
-
784
+ #p ddd.get_device_type_info_by_model("BGT_PPMC11")
730
785
 
786
+ #fff = HuaIot.new("134.17.93.4", "443", "/Users/imperituroard/Desktop/cert.crt", "/Users/imperituroard/Desktop/key.pem")
787
+ #p fff.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "eWllZpHgXgvgDAQlJjWs4QXaR94a", [91234])
788
+ end
731
789
 
732
790
  end
733
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imperituroard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Buynovskiy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
11
+ date: 2020-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -158,6 +158,7 @@ files:
158
158
  - imperituroard.gemspec
159
159
  - lib/.DS_Store
160
160
  - lib/imperituroard.rb
161
+ - lib/imperituroard/.DS_Store
161
162
  - lib/imperituroard/phpipamcps.rb
162
163
  - lib/imperituroard/phpipamdb.rb
163
164
  - lib/imperituroard/projects/iot/add_functions.rb