imperituroard 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/imperituroard/iot.rb +785 -0
- data/lib/imperituroard/platforms/cps/qps_connector.rb +45 -0
- data/lib/imperituroard/platforms/cps/request_formatter.rb +43 -0
- data/lib/imperituroard/projects/iot/internal_functions.rb +19 -0
- data/lib/imperituroard/projects/iot/mongoconnector.rb +82 -0
- data/lib/imperituroard/version.rb +1 -1
- data/lib/imperituroard.rb +52 -525
- metadata +5 -2
@@ -0,0 +1,785 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../projects/iot", __dir__)
|
2
|
+
|
3
|
+
require 'imperituroard/projects/iot/mongoconnector'
|
4
|
+
require 'imperituroard/projects/iot/hua_oceanconnect_adapter'
|
5
|
+
require 'imperituroard/projects/iot/add_functions'
|
6
|
+
require 'imperituroard/projects/iot/internal_functions'
|
7
|
+
require "imperituroard/phpipamdb"
|
8
|
+
require "imperituroard/phpipamcps"
|
9
|
+
require 'json'
|
10
|
+
require 'ipaddr'
|
11
|
+
require 'date'
|
12
|
+
require 'net/http'
|
13
|
+
require 'uri'
|
14
|
+
|
15
|
+
|
16
|
+
class IotFunctions_2
|
17
|
+
|
18
|
+
attr_accessor :mongoip,
|
19
|
+
:mongoport,
|
20
|
+
:iotip,
|
21
|
+
:mongo_database,
|
22
|
+
:iotplatform_ip,
|
23
|
+
:iotplatform_port,
|
24
|
+
:cert_path,
|
25
|
+
:key_path,
|
26
|
+
:mongo_client,
|
27
|
+
:add_functions_connector,
|
28
|
+
:real_ip, #real ip address of procedure caller
|
29
|
+
:remote_ip, #ip address of balancer
|
30
|
+
:hua_aceanconnect_connector,
|
31
|
+
:internal_func
|
32
|
+
|
33
|
+
def initialize(mongoip, mongoport, iotip, mongo_database,
|
34
|
+
iotplatform_ip, iotplatform_port, cert_path, key_path, telegram_api_url, telegram_chat_id, real_ip, remote_ip)
|
35
|
+
@mongoip = mongoip
|
36
|
+
@mongoport = mongoport
|
37
|
+
@iotip = iotip
|
38
|
+
@mongo_database = mongo_database
|
39
|
+
@iotplatform_ip = iotplatform_ip
|
40
|
+
@iotplatform_port = iotplatform_port
|
41
|
+
@cert_path = cert_path
|
42
|
+
@key_path = key_path
|
43
|
+
@mongo_client = MongoIot.new(mongoip, mongoport, mongo_database)
|
44
|
+
@add_functions_connector = AdditionalFunc.new(telegram_api_url, telegram_chat_id)
|
45
|
+
@real_ip = real_ip
|
46
|
+
@remote_ip = remote_ip
|
47
|
+
@hua_aceanconnect_connector = HuaIot.new(iotplatform_ip, iotplatform_port, cert_path, key_path)
|
48
|
+
@internal_func = InternalFunc.new
|
49
|
+
end
|
50
|
+
|
51
|
+
#!!1. Add device to profile (only for new device)
|
52
|
+
#login - login for client identification
|
53
|
+
#profile - profile for device
|
54
|
+
#imei_list - device identificator
|
55
|
+
#imei_list = [{"imei" => 131234123412341233, "description" => "dfdsf", "note"=>"second description", "profile"=>0, "device_type"=>"phone"},
|
56
|
+
#{"imei" => 56213126347645784, "description" => "dfdsf", "note"=>"second description", "profile"=>0}]
|
57
|
+
#massive commands
|
58
|
+
#+++
|
59
|
+
#iot logic added
|
60
|
+
def add_device_to_prof_2(login, imei_list)
|
61
|
+
input_json = {:login => login, :imei_list => imei_list}
|
62
|
+
resp_out = {}
|
63
|
+
begin
|
64
|
+
|
65
|
+
thr1 = Thread.new do
|
66
|
+
if add_functions_connector.check_input_1(login, imei_list)[:code]==200
|
67
|
+
|
68
|
+
imei = []
|
69
|
+
list1 = {}
|
70
|
+
for_insert = []
|
71
|
+
not_processed_list = []
|
72
|
+
processed_list = []
|
73
|
+
|
74
|
+
for ii in imei_list
|
75
|
+
list1[ii["imei"]] = ii
|
76
|
+
imei.append(ii["imei"])
|
77
|
+
end
|
78
|
+
list_checked = mongo_client.check_imei_exists(imei)
|
79
|
+
for ss in list_checked[:body][:exists]
|
80
|
+
not_processed_list.append({:imei => ss, :error => "Device exists in database"})
|
81
|
+
end
|
82
|
+
|
83
|
+
for jj in list_checked[:body][:not_exists]
|
84
|
+
begin
|
85
|
+
get_login_info = mongo_client.check_login_prof_perm_id_one(login, list1[jj]["profile"])[:code]
|
86
|
+
if get_login_info==200
|
87
|
+
for_insert.append(list1[jj])
|
88
|
+
else
|
89
|
+
not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"})
|
90
|
+
end
|
91
|
+
rescue
|
92
|
+
not_processed_list.append({:imei => list1[jj], :error => "Unknown error"})
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
begin
|
97
|
+
added_on_iot_platf = []
|
98
|
+
if for_insert!=[]
|
99
|
+
##Logic for IOT Platform connection###
|
100
|
+
|
101
|
+
credentials = mongo_client.get_iot_oceanconnect_credent(login)
|
102
|
+
|
103
|
+
if credentials[:code]==200
|
104
|
+
for aaa in for_insert
|
105
|
+
begin
|
106
|
+
dev_name = aaa["imei"].to_s
|
107
|
+
|
108
|
+
#get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"}
|
109
|
+
#from database
|
110
|
+
model_data = mongo_client.get_device_type_info_by_model(aaa["device_type"])
|
111
|
+
resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id],
|
112
|
+
credentials[:body][:secret],
|
113
|
+
aaa["imei"],
|
114
|
+
dev_name,
|
115
|
+
aaa["description"],
|
116
|
+
model_data[:body]["device_type"],
|
117
|
+
aaa["profile"],
|
118
|
+
model_data[:body]["ManufacturerID"],
|
119
|
+
model_data[:body]["ManufacturerNAME"],
|
120
|
+
model_data[:body]["model"]
|
121
|
+
)
|
122
|
+
if resss[:code]=="200"
|
123
|
+
s1 = aaa
|
124
|
+
s1[:huadata] = resss
|
125
|
+
s1[:created] = DateTime.now
|
126
|
+
added_on_iot_platf.append(s1)
|
127
|
+
else
|
128
|
+
not_processed_list.append({:imei => aaa["imei"], :error => resss})
|
129
|
+
end
|
130
|
+
rescue
|
131
|
+
not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"})
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
#########end iot platform logic#######
|
136
|
+
|
137
|
+
mongo_client.imei_insert_list(added_on_iot_platf)
|
138
|
+
resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
|
139
|
+
else
|
140
|
+
resp_out = {:code => 400, :result => "IOT platform credentials not found"}
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
else
|
145
|
+
resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
|
146
|
+
|
147
|
+
end
|
148
|
+
rescue
|
149
|
+
resp_out = {:code => 505, :result => "Error with database communication"}
|
150
|
+
end
|
151
|
+
|
152
|
+
else
|
153
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
rescue
|
157
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
158
|
+
end
|
159
|
+
thr1.join
|
160
|
+
|
161
|
+
mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip)
|
162
|
+
resp_out
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
#!!2 Find device (only mongo datebase. IOT platform not need)
|
167
|
+
# procedure for data selection from mongo database.
|
168
|
+
# for this function IOT platform not need
|
169
|
+
# login
|
170
|
+
# imei
|
171
|
+
# imei_list =[41234,23452345,132412]
|
172
|
+
#++
|
173
|
+
def device_find_2(login, imei_list)
|
174
|
+
input_json = {:login => login, :imei_list => imei_list}
|
175
|
+
ime_list_approved = []
|
176
|
+
ime_list_notapproved = []
|
177
|
+
resp_out = {}
|
178
|
+
begin
|
179
|
+
thr2 = Thread.new do
|
180
|
+
if add_functions_connector.check_input_2(login, imei_list)[:code]==200
|
181
|
+
|
182
|
+
for t in imei_list
|
183
|
+
prof_name1 = mongo_client.get_profile_name_from_imei(t)
|
184
|
+
if prof_name1[:code]==200
|
185
|
+
begin
|
186
|
+
permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
|
187
|
+
p "permiss1"
|
188
|
+
p permiss1
|
189
|
+
if permiss1[:code]==200
|
190
|
+
ime_list_approved.append(t)
|
191
|
+
else
|
192
|
+
ime_list_notapproved.append({:imei => t, :error => permiss1})
|
193
|
+
end
|
194
|
+
rescue
|
195
|
+
ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}})
|
196
|
+
end
|
197
|
+
else
|
198
|
+
ime_list_notapproved.append({:imei => t, :error => prof_name1})
|
199
|
+
end
|
200
|
+
end
|
201
|
+
begin
|
202
|
+
if ime_list_approved != []
|
203
|
+
data = mongo_client.get_imei_info_from_db(ime_list_approved)
|
204
|
+
|
205
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}}
|
206
|
+
|
207
|
+
else
|
208
|
+
resp_out = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}}
|
209
|
+
end
|
210
|
+
rescue
|
211
|
+
resp_out = {:code => 504, :result => "Unsuccessfully data transfer"}
|
212
|
+
end
|
213
|
+
else
|
214
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
215
|
+
end
|
216
|
+
end
|
217
|
+
rescue
|
218
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
219
|
+
end
|
220
|
+
thr2.join
|
221
|
+
mongo_client.audit_logger("device_find", remote_ip, input_json, resp_out, real_ip)
|
222
|
+
resp_out
|
223
|
+
end
|
224
|
+
|
225
|
+
#!3 device modify, change imei
|
226
|
+
#login
|
227
|
+
#imei_old
|
228
|
+
#imei_new
|
229
|
+
#massive commands
|
230
|
+
#im_list = [{"imei_old"=>7967843245667, "imei_new"=>7967843245665}]
|
231
|
+
#++
|
232
|
+
def imei_replace_2(login, im_list)
|
233
|
+
input_json = {:login => login, :imei_list => im_list}
|
234
|
+
resp_out = {}
|
235
|
+
|
236
|
+
begin
|
237
|
+
|
238
|
+
thr3 = Thread.new do
|
239
|
+
|
240
|
+
if add_functions_connector.check_input_3(login, im_list)[:code]==200
|
241
|
+
|
242
|
+
li_new_imei = []
|
243
|
+
list1 = {}
|
244
|
+
|
245
|
+
#dictionary for imeis which not processed. Final dictionary
|
246
|
+
not_processed_list = []
|
247
|
+
|
248
|
+
#dictionary for devices which was processed correctly
|
249
|
+
processed_list = []
|
250
|
+
|
251
|
+
#array for translations from old imei to new
|
252
|
+
old_new_translation = {}
|
253
|
+
|
254
|
+
approved_list = []
|
255
|
+
|
256
|
+
|
257
|
+
#old_imei_list for query to iot platform for data request
|
258
|
+
step1_approved_dict_old=[]
|
259
|
+
|
260
|
+
#form dictionary for processing
|
261
|
+
for pr1 in im_list
|
262
|
+
li_new_imei.append(pr1["imei_new"])
|
263
|
+
list1[pr1["imei_new"]]=pr1["imei_old"]
|
264
|
+
old_new_translation[pr1["imei_old"]]=pr1["imei_new"]
|
265
|
+
end
|
266
|
+
|
267
|
+
#check if imei_new exists in database. If exists - not process this imei
|
268
|
+
list_checked = mongo_client.check_imei_exists(li_new_imei)
|
269
|
+
|
270
|
+
internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug")
|
271
|
+
|
272
|
+
#add already exists new IMEI in error dictionary
|
273
|
+
for ss in list_checked[:body][:exists]
|
274
|
+
not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"})
|
275
|
+
end
|
276
|
+
|
277
|
+
#new_imei list which processed step1
|
278
|
+
step2_list = list_checked[:body][:not_exists]
|
279
|
+
|
280
|
+
internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug")
|
281
|
+
|
282
|
+
|
283
|
+
for a in step2_list
|
284
|
+
begin
|
285
|
+
|
286
|
+
#step3 checking permission for writing for imei list
|
287
|
+
prof_name1 = mongo_client.get_profile_name_from_imei(list1[a])
|
288
|
+
|
289
|
+
internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug")
|
290
|
+
|
291
|
+
if prof_name1[:code]==200
|
292
|
+
permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code]
|
293
|
+
internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug")
|
294
|
+
if permiss1==200
|
295
|
+
|
296
|
+
approved_list.append({:imei_old => list1[a], :imei_new => a})
|
297
|
+
step1_approved_dict_old.append(list1[a])
|
298
|
+
|
299
|
+
else
|
300
|
+
not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"})
|
301
|
+
end
|
302
|
+
else
|
303
|
+
not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"})
|
304
|
+
end
|
305
|
+
rescue
|
306
|
+
not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"})
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
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")
|
311
|
+
|
312
|
+
|
313
|
+
##Logic for IOT Platform connection###
|
314
|
+
|
315
|
+
list_from_iot = self.get_info_by_imeilist_from_iot(login, step1_approved_dict_old)
|
316
|
+
|
317
|
+
internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug")
|
318
|
+
|
319
|
+
#processing data. modifying data on iot platform and mongoDB
|
320
|
+
if list_from_iot[:code]=="200"
|
321
|
+
|
322
|
+
for ard in list_from_iot[:body]["devices"]
|
323
|
+
p ard
|
324
|
+
new_data_cur_dev = {}
|
325
|
+
mongo_answer = {}
|
326
|
+
current_old_dev = ard["deviceInfo"]["nodeId"]
|
327
|
+
current_device_id = ard["deviceId"]
|
328
|
+
new_data_cur_dev = ard["deviceInfo"]
|
329
|
+
new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s
|
330
|
+
|
331
|
+
credentials = mongo_client.get_iot_oceanconnect_credent(login)
|
332
|
+
|
333
|
+
if credentials[:code]==200
|
334
|
+
flag_remove=0
|
335
|
+
flag_create=0
|
336
|
+
remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id)
|
337
|
+
create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev)
|
338
|
+
|
339
|
+
if remove_answer[:code]=="204" || remove_answer[:code]=="200"
|
340
|
+
flag_remove=1
|
341
|
+
end
|
342
|
+
if create_answer[:code]=="200"
|
343
|
+
flag_create=1
|
344
|
+
end
|
345
|
+
if flag_remove==1 && flag_create==1
|
346
|
+
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})
|
347
|
+
processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]})
|
348
|
+
else
|
349
|
+
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"})
|
350
|
+
end
|
351
|
+
|
352
|
+
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")
|
353
|
+
|
354
|
+
else
|
355
|
+
approved_list=[]
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
else
|
360
|
+
approved_list=[]
|
361
|
+
end
|
362
|
+
|
363
|
+
if approved_list!=[]
|
364
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
|
365
|
+
else
|
366
|
+
resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
|
367
|
+
end
|
368
|
+
|
369
|
+
else
|
370
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
371
|
+
end
|
372
|
+
|
373
|
+
end
|
374
|
+
|
375
|
+
rescue
|
376
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
377
|
+
end
|
378
|
+
|
379
|
+
thr3.join
|
380
|
+
mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out, real_ip)
|
381
|
+
|
382
|
+
resp_out
|
383
|
+
|
384
|
+
end
|
385
|
+
|
386
|
+
|
387
|
+
#!!4 remove device
|
388
|
+
#login
|
389
|
+
#imei
|
390
|
+
# not massive commands
|
391
|
+
#imei=11341341234
|
392
|
+
#login="test"
|
393
|
+
#+++
|
394
|
+
#IOT logic added
|
395
|
+
def device_remove_2(login, imei)
|
396
|
+
|
397
|
+
input_json = {:login => login, :imei_list => imei}
|
398
|
+
resp_out = {}
|
399
|
+
|
400
|
+
begin
|
401
|
+
thr4 = Thread.new do
|
402
|
+
|
403
|
+
if add_functions_connector.check_input_4(login, imei)[:code]==200
|
404
|
+
|
405
|
+
prof_name = mongo_client.get_profile_name_from_imei(imei)
|
406
|
+
if prof_name[:code]==200
|
407
|
+
permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
|
408
|
+
if permiss[:code]==200
|
409
|
+
|
410
|
+
##Logic for IOT Platform connection###
|
411
|
+
|
412
|
+
credentials = mongo_client.get_iot_oceanconnect_credent(login)
|
413
|
+
resp = {}
|
414
|
+
|
415
|
+
if credentials[:code]==200
|
416
|
+
|
417
|
+
imei_data = mongo_client.get_imei_info_from_db([imei])
|
418
|
+
if imei_data[:body]!=[]
|
419
|
+
ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"])
|
420
|
+
p ans
|
421
|
+
if ans[:code]=="204" or ans[:code]=="200"
|
422
|
+
resp = mongo_client.device_remove_single_mongo(imei)
|
423
|
+
else
|
424
|
+
resp = {:code => 500, :result => "Unknown IOT platform error", :body => ans}
|
425
|
+
end
|
426
|
+
else
|
427
|
+
resp_out = {:code => 404, :result => "Data not found"}
|
428
|
+
end
|
429
|
+
|
430
|
+
#########end iot platform logic#######
|
431
|
+
|
432
|
+
if resp[:code]==200
|
433
|
+
resp_out = {:code => 200, :result => "Request completed successfully"}
|
434
|
+
else
|
435
|
+
resp_out=resp
|
436
|
+
end
|
437
|
+
else
|
438
|
+
resp_out = {:code => 400, :result => "IOT platform credentials not found"}
|
439
|
+
end
|
440
|
+
else
|
441
|
+
resp_out=permiss
|
442
|
+
end
|
443
|
+
else
|
444
|
+
resp_out=prof_name
|
445
|
+
end
|
446
|
+
else
|
447
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
448
|
+
end
|
449
|
+
|
450
|
+
end
|
451
|
+
|
452
|
+
rescue
|
453
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
454
|
+
end
|
455
|
+
thr4.join
|
456
|
+
mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip)
|
457
|
+
resp_out
|
458
|
+
end
|
459
|
+
|
460
|
+
|
461
|
+
#!5 add address to device
|
462
|
+
#login
|
463
|
+
#imei = newdevice_list
|
464
|
+
#address = newdevice_list
|
465
|
+
#newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}]
|
466
|
+
#+++
|
467
|
+
#iot platform integration completed
|
468
|
+
def device_add_address_2(login, newdevice_list)
|
469
|
+
#add_functions_connector.telegram_message(newdevice_list.to_s)
|
470
|
+
p newdevice_list
|
471
|
+
p "gas"
|
472
|
+
p MyJSON.valid?(newdevice_list[0].to_s)
|
473
|
+
p "sdfsdfgs"
|
474
|
+
input_json = {:login => login, :devices => newdevice_list}
|
475
|
+
resp_out = {}
|
476
|
+
not_processed = []
|
477
|
+
processed = []
|
478
|
+
begin
|
479
|
+
thr5 = Thread.new do
|
480
|
+
if add_functions_connector.check_input_5(login, newdevice_list)[:code]==200
|
481
|
+
|
482
|
+
for p in newdevice_list
|
483
|
+
prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
|
484
|
+
|
485
|
+
if prof_name[:code]==200
|
486
|
+
permiss = mongo_client.check_login_profile_permiss(login, prof_name[:body]["profile"])
|
487
|
+
if permiss[:code]==200
|
488
|
+
|
489
|
+
##Logic for IOT Platform connection###
|
490
|
+
credentials = mongo_client.get_iot_oceanconnect_credent(login)
|
491
|
+
resp = {}
|
492
|
+
|
493
|
+
if credentials[:code]==200
|
494
|
+
imei_data = mongo_client.get_imei_info_from_db([p[:imei]])
|
495
|
+
if imei_data[:body]!=[]
|
496
|
+
ans =hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address])
|
497
|
+
|
498
|
+
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")
|
499
|
+
end
|
500
|
+
|
501
|
+
end
|
502
|
+
|
503
|
+
#########end iot platform logic#######
|
504
|
+
|
505
|
+
resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
|
506
|
+
if resp[:code]==200
|
507
|
+
processed.append({:imei => p[:imei]})
|
508
|
+
end
|
509
|
+
else
|
510
|
+
not_processed.append({:imei => p[:imei], :address => p[:address], :error => permiss})
|
511
|
+
end
|
512
|
+
else
|
513
|
+
not_processed.append({:imei => p[:imei], :address => p[:address], :error => prof_name})
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
if processed!=[]
|
518
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
|
519
|
+
else
|
520
|
+
resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}}
|
521
|
+
end
|
522
|
+
else
|
523
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
524
|
+
end
|
525
|
+
end
|
526
|
+
rescue
|
527
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
528
|
+
end
|
529
|
+
thr5.join
|
530
|
+
mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out, real_ip)
|
531
|
+
resp_out
|
532
|
+
end
|
533
|
+
|
534
|
+
|
535
|
+
#6 add service by SPA
|
536
|
+
#imei
|
537
|
+
#profile
|
538
|
+
#imsi
|
539
|
+
#msisdn
|
540
|
+
#newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}]
|
541
|
+
#+
|
542
|
+
def add_service_2(login, device_list)
|
543
|
+
resp_out = {}
|
544
|
+
not_processed = []
|
545
|
+
processed = []
|
546
|
+
|
547
|
+
input_json = {:login => login, :devices => device_list}
|
548
|
+
|
549
|
+
begin
|
550
|
+
|
551
|
+
thr6 = Thread.new do
|
552
|
+
|
553
|
+
if add_functions_connector.check_input_5(login, device_list)[:code]==200
|
554
|
+
|
555
|
+
for g in device_list
|
556
|
+
|
557
|
+
prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"])
|
558
|
+
|
559
|
+
if prof_name1[:code]==200
|
560
|
+
permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])
|
561
|
+
if permiss1[:code]==200
|
562
|
+
|
563
|
+
if g["attributes"].key?("profile")
|
564
|
+
permiss2 = mongo_client.check_login_profile_permiss(login, g["attributes"]["profile"])[:code]
|
565
|
+
|
566
|
+
if permiss2==200
|
567
|
+
|
568
|
+
attr = g["attributes"]
|
569
|
+
#mod_attr = {}
|
570
|
+
|
571
|
+
if attr.key?("profile")
|
572
|
+
if attr["profile"].is_a? Integer
|
573
|
+
p "Ok"
|
574
|
+
else
|
575
|
+
p new = mongo_client.get_profile_id_by_name(attr["profile"])
|
576
|
+
attr["profile"] = new["profile_id"]
|
577
|
+
end
|
578
|
+
end
|
579
|
+
mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
|
580
|
+
processed.append(g["imei"])
|
581
|
+
else
|
582
|
+
not_processed.append({:imei => g["imei"], :description => "New profile permission error", :error => permiss2})
|
583
|
+
end
|
584
|
+
|
585
|
+
else
|
586
|
+
attr = g["attributes"]
|
587
|
+
mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
|
588
|
+
processed.append(g["imei"])
|
589
|
+
end
|
590
|
+
else
|
591
|
+
not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1})
|
592
|
+
end
|
593
|
+
|
594
|
+
else
|
595
|
+
not_processed.append({:imei => g["imei"], :error => prof_name1})
|
596
|
+
end
|
597
|
+
|
598
|
+
end
|
599
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
|
600
|
+
else
|
601
|
+
resp_out = {:code => 509, :result => "Input data invalid"}
|
602
|
+
end
|
603
|
+
|
604
|
+
end
|
605
|
+
|
606
|
+
rescue
|
607
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
608
|
+
end
|
609
|
+
thr6.join
|
610
|
+
mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip)
|
611
|
+
resp_out
|
612
|
+
end
|
613
|
+
|
614
|
+
|
615
|
+
#7 procedure for subscriber autorization
|
616
|
+
def autorize_subscriber_2(login, password)
|
617
|
+
input_json = {:login => login, :password => password}
|
618
|
+
resp_out = {}
|
619
|
+
thr7 = Thread.new do
|
620
|
+
begin
|
621
|
+
login_inform = mongo_client.get_login_info(login)
|
622
|
+
if login_inform[:code]==200
|
623
|
+
pass_mongo = login_inform[:body]["password"]
|
624
|
+
if pass_mongo == password
|
625
|
+
resp_out = {:code => 200, :result => "Access granted"}
|
626
|
+
else
|
627
|
+
resp_out = {:code => 400, :result => "Access denied"}
|
628
|
+
end
|
629
|
+
else
|
630
|
+
resp_out = {:code => 401, :result => "Access denied. Incorrect login"}
|
631
|
+
end
|
632
|
+
rescue
|
633
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
634
|
+
end
|
635
|
+
end
|
636
|
+
thr7.join
|
637
|
+
mongo_client.audit_logger("autorize_subscriber", remote_ip, input_json, resp_out, real_ip)
|
638
|
+
resp_out
|
639
|
+
end
|
640
|
+
|
641
|
+
|
642
|
+
def answ_dev_query_format_process(dev_list)
|
643
|
+
add_functions_connector.answ_dev_query_format_process(dev_list)
|
644
|
+
end
|
645
|
+
|
646
|
+
|
647
|
+
def get_available_prof_2(login)
|
648
|
+
|
649
|
+
input_params = {:login => login}
|
650
|
+
resp_out = {}
|
651
|
+
|
652
|
+
begin
|
653
|
+
data_processed = []
|
654
|
+
login_info = mongo_client.get_login_inf2_select(login)
|
655
|
+
if login_info[:code]==200
|
656
|
+
permit_profiles = login_info[:body]["permit_profiles"]
|
657
|
+
permit_types = login_info[:body]["permit_types"]
|
658
|
+
data_from_mongo = mongo_client.get_profile_list_by_id(permit_profiles)
|
659
|
+
|
660
|
+
for ff in data_from_mongo
|
661
|
+
data_processed.append({:profile_id => ff["profile_id"],
|
662
|
+
:description => ff["description"],
|
663
|
+
:note => ff["note"],
|
664
|
+
:form => ff["form"],
|
665
|
+
:profile => ff["profile"],
|
666
|
+
:info => ff["info"]
|
667
|
+
})
|
668
|
+
end
|
669
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed}}
|
670
|
+
else
|
671
|
+
resp_out = {:code => login_info[:code], :result => login_info[:result]}
|
672
|
+
end
|
673
|
+
|
674
|
+
rescue
|
675
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
676
|
+
end
|
677
|
+
mongo_client.audit_logger("get_available_prof_2", remote_ip, input_params, resp_out, real_ip)
|
678
|
+
resp_out
|
679
|
+
end
|
680
|
+
|
681
|
+
|
682
|
+
def get_available_types_2(login)
|
683
|
+
|
684
|
+
input_params = {:login => login}
|
685
|
+
resp_out = {}
|
686
|
+
|
687
|
+
begin
|
688
|
+
data_processed = []
|
689
|
+
login_info = mongo_client.get_login_inf2_select(login)
|
690
|
+
if login_info[:code]==200
|
691
|
+
|
692
|
+
permit_profiles = login_info[:body]["permit_profiles"]
|
693
|
+
permit_types = login_info[:body]["permit_types"]
|
694
|
+
|
695
|
+
data_from_mongo = mongo_client.get_type_list_by_id(permit_types)
|
696
|
+
|
697
|
+
for ff in data_from_mongo
|
698
|
+
data_processed.append({:type_id => ff["type_id"],
|
699
|
+
:description => ff["description"],
|
700
|
+
:note => ff["note"],
|
701
|
+
:form => ff["form"],
|
702
|
+
:ManufacturerID => ff["ManufacturerID"],
|
703
|
+
:ManufacturerNAME => ff["ManufacturerNAME"],
|
704
|
+
:device_type => ff["device_type"],
|
705
|
+
:model => ff["model"],
|
706
|
+
:info => ff["info"]
|
707
|
+
})
|
708
|
+
end
|
709
|
+
resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed}}
|
710
|
+
else
|
711
|
+
resp_out = {:code => login_info[:code], :result => login_info[:result]}
|
712
|
+
end
|
713
|
+
|
714
|
+
rescue
|
715
|
+
resp_out = {:code => 507, :result => "Unknown SDK error"}
|
716
|
+
end
|
717
|
+
mongo_client.audit_logger("get_available_types_2", remote_ip, input_params, resp_out, real_ip)
|
718
|
+
resp_out
|
719
|
+
end
|
720
|
+
|
721
|
+
|
722
|
+
#10 get info for device type
|
723
|
+
def get_info_data_type_2(type)
|
724
|
+
|
725
|
+
input_params = {:type => type}
|
726
|
+
output_answ = {}
|
727
|
+
mon_answer = {}
|
728
|
+
begin
|
729
|
+
if type.is_a? String
|
730
|
+
mon_answer = mongo_client.get_type_by_name(type)
|
731
|
+
else
|
732
|
+
mon_answer = mongo_client.get_type_by_id(type)
|
733
|
+
end
|
734
|
+
|
735
|
+
if mon_answer != {} && mon_answer != []
|
736
|
+
output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"],
|
737
|
+
:model => mon_answer["model"],
|
738
|
+
:type_id => mon_answer["type_id"]
|
739
|
+
}}}
|
740
|
+
else
|
741
|
+
output_answ = {:code => 505, :result => "Unknown SDK error"}
|
742
|
+
end
|
743
|
+
rescue
|
744
|
+
output_answ = {:code => 507, :result => "Unknown SDK error"}
|
745
|
+
end
|
746
|
+
mongo_client.audit_logger("get_info_data_type_2", remote_ip, input_params, output_answ, real_ip)
|
747
|
+
output_answ
|
748
|
+
end
|
749
|
+
|
750
|
+
|
751
|
+
#11 get info for profile
|
752
|
+
def get_info_data_profile_2(profile)
|
753
|
+
|
754
|
+
input_params = {:profile => profile}
|
755
|
+
output_answ = {}
|
756
|
+
|
757
|
+
mon_answer = {}
|
758
|
+
|
759
|
+
begin
|
760
|
+
if profile.is_a? String
|
761
|
+
mon_answer = mongo_client.get_profile_id_by_name(profile)
|
762
|
+
else
|
763
|
+
mon_answer = mongo_client.get_profile_name_by_id(profile)
|
764
|
+
end
|
765
|
+
|
766
|
+
if mon_answer != {} && mon_answer != []
|
767
|
+
output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"],
|
768
|
+
:profile => mon_answer["profile"],
|
769
|
+
:profile_id => mon_answer["profile_id"]
|
770
|
+
}}}
|
771
|
+
else
|
772
|
+
output_answ = {:code => 505, :result => "Unknown SDK error"}
|
773
|
+
end
|
774
|
+
|
775
|
+
rescue
|
776
|
+
|
777
|
+
output_answ = {:code => 507, :result => "Unknown SDK error"}
|
778
|
+
end
|
779
|
+
|
780
|
+
mongo_client.audit_logger("get_info_data_profile_2", remote_ip, input_params, output_answ, real_ip)
|
781
|
+
output_answ
|
782
|
+
|
783
|
+
end
|
784
|
+
|
785
|
+
end
|