imperituroard 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a07941a7445bf4aa9aa71e24b29e541ffbc92d4
4
- data.tar.gz: 1efa8e5bc158c4450aaadf8928cac6930a673bf9
3
+ metadata.gz: 6c27b1af6fc8da6333b67a5c283bb1ae02a7a475
4
+ data.tar.gz: 1191a3d14976315a5d03b4c4efd10208a36e65f9
5
5
  SHA512:
6
- metadata.gz: 9248cfe42be47e1d1838db5ee49663a345a34d71ba42cb22e212ee8ff22ccf7b1ac14baeb389e242f2bdf43fad35e72304057e00fc6e209cb00de5badc67a55d
7
- data.tar.gz: 35a571da88e98b2dd14554a6725006c1c9d02d68d9c7657635b0864a42a02f565306848ca9c70f5f4a96279b18ab8e9f869e316e0746c8b112ef860298c58baa
6
+ metadata.gz: fe7261a30e0f300ad63eda191ffe88ec4fae8fac984fa5dc460df5db13da1f096df2b1b378220dee2ec9d4abd9541fb22edeec1f0731953540e863fbb7c99955
7
+ data.tar.gz: 4abebbab6074f3dd1ae35d5bb66cab71eb424950d3aafb640669200f4ff976cdf1f84231c3c77ff96c4c4262b0b037e907232c838cb2c2eb68842d7462801616
@@ -0,0 +1,21 @@
1
+ <component name="ProjectRunConfigurationManager">
2
+ <configuration default="false" name="hua_oceanconnect_adapter" type="RubyRunConfigurationType" factoryName="Ruby">
3
+ <module name="imperituroard" />
4
+ <RUBY_RUN_CONFIG NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
5
+ <RUBY_RUN_CONFIG NAME="WORK DIR" VALUE="$MODULE_DIR$/lib/imperituroard/projects/iot" />
6
+ <RUBY_RUN_CONFIG NAME="SHOULD_USE_SDK" VALUE="false" />
7
+ <RUBY_RUN_CONFIG NAME="ALTERN_SDK_NAME" VALUE="" />
8
+ <RUBY_RUN_CONFIG NAME="myPassParentEnvs" VALUE="true" />
9
+ <envs />
10
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="true" />
11
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
12
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" track_test_folders="true" runner="rcov">
13
+ <COVERAGE_PATTERN ENABLED="true">
14
+ <PATTERN REGEXPS="/.rvm/" INCLUDED="false" />
15
+ </COVERAGE_PATTERN>
16
+ </EXTENSION>
17
+ <RUBY_RUN_CONFIG NAME="SCRIPT_PATH" VALUE="$MODULE_DIR$/lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb" />
18
+ <RUBY_RUN_CONFIG NAME="SCRIPT_ARGS" VALUE="" />
19
+ <method />
20
+ </configuration>
21
+ </component>
data/Gemfile CHANGED
@@ -12,4 +12,4 @@ gem 'json', '2.3.0'
12
12
  gem 'mongo', '2.11.4'
13
13
  gem 'nokogiri'
14
14
  gem 'rails'
15
-
15
+ gem 'ipaddr'
@@ -36,4 +36,5 @@ Gem::Specification.new do |spec|
36
36
  #spec.add_development_dependency "uri"
37
37
  spec.add_development_dependency "mongo", "2.11.4"
38
38
  spec.add_development_dependency "nokogiri"
39
+ spec.add_development_dependency "ipaddr"
39
40
  end
data/lib/imperituroard.rb CHANGED
@@ -6,10 +6,13 @@ require "imperituroard/phpipamdb"
6
6
  require "imperituroard/phpipamcps"
7
7
  require "imperituroard/projects/iot/mongoconnector"
8
8
  require "imperituroard/projects/iot/hua_oceanconnect_adapter"
9
+ require "imperituroard/projects/iot/add_functions"
9
10
  require 'json'
11
+ require 'ipaddr'
10
12
 
11
13
  module Imperituroard
12
- class Error < StandardError; end
14
+ class Error < StandardError;
15
+ end
13
16
 
14
17
  def initialize()
15
18
  end
@@ -26,9 +29,20 @@ module Phpipam
26
29
  end
27
30
  end
28
31
 
32
+ class MyJSON
33
+ def self.valid?(value)
34
+ result = JSON.parse(value)
35
+
36
+ result.is_a?(Hash) || result.is_a?(Array)
37
+ rescue JSON::ParserError, TypeError
38
+ false
39
+ end
40
+ end
41
+
29
42
  class Pipam
30
43
 
31
44
  attr_accessor :username, :password, :ip, :database_class, :cps_class
45
+
32
46
  def initialize(db_username, db_password, db_ip, cps_wsdl, cps_endpoint, cps_namespace)
33
47
  @username = db_username
34
48
  @password = db_password
@@ -66,13 +80,25 @@ class Pipam
66
80
  "failed"
67
81
  end
68
82
  end
83
+
69
84
  include Phpipam
70
85
  end
71
86
 
72
87
  class Iot
