Class: IotFunctions_2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mongoip, mongoport, iotip, mongo_database, iotplatform_ip, iotplatform_port, cert_path, key_path, telegram_api_url, telegram_chat_id) ⇒ IotFunctions_2

Returns a new instance of IotFunctions_2.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/imperituroard/projects/iot.rb', line 33

def initialize(mongoip,
               mongoport,
               iotip,
               mongo_database,
               iotplatform_ip,
               iotplatform_port,
               cert_path,
               key_path,
               telegram_api_url,
               telegram_chat_id
  #real_ip, remote_ip
)
  @mongoip = mongoip
  @mongoport = mongoport
  #@iotip = iotip
  #@mongo_database = mongo_database
  #@iotplatform_ip = iotplatform_ip
  #@iotplatform_port = iotplatform_port
  @cert_path = cert_path
  @key_path = key_path
  @mongo_client = MongoIot.new(mongoip, mongoport, mongo_database)
  @add_functions_connector = AdditionalFunc.new(telegram_api_url, telegram_chat_id)
  #@real_ip = real_ip
  #@remote_ip = remote_ip
  @hua_aceanconnect_connector = HuaIot.new(iotplatform_ip, iotplatform_port, cert_path, key_path, mongoip, mongoport, mongo_database)
  @internal_func = InternalFunc.new
end

Instance Attribute Details

#add_functions_connectorObject

Returns the value of attribute add_functions_connector.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def add_functions_connector
  @add_functions_connector
end

#cert_pathObject

Returns the value of attribute cert_path.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def cert_path
  @cert_path
end

#hua_aceanconnect_connectorObject

Returns the value of attribute hua_aceanconnect_connector.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def hua_aceanconnect_connector
  @hua_aceanconnect_connector
end

#internal_funcObject

Returns the value of attribute internal_func.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def internal_func
  @internal_func
end

#key_pathObject

Returns the value of attribute key_path.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def key_path
  @key_path
end

#mongo_clientObject

Returns the value of attribute mongo_client.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def mongo_client
  @mongo_client
end

#mongoipObject

Returns the value of attribute mongoip.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def mongoip
  @mongoip
end

#mongoportObject

Returns the value of attribute mongoport.



18
19
20
# File 'lib/imperituroard/projects/iot.rb', line 18

def mongoport
  @mongoport
end

Instance Method Details

#add_device_to_prof2_2(request_full, real_ip, remote_ip) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/imperituroard/projects/iot.rb', line 217

