imperituroard 0.1.9 → 0.2.0

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: d1762929c72ac24fe31ae33aa251710b1faaa610
4
- data.tar.gz: 80eec415adf4d755cc17ea6ad2a7bfcec3984901
3
+ metadata.gz: d355c409ea8712d63e24193ade8edc75da42d22c
4
+ data.tar.gz: 57d1a26a82303b7ff92dcae72080d8483b04f760
5
5
  SHA512:
6
- metadata.gz: 8e874469cd9a098e43201ede8d7f120549a36c3f39fa3c396daf2b19abe60709cca051825dffb676f2d5ea20f95324c311d35ae78822603222aabda8961908d0
7
- data.tar.gz: d1cb5442becd3ac0441248ee540fdfa74892dac0efa328874f368fa43e9c59b69f0000ce8ec4f22b1223159edbcb41e1da136a91fdfad43e28843b9a10f57f04
6
+ metadata.gz: 0777e7b6d50cdf31b7a0e01469510ea12ab8bc33d2f94566a2c8663021305e5018b10729c84caf4c5ddc571edc63ba622430682e8fbf36b6ca8a8dd4284523d1
7
+ data.tar.gz: a3ac19f8a9d18a3faeb807bd445aa558291b016f9676ffff9ae66f11124a42ad40420993cab190d3a47498a153ac29408a620cf1ce5e0a415408238934e64d48
@@ -114,7 +114,9 @@ class HuaIot
114
114
  token = get_token(app_id, secret)[:body]["accessToken"]
115
115
  path = "/iocm/app/dm/v1.1.0/queryDeviceIdByNodeId?nodeId=" + node_id
116
116
  p path
117
+ p path
117
118
  url_string = "https://" + platformip + ":" + platformport + path
119
+ p url_string
118
120
  uri = URI.parse url_string
119
121
  https = Net::HTTP.new(uri.host, uri.port)
120
122
  https.use_ssl = true
@@ -125,6 +127,7 @@ class HuaIot
125
127
  request.content_type = 'application/json'
126
128
  request['Authorization'] = 'Bearer ' + token
127
129
  request['app_key'] = app_id
130
+ p request.body
128
131
  res = https.request(request)
129
132
  p res.body.to_s
130
133
  {:code => res.code, :message => res.message, :body => JSON.parse(res.body.to_s)}
@@ -1,29 +1,31 @@
1
1
  require 'mongo'
2
2
 
3
+ require 'json'
4
+ require 'rubygems'
5
+ require 'nokogiri'
6
+ require 'rails'
7
+
3
8
  #class for communication with mongo database for iot API
4
9
  class MongoIot
5
10
 
6
- attr_accessor :mongoip, :mongoport, :client, :database
11
+ attr_accessor :mongo_ip, :mongo_port, :client, :mongo_database
7
12
 
8
- def initialize(mongoip, mongoport, iotip, database)
9
- @database = database
10
- @mongoip = mongoip
11
- @mongoport = mongoport
12
- @iotip = iotip
13
- client_host = [mongoip + ":" + mongoport]
14
- @client = Mongo::Client.new(client_host, :database => database)
13
+ def initialize(mongo_ip, mongo_port, mongo_database)
14
+ @mongo_database = mongo_database
15
+ @mongo_ip = mongo_ip
16
+ @mongo_port = mongo_port
17
+ client_host = [mongo_ip + ":" + mongo_port]
18
+ @client = Mongo::Client.new(client_host, :database => mongo_database)
15
19
  end
16
20
 
17
- def audit_logger(proc_name)
21
+ def audit_logger(proc_name, src_ip, input_json, output_json)
18
22
  begin
19
23
  collection = client[:audit]
20
24
  doc = {
21
25
  proc_name: proc_name,
22
- hobbies: [ 'hiking', 'tennis', 'fly fishing' ],
23
- siblings: {
24
- brothers: 0,
25
- sisters: 1
26
- }
26
+ sender: {src_ip: src_ip},
27
+ input_params: input_json,
28
+ output_params: output_json
27
29
  }
28
30
  result = collection.insert_one(doc)
29
31
  p result
@@ -32,6 +34,184 @@ class MongoIot
32
34
  end
33
35
  end
34
36
 