73
- attr_accessor :mongoip, :mongoport, :iotip, :iottoken, :mongo_database, :iotplatform_ip, :iotplatform_port, :cert_path, :key_path, :mongo_client
74
-
75
- def initialize(mongoip, mongoport, iotip, mongo_database, iotplatform_ip, iotplatform_port, cert_path, key_path)
88
+ attr_accessor :mongoip,
89
+ :mongoport,
90
+ :iotip,
91
+ :iottoken,
92
+ :mongo_database,
93
+ :iotplatform_ip,
94
+ :iotplatform_port,
95
+ :cert_path,
96
+ :key_path,
97
+ :mongo_client,
98
+ :add_functions_connector
99
+
100
+ def initialize(mongoip, mongoport, iotip, mongo_database,
101
+ iotplatform_ip, iotplatform_port, cert_path, key_path, telegram_api_url, telegram_chat_id)
76
102
  @mongoip = mongoip
77
103
  @mongoport = mongoport
78
104
  @iotip = iotip
@@ -82,88 +108,128 @@ class Iot
82
108
  @cert_path = cert_path
83
109
  @key_path = key_path
84
110
  @mongo_client = MongoIot.new(mongoip, mongoport, mongo_database)
111
+ @add_functions_connector = AdditionalFunc.new(telegram_api_url, telegram_chat_id)
85
112
  end
86
113
 
87
114
  #error list
88
115
 
89
116
  #:code => 507, :result => "Unknown SDK error"
117
+ #{:code => 200, :result => "Request completed successfully", :body => result_ps}
90
118
 
91
119
 
92
- #1. Add device to profile
120
+ #!1. Add device to profile (only for new device)
93
121
  #login - login for client identification
94
122
  #profile - profile for device
95
123
  #imei_list - device identificator
96
- #imei_list = {
97
- # imei: imei,
98
- # imsi: imsi,
99
- # msisdn: msisdn,
100
- # description: description,
101
- # note: note,
102
- # profile: profile,
103
- # type: type,
104
- # address: address
105
- #}
124
+ #imei_list = [{"imei" => 131234123412341233, "description" => "dfdsf", "note"=>"second description", "profile"=>0},
125
+ #{"imei" => 56213126347645784, "description" => "dfdsf", "note"=>"second description", "profile"=>0}]
106
126
  #massive commands
107
- #+
108
- def add_device_to_profile(login, imei_list)
109
- input_json = {:login => login, :imei_list =>imei_list}
110
-
127
+ #++
128
+ def add_device_to_profile(login, imei_list, remote_ip)
129
+ input_json = {:login => login, :imei_list => imei_list}
130
+ resp_out = {}
111
131
  begin
112
- imei = []
113
- list1 = {}
114
- for_insert = []
115
- for t in imei_list
116
- imei.append(t["imei"])
117
- list1[t["imei"]] = t
118
- end
119
- get_login_info = mongo_client.check_login_profile_permiss(login, "profile")[:code]
120
- if get_login_info==200
121
- list_exists = mongo_client.check_imei_exists(imei)
122
- for_upload = imei - list_exists
123
- for h in for_upload
124
- for_insert.append(list1[h])
132
+ thr1 = Thread.new do
133
+
134
+ imei = []
135
+ list1 = {}
136
+ for_insert = []
137
+ not_processed_list = []
138
+ processed_list = []
139
+
140
+ for ii in imei_list
141
+ list1[ii["imei"]] = ii
142
+ imei.append(ii["imei"])
143
+ end
144
+ list_checked = mongo_client.check_imei_exists(imei)
145
+ for ss in list_checked[:body][:exists]
146
+ not_processed_list.append({:imei=>ss, :error => "Device exists in database"})
125
147
  end
148
+
149
+ for jj in list_checked[:body][:not_exists]
150
+ begin
151
+ get_login_info = mongo_client.check_login_prof_perm_id_one(login, list1[jj]["profile"])[:code]
152
+ if get_login_info==200
153
+ for_insert.append(list1[jj])
154
+ else
155
+ not_processed_list.append({:imei=>list1[jj], :error => "Permission denied for this profile"})
156
+ end
157
+ rescue
158
+ not_processed_list.append({:imei=>list1[jj], :error => "Unknown error"})
159
+ end
160
+ end
161
+
162
+ begin
163
+ if for_insert!=[]
126
164
  mongo_client.imei_insert_list(for_insert)