def add_device_to_prof2_2(request_full, real_ip, remote_ip)

  #{"autorization"=>{"login"=>"test", "token"=>"tE3aFvs27"}, "devlist"=>[{"devices"=>{"imei"=>123419520034999, "type"=>"1000002", "profile"=>0}}]}

   = request_full["autorization"]["login"]

  im_li =[]

  for sd in request_full["devlist"]
    im_li.append(sd["devices"])
  end
  imei_list = im_li

  input_json = {:request_full => request_full}
  input_json22 = {:login => , :imei_list => imei_list}
  resp_out = {}
  begin

    thr1 = Thread.new do
      if add_functions_connector.check_input_1(, imei_list)[:code]==200

        imei = []
        list1 = {}
        for_insert = []
        not_processed_list = []
        processed_list = []

        for ii in imei_list
          valid_resp = internal_func.imei_validate(ii["imei"])
          if valid_resp[:checked]
            list1[ii["imei"]] = ii
            imei.append(ii["imei"])
          else
            not_processed_list.append({:imei => ii["imei"], :error => valid_resp[:result]})
          end
        end

        if imei != []
          list_checked = mongo_client.check_imei_exists(imei)

          for ss in list_checked[:body][:exists]
            not_processed_list.append({:imei => ss, :error => "Device exists in database"})
            p ss
            p "ss"
            p list_checked
          end

          for jj in list_checked[:body][:not_exists]
            begin
               = mongo_client.(, list1[jj]["profile"])[:code]
              if ==200
                for_insert.append(list1[jj])
              else
                not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"})
              end
            rescue
              not_processed_list.append({:imei => list1[jj], :error => "Unknown error"})
            end
          end
        end

        begin
          added_on_iot_platf = []
          if for_insert != []
            ##Logic for IOT Platform connection###


            for aaa in for_insert

              #########change 2020.05.11. imperituroard
              #########replace process for get iot app_id and password from users to profile

              #credentials = mongo_client.get_iot_oceanconnect_credent(login)
              credentials = mongo_client.get_iot_oceanconn_credent_2(aaa["profile"])

              #########

              if aaa.key?("type") && !aaa.key?("device_type")
                aaa["device_type"] = aaa["type"]
                aaa = internal_func.delete_key_hash(aaa, "type")
              end

              aaa = internal_func.hash_val_to_string(aaa)

              if credentials[:code] == 200

                begin
                  dev_name = aaa["imei"].to_s

                  #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"}
                  #from database
                  model_data = mongo_client.get_device_type_info_universal(aaa["device_type"])
                  profile_data = mongo_client.get_profile_universal(aaa["profile"])
                  p profile_data
                  p model_data
                  p "model_data"
                  resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id],
                                                                              credentials[:body][:secret],
                                                                              aaa["imei"],
                                                                              dev_name,
                                                                              aaa["description"],
                                                                              model_data[:body]["device_type"],
                                                                              aaa["profile"],
                                                                              model_data[:body]["ManufacturerID"],
                                                                              model_data[:body]["ManufacturerNAME"],
                                                                              model_data[:body]["model"]
                  )
                  if resss[:code]=="200"
                    if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200
                      not_processed_list.append({:imei => aaa["imei"], :error => resss})
                    else
                      s1 = aaa
                      s1[:huadata] = resss
                      s1[:created] = DateTime.now
                      s1["device_type"] = model_data[:body]["model"]
                      s1["profile"] = profile_data["profile_id"]
                      added_on_iot_platf.append(s1)
                    end
                  else
                    not_processed_list.append({:imei => aaa["imei"], :error => resss})
                  end
                rescue
                  not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"})
                end
                #########end iot platform logic#######

              else
                resss_err = {:code => 400, :result => "IOT platform credentials not found"}
                not_processed_list.append({:imei => aaa["imei"], :error => resss_err})
              end

            end

            if added_on_iot_platf.length != 0
              mongo_client.imei_insert_list(added_on_iot_platf)
              resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
            else
              resp_out = {:code => 202, :result => "Empty list. Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
            end
          else
            resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
          end
        rescue
          resp_out = {:code => 505, :result => "Error with database communication"}
        end
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end
    end
  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr1.join

  resp_out[:body][:tosoapgw] = internal_func.iot_create_dev_soapgw_answer(input_json22, resp_out)
  mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#add_device_to_prof_2(login, imei_list, real_ip, remote_ip) ⇒ Object

!!1. Add device to profile (only for new device) login - login for client identification profile - profile for device imei_list - device identificator imei_list = [=> 131234123412341233, “description” => “dfdsf”, “note”=>“second description”, “profile”=>0, “device_type”=>“phone”, => 56213126347645784, “description” => “dfdsf”, “note”=>“second description”, “profile”=>0] massive commands +++ iot logic added



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
126
127
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/imperituroard/projects/iot.rb', line 70

def add_device_to_prof_2(, imei_list, real_ip, remote_ip)
  input_json = {:login => , :imei_list => imei_list}
  resp_out = {}
  begin

    thr1 = Thread.new do
      if add_functions_connector.check_input_1(, imei_list)[:code]==200

        imei = []
        list1 = {}
        for_insert = []
        not_processed_list = []
        processed_list = []

        for ii in imei_list
          valid_resp = internal_func.imei_validate(ii["imei"])
          if valid_resp[:checked]
            list1[ii["imei"]] = ii
            imei.append(ii["imei"])
          else
            not_processed_list.append({:imei => ii["imei"], :error => valid_resp[:result]})
          end
        end

        if imei != []
          list_checked = mongo_client.check_imei_exists(imei)

          for ss in list_checked[:body][:exists]
            not_processed_list.append({:imei => ss, :error => "Device exists in database"})
            p ss
            p "ss"
            p list_checked
          end

          for jj in list_checked[:body][:not_exists]
            begin
               = mongo_client.(, list1[jj]["profile"])[:code]
              if ==200
                for_insert.append(list1[jj])
              else
                not_processed_list.append({:imei => list1[jj], :error => "Permission denied for this profile"})
              end
            rescue
              not_processed_list.append({:imei => list1[jj], :error => "Unknown error"})
            end
          end
        end

        begin
          added_on_iot_platf = []
          if for_insert != []
            ##Logic for IOT Platform connection###


            for aaa in for_insert

              #########change 2020.05.11. imperituroard
              #########replace process for get iot app_id and password from users to profile

              #credentials = mongo_client.get_iot_oceanconnect_credent(login)
              credentials = mongo_client.get_iot_oceanconn_credent_2(aaa["profile"])

              #########

              if aaa.key?("type") && !aaa.key?("device_type")
                aaa["device_type"] = aaa["type"]
                aaa = internal_func.delete_key_hash(aaa, "type")
              end

              aaa = internal_func.hash_val_to_string(aaa)

              if credentials[:code] == 200

                begin
                  dev_name = aaa["imei"].to_s

                  #get {"model"=>"BGT_PPMC", "ManufacturerID"=>"unknown", "ManufacturerNAME"=>"unknown", "device_type"=>"unknown"}
                  #from database
                  model_data = mongo_client.get_device_type_info_universal(aaa["device_type"])
                  profile_data = mongo_client.get_profile_universal(aaa["profile"])
                  p profile_data
                  p model_data
                  p "model_data"
                  resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials[:body][:app_id],
                                                                              credentials[:body][:secret],
                                                                              aaa["imei"],
                                                                              dev_name,
                                                                              aaa["description"],
                                                                              model_data[:body]["device_type"],
                                                                              aaa["profile"],
                                                                              model_data[:body]["ManufacturerID"],
                                                                              model_data[:body]["ManufacturerNAME"],
                                                                              model_data[:body]["model"]
                  )
                  if resss[:code]=="200"
                    if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200
                      not_processed_list.append({:imei => aaa["imei"], :error => resss})
                    else
                      s1 = aaa
                      s1[:huadata] = resss
                      s1[:created] = DateTime.now
                      s1["device_type"] = model_data[:body]["model"]
                      s1["profile"] = profile_data["profile_id"]
                      added_on_iot_platf.append(s1)
                    end
                  else
                    not_processed_list.append({:imei => aaa["imei"], :error => resss})
                  end
                rescue
                  not_processed_list.append({:imei => aaa["imei"], :error => "Unknown error with insertion imei on IOT platform"})
                end
                #########end iot platform logic#######

              else
                resss_err = {:code => 400, :result => "IOT platform credentials not found"}
                not_processed_list.append({:imei => aaa["imei"], :error => resss_err})
              end

            end

            if added_on_iot_platf.length != 0
              mongo_client.imei_insert_list(added_on_iot_platf)
              resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
            else
              resp_out = {:code => 202, :result => "Empty list. Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
            end
          else
            resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => added_on_iot_platf, :error_list => not_processed_list}}
          end
        rescue
          resp_out = {:code => 505, :result => "Error with database communication"}
        end
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end
    end
  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr1.join

  resp_out[:body][:tosoapgw] = internal_func.iot_create_dev_soapgw_answer(input_json, resp_out)
  mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#add_model_to_mongo(model, manufacture_id, manufacture_name, device_type, description, note) ⇒ Object

for internal use. Add new device model



1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
# File 'lib/imperituroard/projects/iot.rb', line 1434

def add_model_to_mongo(model, manufacture_id, manufacture_name, device_type, description, note)
  model = {
      model: model,
      ManufacturerID: manufacture_id,
      ManufacturerNAME: manufacture_name,
      device_type: device_type,
      description: description,
      note: note,
      created: DateTime.now
  }
  mongo_client.imei_insert_model(model)
end

#add_service_2(login, device_list, real_ip, remote_ip) ⇒ Object