37
+ def get_profiles_by_login(login)
38
+ begin
39
+ login_profiles = []
40
+ req2 = []
41
+ result_ps = []
42
+ collection = client[:users]
43
+ collection2 = client[:device_profiles]
44
+ collection.find({:login => login}).each { |row|
45
+ login_profiles = row["permit_profiles"]
46
+ }
47
+ for i in login_profiles
48
+ req2.append({:profile_id => i})
49
+ end
50
+ collection2.find({:$or => req2}, {:_id => 0}).each { |row|
51
+ result_ps.append(row)
52
+ }
53
+ result_ps
54
+ rescue
55
+ []
56
+ end
57
+ end
58
+
59
+ def get_imei_info_from_db(imeilist)
60
+ begin
61
+ req2 = []
62
+ result_ps = []
63
+ collection = client[:device_imei]
64
+ for i in imeilist
65
+ req2.append({:imei => i})
66
+ end
67
+ p req2
68
+ collection.find({:$or => req2}, {:_id => 0}).each { |row|
69
+ result_ps.append(row)
70
+ }
71
+ result_ps
72
+ rescue
73
+ []
74
+ end
75
+ end
76
+
77
+ def get_profile_id_by_name(profile_name)
78
+ begin
79
+ result_ps = []
80
+ collection = client[:device_profiles]
81
+ collection.find({"profile" => profile_name}).each { |row|
82
+ result_ps.append(row)
83
+ }
84
+ result_ps[0]
85
+ rescue
86
+ []
87
+ end
88
+ end
89
+
90
+ def get_profile_name_by_id(profile_id)
91
+ begin
92
+ result_ps = []
93
+ collection = client[:device_profiles]
94
+ collection.find({"profile_id" => profile_id}).each { |row|
95
+ result_ps.append(row)
96
+ }
97
+ result_ps[0]
98
+ rescue
99
+ []
100
+ end
101
+ end
102
+
103
+ def check_login_profile_permiss(login, profile)
104
+ p "profile"
105
+ p profile
106
+ get_login_info = get_profiles_by_login(login)
107
+ p "get_login_info"
108
+ p get_login_info
109
+ dst_profile = get_profile_id_by_name(profile)
110
+ p "dst_profile"
111
+ p dst_profile
112
+ access=1
113
+ if get_login_info!=[]
114
+ if dst_profile!=[]
115
+ for j in get_login_info
116
+ p j
117
+ if j["profile_id"]==dst_profile["profile_id"]
118
+ access=0
119
+ end
120
+ if access==0
121
+ return {:code => 200, :result => "Permission granted"}
122
+ else
123
+ return {:code => 400,:result => "Access denied. This incident will be reported."}
124
+ end
125
+ end
126
+ else return {:code => 501,:result => "Profile not found"}
127
+ end
128
+ else {:code => 500,:result => "Login not found"}
129
+ end
130
+ end
131
+
132
+ def check_imei_exists(imei_list)
133
+ res = []
134
+ imei_list = get_imei_info_from_db(imei_list)
135
+ for k in imei_list
136
+ res.append(k["imei"])
137
+ end
138
+ res
139
+ end
140
+
141
+
142
+ def imei_insert_list(imei_list)
143
+ begin
144
+ collection = client[:device_imei]
145
+ p imei_list
146
+ for l in imei_list
147
+ doc = {
148
+ imei: l,
149
+ imsi: "",
150
+ msisdn: "",
151
+ description: "test imei",
152
+ note: "second description",
153
+ profile: 0,
154
+ type: 0,
155
+ address: "unknown"
156
+ }
157
+ result = collection.insert_one(l)
158
+ p result
159
+ end
160
+ rescue
161
+ continue
162
+ end
163
+ end
164
+
165
+ def get_profile_name_from_imei(imei)
166
+ info = get_imei_info_from_db([imei])
167
+ p "123"
168
+ p info
169
+ id = info[0]["profile"]
170
+ get_profile_name_by_id(id)
171
+ end
172
+
173
+
174
+ def device_remove_single_mongo(imei)
175
+
176
+ collection = client[:device_imei]
177
+ doc = {
178
+ "imei" => imei
179
+ }
180
+ result = collection.delete_many(doc)
181
+ p result
182
+ end
183
+
184
+ def device_modify_attr_mongo(imei,address)
185
+ begin
186
+ collection = client[:device_imei]
187
+ doc = {
188
+ "imei" => imei
189
+ }
190
+ sett = {'$set' => { address: address}}
191
+ result = collection.update_one(doc, sett)
192
+ p result
193
+ rescue
194
+ continue
195
+ end
196
+ end
197
+
198
+ def device_modify_any_attr_mongo(imei,attr_list)
199
+ begin
200
+ collection = client[:device_imei]
201
+ doc = {
202
+ "imei" => imei
203
+ }
204
+ sett = {'$set' => attr_list}
205
+ result = collection.update_one(doc, sett)
206
+ p result
207
+ rescue
208
+ continue
209
+ end
210
+ end
211
+
212
+
213
+
214
+
35
215
  def ttt