127
- else get_login_info
128
- end
165
+ resp_out = {:code => 200, :result => "Data processed", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
166
+ else
167
+ resp_out = {:code => 202, :result => "Nothing for insertion", :body => {:imei_processed => for_insert, :error_list => not_processed_list}}
168
+
169
+ end
170
+ rescue
171
+ resp_out = {:code => 505, :result => "Error with database communication"}
172
+ end
173
+ end
129
174
  rescue
130
- return {:code => 507, :result => "Unknown SDK error"}
175
+ resp_out = {:code => 507, :result => "Unknown SDK error"}
131
176
  end
132
- mongo_client.audit_logger("add_device_to_profile", "127.0.0.1", input_json, "resp")
177
+ thr1.join
178
+ mongo_client.audit_logger("add_device_to_profile", remote_ip, input_json, resp_out)
179
+ resp_out
133
180
  end
134
181
 
135
182
 
136
- #2 Find device (only mongo datebase. IOT platform not need)
183
+ #!2 Find device (only mongo datebase. IOT platform not need)
184
+ # procedure for data selection from mongo database.
185
+ # for this function IOT platform not need
137
186
  # login
138
187
  # imei
139
188
  # imei_list =[41234,23452345,132412]
140
- #+
141
- def device_find(login, imei_list)
142
- input_json = {:login => login, :imei_list =>imei_list}
143
-
189
+ #++
190
+ def device_find(login, imei_list, remote_ip)
191
+ input_json = {:login => login, :imei_list => imei_list}
144
192
  ime_list_approved = []
145
193
  ime_list_notapproved = []
146
-
194
+ resp = {}
147
195
  begin
148
- for t in imei_list
149
- prof_name1 = mongo_client.get_profile_name_from_imei(t)
150
- p "prof_name1"
151
- p prof_name1
152
- if prof_name1!=nil
153
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
154
- if permiss1==200
155
- ime_list_approved.append(t)
196
+ thr2 = Thread.new do
197
+ for t in imei_list
198
+ prof_name1 = mongo_client.get_profile_name_from_imei(t)
199
+ if prof_name1[:code]==200
200
+ begin
201
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1[:body]["profile"])[:code]
202
+ if permiss1==200
203
+ ime_list_approved.append(t)
204
+ else
205
+ ime_list_notapproved.append({:imei => t, :error => permiss1})
206
+ end
207
+ rescue
208
+ ime_list_notapproved.append({:imei => t, :error => {:code => 405, :result => "Unknown error when check_login_profile_permiss imei #{t.to_s}"}})
209
+ end
210
+ else
211
+ ime_list_notapproved.append({:imei => t, :error => prof_name1})
156
212
  end
157
213
  end
214
+ begin
215
+ if ime_list_approved != []
216
+ data = mongo_client.get_imei_info_from_db(ime_list_approved)
217
+
218
+ resp = {:code => 200, :result => "Request completed successfully", :data => {:approved_list => data, :unapproved_list => ime_list_notapproved}}
219
+
220
+ else
221
+ resp = {:code => 404, :result => "Invalidate data", :data => {:approved_list => [], :unapproved_list => ime_list_notapproved}}
222
+ end
223
+ rescue
224
+ resp = {:code => 504, :result => "Unsuccessfully data transfer"}
225
+ end
158
226
  end
159
- data = mongo_client.get_imei_info_from_db(ime_list_approved)
160
- {:code => 507, :result => "Unknown SDK error", :data => data}
161
227
  rescue
162
- {:code => 507, :result => "Unknown SDK error"}
228
+ resp = {:code => 507, :result => "Unknown SDK error"}
163
229
  end
164
-
165
- mongo_client.audit_logger("add_device_to_profile", "127.0.0.1", input_json, "resp")
166
-
230
+ thr2.join
231
+ mongo_client.audit_logger("device_find", remote_ip, input_json, resp)
232
+ resp
167
233
  end
168
234
 
169
235
  #3 device modify, change imei
@@ -174,17 +240,17 @@ class Iot
174
240
  #im_list = [{:imei_old=>7967843245667, :imei_new=>7967843245665}]
175
241
  #+
176
242
  def imei_replace(login, im_list)
177
- input_json = {:login => login, :imei_list =>im_list}
243
+ input_json = {:login => login, :imei_list => im_list}
178
244
  begin
179
- for a in im_list
180
- p a
181
- prof_name1 = mongo_client.get_profile_name_from_imei(a[:imei_old])
182
- p prof_name1
183
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
184
- if permiss1==200
185
- mongo_client.device_modify_any_attr_mongo(a[:imei_old],{:imei=>a[:imei_new]})
245
+ for a in im_list
246
+ p a
247
+ prof_name1 = mongo_client.get_profile_name_from_imei(a[:imei_old])
248
+ p prof_name1
249
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
250
+ if permiss1==200
251
+ mongo_client.device_modify_any_attr_mongo(a[:imei_old], {:imei => a[:imei_new]})
252
+ end
186
253
  end
187
- end
188
254
  rescue
189
255
  {:code => 507, :result => "Unknown SDK error"}
190
256
  end
@@ -203,16 +269,16 @@ class Iot
203
269
  #+
204
270
  def device_remove(login, imei)
205
271
 
206
- input_json = {:login => login, :imei_list =>imei}
272
+ input_json = {:login => login, :imei_list => imei}
207
273
  resp = {}
208
274
 
209
275
  begin
210
276
 
211
- prof_name = mongo_client.get_profile_name_from_imei(imei)
212
- permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
213
- if permiss==200
214
- resp = mongo_client.device_remove_single_mongo(imei)
215
- end
277
+ prof_name = mongo_client.get_profile_name_from_imei(imei)
278
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
279
+ if permiss==200
280
+ resp = mongo_client.device_remove_single_mongo(imei)
281
+ end
216
282
  rescue
217
283
  {:code => 507, :result => "Unknown SDK error"}
218
284
  end
@@ -228,19 +294,24 @@ class Iot
228
294
  #newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}]
229
295
  #+
230
296
  def device_add_address(login, newdevice_list)
297
+ add_functions_connector.telegram_message(newdevice_list.to_s)
298
+ p newdevice_list
299
+ p "gas"
300
+ p MyJSON.valid?(newdevice_list[0].to_s)
301
+ p "sdfsdfgs"
231
302
  input_json = {:login => login, :devices => newdevice_list}
232
303
  begin
