Class: MongoIot

Inherits:
Object
  • Object
show all
Defined in:
lib/imperituroard/projects/iot/mongoconnector.rb

Overview

class for communication with mongo database for iot API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mongo_ip, mongo_port, mongo_database) ⇒ MongoIot

Returns a new instance of MongoIot.



14
15
16
17
18
19
20
21
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 14

def initialize(mongo_ip, mongo_port, mongo_database)
  @mongo_database = mongo_database
  @mongo_ip = mongo_ip
  @mongo_port = mongo_port
  client_host = [mongo_ip + ":" + mongo_port]
  @client = Mongo::Client.new(client_host, :database => mongo_database)
  @internal_func = InternalFunc.new
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



12
13
14
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 12

def client
  @client
end

#internal_funcObject

Returns the value of attribute internal_func.



12
13
14
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 12

def internal_func
  @internal_func
end

#mongo_databaseObject

Returns the value of attribute mongo_database.



12
13
14
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 12

def mongo_database
  @mongo_database
end

#mongo_ipObject

Returns the value of attribute mongo_ip.



12
13
14
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 12

def mongo_ip
  @mongo_ip
end

#mongo_portObject

Returns the value of attribute mongo_port.



12
13
14
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 12

def mongo_port
  @mongo_port
end

Instance Method Details

#audit_iot_logger(proc_name, url_str, input_json, output_json) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 45