36
216
  p "111111"
37
217
  begin
@@ -1,3 +1,3 @@
1
1
  module Imperituroard
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/imperituroard.rb CHANGED
@@ -70,27 +70,192 @@ class Pipam
70
70
  end
71
71
 
72
72
  class Iot
73
- attr_accessor :mongoip, :mongoport, :iotip, :iottoken, :database, :iotplatform_ip, :iotplatform_port
73
+ attr_accessor :mongoip, :mongoport, :iotip, :iottoken, :mongo_database, :iotplatform_ip, :iotplatform_port, :cert_path, :key_path, :mongo_client
74
74
 
75
- def initialize(mongoip, mongoport, iotip, database, iotplatform_ip, iotplatform_port)
75
+ def initialize(mongoip, mongoport, iotip, mongo_database, iotplatform_ip, iotplatform_port, cert_path, key_path)
76
76
  @mongoip = mongoip
77
77
  @mongoport = mongoport
78
78
  @iotip = iotip
79
- @database = database
79
+ @mongo_database = mongo_database
80
80
  @iotplatform_ip = iotplatform_ip
81
81
  @iotplatform_port = iotplatform_port
82
+ @cert_path = cert_path
83
+ @key_path = key_path
84
+ @mongo_client = MongoIot.new(mongoip, mongoport, mongo_database)
82
85
  end
83
86
 
84
87
 
