-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.rb
208 lines (185 loc) · 4.49 KB
/
api.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
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra'
require "sinatra/jsonp"
require "sinatra/reloader" #if development?
require 'mongoid'
require 'pusher'
load "app/lib/sigfox_tools.rb"
require ::File.dirname(__FILE__) + '/config/environment'
class OpenSensorApi < Sinatra::Base
helpers Sinatra::Jsonp
configure do
Mongoid.load!("config/mongoid.yml")
end
get '/' do
"Please read the doc"
end
before do
protected! #if request.path =~ %r{\A/api}
end
before do
case request.path[0, 4]
when "/api"
cache_control :private, max_age: 86400 # 24 hours
when "/doc"
cache_control :public, max_age: 86400
else
cache_control :no_store
end
end
def protected!
return if authorized?
halt 401, {"Error"=>"Not authorized"}
end
def authorized?
puts request.env
api_key=request.env["HTTP_X_APIKEY"]||params["api_key"]
if api_key
u=User.where(:api_key=>api_key).first
@current_user=u
end
return u!=nil
end
get "/sensor/:sensor_id" do
sensor=Sensor.find params["sensor_id"]
res=sensor.as_json
res[:measures_count]=sensor.measures.count
res.to_json
end
get "/sensor" do
@current_user.sensors.as_json
end
post "/sensor/:sensor_id" do
begin
sensor=Sensor.find params["sensor_id"]
puts sensor
newMeasure=JSON.parse request.body.read
measure=Measure.new(newMeasure)
measure.sensor=sensor
measure.save
{:code=>1}.to_json
rescue Exception=>e
{:error=>e.to_s}
end
end
def conv_time in_time
times=in_time.split('_')
if times.size==3
return Time.utc(times[2].to_i,times[1].to_i,times[0].to_i)
elsif times.size==5
return Time.utc(times[2].to_i,times[1].to_i,times[0].to_i,times[3].to_i,times[4].to_i)
end
end
get "/sensor/:sensor_id/measures" do
begin
sensor = Sensor.find params["sensor_id"]
limit = params[:numbers]||100_000
page = (params[:page]||0).to_i
criteria = sensor.measures.desc(:timeStamp).skip(limit*page).limit(limit)
if(params[:startTime]) then criteria = criteria.where(:timeStamp.gte=>conv_time(params[:startTime])) end
if(params[:endTime]) then criteria = criteria.where(:timeStamp.lt=>conv_time(params[:endTime])) end
puts sensor
jsonp criteria.map{|m|[m.timeStamp.to_i*1000,m.value]}.reverse
rescue Exception=>e
{:error=>e.to_s}
end
end
get "/devices" do
devices=@current_user.devices
devices.to_json
end
get "/device/:device_id" do
begin
device = Device.find params["device_id"]
res = device.as_json
res[:sensors] = device.sensors.as_json
res.to_json
rescue Exception=>e
{:error=>e.to_s}
end
end
post "/device/sigfox" do
begin
if params[:device]
device = Device.where(:sigfox_device_id=>params[:device]).first
if device
device.parse_binary_data params
{:done=>"ok"}.to_json
else
halt 302,{:error=>"Device not found"}.to_json
end
else
end
rescue Exception=>e
{:error=>e.to_s}
end
end
post "/device/lora" do
begin
if params[:device]
device = Device.where(:external_device_id=>params[:device]).first
if device
LoraDecoder.decode device,params
{:done=>"ok"}.to_json
else
halt 302,{:error=>"Device not found"}.to_json
end
else
end
rescue Exception=>e
{:error=>e.to_s}
end
end
post "/device/:device_id/binary" do
begin
sensors = []
device = Device.find params["device_id"]
device.parse_binary_data params
{:done=>"ok"}.to_json
rescue Exception => e
puts "ERROR:#{e.to_s}"
{:error => e.to_s}.to_json
end
end
#
#
post "/device/:device_id" do
begin
errors = []
device = Device.find params["device_id"]
sensors = []
newMeasure = JSON.parse request.body.read
newMeasure.each do |sensor_data|
begin
if sensor_data['sensor_id']
sensor = Sensor.where(:id=>sensor_data["sensor_id"],:device=>device).first
else
sensor = Sensor.find_by_name_or_create sensor_data["name"],device
end
rescue Exception =>e
puts e
sensor = nil
errors<<e.to_s
end
if sensor
if sensor_data["data"] then sensor_data=sensor_data["data"] else sensor_data=[sensor_data] end
puts sensor_data
sensor_data.each do |data|
puts"data:#{data}"
measure = sensor.add_measure(data["value"],data["time_stamp"]||Time.now )
end
sensors<<sensor
end
end
res={}
Sensor.check_and_update sensors
if errors.size>0 then res[:errors]=errors.join(',')
else
res[:msg] = "Updated #{sensors.count}"
end
res.to_json
rescue Exception=>e
{:error=>e.to_s}
end
end
end