6 add service by SPA imei profile imsi msisdn newdevice_list=[:attributes=>{:address=>“Golubeva51”, :profile=>“wqeqcqeqwev”, :msisdn=>375298766719, :imsi=>25702858586756875}] +



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# File 'lib/imperituroard/projects/iot.rb', line 786

def add_service_2(, device_list, real_ip, remote_ip)
  resp_out = {}
  not_processed = []
  processed = []

  input_json = {:login => , :devices => device_list}

  begin

    thr6 = Thread.new do

      if add_functions_connector.check_input_5(, device_list)[:code]==200

        for g in device_list

          prof_name1 = mongo_client.get_profile_name_from_imei(g["imei"])

          if prof_name1[:code]==200
            permiss1 = mongo_client.(, prof_name1[:body]["profile"])
            if permiss1[:code]==200

              if g["attributes"].key?("profile")
                permiss2 = mongo_client.(, g["attributes"]["profile"])[:code]

                if permiss2==200

                  attr = g["attributes"]
                  #mod_attr = {}

                  if attr.key?("profile")
                    if attr["profile"].is_a? Integer
                      p "Ok"
                    else
                      p new = mongo_client.get_profile_universal(attr["profile"])
                      attr["profile"] = new["profile_id"]
                    end
                  end
                  mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
                  processed.append(g["imei"])
                else
                  not_processed.append({:imei => g["imei"], :description => "New profile permission error", :error => permiss2})
                end

              else
                attr = g["attributes"]
                mongo_client.device_modify_any_attr_mongo(g["imei"], attr)
                processed.append(g["imei"])
              end
            else
              not_processed.append({:imei => g["imei"], :description => "Old profile permission error", :error => permiss1})
            end

          else
            not_processed.append({:imei => g["imei"], :error => prof_name1})
          end

        end
        resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end
    end
  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr6.join
  mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#answ_dev_query_format_process(dev_list) ⇒ Object



885
886
887
# File 'lib/imperituroard/projects/iot.rb', line 885

def answ_dev_query_format_process(dev_list)
  add_functions_connector.answ_dev_query_format_process(dev_list)
end

#autorize_subscriber_2(login, password, real_ip, remote_ip) ⇒ Object

7 procedure for subscriber autorization save data to mongodb



859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
# File 'lib/imperituroard/projects/iot.rb', line 859

def autorize_subscriber_2(, password, real_ip, remote_ip)
  input_json = {:login => , :password => password}
  resp_out = {}
  thr7 = Thread.new do
    begin
       = mongo_client.()
      if [:code]==200
        pass_mongo = [:body]["password"]
        if pass_mongo == password
          resp_out = {:code => 200, :result => "Access granted"}
        else
          resp_out = {:code => 400, :result => "Access denied"}
        end
      else
        resp_out = {:code => 401, :result => "Access denied. Incorrect login"}
      end
    rescue
      resp_out = {:code => 507, :result => "Unknown SDK error"}
    end
  end
  thr7.join
  mongo_client.audit_logger("autorize_subscriber", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#delete_all_devices_in_databaseObject

technology procedures###############



1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
# File 'lib/imperituroard/projects/iot.rb', line 1418

def delete_all_devices_in_database

  imei_list = []
  all_imei = mongo_client.get_all_imei_from_db
  all_imei[:body].each do |immm|
    imei_list.append(immm["imei"])
  end

  imei_list.each do |del_imei|
    p del_imei
    self.device_remove_2("test", del_imei, "localhost", "localhost")
  end
end

#device_add_address_2(login, newdevice_list, real_ip, remote_ip) ⇒ Object

!5 add address to device login imei = newdevice_list address = newdevice_list newdevice_list=[:address=>“Golubeva51”] +++ iot platform integration completed



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/imperituroard/projects/iot.rb', line 706

def device_add_address_2(, newdevice_list, real_ip, remote_ip)
  #add_functions_connector.telegram_message(newdevice_list.to_s)
  p newdevice_list
  p "gas"
  p MyJSON.valid?(newdevice_list[0].to_s)
  p "sdfsdfgs"
  input_json = {:login => , :devices => newdevice_list}
  resp_out = {}
  not_processed = []
  processed = []
  begin
    thr5 = Thread.new do
      if add_functions_connector.check_input_5(, newdevice_list)[:code]==200

        for p in newdevice_list
          prof_name = mongo_client.get_profile_name_from_imei(p[:imei])

          if prof_name[:code]==200
            permiss = mongo_client.(, prof_name[:body]["profile"])
            if permiss[:code]==200

              ##Logic for IOT Platform connection###

              #########change 2020.05.11. imperituroard
              #########replace process for get iot app_id and password from users to profile

              #credentials = mongo_client.get_iot_oceanconnect_credent(login)
              credentials = mongo_client.get_iot_oceanconn_credent_2(prof_name[:body]["profile"])

              #########

              resp = {}

              if credentials[:code]==200
                imei_data = mongo_client.get_imei_info_from_db([p[:imei]])
                if imei_data[:body]!=[]
                  ans = hua_aceanconnect_connector.modify_location_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"], p[:address])
                  internal_func.printer_texter({:function => "device_add_address Step2", :ans => ans, :descrition => "answer from hua IOT", :input => {:did => imei_data[:body][0]["huadata"]["body"]["deviceId"], :appid => credentials[:body][:app_id], :secret => credentials[:body][:secret], :address => p[:address]}}, "debug")
                end
              end

              #########end iot platform logic#######

              resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
              if resp[:code]==200
                processed.append({:imei => p[:imei]})
              end
            else
              not_processed.append({:imei => p[:imei], :address => p[:address], :error => permiss})
            end
          else
            not_processed.append({:imei => p[:imei], :address => p[:address], :error => prof_name})
          end
        end

        if processed!=[]
          resp_out = {:code => 200, :result => "Request completed successfully", :body => {:imei_processed => processed, :error_list => not_processed}}
        else
          resp_out = {:code => 202, :result => "Nothing processed", :body => {:imei_processed => processed, :error_list => not_processed}}
        end
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end
    end
  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr5.join
  mongo_client.audit_logger("device_add_address", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#device_find_2(login, imei_list, real_ip, remote_ip) ⇒ Object

!!2 Find device (only mongo datebase. IOT platform not need) procedure for data selection from mongo database. for this function IOT platform not need login imei imei_list =[41234,23452345,132412] ++



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/imperituroard/projects/iot.rb', line 384

def device_find_2(, imei_list, real_ip, remote_ip)
  input_json = {:login => , :imei_list => imei_list}
  ime_list_approved = []
  ime_list_notapproved = []
  resp_out = {}
  begin
    thr2 = Thread.new do
      if add_functions_connector.check_input_2(, imei_list)[:code] == 200

        for t in imei_list
          prof_name1 = mongo_client.get_profile_name_from_imei(t)
          if prof_name1[:code] == 200
            begin
              permiss1 = mongo_client.(, prof_name1[:body]["profile"])
              p "permiss1"
              p permiss1
              if permiss1[:code] == 200
                ime_list_approved.append(t)
              else
                ime_list_notapproved.append({:imei => t, :error => permiss1})
              end
            rescue
              ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}})
            end
          else
            ime_list_notapproved.append({:imei => t, :error => prof_name1})
          end
        end
        begin
          if ime_list_approved != []
            data = mongo_client.get_imei_info_from_db(ime_list_approved)

            resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}}

          else
            resp_out = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}}
          end
        rescue
          resp_out = {:code => 504, :result => "Unsuccessfully data transfer"}
        end
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end
    end
  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr2.join

  resp_out[:data][:tosoapgw] = add_functions_connector.iot_query_dev_soapgw_answer(input_json, resp_out)

  mongo_client.audit_logger("device_find", remote_ip, input_json, resp_out, real_ip, {})
  resp_out