88
+ #1. Add device to profile
89
+ #login - login for client identification
90
+ #profile - profile for device
91
+ #imei_list - device identificator
92
+ #imei_list = {
93
+ # imei: imei,
94
+ # imsi: imsi,
95
+ # msisdn: msisdn,
96
+ # description: description,
97
+ # note: note,
98
+ # profile: profile,
99
+ # type: type,
100
+ # address: address
101
+ #}
102
+ #massive commands
103
+ #+
104
+ def add_device_to_profile(login, profile, imei_list)
105
+ imei = []
106
+ list1 = {}
107
+ for_insert = []
108
+ for t in imei_list
109
+ imei.append(t["imei"])
110
+ list1[t["imei"]] = t
111
+ end
112
+ get_login_info = mongo_client.check_login_profile_permiss(login, profile)[:code]
113
+ if get_login_info==200
114
+ list_exists = mongo_client.check_imei_exists(imei)
115
+ for_upload = imei - list_exists
116
+ for h in for_upload
117
+ for_insert.append(list1[h])
118
+ end
119
+ mongo_client.imei_insert_list(for_insert)
120
+ else get_login_info
121
+ end
122
+ end
123
+
124
+
125
+ #2 Find device (only mongo datebase. IOT platform not need)
126
+ # login
127
+ # imei
128
+ # imei_list =[41234,23452345,132412]
129
+ #+
130
+ def device_find(login, imei_list)
131
+
132
+ ime_list_approved = []
133
+ ime_list_notapproved = []
134
+ begin
135
+ for t in imei_list
136
+ prof_name1 = mongo_client.get_profile_name_from_imei(t)
137
+ p "prof_name1"
138
+ p prof_name1
139
+ if prof_name1!=nil
140
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
141
+ if permiss1==200
142
+ ime_list_approved.append(t)
143
+ end
144
+ end
145
+ end
146
+ data = mongo_client.get_imei_info_from_db(ime_list_approved)
147
+ {:code=>507, :result => "Unknown error", :data => data}
148
+ rescue
149
+ {:code=>507, :result => "Unknown error"}
150
+ end
151
+ end
152
+
153
+ #3 device modify, change imei
154
+ #login
155
+ #imei_old
156
+ #imei_new
157
+ #massive commands
158
+ #im_list = [{:imei_old=>7967843245667, :imei_new=>7967843245665}]
159
+ #+
160
+ def imei_replace(login, im_list)
161
+ for a in im_list
162
+ p a
163
+ prof_name1 = mongo_client.get_profile_name_from_imei(a[:imei_old])
164
+ p prof_name1
165
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
166
+ if permiss1==200
167
+ mongo_client.device_modify_any_attr_mongo(a[:imei_old],{:imei=>a[:imei_new]})
168
+ end
169
+ end
170
+ end
171
+
172
+
173
+ #4 remove device
174
+ #login
175
+ #imei
176
+ # not massive commands
177
+ #imei=11341341234
178
+ #login="test"
179
+ #+
180
+ def device_remove(login, imei)
181
+ prof_name = mongo_client.get_profile_name_from_imei(imei)
182
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
183
+ if permiss==200
184
+ mongo_client.device_remove_single_mongo(imei)
185
+ end
186
+ end
187
+
188
+
189
+ #5 add address to device
190
+ #login
191
+ #imei = newdevice_list
192
+ #address = newdevice_list
193
+ #newdevice_list=[{:imei=>7967843245665, :address=>"Golubeva51"}]
194
+ #+
195
+ def device_add_address(login, newdevice_list)
196
+ for p in newdevice_list
197
+ prof_name = mongo_client.get_profile_name_from_imei(p[:imei])
198
+ p "prof_name"
199
+ p prof_name
200
+ permiss = mongo_client.check_login_profile_permiss(login, prof_name["profile"])[:code]
201
+ if permiss==200
202
+ mongo_client.device_modify_attr_mongo(p[:imei],p[:address])
203
+ end
204
+ end
205
+ end
206
+
207
+ #6 add service by SPA
208
+ #imei
209
+ #profile
210
+ #imsi
211
+ #msisdn
212
+ #newdevice_list=[{:imei=>7967843245665, :attributes=>{:address=>"Golubeva51", :profile=>"wqeqcqeqwev", :msisdn=>375298766719, :imsi=>25702858586756875}}]
213
+ #+
214
+ def add_service(login, device_list)
215
+ for g in device_list
216
+ prof_name1 = mongo_client.get_profile_name_from_imei(g[:imei])
217
+ p prof_name1
218
+ permiss1 = mongo_client.check_login_profile_permiss(login, prof_name1["profile"])[:code]
219
+ p permiss1
220
+ permiss2 = mongo_client.check_login_profile_permiss(login, g[:attributes][:profile])[:code]
221
+ if permiss1==200 && permiss2==200
222
+
223
+ attr = g[:attributes]
224
+ #mod_attr = {}
225
+
226
+ if attr.key?(:profile)
227
+ if attr[:profile].is_a? Integer
228
+ p "Ok"
229
+ else
230
+ p new = mongo_client.get_profile_id_by_name(attr[:profile])
231
+ attr[:profile] = new["profile_id"]
232
+ end
233
+ end
234
+ p attr
235
+
236
+ mongo_client.device_modify_any_attr_mongo(g[:imei],attr)
237
+ end
238
+ end
239
+ end
240
+
241
+
242
+
85
243
 
86
244
  def test()
87
- ddd = MongoIot.new(mongoip, mongoport, iotip, database)
88
- ddd.ttt
245
+ ddd = MongoIot.new(mongoip, mongoport, mongo_database)
246
+ #ddd.get_profiles_by_login("test")
247
+
248
+ ff = [131234123412341233, 131234123127341233]
249
+ #ddd.get_imsi_info_from_db(ff)
250
+
251
+ p ddd.get_profile_id_by_name("1341241")
89
252
  end
90
253
 
254
+
255
+
91
256
  def testhua()
92
- cert_file = "/Users/imperituroard/Desktop/cert.crt"
93
- key_file = "/Users/imperituroard/Desktop/key.pem"
257
+ cert_file = cert_path
258
+ key_file = key_path
94
259
  ddd1 = HuaIot.new(iotplatform_ip, iotplatform_port, "", "", cert_file, key_file)
95
260
  #p ddd1.dev_register_verif_code_mode("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "7521234165452")
96
261
  #ddd1.querying_device_id("Cd1v0k2gTBCbpQlMVlW1FVqOSqga", "kbfo5JlBxTIhjVwtjHleWS5Iw5wa", "a8834c5e-4b4d-4f0f-ad87-14e916f3d0bb")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imperituroard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Buynovskiy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-07 00:00:00.000000000 Z
11
+ date: 2020-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler