-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyr_reader_tier.rb
251 lines (213 loc) · 6.67 KB
/
gyr_reader_tier.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61
62
63
64
65
66
67
68
69
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
215
216
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
#append the current directory to the search path
$: << File.dirname(__FILE__)
#Add the default relative library location to the search path
$: << File.join(File.dirname(__FILE__),"","thinkify_api")
# #require our library
# require 'thinkifyreader'
require './models/mongo_db'
require './newTag'
require './testTag'
require 'yaml'
require 'pry-byebug'
require 'time_difference'
class ReaderApp
@@config = YAML::load_file(File.join(__dir__, "gyruss_values.yml"))
def persist
# @tag_list = @r.tag_list
people = []
@tag_list = []
@tag_list << Testtag.new("PX2D Z9O7 YD4I N9LO JEKR HUHW B2N7", "50.95310179804325", 16, "2019/08/25 18:23:00.000", "2019/08/25 18:23:00.000")
counter = Read.new
repeat = Repeat.new
newtag = Newtag.new
@tag_list.each do |t|
counter.discovery = t.disc
counter.epc = t.epc
counter.rssi = t.rssi
counter.count = t.count
repeat.discovery = t.disc
repeat.epc = t.epc
repeat.rssi = t.rssi
repeat.count = t.count
newtag.epc = t.epc
newtag.count = t.count
newtag.discovery = t.disc
newtag.rssi = t.rssi
newtag.last_tag_read = t.last
people.push(t)
end
# File.open("people.yml","w") { |file| file.write(people.to_yaml)}
@@people = YAML::load_file(File.join(__dir__, "people.yml"))
File.open('people.yml', 'w') do |file|
file.write(Psych.dump(people))
end
g = {}
counter.instance_variables.each {|var| g[var.to_s.delete("@")] = counter.instance_variable_get(var) }
g
READS.insert_one(g)
@tag = TAGS.find(epc: newtag.epc).to_a
linger_threshold = @@config["linger_threshold"]
if @tag.empty? && newtag.epc
read = 1
time_difference_unique = 0
newtag.time_difference_unique = time_difference_unique
newtag.read = read
tag = Tag.new
tag.epc = newtag.epc
tag.count = newtag.count
tag.discovery = newtag.discovery
tag.rssi = newtag.rssi
tag.last_tag_read = newtag.last_tag_read
tag.read = newtag.read
tag.time_difference_unique = newtag.time_difference_unique
h = {}
tag.instance_variables.each {|var| h[var.to_s.delete("@")] = tag.instance_variable_get(var) }
h
TAGS.insert_one(h)
elsif @tag && newtag.epc
#Increment times tag has been read
increment = {}
@tag.map { |h| increment = h }
increment['read'] += 1
number = increment['read']
end_time = DateTime.now.strftime("%Y/%m/%d %H:%M:%S.%L")
start_time = @tag[0]['last_tag_read']
time_difference = TimeDifference.between(start_time, end_time).in_minutes
if @tag[0]['read'] > 1 && time_difference > linger_threshold
j = {}
repeat.instance_variables.each {|var| j[var.to_s.delete("@")] = repeat.instance_variable_get(var) }
j
REPEATS.insert_one(j)
end
TAGS.update_one({epc: newtag.epc }, '$set' => { 'read' => number })
else
# puts "Taglist empty"
end
end
# ****************************************************************************
# Create a reader grab our configuraiton and get ready to run
# ****************************************************************************
# def initialize
# # The PC this code is running on provides the networking / mac address we use
# #in the DB. Let's make a "reader" in the DB using these paramters.
#
# #We can specify parameters in a config file for easier deployment.
# @tag_added = false
#
# # Create a thinkify reader to work with.
# # On windows you can just call .new and the class will scan for the first reader
# # it can find (upto com20)
# if RUBY_PLATFORM.include?("linux")
# puts("Configure linux reader on /dev/ttyUSB0")
# @r = ThinkifyReader.new('/dev/ttyUSB0') #Linux Ubuntu
# # @r = ThinkifyReader.new('/dev/ttyACM0') #Linux Ubuntu
# end
#
# if RUBY_PLATFORM.include?("mingw32")
# puts("Configure windows reader")
# @r = ThinkifyReader.new
# end
#
# if RUBY_PLATFORM.include?("mswin32")
# puts("Configure windows reader")
# @r = ThinkifyReader.new
# end
#
# # Our API supports a callback mechanism. Simply tie a block to the .tag_added or
# # .tag_updated or .tag_removed to perform a set of tasks whenever these events
# # occur.
# #puts "setting up tag added callback"
# # When we first observe a tag, add it to the db if it doesn't exist and then add
# # an 'added' event.
# @r.tag_list.tag_added do |tag|
# # puts "A new tag was seen! #{tag.epc}"
# @tag_added = true #We just set a flag...
# end
#
#
# end
#
# # ****************************************************************************
# # Do one cycle to do notification.
# #****************************************************************************
# def run_cycle
#
# @r.reading_active=true
#
# # Read for a few seconds...
# sleep(@@config['reader_duty_cycle'])
#
# # The reader will put the tags it finds into its tag_list... An array of tags.
# # puts "Total Tags: #{@r.tag_list.length}"
#
# # if @tag_added
# # #We've got at least one new tag on the list. -- Report the new Tags...
# # @r.tag_list
# # @tag_added = false
# persist
# # end
#
# #Clean out Stale tags.
# @r.tag_list.clear
#
# end
#
# def run
#
# # puts
# # puts "Reading Tags:"
# # @r.reading_active=true
#
# # puts "lifetime"
# # p @r.tag_list.tag_lifetime
#
# if @r.connected == true
#
# status = "Connected"
# reader_id = 1
#
# @reader_status = READERS.find(reader: reader_id).to_a
#
# s = {'status' => status, "reader" => reader_id }
#
# if @reader_status.empty?
# READERS.insert_one(s)
# else
# READERS.update_one({reader: reader_id }, '$set' => { 'status' => status })
# end
#
# # @@status = "Connected"
# end
#
# begin
#
# while(1)
# run_cycle
# end
#
# rescue Interrupt
# reader_id = 1
#
# READERS.update_one({reader: reader_id }, '$set' => { 'status' => 'Not Connected' })
# rescue Exception
# reader_id = 1
#
# READERS.update_one({reader: reader_id }, '$set' => { 'status' => 'Not Connected' })
#
# puts "Exception Thrown."
#
# p $!
# ensure
#
# # Turn off reading.
# @r.reading_active=false
#
# end
#
# end
end
#***********************************************************
# Make an instance and Go!
rn = ReaderApp.new()
# rn.run
rn.persist