233
- resp = {}
234
- for p in newdevice_list
235
- prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
236
-
237
- p "prof_name"
238
- p prof_name
239
- permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
240
- if permiss==200
241
- resp = mongo_client.device_modify_attr_mongo(p[:imei],p[:address])
304
+ resp = {}
305
+ for p in newdevice_list
306
+ prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
307
+
308
+ p "prof_name"
309
+ p prof_name
310
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
311
+ if permiss==200
312
+ resp = mongo_client.device_modify_attr_mongo(p[:imei], p[:address])
313
+ end
242
314
  end
243
- end
244
315
  return resp
245
316
  rescue
246
317
  {:code => 507, :result => "Unknown SDK error"}
@@ -256,34 +327,36 @@ class Iot
256
327
  #newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}]
257
328
  #+
258
329
  def add_service(login, device_list)
259
- input_json = {:login => login, :devices => device_list}
260
330
 
261
- begin
262
331
 
263
- for g in device_list
264
- prof_name1 = mongo_client.get_profile_name_from_imei(g[:imei])
265
- p prof_name1
266
- permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
267
- p permiss1
268
- permiss2 = mongo_client.check_login_profile_permiss(login, g[:attributes][:profile])[:code]
269
- if permiss1==200 && permiss2==200
332
+ input_json = {:login => login, :devices => device_list}
270
333
 
271
- attr = g[:attributes]
272
- #mod_attr = {}
334
+ begin
273
335
 
274
- if attr.key?(:profile)
275
- if attr[:profile].is_a? Integer
276
- p "Ok"
277
- else
278
- p new = mongo_client.get_profile_id_by_name(attr[:profile])
279
- attr[:profile] = new["profile_id"]
336
+ for g in device_list
337
+ prof_name1 = mongo_client.get_profile_name_from_imei(g[:imei])
338
+ p prof_name1
339
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
340
+ p permiss1
341
+ permiss2 = mongo_client.check_login_profile_permiss(login, g[:attributes][:profile])[:code]
342
+ if permiss1==200 && permiss2==200
343
+
344
+ attr = g[:attributes]
345
+ #mod_attr = {}
346
+
347
+ if attr.key?(:profile)
348
+ if attr[:profile].is_a? Integer
349
+ p "Ok"
350
+ else
351
+ p new = mongo_client.get_profile_id_by_name(attr[:profile])
352
+ attr[:profile] = new["profile_id"]
353
+ end
280
354
  end
281
- end
282
- p attr
355
+ p attr
283
356
 
284
- mongo_client.device_modify_any_attr_mongo(g[:imei],attr)
357
+ mongo_client.device_modify_any_attr_mongo(g[:imei], attr)
358
+ end
285
359
  end
286
- end
287
360
  rescue
288
361
  {:code => 507, :result => "Unknown SDK error"}
289
362
  end
@@ -293,10 +366,8 @@ class Iot
293
366
  end
294
367
 
295
368
 
296
-
297
-
298
369
  def test()
299
- ddd = MongoIot.new(mongoip, mongoport, mongo_database)
370
+ ddd = MongoIot.new(mongoip, mongoport, mongo_database)
300
371
  #ddd.get_profiles_by_login("test")
301
372
 
302
373
  ff = [131234123412341233, 131234123127341233]
@@ -306,7 +377,6 @@ class Iot
306
377
  end
307
378
 
308
379
 
309
-
310
380
  def testhua()
311
381
  cert_file = cert_path
312
382
  key_file = key_path
@@ -327,10 +397,11 @@ class Iot
327
397
  tt = "{\"deviceId\":\"fad0a417-b6a3-4b0b-abfc-fa2b0af9691a\",\"verifyCode\":\"6cb6dcca\",\"timeout\":180,\"psk\":\"1d16b55d577bc1f2e5e75d416ce6b8a2\"}"
328
398
  #tt = tt.gsub("\\","")
329
399
  #p tt
330
- ff = tt.to_s
400
+ ff = tt.to_s
331
401
  p ff
332
402
  gg = JSON.parse(ff)
333
403
  p gg
334
404
  end
335
405
 
336
406
  end
407
+
@@ -0,0 +1,34 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+
6
+ class AdditionalFunc
7
+
8
+ attr_accessor :telegram_api_url, :telegram_chat_id
9
+
10
+ def initialize(telegram_api_url, telegram_chat_id)
11
+ @telegram_api_url = telegram_api_url
12
+ @telegram_chat_id = telegram_chat_id
13
+ end
14
+
15
+ #procedure for send log to telegram chat
16
+ def telegram_message(message)
17
+ begin
18
+ uri = URI.parse(telegram_api_url)
19
+ https_connector = Net::HTTP.new(uri.host, uri.port)
20
+ https_connector.use_ssl = true
21
+ data = {chat_id: telegram_chat_id, text: message}
22
+ request_mess = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
23
+ request_mess.body = data.to_json
24
+ response_mess = https_connector.request(request_mess)
25
+ body = response_mess.body
26
+ return {:code => 200,
27
+ :result => "Request completed successfully",
28
+ :body => {:telegram_resp => JSON.parse(body.to_s),
29
+ :description => "Telegram message to telegram_chat_id: #{telegram_chat_id.to_s}"}}
30
+ rescue
31
+ return {:code => 507, :result => "Unknown SDK error"}
32
+ end
33
+ end
34
+ end
@@ -24,27 +24,27 @@ class HuaIot
24
24
 
25
25
  def parse_token(str)
26
26
  begin
