-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
211 lines (163 loc) · 5.21 KB
/
app.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
#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 'sinatra'
require './models/mongo_db'
require 'net/http'
require 'uri'
require 'json'
require 'pry-byebug'
require 'time_difference'
require 'yaml'
config = YAML::load_file(File.join(__dir__, "gyruss_values.yml"))
# API to grab tags
get '/taglist' do
content_type :json
@repeats = REPEATS.find.to_a
@all_tags = TAGS.find.to_a
u_interval_5 = 0
u_interval_30 = 0
u_interval_60 = 0
u_interval_8 = 0
u_interval_24 = 0
r_interval_5 = 0
r_interval_30 = 0
r_interval_60 = 0
r_interval_8 = 0
r_interval_24 = 0
@interval_array = []
@counter = @repeats.map do |r|
@read = r
end_time_r = DateTime.now.strftime("%Y/%m/%d %H:%M:%S.%L")
start_time_r = @read['discovery']
time_difference_repeat = TimeDifference.between(start_time_r, end_time_r).in_minutes
@read['time_difference_repeat'] = time_difference_repeat
@read
end
@new_array = @all_tags.map do |t|
@tag = t
end_time_u = DateTime.now.strftime("%Y/%m/%d %H:%M:%S.%L")
start_time_u = @tag['last_tag_read']
time_difference_unique = TimeDifference.between(start_time_u, end_time_u).in_minutes
@tag['time_difference_unique'] = time_difference_unique
@tag
end
@new_array.each do |tag|
if tag['time_difference_unique'] < 5
u_interval_5 += 1
u_interval_30 += 1
u_interval_60 += 1
u_interval_8 += 1
u_interval_24 += 1
elsif tag['time_difference_unique'] > 5 && tag['time_difference_unique'] < 30
u_interval_30 += 1
u_interval_60 += 1
u_interval_8 += 1
u_interval_24 += 1
elsif tag['time_difference_unique'] > 30 && tag['time_difference_unique'] < 60
u_interval_60 += 1
u_interval_8 += 1
u_interval_24 += 1
elsif tag['time_difference_unique'] > 60 && tag['time_difference_unique'] < 480
u_interval_8 += 1
u_interval_24 += 1
elsif tag['time_difference_unique'] > 480 && tag['time_difference_unique'] < 1440
u_interval_24 += 1
else
# puts "Uniques udpated"
end
@interval_array << { u_interval_5: u_interval_5 , u_interval_30: u_interval_30, u_interval_60: u_interval_60, u_interval_8: u_interval_8, u_interval_24: u_interval_24 }
end
@counter.each do |count|
if count['time_difference_repeat'] < 5
r_interval_5 += 1
r_interval_30 += 1
r_interval_60 += 1
r_interval_8 += 1
r_interval_24 += 1
elsif count['time_difference_repeat'] > 5 && count['time_difference_repeat'] < 30
r_interval_30 += 1
r_interval_60 += 1
r_interval_8 += 1
r_interval_24 += 1
elsif count['time_difference_repeat'] > 30 && count['time_difference_repeat'] < 60
r_interval_60 += 1
r_interval_8 += 1
r_interval_24 += 1
elsif count['time_difference_repeat'] > 60 && count['time_difference_repeat'] < 480
r_interval_8 += 1
r_interval_24 += 1
elsif count['time_difference_repeat'] > 480 && count['time_difference_repeat'] < 1440
r_interval_24 += 1
else
# puts "Repeats updated"
end
@interval_array << { r_interval_5: r_interval_5, r_interval_30: r_interval_30, r_interval_60: r_interval_60, r_interval_8: r_interval_8, r_interval_24: r_interval_24 }
end
@interval_array.to_json
end #get
# Route for main page
get '/' do
erb :dashboard
end
get '/settings' do
@all_tags = TAGS.find.sort(_id: -1).limit(3)
@reader_info = READERS.find.to_a
@reader_status = @reader_info[0]['status']
@linger_threshold = config['linger_threshold']
@reader_duty_cycle = config['reader_duty_cycle']
@serial_port = config['serial_port']
@reader_ID = config['reader_ID']
if RUBY_PLATFORM.include?("linux")
@platform = "linux"
elsif RUBY_PLATFORM.include?("mingw32")
@platform = "mingw32"
elsif RUBY_PLATFORM.include?("mswin32")
@platform = 'mswin32'
else
@platform = "Unkown"
end
erb :settings
end
post '/output' do
READS.delete_many({})
TAGS.delete_many({})
REPEATS.delete_many({})
redirect '/'
end
post '/linger' do
linger = params['linger_threshold']
linger_threshold = linger.to_i
config['linger_threshold'] = linger_threshold
File.open("./gyruss_values.yml", 'w') { |f| YAML.dump(config, f) }
redirect '/settings'
end
post '/duty' do
duty = params['duty']
reader_duty_cycle = duty.to_i
config['reader_duty_cycle'] = reader_duty_cycle
File.open("./gyruss_values.yml", 'w') { |f| YAML.dump(config, f) }
redirect '/settings'
end
get '/taglist_2' do
content_type :json
@all_tags = READS.find.to_a
@all_tags.to_json
end
get '/debug' do
erb :debug
end
get '/trakr_list' do
content_type :json
people = YAML::load_file(File.join(__dir__, "people.yml"))
@people = people
@people.to_json
end
get '/trakr' do
people = YAML::load_file(File.join(__dir__, "people.yml"))
@people = people
erb :trakr
end