def audit_iot_logger(proc_name, url_str, input_json, output_json)
  out_resp = {}
  p "iiiiiiiot"
  p input_json
  begin
    current = internal_func.datetimenow
    collection = client[:audit_iot_platform]
    doc = {
        :proc_name => proc_name,
        :date => current,
        :url => url_str,
        :iot_platform_request => input_json,
        :iot_platform_responce => output_json
    }
    p doc
    result = collection.insert_one(doc)
    out_resp = {:code => 200, :result => "audit_logger: Request completed successfully", :body => result}
  rescue
    out_resp = {:code => 507, :result => "audit_logger: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#audit_logger(proc_name, src_ip, input_json, output_json, real_ip, add_params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 23

def audit_logger(proc_name, src_ip, input_json, output_json, real_ip, add_params)
  out_resp = {}
  begin
    current = internal_func.datetimenow
    collection = client[:audit]
    doc = {
        :proc_name => proc_name,
        :date => current,
        :sender => {:src_ip => src_ip, :real_ip => real_ip},
        :input_params => input_json,
        :output_params => output_json,
        :add_params => add_params
    }
    result = collection.insert_one(doc)
    out_resp = {:code => 200, :result => "audit_logger: Request completed successfully", :body => result}
  rescue
    out_resp = {:code => 507, :result => "audit_logger: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#audit_spa_logger(proc_name, url_str, input_json, output_json) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 69

def audit_spa_logger(proc_name, url_str, input_json, output_json)
  out_resp = {}
  p "iiiiiiiot"
  p input_json
  begin
    current = internal_func.datetimenow
    collection = client[:audit_spa]
    doc = {
        :proc_name => proc_name,
        :date => current,
        :url => url_str,
        :iot_platform_request => input_json,
        :iot_platform_responce => output_json
    }
    p doc
    result = collection.insert_one(doc)
    out_resp = {:code => 200, :result => "audit_logger: Request completed successfully", :body => result}
  rescue
    out_resp = {:code => 507, :result => "audit_logger: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#check_imei_exists(imei_list) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 346

def check_imei_exists(imei_list)
  out_resp = {}
  res_exists = []
  imei_list_res = get_imei_info_from_db(imei_list)
  for k in imei_list_res[:body]
    res_exists.append(k["imei"])
  end
  not_ex = imei_list - res_exists
  out_resp = {:code => 200, :result => "check_imei_exists: Request completed successfully",
              :body => {:exists => res_exists, :not_exists => not_ex}}
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#check_login_prof_perm_id_one(login, profile_id) ⇒ Object

universal procedure. You can use profile id or profile name as profile_id (string or int)



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 323

def (, profile_id)
  out_resp = {}
   = ()
  profile_data = self.get_profile_universal(profile_id)
  access=1
  if [:body]!=[]
    for j in [:body]
      if j["profile_id"].to_i==profile_data["profile_id"]
        access=0
      end
      if access==0
        out_resp = {:code => 200, :result => "check_login_prof_perm_id_one: Permission granted"}
      else
        out_resp = {:code => 400, :result => "check_login_prof_perm_id_one: Access denied. This incident will be reported."}
      end
    end
  else
    out_resp = {:code => 500, :result => "check_login_prof_perm_id_one: Login not found"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#check_login_profile_permiss(login, profile) ⇒ Object

universal procedure. might be profile id or name



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 290

def (, profile)
  out_resp = {}
   = ()
  if [:code] == 200
    dst_profile = get_profile_universal(profile)
    access = 1
    if [:body] != []
      if dst_profile != []
        for j in [:body]
          if j["profile_id"].to_i == dst_profile["profile_id"].to_i
            access = 0
          end
          if access == 0
            out_resp = {:code => 200, :result => "check_login_profile_permiss: Permission granted"}
          else
            out_resp = {:code => 400, :result => "check_login_profile_permiss: Access denied. This incident will be reported."}
          end
        end
      else
        out_resp = {:code => 501, :result => "check_login_profile_permiss: Profile not found"}
      end
    else
      out_resp = {:code => 500, :result => "check_login_profile_permiss: Access denied. Login not found"}
    end
  else
    out_resp = {:code => 500, :result => "check_login_profile_permiss: Access denied. Login not found"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#compare_device_types(type1, type2) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 666

def compare_device_types(type1, type2)
  input_inf = {:type1 => type1, :type2 => type2}
  out_answ = {}
  tp1 = internal_func.if_digit_or_string(type1)
  tp2 = internal_func.if_digit_or_string(type2)
  p tp1
  p tp2
  p "tp"

  if tp1[:body][:string] == tp2[:body][:string] && tp1[:body][:string] == false
    if type1.to_i == type2.to_i
      out_answ = {:code => 200, :result => "compare_device_types: types are the same", :is_the_same => true, :data_type => "integer"}
    else
      out_answ = {:code => 200, :result => "compare_device_types: types are different", :is_the_same => false, :data_type => "integer"}
    end
  elsif tp1[:body][:string] == tp2[:body][:string] && tp1[:body][:string] == true
    if type1 == type2
      out_answ = {:code => 200, :result => "compare_device_types: types are the same", :is_the_same => true, :data_type => "string"}
    else
      out_answ = {:code => 200, :result => "compare_device_types: types are different", :is_the_same => false, :data_type => "string"}
    end
  else
    tp1_data = {}
    tp2_data = {}
    p "gggggggg"
    if tp1[:body][:string]
      tp1_data = self.get_type_by_name(type1)
    else
      tp1_data = self.get_type_by_id(type1.to_i)
    end

    if tp2[:body][:string]
      tp2_data = self.get_type_by_name(type2)
    else
      tp2_data = self.get_type_by_id(type2.to_i)
    end

    p tp1_data
    p tp2_data
    p type1
    p type2

    if tp1_data["type_id"] == tp2_data["type_id"]
      out_answ = {:code => 200, :result => "compare_device_types: profiles are the same", :is_the_same => true, :data_type => "different"}
    else
      out_answ = {:code => 200, :result => "compare_device_types: profiles are different", :is_the_same => false, :data_type => "different"}
    end
  end
  internal_func.printer_texter({:input_inf => input_inf, :out => out_answ, :fun => "compare_device_types"}, "debug")
  out_answ
end

#compare_profiles(profile1, profile2) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 624

def compare_profiles(profile1, profile2)
  input_inf = {:profile1 => profile1, :profile2 => profile2}
  out_answ = {}
  ch1 = internal_func.if_digit_or_string(profile1)
  ch2 = internal_func.if_digit_or_string(profile2)
  if ch1[:body][:string] == ch2[:body][:string] && ch1[:body][:string] == false
    if profile1.to_i == profile2.to_i
      out_answ = {:code => 200, :result => "compare_profiles: profiles are the same", :is_the_same => true, :data_type => "integer"}
    else
      out_answ = {:code => 200, :result => "compare_profiles: profiles are different", :is_the_same => false, :data_type => "integer"}
    end
  elsif ch1[:body][:string] == ch2[:body][:string] && ch1[:body][:string] == true
    if profile1 == profile2
      out_answ = {:code => 200, :result => "compare_profiles: profiles are the same", :is_the_same => true, :data_type => "string"}
    else
      out_answ = {:code => 200, :result => "compare_profiles: profiles are different", :is_the_same => false, :data_type => "string"}
    end
  else
    ch1_data = {}
    ch2_data = {}
    if ch1[:body][:string]
      ch1_data = self.get_profile_id_by_name(profile1)
    else
      ch1_data = self.get_profile_name_by_id(profile1.to_i)
    end

    if ch2[:body][:string]
      ch2_data = self.get_profile_id_by_name(profile2)
    else
      ch2_data = self.get_profile_name_by_id(profile2.to_i)
    end

    if ch1_data["profile_id"] == ch2_data["profile_id"]
      out_answ = {:code => 200, :result => "compare_profiles: profiles are the same", :is_the_same => true, :data_type => "different"}
    else
      out_answ = {:code => 200, :result => "compare_profiles: profiles are different", :is_the_same => false, :data_type => "different"}
    end
  end
  internal_func.printer_texter({:input_inf => input_inf, :out => out_answ, :fun => "compare_profiles"}, "debug")
  out_answ
end

#device_modify_any_attr_mongo(imei, attr_list) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 496

def device_modify_any_attr_mongo(imei, attr_list)
  out_resp = {}
  begin
    collection = client[:device_imei]
    doc = {
        "imei" => imei
    }
    sett = {'$set' => attr_list}
    result = collection.update_one(doc, sett)
    p result
  rescue
    continue
  end
end

#device_modify_attr_mongo(imei, address) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 479

def device_modify_attr_mongo(imei, address)
  out_resp = {}
  begin
    collection = client[:device_imei]
    doc = {
        "imei" => imei
    }
    sett = {'$set' => {address: address}}
    result = collection.update_one(doc, sett)
    out_resp = {:code => 200, :result => "device_modify_attr_mongo: Request completed successfully"}
  rescue
    out_resp = {:code => 507, :result => "device_modify_attr_mongo: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#device_remove_single_mongo(imei) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 462

def device_remove_single_mongo(imei)
  out_resp = {}
  begin
    collection = client[:device_imei]
    doc = {
        "imei" => imei
    }
    result = collection.delete_many(doc)
    p result
    out_resp = {:code => 200, :result => "device_remove_single_mongo: Request completed successfully"}
  rescue
    out_resp = {:code => 507, :result => "device_remove_single_mongo: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_all_imei_from_dbObject



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 753

def get_all_imei_from_db
  out_resp = {}
  begin
    req2 = []
    result_ps = []
    collection = client[:device_imei]
    collection.find({}, {:_id => 0}).each {|row|
      result_ps.append(row)
    }
    out_resp = {:code => 200, :result => "get_all_imei_from_db: Request completed successfully", :body => result_ps}
  rescue
    out_resp = {:code => 507, :result => "get_all_imei_from_db: Unknown SDK error"}
  end
  #internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_device_type_info_by_model(device_model) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 561

def get_device_type_info_by_model(device_model)
  out_resp = {}
  begin
    result_ps = []
    collection = client[:device_types]
    collection.find({"model" => device_model}).each {|row|
      result_ps.append(row)
    }
    dattaa = result_ps[0]
    if dattaa != nil
      out_resp = {:code => 200, :result => "get_device_type_info_by_model: Request completed successfully", :body => dattaa}
    else
      out_resp = {:code => 404, :result => "get_device_type_info_by_model: Device info not found", :body => {"model" => device_model, "ManufacturerID" => "unknown", "ManufacturerNAME" => "unknown", "device_type" => "unknown"}}
    end
  rescue
    out_resp = {:code => 500, :result => "get_device_type_info_by_model: procedure error", :body => {"model" => device_model, "ManufacturerID" => "unknown", "ManufacturerNAME" => "unknown", "device_type" => "unknown"}}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_device_type_info_universal(device_model) ⇒ Object

universal procedure. Use id or name



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 583

def get_device_type_info_universal(device_model)
  input_fun = {:device_model => device_model}
  out_resp = {}
  begin
    as = internal_func.if_digit_or_string(device_model)

    if as[:body][:string]
      result_ps = []
      collection = client[:device_types]
      collection.find({"model" => device_model}).each {|row|
        result_ps.append(row)
      }
      dattaa = result_ps[0]
      if dattaa!=nil
        out_resp = {:code => 200, :result => "get_device_type_info_universal: Request completed successfully", :body => dattaa}
      else
        out_resp = {:code => 404, :result => "get_device_type_info_universal: Device info not found", :body => {"model" => device_model, "ManufacturerID" => "unknown", "ManufacturerNAME" => "unknown", "device_type" => "unknown"}}
      end
    else
      result_ps2 = []
      collection = client[:device_types]
      collection.find({"type_id" => device_model.to_i}).each {|row|
        result_ps2.append(row)
      }
      dattaa = result_ps2[0]
      if dattaa!=nil
        out_resp = {:code => 200, :result => "get_device_type_info_universal: Request completed successfully", :body => dattaa}
      else
        out_resp = {:code => 404, :result => "get_device_type_info_universal: Device info not found", :body => {"model" => device_model, "ManufacturerID" => "unknown", "ManufacturerNAME" => "unknown", "device_type" => "unknown"}}
      end
    end

  rescue
    out_resp = {:code => 500, :result => "get_device_type_info_universal: procedure error", :body => {"model" => device_model, "ManufacturerID" => "unknown", "ManufacturerNAME" => "unknown", "device_type" => "unknown"}}
  end
  internal_func.printer_texter({:input_fun => input_fun, :out_resp => out_resp, :fun => "get_device_type_info_universal"}, "debug")
  out_resp
end

#get_imei_info_from_db(imeilist) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 184

def get_imei_info_from_db(imeilist)
  out_resp = {}
  p imeilist
  p "imeilist"
  begin
    req2 = []
    result_ps = []
    collection = client[:device_imei]
    for i in imeilist
      req2.append({:imei => i})
    end
    collection.find({:$or => req2}, {:_id => 0}).each {|row|
      result_ps.append(row)
    }
    out_resp = {:code => 200, :result => "get_imei_info_from_db: Request completed successfully", :body => result_ps}
  rescue
    out_resp = {:code => 507, :result => "get_imei_info_from_db: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_iot_oceanconn_credent_2(profile) ⇒ Object

function for get iot platform credentials from profile universal procedure. use profile or profile id



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 534

def get_iot_oceanconn_credent_2(profile)
  input_in = {:profile => profile}
  out_resp = {}
  begin
    result_ps = []
    collection = client[:device_profiles]

    if internal_func.if_digit_or_string(profile.to_s)[:body][:string]
      collection.find({"profile" => profile}).each {|row|
        result_ps.append(row)
      }
    else
      collection.find({"profile_id" => profile.to_i}).each {|row|
        result_ps.append(row)
      }
    end

    app_id = result_ps[0][:iot_data][:app_id]
    secret = result_ps[0][:iot_data][:secret]
    out_resp = {:code => 200, :result => "get_iot_oceanconn_credent_2: Request completed successfully", :body => {:app_id => app_id, :secret => secret}}
  rescue
    out_resp = {:code => 500, :result => "get_iot_oceanconn_credent_2: Process failed"}
  end
  internal_func.printer_texter({:input_in => input_in, :out_resp => out_resp, :fun => "get_iot_oceanconn_credent_2"}, "debug")
  out_resp
end

#get_iot_oceanconnect_credent(login) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 512

def get_iot_oceanconnect_credent()
  out_resp = {}
  begin
    result_ps = []
    collection = client[:users]
    collection.find({"login" => }).each {|row|
      result_ps.append(row)
    }
    p result_ps[0]
    app_id = result_ps[0][:iot_data][:app_id]
    secret = result_ps[0][:iot_data][:secret]
    out_resp = {:code => 200, :result => "get_iot_oceanconnect_credent: Request completed successfully", :body => {:app_id => app_id, :secret => secret}}
  rescue
    out_resp = {:code => 500, :result => "get_iot_oceanconnect_credent: Process failed"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_login_inf2_select(login) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 159

def ()
  out_resp = {}
  begin
     = []
    req2 = []
    result_ps = []
    collection = client[:users]
    collection2 = client[:device_profiles]
    collection.find({:login => }).each {|row|
       = row
    }
    if  != [] && ["login"] != nil && ["login"] != ""
      out_resp = {:code => 200, :result => "get_login_info: Request completed successfully", :body => }
    else
      out_resp = {:code => 404, :result => "get_login_info: login not found in database"}
    end
  rescue
    out_resp = {:code => 507, :result => "get_login_info: Unknown SDK error"}
  end
  p out_resp
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_login_info(login) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 128

def ()
  out_resp = {}
  begin
     = []
    req2 = []
    result_ps = []
    collection = client[:users]
    collection2 = client[:device_profiles]
    collection.find({:login => }).each {|row|
       = row
    }
    p "login_profiles"
    p 
    if  != [] && ["login"] != nil && ["login"] != ""
      passw_check = internal_func.check_pass_format(["password"])
      if passw_check[:code]==200
        out_resp = {:code => 200, :result => "get_login_info: Request completed successfully", :body => }
      else
        out_resp = {:code => 405, :result => "get_login_info: incorrect password"}
      end
    else
      out_resp = {:code => 404, :result => "get_login_info: login not found in database"}
    end
  rescue
    out_resp = {:code => 507, :result => "get_login_info: Unknown SDK error"}
  end
  p out_resp
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_profile_id_by_name(profile_name) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 206

def get_profile_id_by_name(profile_name)
  begin
    result_ps = []
    collection = client[:device_profiles]
    collection.find({"profile" => profile_name}).each {|row|
      result_ps.append(row)
    }
    result_ps[0]
  rescue
    []
  end
end

#get_profile_list_by_id(profile_id_list) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 234

def get_profile_list_by_id(profile_id_list)
  begin
    result_ps = []
    collection = client[:device_profiles]
    list_id = internal_func.prof_id_list_transform_mongo(profile_id_list)
    collection.find({:$or => list_id}).each {|row|
      result_ps.append(row)
    }
    result_ps
  rescue
    []
  end
end

#get_profile_name_by_id(profile_id) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 219

def get_profile_name_by_id(profile_id)
  begin
    result_ps = []
    collection = client[:device_profiles]
    collection.find({"profile_id" => profile_id.to_i}).each {|row|
      result_ps.append(row)
    }
    result_ps[0]
  rescue
    []
  end
end

#get_profile_name_from_imei(imei) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 418

def get_profile_name_from_imei(imei)
  out_resp = {}
  info = {}
  begin
    id = ""
    begin
      info = get_imei_info_from_db([imei])
      if info[:body]==[]
        out_resp = {:code => 505, :result => "get_profile_name_from_imei: get_imei_info_from_db returned empty list from database. IMEIS not found"}
      else
        id = info[:body][0]["profile"]
        begin
          p id
          res = get_profile_name_by_id(id)
          p res
          if res.key?("profile")
            res=res
          else
            out_resp = {:code => 505, :result => "get_profile_name_from_imei: Function get_profile_name_by_id not returned profile. Invalid data in database and returned: #{res.to_s}"}
          end
        rescue
          out_resp = {:code => 506, :result => "get_profile_name_from_imei: Function get_profile_name_by_id not processed correctly and returned: #{res.to_s}"}

        end
        begin
          if res["profile"]!=nil
            out_resp = {:code => 200, :result => "get_profile_name_from_imei: Request completed successfully", :body => res, :imei_info => info}
          end
        rescue
          out_resp = {:code => 506, :result => "get_profile_name_from_imei: Function get_profile_name_by_id not processed correctly and returned: #{res.to_s}"}
        end
      end
    rescue
      out_resp = {:code => 506, :result => "get_profile_name_from_imei: Function get_imei_info_from_db not processed correctly and returned: #{info.to_s}"}
    end

  rescue
    out_resp = {:code => 507, :result => "get_profile_name_from_imei: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_profile_universal(profile) ⇒ Object

universal procedure. put there profile name or id in string or int



720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 720

def get_profile_universal(profile)
  input_inf = {:profile => profile}
  out = {}
  inn = internal_func.if_digit_or_string(profile)
  out = {}
  if inn[:body][:string]
    out = self.get_profile_id_by_name(profile)
  else
    out = self.get_profile_name_by_id(profile.to_i)
  end
  internal_func.printer_texter({:input_inf => input_inf, :out => out, :fun => "get_profile_universal"}, "debug")
  out
end

#get_profiles_by_login(login) ⇒ Object

:code => 507, :result => “Unknown SDK error” => 200, :result => “Request completed successfully”, :body => result_ps



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 96

def ()
  out_resp = {}
  begin
    p "get_profiles_by_login get_profiles_by_login"
     = []
    req2 = []
    result_ps = []
    collection = client[:users]
    collection2 = client[:device_profiles]
    collection.find({:login => }).each {|row|
       = row["permit_profiles"]
    }
    p 
    if  !=[]
      for i in 
        req2.append({:profile_id => i})
      end
      collection2.find({:$or => req2}, {:_id => 0}).each {|row|
        result_ps.append(row)
      }
      out_resp = {:code => 200, :result => "get_profiles_by_login: Request completed successfully", :body => result_ps}
    else
      out_resp = {:code => 404, :result => "get_profiles_by_login: Access denied. Incorrect login"}
    end
  rescue
    out_resp = {:code => 507, :result => "get_profiles_by_login: Unknown SDK error"}
  end
  internal_func.printer_texter(out_resp, "debug")
  out_resp
end

#get_type_by_id(type_id) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 263

def get_type_by_id(type_id)
  begin
    result_ps = []
    collection = client[:device_types]
    collection.find({"type_id" => type_id.to_i}).each {|row|
      result_ps.append(row)
    }
    result_ps[0]
  rescue
    []
  end
end

#get_type_by_name(type_name) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 276

def get_type_by_name(type_name)
  begin
    result_ps = []
    collection = client[:device_types]
    collection.find({"model" => type_name}).each {|row|
      result_ps.append(row)
    }
    result_ps[0]
  rescue
    []
  end
end

#get_type_list_by_id(type_id_list) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 249

def get_type_list_by_id(type_id_list)
  begin
    result_ps = []
    collection = client[:device_types]
    list_id = internal_func.type_id_list_transform_mongo(type_id_list)
    collection.find({:$or => list_id}).each {|row|
      result_ps.append(row)
    }
    result_ps
  rescue
    []
  end
end

#imei_insert_list(imei_list) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 361

def imei_insert_list(imei_list)
  begin
    collection = client[:device_imei]
    for l in imei_list
      doc = {
          imei: l,
          imsi: "unknown",
          msisdn: "unknown",
          description: "test imei",
          note: "second description",
          profile: 0,
          device_type: 0,
          address: "unknown"
      }
      result = collection.insert_one(l)
      p result
    end
  rescue
    nil
  end
end

#imei_insert_list2(imei_list) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 384

def imei_insert_list2(imei_list)
  begin
    collection = client[:device_imei]
    for l in imei_list
      doc = {
          imei: l,
          imsi: "unknown",
          msisdn: "unknown",
          description: "test imei",
          note: "second description",
          profile: 0,
          device_type: 0,
          address: "unknown"
      }
      result = collection.insert_one(l)
      p result
    end
  rescue
    nil
  end
end

#imei_insert_model(model) ⇒ Object



407
408
409
410
411
412
413
414
415
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 407

def imei_insert_model(model)
  begin
    collection = client[:device_types]
    result = collection.insert_one(model)
    p result
  rescue
    nil
  end
end

#modify_attr_mongo_universal(imei, attribute) ⇒ Object



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/imperituroard/projects/iot/mongoconnector.rb', line 734

def modify_attr_mongo_universal(imei, attribute)
  input_add = {:imei => imei, :attribute => attribute}
  out_resp = {}
  begin
    collection = client[:device_imei]
    doc = {
        "imei" => imei
    }
    sett = {'$set' => attribute}
    result = collection.update_one(doc, sett)
    out_resp = {:code => 200, :result => "modify_attr_mongo_universal: Request completed successfully"}
  rescue
    out_resp = {:code => 507, :result => "modify_attr_mongo_universal: Unknown SDK error"}
  end
  internal_func.printer_texter({:input_add => input_add, :out_resp => out_resp, :fun => "modify_attr_mongo_universal"}, "debug")
  out_resp
end