27
- dd = str.split(",")
28
- acc_token = ""
29
- refr_token = ""
30
- exp_in = ""
31
-
32
- access_token = /\"accessToken\":\"(\S+)\"/
33
- refresh_token = /\"refreshToken\":\"(.+)\"/
34
- expires_in = /\"expiresIn\":(\d+)/
35
-
36
- for i in dd
37
- if i.to_s.include?("accessToken")
38
- acc_token = access_token.match(i)
39
- elsif i.to_s.include?("refreshToken")
40
- refr_token = refresh_token.match(i)
41
- elsif i.to_s.include?("expiresIn")
42
- exp_in = expires_in.match(i)
27
+ dd = str.split(",")
28
+ acc_token = ""
29
+ refr_token = ""
30
+ exp_in = ""
31
+
32
+ access_token = /\"accessToken\":\"(\S+)\"/
33
+ refresh_token = /\"refreshToken\":\"(.+)\"/
34
+ expires_in = /\"expiresIn\":(\d+)/
35
+
36
+ for i in dd
37
+ if i.to_s.include?("accessToken")
38
+ acc_token = access_token.match(i)
39
+ elsif i.to_s.include?("refreshToken")
40
+ refr_token = refresh_token.match(i)
41
+ elsif i.to_s.include?("expiresIn")
42
+ exp_in = expires_in.match(i)
43
+ end
43
44
  end
44
- end
45
- {:status=> 200, :result=>"OK", :accessToken=>acc_token[1], :refreshToken=>refr_token[1], :expiresIn => exp_in[1] }
45
+ {:status => 200, :result => "OK", :accessToken => acc_token[1], :refreshToken => refr_token[1], :expiresIn => exp_in[1]}
46
46
  rescue
47
- {:status=> 500, :result=>"failed"}
47
+ {:status => 500, :result => "failed"}
48
48
  end
49
49
  end
50
50
 
@@ -54,10 +54,10 @@ class HuaIot
54
54
  uri = URI.parse url_string
55
55
  https = Net::HTTP.new(uri.host, uri.port)
56
56
  https.use_ssl = true
57
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
58
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file) )
57
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
58
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
59
59
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
60
- request = Net::HTTP::Post.new( uri.path)
60
+ request = Net::HTTP::Post.new(uri.path)
61
61
  data = {
62
62
  :appId => app_id,
63
63
  :secret => secret
@@ -76,10 +76,10 @@ class HuaIot
76
76
  uri = URI.parse url_string
77
77
  https = Net::HTTP.new(uri.host, uri.port)
78
78
  https.use_ssl = true
79
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
80
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file) )
79
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
80
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
81
81
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
82
- request = Net::HTTP::Post.new( uri.path)
82
+ request = Net::HTTP::Post.new(uri.path)
83
83
  request.content_type = 'application/json'
84
84
  request['Authorization'] = 'Bearer ' + token
85
85
  request['app_key'] = app_id
@@ -92,15 +92,15 @@ class HuaIot
92
92
 
93
93
  def dev_delete(app_id, secret, node_id)
94
94
  token = get_token(app_id, secret)[:body]["accessToken"]
95
- path = "/iocm/app/dm/v1.1.0/devices/" + node_id + "?app_Id=" + app_id + "&cascade=true"
95
+ path = "/iocm/app/dm/v1.1.0/devices/" + node_id + "?app_Id=" + app_id + "&cascade=true"
96
96
  url_string = "https://" + platformip + ":" + platformport + path
97
97
  uri = URI.parse url_string
98
98
  https = Net::HTTP.new(uri.host, uri.port)
99
99
  https.use_ssl = true
100
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
101
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
100
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
101
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
102
102
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
103
- request = Net::HTTP::Delete.new( uri.path)
103
+ request = Net::HTTP::Delete.new(uri.path)
104
104
  request.content_type = 'application/json'
105
105
  request['Authorization'] = 'Bearer ' + token
106
106
  request['app_key'] = app_id
@@ -120,10 +120,10 @@ class HuaIot
120
120
  uri = URI.parse url_string
121
121
  https = Net::HTTP.new(uri.host, uri.port)
122
122
  https.use_ssl = true
123
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
124
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
123
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
124
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
125
125
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
126
- request = Net::HTTP::Get.new( uri.path)
126
+ request = Net::HTTP::Get.new(uri.path)
127
127
  request.content_type = 'application/json'
128
128
  request['Authorization'] = 'Bearer ' + token
129
129
  request['app_key'] = app_id
@@ -136,15 +136,15 @@ class HuaIot
136
136
  #2.2.14 Querying Device Activation Status
137
137
  def querying_device_activ_status(app_id, secret, device_id)
138
138
  token = get_token(app_id, secret)[:body]["accessToken"]
139
- path = "/iocm/app/reg/v1.1.0/devices/" + device_id + "?app_Id=" + app_id
139
+ path = "/iocm/app/reg/v1.1.0/devices/" + device_id + "?app_Id=" + app_id
140
140
  url_string = "https://" + platformip + ":" + platformport + path
141
141
  uri = URI.parse url_string
142
142
  https = Net::HTTP.new(uri.host, uri.port)
143
143
  https.use_ssl = true
144
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
145
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
144
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
145
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
146
146
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
147
- request = Net::HTTP::Get.new( uri.path)
147
+ request = Net::HTTP::Get.new(uri.path)
148
148
  request.content_type = 'application/json'
149
149
  request['Authorization'] = 'Bearer ' + token