end

#device_remove_2(login, imei, real_ip, remote_ip) ⇒ Object

!!4 remove device login imei not massive commands imei=11341341234 login=“test” +++ IOT logic added



622
623
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
665
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
# File 'lib/imperituroard/projects/iot.rb', line 622

def device_remove_2(, imei, real_ip, remote_ip)

  input_json = {:login => , :imei_list => imei}
  resp_out = {}

  begin
    thr4 = Thread.new do

      if add_functions_connector.check_input_4(, imei)[:code] == 200

        prof_name = mongo_client.get_profile_name_from_imei(imei)
        if prof_name[:code] == 200
          permiss = mongo_client.(, prof_name[:body]["profile"])
          if permiss[:code] == 200

            ##Logic for IOT Platform connection###

            #########change 2020.05.11. imperituroard
            #########replace process for get iot app_id and password from users to profile

            #credentials = mongo_client.get_iot_oceanconnect_credent(login)
            credentials = mongo_client.get_iot_oceanconn_credent_2(prof_name[:body]["profile"])

            #########

            resp = {}

            if credentials[:code] == 200

              imei_data = mongo_client.get_imei_info_from_db([imei])
              if imei_data[:body] != []
                ans = {}
                if imei_data[:body][0]["huadata"]["body"]["deviceId"] != nil
                  ans = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"])
                else
                  ans = {:code => "204", :result => "Not processed by iot platform. deviceId not found in mongoDB"}
                end
                if ans[:code] == "204" or ans[:code] == "200"
                  resp = mongo_client.device_remove_single_mongo(imei)
                else
                  resp = {:code => 500, :result => "Unknown IOT platform error", :body => ans}
                end
              else
                resp_out = {:code => 404, :result => "Data not found"}
              end

              #########end iot platform logic#######

              if resp[:code]==200
                resp_out = {:code => 200, :result => "Request completed successfully"}
              else
                resp_out=resp
              end
            else
              resp_out = {:code => 400, :result => "IOT platform credentials not found"}
            end
          else
            resp_out=permiss
          end
        else
          resp_out=prof_name
        end
      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end

    end

  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end
  thr4.join
  mongo_client.audit_logger("device_remove", remote_ip, input_json, resp_out, real_ip, {})
  resp_out
end

#get_available_prof_2(login, real_ip, remote_ip) ⇒ Object

8 get available profiles by login



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/imperituroard/projects/iot.rb', line 891

def get_available_prof_2(, real_ip, remote_ip)

  input_params = {:login => }
  resp_out = {}

  begin
    data_processed = []
    data_soapgw = []
     = mongo_client.()
    if [:code]==200
      permit_profiles = [:body]["permit_profiles"]
      permit_types = [:body]["permit_types"]
      data_from_mongo = mongo_client.get_profile_list_by_id(permit_profiles)
      p data_from_mongo

      for ff in data_from_mongo
        data_processed.append({:profile_id => ff["profile_id"],
                               :description => ff["description"],
                               :note => ff["note"],
                               :form => ff["form"],
                               :profile => ff["profile"],
                               :info => ff["info"]
                              })
        data_soapgw.append({:id => ff["profile_id"], :form => ff["form"], :description => ff["description"], :profile => ff["profile"]})

      end
      tosoapgw = {:code => 200,
                  :result => "Request completed successfully",
                  :profiles => data_soapgw
      }

      resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}}
    else
      tosoapgw = {:code => [:code],
                  :result => [:result],
                  :profiles => data_soapgw
      }
      resp_out = {:code => [:code], :result => [:result], :body => {:tosoapgw => tosoapgw}}
    end

  rescue
    tosoapgw = {:code => 507,
                :result => "Unknown SDK error",
                :profiles => data_soapgw
    }
    resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}}
  end
  mongo_client.audit_logger("get_available_prof_2", remote_ip, input_params, resp_out, real_ip, {})
  resp_out
end

#get_available_types2_2(login, profile, real_ip, remote_ip) ⇒ Object

10 get available device types by login and profile. procedure update



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/imperituroard/projects/iot.rb', line 1000