150
150
  request['app_key'] = app_id
@@ -156,15 +156,15 @@ class HuaIot
156
156
  #2.9.1 Querying Information About a Device
157
157
  def querying_device_info(app_id, secret, device_id)
158
158
  token = get_token(app_id, secret)[:body]["accessToken"]
159
- path = "/iocm/app/dm/v1.1.0/devices/" + device_id + "?app_Id=" + app_id
159
+ path = "/iocm/app/dm/v1.1.0/devices/" + device_id + "?app_Id=" + app_id
160
160
  url_string = "https://" + platformip + ":" + platformport + path
161
161
  uri = URI.parse url_string
162
162
  https = Net::HTTP.new(uri.host, uri.port)
163
163
  https.use_ssl = true
164
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
165
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
164
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
165
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
166
166
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
167
- request = Net::HTTP::Get.new( uri.path)
167
+ request = Net::HTTP::Get.new(uri.path)
168
168
  request.content_type = 'application/json'
169
169
  request['Authorization'] = 'Bearer ' + token
170
170
  request['app_key'] = app_id
@@ -180,10 +180,10 @@ class HuaIot
180
180
  uri = URI.parse url_string
181
181
  https = Net::HTTP.new(uri.host, uri.port)
182
182
  https.use_ssl = true
183
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
184
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
183
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
184
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
185
185
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
186
- request = Net::HTTP::Post.new( uri.path)
186
+ request = Net::HTTP::Post.new(uri.path)
187
187
  request.content_type = 'application/json'
188
188
  request['Authorization'] = 'Bearer ' + token
189
189
  request['app_key'] = app_id
@@ -201,10 +201,10 @@ class HuaIot
201
201
  uri = URI.parse url_string
202
202
  https = Net::HTTP.new(uri.host, uri.port)
203
203
  https.use_ssl = true
204
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
205
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file))
204
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
205
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
206
206
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
207
- request = Net::HTTP::Get.new( uri.path)
207
+ request = Net::HTTP::Get.new(uri.path)
208
208
  request.content_type = 'application/json'
209
209
  request['Authorization'] = 'Bearer ' + token
210
210
  request['app_key'] = app_id
@@ -216,12 +216,12 @@ class HuaIot
216
216
 
217
217
  url_string = "https://134.17.93.4:8743/iocm/app/sec/v1.1.0/login"
218
218
  headers = {
219
- 'Authorization'=>'Bearer ppeMsOq6zdb2fSUH4GoRooS_FgEa',
220
- 'Content-Type' =>'application/json',
221
- 'Accept'=>'application/json'
219
+ 'Authorization' => 'Bearer ppeMsOq6zdb2fSUH4GoRooS_FgEa',
220
+ 'Content-Type' => 'application/json',
221
+ 'Accept' => 'application/json'
222
222
  }
223
223
 
224
- req = {"dstAppId": "bCRahH5zSi9SNmyfqv3BkJABAq8a" }
224
+ req = {"dstAppId": "bCRahH5zSi9SNmyfqv3BkJABAq8a"}
225
225
  post_data = URI.encode_www_form(req)
226
226
 
227
227
  uri = URI.parse url_string
@@ -234,13 +234,13 @@ class HuaIot
234
234
  cert_file = "/Users/imperituroard/Desktop/cert.crt"
235
235
  key_file = "/Users/imperituroard/Desktop/key.pem"
236
236
 
237
- p https = Net::HTTP.new(uri.host, uri.port)
237
+ p https = Net::HTTP.new(uri.host, uri.port)
238
238
  https.use_ssl = true
239
- https.cert = OpenSSL::X509::Certificate.new( File.read(cert_file) )
240
- https.key = OpenSSL::PKey::RSA.new( File.read(key_file) )
239
+ https.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
240
+ https.key = OpenSSL::PKey::RSA.new(File.read(key_file))
241
241
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
242
242
 
243
- request = Net::HTTP::Post.new( uri.path)
243
+ request = Net::HTTP::Post.new(uri.path)
244
244
 