def get_available_types2_2(, profile, real_ip, remote_ip)

  input_params = {:login => }
  resp_out = {}

  begin
    data_processed = []
    data_soapgw = []
     = mongo_client.()
    prof_data = mongo_client.get_profile_universal(profile)
    if [:code]==200

      permit_profiles = [:body]["permit_profiles"]
      permit_types = [:body]["permit_types"]
      perm_tp_prof = prof_data["permit_types"]

      if permit_profiles.include?(prof_data["profile_id"])
        res_types_permitted = internal_func.compare_dict(permit_types, perm_tp_prof)
        data_from_mongo = mongo_client.get_type_list_by_id(res_types_permitted)

        for ff in data_from_mongo
          data_processed.append({:type_id => ff["type_id"],
                                 :description => ff["description"],
                                 :note => ff["note"],
                                 :form => ff["form"],
                                 :ManufacturerID => ff["ManufacturerID"],
                                 :ManufacturerNAME => ff["ManufacturerNAME"],
                                 :device_type => ff["device_type"],
                                 :model => ff["model"],
                                 :info => ff["info"]
                                })
          data_soapgw.append({:name => ff["model"], :form => ff["form"], :description => ff["description"], :type_id => ff["type_id"]})
        end

        tosoapgw = {:code => 200,
                    :result => "Request completed successfully",
                    :types => data_soapgw
        }

        resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}}

      else
        tosoapgw = {:code => 700,
                    :result => "Profile not permitted",
                    :types => data_soapgw
        }
        resp_out = {:code => 700, :result => "Profile not permitted", :body => {:tosoapgw => tosoapgw}}
      end


    else
      tosoapgw = {:code => [:code],
                  :result => [:result],
                  :types => data_soapgw
      }
      resp_out = {:code => [:code], :result => [:result], :body => {:tosoapgw => tosoapgw}}
    end

  rescue
    tosoapgw = {:code => 507,
                :result => "Unknown SDK error",
                :types => data_soapgw
    }
    resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}}
  end
  mongo_client.audit_logger("get_available_types_2", remote_ip, input_params, resp_out, real_ip, {})
  resp_out
end

#get_available_types_2(login, real_ip, remote_ip) ⇒ Object

9 get available device types by login



943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
# File 'lib/imperituroard/projects/iot.rb', line 943

def get_available_types_2(, real_ip, remote_ip)

  input_params = {:login => }
  resp_out = {}

  begin
    data_processed = []
    data_soapgw = []
     = mongo_client.()
    if [:code]==200

      permit_profiles = [:body]["permit_profiles"]
      permit_types = [:body]["permit_types"]

      data_from_mongo = mongo_client.get_type_list_by_id(permit_types)

      for ff in data_from_mongo
        data_processed.append({:type_id => ff["type_id"],
                               :description => ff["description"],
                               :note => ff["note"],
                               :form => ff["form"],
                               :ManufacturerID => ff["ManufacturerID"],
                               :ManufacturerNAME => ff["ManufacturerNAME"],
                               :device_type => ff["device_type"],
                               :model => ff["model"],
                               :info => ff["info"]
                              })
        data_soapgw.append({:name => ff["model"], :form => ff["form"], :description => ff["description"], :type_id => ff["type_id"]})
      end

      tosoapgw = {:code => 200,
                  :result => "Request completed successfully",
                  :types => data_soapgw
      }

      resp_out = {:code => 200, :result => "Request completed successfully", :body => {:data => data_processed, :tosoapgw => tosoapgw}}
    else
      tosoapgw = {:code => [:code],
                  :result => [:result],
                  :types => data_soapgw
      }
      resp_out = {:code => [:code], :result => [:result], :body => {:tosoapgw => tosoapgw}}
    end

  rescue
    tosoapgw = {:code => 507,
                :result => "Unknown SDK error",
                :types => data_soapgw
    }
    resp_out = {:code => 507, :result => "Unknown SDK error", :body => {:tosoapgw => tosoapgw}}
  end
  mongo_client.audit_logger("get_available_types_2", remote_ip, input_params, resp_out, real_ip, {})
  resp_out
end

#get_info_by_imeilist_from_iot(login, imei_list) ⇒ Object

14 additional procedure for checking status on iot platform



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
# File 'lib/imperituroard/projects/iot.rb', line 1309

def get_info_by_imeilist_from_iot(, imei_list)
  resp_out={}
  begin
    dev_id_list = []
    for_process_list = {}
    resss = {}
    data_from_db = mongo_client.get_imei_info_from_db(imei_list)
    p data_from_db
    profile = 0
    for g in data_from_db[:body]
      p g
      p "ggggg"
      dev_id_list.append(g["huadata"]["body"]["deviceId"])

      if for_process_list[g["profile"]] == nil
        for_process_list[g["profile"]] = [g["huadata"]["body"]["deviceId"]]
      else
        for_process_list[g["profile"]].append(g["huadata"]["body"]["deviceId"])
      end

      if g["profile"] != nil && g["profile"] != ""
        profile = g["profile"]
      end
    end

    p dev_id_list
    p "dev_id_list"
    p for_process_list

    for ard in for_process_list
      ##Logic for IOT Platform connection###

      #########change 2020.05.11. imperituroard
      #########replace process for get iot app_id and password from users to profile

      #credentials = mongo_client.get_iot_oceanconnect_credent(login)
      credentials = mongo_client.get_iot_oceanconn_credent_2(ard[0])

      #########

      if credentials[:code] == 200
        resp_out[:code] = "200"
        resp_out[:result] = "Processed"
        p apid = credentials[:body][:app_id]
        p secre = credentials[:body][:secret]
        resp_out_1 = hua_aceanconnect_connector.quer_dev_query_list(apid, secre, ard[1])
        p resp_out_1
        p "resp_out_1"
        p resp_out[:body]
        p "resp_out[:body][\"devices\"]"

        if resp_out[:body] == nil || resp_out[:body] == {} || resp_out[:body] == {"devices" => nil}
          p "step1111"
          p resp_out_1[:body]["devices"]
          resp_out[:body] = {'devices' => resp_out_1[:body]["devices"]}
          p "step22222"
        else
          if resp_out_1[:body]["devices"] != nil
            resp_out[:body]["devices"] = resp_out[:body]["devices"] + resp_out_1[:body]["devices"]
          end
        end
      end
    end

    p resp_out
    p "resp_out"

  rescue
    resp_out = {:code => "500", :message => "get_info_by_imeilist_from_iot: Something wrong", :body => {"devices" => []}}
  end
  internal_func.printer_texter(resp_out, "debug")
  resp_out