245
245
  data = {
246
246
  :appId => "bCRahH5zSi9SNmyfqv3BkJABAq8a",
@@ -261,9 +261,8 @@ class HuaIot
261
261
 
262
262
  end
263
263
 
264
- # App ID
265
- # password O2k2aMStOweZOeSoVDYjI3c6uaMa
266
-
264
+ # App ID
265
+ # password O2k2aMStOweZOeSoVDYjI3c6uaMa
267
266
 
268
267
 
269
268
  end
@@ -19,19 +19,26 @@ class MongoIot
19
19
  end
20
20
 
21
21
  def audit_logger(proc_name, src_ip, input_json, output_json)
22
- collection = client[:audit]
23
- doc = {
24
- :proc_name => proc_name,
25
- :sender => {:src_ip => src_ip},
26
- :input_params => input_json,
27
- :output_params => output_json
28
- }
29
- p doc
30
- result = collection.insert_one(doc)
31
- p result
32
-
22
+ begin
23
+ d = DateTime.now
24
+ current = d.strftime("%d/%m/%Y %H:%M:%S")
25
+ collection = client[:audit]
26
+ doc = {
27
+ :proc_name => proc_name,
28
+ :date => current,
29
+ :sender => {:src_ip => src_ip},
30
+ :input_params => input_json,
31
+ :output_params => output_json
32
+ }
33
+ result = collection.insert_one(doc)
34
+ return {:code => 200, :result => "Request completed successfully", :body => result}
35
+ rescue
36
+ return {:code => 507, :result => "Unknown SDK error"}
37
+ end
33
38
  end
34
39
 
40
+ #:code => 507, :result => "Unknown SDK error"
41
+ #{:code => 200, :result => "Request completed successfully", :body => result_ps}
35
42
  def get_profiles_by_login(login)
36
43
  begin
37
44
  login_profiles = []
@@ -39,18 +46,18 @@ class MongoIot
39
46
  result_ps = []
40
47
  collection = client[:users]
41
48
  collection2 = client[:device_profiles]
42
- collection.find({:login => login}).each { |row|
49
+ collection.find({:login => login}).each {|row|
43
50
  login_profiles = row["permit_profiles"]
44
51
  }
45
52
  for i in login_profiles
46
53
  req2.append({:profile_id => i})
47
54
  end
48
- collection2.find({:$or => req2}, {:_id => 0}).each { |row|
55
+ collection2.find({:$or => req2}, {:_id => 0}).each {|row|
49
56
  result_ps.append(row)
50
57
  }
51
- result_ps
58
+ return {:code => 200, :result => "Request completed successfully", :body => result_ps}
52
59
  rescue
53
- []
60
+ return {:code => 507, :result => "Unknown SDK error"}
54
61
  end
55
62
  end
56
63
 
@@ -62,13 +69,12 @@ class MongoIot
62
69
  for i in imeilist
63
70
  req2.append({:imei => i})
64
71
  end
65
- p req2
66
- collection.find({:$or => req2}, {:_id => 0}).each { |row|
72
+ collection.find({:$or => req2}, {:_id => 0}).each {|row|
67
73
  result_ps.append(row)
68
74
  }
69
- result_ps
75
+ return {:code => 200, :result => "Request completed successfully", :body => result_ps}
70
76
  rescue
71
- []
77
+ return {:code => 507, :result => "Unknown SDK error"}
72
78
  end
73
79
  end
74
80
 
@@ -76,7 +82,7 @@ class MongoIot
76
82
  begin
77
83
  result_ps = []
78
84
  collection = client[:device_profiles]
79
- collection.find({"profile" => profile_name}).each { |row|
85
+ collection.find({"profile" => profile_name}).each {|row|
80
86
  result_ps.append(row)
81
87
  }
82
88
  result_ps[0]
@@ -89,7 +95,7 @@ class MongoIot
89
95
  begin
90
96
  result_ps = []
91
97
  collection = client[:device_profiles]
92
- collection.find({"profile_id" => profile_id}).each { |row|
98
+ collection.find({"profile_id" => profile_id}).each {|row|
93
99
  result_ps.append(row)
94
100
  }
95
101
  result_ps[0]
@@ -106,11 +112,12 @@ class MongoIot
106
112
  p get_login_info
107
113
  dst_profile = get_profile_id_by_name(profile)
108
114
  p "dst_profile"
109
- p dst_profile
115
+ p dst_profile
110
116
  access=1
111
- if get_login_info!=[]
117
+ if get_login_info[:body]!=[]
112
118
  if dst_profile!=[]
113
- for j in get_login_info
119
+ p "sgsgsd"
120
+ for j in get_login_info[:body]
114
121
  p j
115
122
  if j["profile_id"]==dst_profile["profile_id"]
116
123
  access=0
@@ -118,22 +125,61 @@ class MongoIot
118
125
  if access==0
119
126
  return {:code => 200, :result => "Permission granted"}
120
127
  else
121
- return {:code => 400,:result => "Access denied. This incident will be reported."}
128
+ return {:code => 400, :result => "Access denied. This incident will be reported."}
122
129
  end
123
130
  end
124
- else return {:code => 501,:result => "Profile not found"}
131
+ else
132
+ return {:code => 501, :result => "Profile not found"}
125
133
  end
126
- else {:code => 500,:result => "Login not found"}
134
+ else
135
+ {:code => 500, :result => "Login not found"}
136
+ end
137
+ end
138
+
139
+
140
+ def check_login_prof_perm_id_one(login, profile_id)
141
+ p "profile"
142
+ p profile_id
143
+ get_login_info = get_profiles_by_login(login)
144
+ p "get_login_info"
145
+ p get_login_info
146
+ access=1
147
+ if get_login_info[:body]!=[]
148
+ p "sgsgsd"
149
+ for j in get_login_info[:body]
150
+ p j
151
+ if j["profile_id"]==profile_id
152
+ access=0
153
+ end
154
+ if access==0
155
+ return {:code => 200, :result => "Permission granted"}
156
+ else
157
+ return {:code => 400, :result => "Access denied. This incident will be reported."}
158
+ end
159
+ end
160
+ else
161
+ {:code => 500, :result => "Login not found"}
127
162
  end
128
163
  end
129
164
 
130
165
  def check_imei_exists(imei_list)
131
- res = []
132
- imei_list = get_imei_info_from_db(imei_list)
133
- for k in imei_list
134
- res.append(k["imei"])
166
+ res_exists = []
167
+ imei_list_res = get_imei_info_from_db(imei_list)
168
+ p imei_list_res
169
+ p "imei_list"
170
+ for k in imei_list_res[:body]
171
+ p k
172
+ res_exists.append(k["imei"])
135
173
  end
136
- res
174
+ p "aaaa"
175
+ p imei_list
176
+ p res_exists
177
+ not_ex = imei_list - res_exists
178
+ p "not_ex"
179
+ p not_ex
180
+ p res_exists
181
+ {:code => 200, :result => "check_imei_exists: Request completed successfully",
182
+ :body => {:exists => res_exists, :not_exists => not_ex}}
137
183
  end
138
184
 
139
185
 
@@ -161,31 +207,62 @@ class MongoIot
161
207
  end
162
208
 
163
209
  def get_profile_name_from_imei(imei)
164
- info = get_imei_info_from_db([imei])
165
- p "123"
166
- p info
167
- id = info[0]["profile"]
168
- get_profile_name_by_id(id)
210
+ begin
211
+ begin
212
+ info = get_imei_info_from_db([imei])
213
+ if info[:body]==[]
214
+ return {:code => 505, :result => "get_profile_name_from_imei: get_imei_info_from_db returned empty list from database. IMEIS not found"}
215
+ else
216
+ p "fshhsdf"
217
+ p info
218
+ id = info[:body][0]["profile"]
219
+ end
220
+ rescue
221
+ return {:code => 506, :result => "get_profile_name_from_imei: Function get_imei_info_from_db not processed correctly and returned: #{info.to_s}"}
222
+ end
223
+ begin
224
+ res = get_profile_name_by_id(id)
225
+ if res.key?("profile")
226
+ res=res
227
+ else
228
+ return {: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}"}
229
+ end
230
+
231
+ rescue
232
+ return {:code => 506, :result => "get_profile_name_from_imei: Function get_profile_name_by_id not processed correctly and returned: #{res.to_s}"}
233
+
234
+ end
235
+ begin
236
+ if res["profile"]!=nil
237
+ return {:code => 200, :result => "get_profile_name_from_imei: Request completed successfully", :body => res}
238
+ end
239
+ rescue
240
+ return {:code => 506, :result => "get_profile_name_from_imei: Function get_profile_name_by_id not processed correctly and returned: #{res.to_s}"}
241
+ end
242
+ rescue
243
+ return {:code => 507, :result => "get_profile_name_from_imei: Unknown SDK error"}
244
+ end
245
+
169
246
  end
170
247
 
171
248
 
172
249
  def device_remove_single_mongo(imei)
173
250
 
174
- collection = client[:device_imei]
175
- doc = {
176
- "imei" => imei
177
- }
178
- result = collection.delete_many(doc)
179
- p result
251
+ collection = client[:device_imei]
252
+ doc = {
253
+ "imei" => imei
254
+ }
255
+ result = collection.delete_many(doc)
256
+ p result
180
257
  end
181
258
 
182
- def device_modify_attr_mongo(imei,address)
259
+ def device_modify_attr_mongo(imei, address)
183
260
  begin
184
261
  collection = client[:device_imei]
185
262
  doc = {
186
263
  "imei" => imei
187
264
  }
188
- sett = {'$set' => { address: address}}
265
+ sett = {'$set' => {address: address}}
189
266
  result = collection.update_one(doc, sett)
190
267
  p result
191
268
  rescue
@@ -193,7 +270,7 @@ class MongoIot
193
270
  end
194
271
  end
195
272
 
196
- def device_modify_any_attr_mongo(imei,attr_list)
273
+ def device_modify_any_attr_mongo(imei, attr_list)
197
274
  begin
198
275
  collection = client[:device_imei]
199
276
  doc = {
@@ -208,8 +285,6 @@ class MongoIot
208
285
  end
209
286
 
210
287
 
211
-
212
-
213
288
  def ttt
214
289
  p "111111"
215
290
  begin
@@ -220,7 +295,7 @@ class MongoIot
220
295
  collection = client[:audit]
221
296
  doc = {
222
297
  name: 'Steve',
223
- hobbies: [ 'hiking', 'tennis', 'fly fishing' ],
298
+ hobbies: ['hiking', 'tennis', 'fly fishing'],
224
299
  siblings: {
225
300
  brothers: 0,
226
301
  sisters: 1
@@ -235,6 +310,5 @@ class MongoIot
235
310
  end
236
311
 
237
312
 
238
-
239
313
  end
240
314
  end
@@ -1,3 +1,3 @@
1
1
  module Imperituroard
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imperituroard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Buynovskiy
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: ipaddr
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: Gem from imperituroard for different actions
126
140
  email:
127
141
  - imperituro.ard@gmail.com
@@ -130,6 +144,7 @@ extensions: []
130
144
  extra_rdoc_files: []
131
145
  files:
132
146
  - ".gitignore"
147
+ - ".idea/runConfigurations/hua_oceanconnect_adapter.xml"
133
148
  - ".ruby-gemset"
134
149
  - ".ruby-version"
135
150
  - ".travis.yml"
@@ -145,6 +160,7 @@ files:
145
160
  - lib/imperituroard.rb
146
161
  - lib/imperituroard/phpipamcps.rb
147
162
  - lib/imperituroard/phpipamdb.rb
163
+ - lib/imperituroard/projects/iot/add_functions.rb
148
164
  - lib/imperituroard/projects/iot/hua_oceanconnect_adapter.rb
149
165
  - lib/imperituroard/projects/iot/mongoconnector.rb
150
166
  - lib/imperituroard/projects/wttx/updatepostcps.rb