end

#get_info_data_profile_2(profile, real_ip, remote_ip) ⇒ Object

12 get info for profile



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/imperituroard/projects/iot.rb', line 1101

def get_info_data_profile_2(profile, real_ip, remote_ip)

  input_params = {:profile => profile}
  output_answ = {}
  mon_answer = {}
  begin
    if internal_func.if_digit_or_string(profile)[:body][:string]
      mon_answer = mongo_client.get_profile_id_by_name(profile)
    else
      mon_answer = mongo_client.get_profile_name_by_id(profile)
    end

    p mon_answer

    if mon_answer != {} && mon_answer != []
      output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"],
                                                                                                    :profile => mon_answer["profile"],
                                                                                                    :profile_id => mon_answer["profile_id"]
      }}}
    else
      output_answ = {:code => 505, :result => "Unknown SDK error"}
    end
  rescue
    output_answ = {:code => 507, :result => "Unknown SDK error"}
  end
  mongo_client.audit_logger("get_info_data_profile_2", remote_ip, input_params, output_answ, real_ip, {})
  output_answ
end

#get_info_data_type_2(type, real_ip, remote_ip) ⇒ Object

11 get info for device type



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/imperituroard/projects/iot.rb', line 1071

def get_info_data_type_2(type, real_ip, remote_ip)

  input_params = {:type => type}
  output_answ = {}
  mon_answer = {}
  begin
    #if type.is_a? String
    if internal_func.if_digit_or_string(type)[:body][:string]
      mon_answer = mongo_client.get_type_by_name(type)
    else
      mon_answer = mongo_client.get_type_by_id(type)
    end

    if mon_answer != {} && mon_answer != []
      output_answ = {:code => 200, :result => "Request completed successfully", :body => {:data => {:info => mon_answer["info"],
                                                                                                    :model => mon_answer["model"],
                                                                                                    :type_id => mon_answer["type_id"]
      }}}
    else
      output_answ = {:code => 505, :result => "Unknown SDK error"}
    end
  rescue
    output_answ = {:code => 507, :result => "Unknown SDK error"}
  end
  mongo_client.audit_logger("get_info_data_type_2", remote_ip, input_params, output_answ, real_ip, {})
  output_answ
end

#imei_replace_2(login, im_list, real_ip, remote_ip) ⇒ Object

!3 device modify, change imei login imei_old imei_new massive commands im_list = [“imei_new”=>7967843245665] ++



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
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
# File 'lib/imperituroard/projects/iot.rb', line 447

def imei_replace_2(, im_list, real_ip, remote_ip)
  input_json = {:login => , :imei_list => im_list}
  resp_out = {}

  begin

    thr3 = Thread.new do

      if add_functions_connector.check_input_3(, im_list)[:code]==200

        li_new_imei = []
        list1 = {}

        #dictionary for imeis which not processed. Final dictionary
        not_processed_list = []

        #dictionary for devices which was processed correctly
        processed_list = []

        #array for translations from old imei to new
        old_new_translation = {}

        approved_list = []


        #old_imei_list for query to iot platform for data request
        step1_approved_dict_old=[]

        #form dictionary for processing
        for pr1 in im_list
          li_new_imei.append(pr1["imei_new"])
          list1[pr1["imei_new"]]=pr1["imei_old"]
          old_new_translation[pr1["imei_old"]]=pr1["imei_new"]
        end

        #check if imei_new exists in database. If exists - not process this imei
        list_checked = mongo_client.check_imei_exists(li_new_imei)

        internal_func.printer_texter({:function => "imei_replace Step1", :list_checked => list_checked}, "debug")

        #add already exists new IMEI in error dictionary
        for ss in list_checked[:body][:exists]
          not_processed_list.append({:record => {:imei_old => list1[ss], :imei_new => ss}, :error => "New IMEI exists in database"})
        end

        #new_imei list which processed step1
        step2_list = list_checked[:body][:not_exists]

        internal_func.printer_texter({:function => "imei_replace Step2", :step2_list => step2_list}, "debug")


        for a in step2_list
          begin

            #step3 checking permission for writing for imei list
            prof_name1 = mongo_client.get_profile_name_from_imei(list1[a])

            internal_func.printer_texter({:function => "imei_replace Step3", :prof_name1 => prof_name1}, "debug")

            if prof_name1[:code]==200
              permiss1 = mongo_client.(, prof_name1[:body]["profile"])[:code]
              internal_func.printer_texter({:function => "imei_replace Step4", :permiss1 => permiss1, :input => prof_name1[:body]["profile"]}, "debug")
              if permiss1==200

                approved_list.append({:imei_old => list1[a], :imei_new => a})
                step1_approved_dict_old.append(list1[a])

              else
                not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI modification denied"})
              end
            else
              not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Old IMEI not exists in database"})
            end
          rescue
            not_processed_list.append({:record => {:imei_old => list1[a], :imei_new => a}, :error => "Unknown error"})
          end
        end

        internal_func.printer_texter({:function => "imei_replace Step5", :not_processed_list => not_processed_list, :input => list1, :approved_list => approved_list, :step1_approved_dict_old => step1_approved_dict_old}, "debug")


        ##Logic for IOT Platform connection###

        list_from_iot = self.get_info_by_imeilist_from_iot(, step1_approved_dict_old)

        internal_func.printer_texter({:function => "imei_replace Step6", :list_from_iot => list_from_iot, :description => "data from iot platform by old imei"}, "debug")

        #processing data. modifying data on iot platform and mongoDB
        if list_from_iot[:code]=="200" && list_from_iot[:body]["devices"] != nil

          for ard in list_from_iot[:body]["devices"]
            p ard
            new_data_cur_dev = {}
            mongo_answer = {}
            current_old_dev = ard["deviceInfo"]["nodeId"]
            current_device_id = ard["deviceId"]
            new_data_cur_dev = ard["deviceInfo"]
            new_data_cur_dev["nodeId"] = old_new_translation[current_old_dev.to_i].to_s

            p list_from_iot
            p "list_from_iot"


            #########change 2020.05.11. imperituroard
            #########replace process for get iot app_id and password from users to profile

            #credentials = mongo_client.get_iot_oceanconnect_credent(login)
            info_from_mongo = mongo_client.get_imei_info_from_db([current_old_dev.to_i])
            credentials = mongo_client.get_iot_oceanconn_credent_2(info_from_mongo[:body][0]["profile"])

            #########


            if credentials[:code]==200
              flag_remove=0
              flag_create=0
              remove_answer = hua_aceanconnect_connector.remove_one_device_from_iot(credentials[:body][:app_id], credentials[:body][:secret], current_device_id)
              create_answer = hua_aceanconnect_connector.add_new_device_on_huawei2(credentials[:body][:app_id], credentials[:body][:secret], new_data_cur_dev)

              if remove_answer[:code]=="204" || remove_answer[:code]=="200"
                flag_remove=1
              end
              if create_answer[:code]=="200"
                flag_create=1
              end
              if flag_remove==1 && flag_create==1
                mongo_answer = mongo_client.device_modify_any_attr_mongo(current_old_dev.to_i, {:imei => old_new_translation[current_old_dev.to_i], :huadata => {:body => create_answer[:body]}, :updated => DateTime.now})
                processed_list.append({:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]})
              else
                not_processed_list.append({:record => {:imei_old => current_old_dev.to_i, :imei_new => old_new_translation[current_old_dev.to_i]}, :error => "Failed for provisioning to IOT platform"})
              end

              internal_func.printer_texter({:function => "imei_replace Step7", :remove_answer => remove_answer, :create_answer => create_answer, :mongo_answer => mongo_answer, :description => "processing imei #{current_old_dev.to_s}"}, "debug")

            else
              approved_list=[]
            end
          end

        else
          approved_list=[]
        end

        if approved_list!=[]
          resp_out = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
        else
          resp_out = {:code => 202, :result => "Nothing to do", :data => {:approved_list => processed_list, :unapproved_list => not_processed_list}}
        end

      else
        resp_out = {:code => 509, :result => "Input data invalid"}
      end

    end

  rescue
    resp_out = {:code => 507, :result => "Unknown SDK error"}
  end

  thr3.join
  mongo_client.audit_logger("imei_replace", remote_ip, input_json, resp_out, real_ip, {})

  resp_out

end

#set_data_replace_2(soapgw_params, real_ip, remote_ip) ⇒ Object

set_data_replace 13 “token”=>“token”, “datalist”=>[“new_profile”=>“3”, “new_type”=>“1000001”, “new_profile”=>“1”, “new_type”=>“1000003”]} answer = :result=>“Success”, :body=>{:processednum=>1, :failednum=>1, :deviceserr=>}}



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/imperituroard/projects/iot.rb', line 1135

def set_data_replace_2(soapgw_params, real_ip, remote_ip)
  input_params = {:soapgw_params => soapgw_params, :real_ip => real_ip, :remote_ip => remote_ip}
  output_answ = {}
  failes_list = []
  answ_to_soapgw = {}
  not_processed_list = []
  processed_list = []

   = soapgw_params["autorization"]["login"]
  inp_dev_list = soapgw_params["datalist"]

  thr13set = Thread.new do
    inp_dev_list.each do |curr_dev|
      begin
        if curr_dev["imei"] != [] && curr_dev["imei"] != nil && curr_dev["imei"] != ""
          list_checked = mongo_client.check_imei_exists([curr_dev["imei"].to_i])
          if list_checked[:code] == 200 && list_checked[:body][:exists] != [] && list_checked[:body][:not_exists] == []

            #step1 checking permission for writing for imei list
            prof_name1 = mongo_client.get_profile_name_from_imei(curr_dev["imei"].to_i)
            p prof_name1
            p "prof_name1"
            if prof_name1[:code] == 200

              if curr_dev["new_profile"] != "nil" && curr_dev["new_profile"] != "null" && curr_dev["new_profile"] != "empty" && curr_dev["new_profile"] != "nill"
                prof_new_id = mongo_client.get_profile_universal(curr_dev["new_profile"])["profile_id"]
              else
                prof_new_id = prof_name1[:body]["profile_id"]
              end

              if curr_dev["new_type"] != "nil" && curr_dev["new_type"] != "null" && curr_dev["new_type"] != "empty" && curr_dev["new_type"] != "nill"
                type_new_pr = curr_dev["new_type"]
              else
                type_new_pr = prof_name1[:imei_info][:body][0]["device_type"]
              end

              p type_new_pr
              p prof_new_id
              p "st111111"

               = mongo_client.(, prof_name1[:body]["profile_id"])
              p 
              p "get_login_info"
              if [:code] == 200
                 = mongo_client.(, prof_new_id)
                p 
                p "get_login_info_new"
                if [:code] == 200
                  cur_comp1 = mongo_client.compare_profiles(prof_name1[:body]["profile_id"], prof_new_id)
                  p cur_comp1
                  p "cur_comp1"
                  cur_comp2 = mongo_client.compare_device_types(prof_name1[:imei_info][:body][0]["device_type"], type_new_pr)


                  p cur_comp2
                  p "cur_comp2"

                  if cur_comp1[:is_the_same] && cur_comp2[:is_the_same]
                    #the same. Nothing to do
                    processed_list.append(curr_dev)
                    ########
                  else
                    credentials_old = mongo_client.get_iot_oceanconn_credent_2(prof_name1[:body]["profile"])
                    credentials_new = mongo_client.get_iot_oceanconn_credent_2(prof_new_id)
                    p credentials_new
                    p "credentials_new"
                    #########
                    if credentials_old[:code] == 200 && credentials_new[:code] == 200
                      imei_data = mongo_client.get_imei_info_from_db([curr_dev["imei"].to_i])
                      p imei_data
                      p "imei_data"
                      if imei_data[:body] != []
                        ans = {}
                        if imei_data[:body][0]["huadata"]["body"]["deviceId"] != nil
                          model_data = mongo_client.get_device_type_info_universal(type_new_pr)
                          p model_data
                          p "model_data"
                          #####all checks completed. then process data#####
                          ans_old = hua_aceanconnect_connector.remove_one_device_from_iot(credentials_old[:body][:app_id], credentials_old[:body][:secret], imei_data[:body][0]["huadata"]["body"]["deviceId"])


                          if ans_old[:code].to_i == 200 || ans_old[:code].to_i == 202 || ans_old[:code].to_i == 204
                            begin
                              resss = hua_aceanconnect_connector.add_new_device_on_huawei(credentials_new[:body][:app_id],
                                                                                          credentials_new[:body][:secret],
                                                                                          curr_dev["imei"].to_i,
                                                                                          curr_dev["imei"].to_s,
                                                                                          prof_name1[:body]["description"],
                                                                                          model_data[:body]["device_type"],
                                                                                          curr_dev["new_profile"],
                                                                                          model_data[:body]["ManufacturerID"],
                                                                                          model_data[:body]["ManufacturerNAME"],
                                                                                          model_data[:body]["model"]
                              )
                              if resss[:code] == "200"
                                if resss[:body].key?("error_code") && resss[:body]["error_code"] != 200
                                  not_processed_list.append({:imei => curr_dev["imei"], :error => resss})
                                else
                                  p resss
                                  new_id_iot = resss[:body]
                                  new_profile_id = prof_new_id
                                  now_date = DateTime.now
                                  #finished success. update database
                                  attribute = {profile: new_profile_id, device_type: model_data[:body]["model"], huadata: resss, updated: now_date}
                                  answ_mongo = mongo_client.modify_attr_mongo_universal(curr_dev["imei"].to_i, attribute)
                                  if answ_mongo[:code] != 200
                                    not_processed_list.append({:imei => curr_dev["imei"], :error => "Critical error. Mongo not updated", :mongo_err => answ_mongo})
                                  else
                                    processed_list.append(curr_dev)
                                  end
                                end
                              else
                                not_processed_list.append({:imei => curr_dev["imei"], :error => resss})
                              end
                            rescue
                              not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown error with insertion imei on IOT platform"})
                            end
                          else
                            not_processed_list.append({:imei => curr_dev["imei"], :error => "Something wrong with IOT platform"})
                          end
                        else
                          not_processed_list.append({:imei => curr_dev["imei"], :error => "Not processed by iot platform. deviceId not found in mongoDB"})
                        end
                      else
                        not_processed_list.append({:imei => curr_dev["imei"], :error => "Failed to get info from database"})
                      end
                    else
                      not_processed_list.append({:imei => curr_dev["imei"], :error => "Failed to get credentials from database"})
                    end
                  end
                else
                  not_processed_list.append({:imei => curr_dev["imei"], :error => "New profile modification not permitted"})
                end
              else
                not_processed_list.append({:imei => curr_dev["imei"], :error => "Old profile modification not permitted"})
              end
            else
              not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown current profile"})
            end
          else
            not_processed_list.append({:imei => curr_dev["imei"], :error => "Device not exists"})
          end
        else
          not_processed_list.append({:imei => curr_dev["imei"], :error => "IMEI can't be nil"})
        end
      rescue
        not_processed_list.append({:imei => curr_dev["imei"], :error => "Unknown SDK error"})
      end

    end

    err_li = []
    if not_processed_list.length > 0
      not_processed_list.each do |err_l|
        err_li.append({:failedimei => err_l[:imei]})
      end
    end

    if processed_list.length > 0
      answ_to_soapgw = {:code => 200, :result => "Success", :body => {:processednum => processed_list.length, :failednum => not_processed_list.length, :deviceserr => err_li}}
    else
      answ_to_soapgw = {:code => 202, :result => "Nothing done", :body => {:processednum => 0, :failednum => not_processed_list.length, :deviceserr => err_li}}
    end
  end
  thr13set.join

  output_answ = {:code => 200, :result => "Success", :body => {:to_soap_gw => answ_to_soapgw, :not_processed_list => not_processed_list, :processed_list => processed_list}}
  mongo_client.audit_logger("set_data_replace_2", remote_ip, input_params, output_answ, real_ip, {})
  output_answ
end

#spa_service_set_2(soapgw_params, real_ip, remote_ip) ⇒ Object

15 add service iot soapgw_params = => “43563456”, :imsi => “2452345”, :unp => “6454127”, :description => “test”, :note => “test2” “token”=>“token”, “service”=>“imsi”=>“5645645”, “msisdn”=>“3423423”, “unp”=>“4564564”, “note”=>“test”, “descriptiot”=>“test 2”}



1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
# File 'lib/imperituroard/projects/iot.rb', line 1389

def spa_service_set_2(soapgw_params, real_ip, remote_ip)
  input_params = {:soapgw_params => soapgw_params, :real_ip => real_ip, :remote_ip => remote_ip}
  output_answ = {}
  begin
    output_answ = {:code => 200,
                   :message => 'Request completed successfully',
                   :body => {
                       :to_soap_gw => {
                           :code => 200,
                           :result => 'Request completed successfully'
                       }}}
    mongo_client.audit_spa_logger('spa_service_set_2', {:remote_ip => remote_ip, :real_ip => real_ip}, input_params, output_answ)
  rescue
    output_answ = {:code => 500,
                   :message => 'Server error',
                   :body => {
                       :to_soap_gw => {
                           :code => 500,
                           :result => 'Server error'
                       }}}
  end
  output_answ
end

#test1278493Object



1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
# File 'lib/imperituroard/projects/iot.rb', line 1448

def test1278493
  #ddd = MongoIot.new(mongoip, mongoport, mongo_database)
  #ddd.get_profiles_by_login("test")

  ff = [131234123412341233, 131234123127341233]
  #ddd.get_imsi_info_from_db(ff)

  #p ddd.get_profile_id_by_name("1341241")
  #p ddd.get_device_type_info_by_model("BGT_PPMC11")
  p "yyyyy"

  #fff = HuaIot.new("134.17.93.4", "443", "/Users/imperituroard/Desktop/cert.crt", "/Users/imperituroard/Desktop/key.pem")
  p hua_aceanconnect_connector.querying_device_id("Jy5KpoBUOScZEcrO2bRZhzfaIOUa", "VFFWTOb9k3_cQhzrGsRWT4HE8Z0a", "123412341284999")
end