ardtweeno 0.0.2 → 0.2.5
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 +7 -0
- data/CHANGELOG +179 -1
- data/COPYING +4 -3
- data/Gemfile +29 -0
- data/Gemfile.lock +76 -0
- data/INSTALL +12 -0
- data/Procfile +1 -0
- data/README.md +26 -2
- data/Rakefile +14 -7
- data/lib/ardtweeno/api.rb +542 -0
- data/lib/ardtweeno/configreader.rb +65 -0
- data/lib/ardtweeno/db.rb +51 -0
- data/lib/ardtweeno/dispatcher.rb +538 -0
- data/lib/ardtweeno/exceptions.rb +33 -0
- data/lib/ardtweeno/node.rb +117 -0
- data/lib/ardtweeno/nodemanager.rb +300 -0
- data/lib/ardtweeno/packet.rb +98 -0
- data/lib/ardtweeno/restapi.rb +266 -0
- data/lib/ardtweeno/serialparser.rb +221 -0
- data/lib/ardtweeno.rb +120 -1
- data/public/glossy_green_button.svg +123 -0
- data/public/glossy_red_button.svg +75 -0
- data/public/main.css +129 -0
- data/public/raspberrypi.jpg +0 -0
- data/resources/conf.yaml +41 -0
- data/resources/nodelist.yaml +26 -0
- data/resources/serialparser.js +84 -0
- data/test/api_test.rb +255 -0
- data/test/dispatcher_test.rb +115 -0
- data/test/node_test.rb +105 -0
- data/test/nodemanager_test.rb +167 -0
- data/test/packet_test.rb +75 -0
- data/test/parser_test.rb +147 -0
- data/test/post_watch +11 -0
- data/test/rest_api_test.rb +248 -0
- data/test/run_mock +17 -0
- data/test/run_packet_push +14 -0
- data/test/serialport_mock.rb +43 -0
- data/test/test_helper.rb +15 -0
- data/test/tty0tty-1.1/AUTHORS +1 -0
- data/test/tty0tty-1.1/COPYING +340 -0
- data/test/tty0tty-1.1/INSTALL +18 -0
- data/test/tty0tty-1.1/README +52 -0
- data/test/tty0tty-1.1/THANKS +4 -0
- data/test/tty0tty-1.1/TODO +3 -0
- data/test/tty0tty-1.1/VERSION +4 -0
- data/test/tty0tty-1.1/module/Makefile +41 -0
- data/{bin/ardtweeno → test/tty0tty-1.1/module/Module.symvers} +0 -0
- data/test/tty0tty-1.1/module/modules.order +1 -0
- data/test/tty0tty-1.1/module/tty0tty.c +678 -0
- data/test/tty0tty-1.1/module/tty0tty.ko +0 -0
- data/test/tty0tty-1.1/module/tty0tty.mod.c +51 -0
- data/test/tty0tty-1.1/module/tty0tty.mod.o +0 -0
- data/test/tty0tty-1.1/module/tty0tty.o +0 -0
- data/test/tty0tty-1.1/pts/Makefile +10 -0
- data/test/tty0tty-1.1/pts/tty0tty +0 -0
- data/test/tty0tty-1.1/pts/tty0tty.c +222 -0
- data/views/createpost.erb +45 -0
- data/views/home.erb +59 -0
- metadata +89 -37
- data/README +0 -1
- data/test/Rakefile +0 -6
- data/test/features/ardtweeno.feature +0 -14
- data/test/features/step_definitions/ardtweeno_steps.rb +0 -24
@@ -0,0 +1,542 @@
|
|
1
|
+
####################################################################################################
|
2
|
+
# @author David Kirwan <davidkirwanirl@gmail.com>
|
3
|
+
# @description API class for the Ardtweeno system
|
4
|
+
#
|
5
|
+
# @date 21-02-2013
|
6
|
+
####################################################################################################
|
7
|
+
|
8
|
+
# Imports
|
9
|
+
require 'rubygems'
|
10
|
+
require 'logger'
|
11
|
+
require 'yaml'
|
12
|
+
require 'json'
|
13
|
+
require 'date'
|
14
|
+
|
15
|
+
module Ardtweeno
|
16
|
+
|
17
|
+
class API
|
18
|
+
class << self
|
19
|
+
|
20
|
+
attr_accessor :log
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
##
|
25
|
+
# Ardtweeno::API#retrievezones method to filter node zone according to REST API request
|
26
|
+
#
|
27
|
+
# * *Args* :
|
28
|
+
# - ++ ->
|
29
|
+
# * *Returns* :
|
30
|
+
# -
|
31
|
+
# * *Raises* :
|
32
|
+
#
|
33
|
+
def retrievezones(config, params)
|
34
|
+
@log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
|
35
|
+
@log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
|
36
|
+
|
37
|
+
params = params.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
|
38
|
+
|
39
|
+
zones = Array.new
|
40
|
+
|
41
|
+
config["zones"].each do |i|
|
42
|
+
data = {:zonename=>i["zonename"], :key=> i["zonekey"], :nodes=>i["zonenodes"]}
|
43
|
+
|
44
|
+
zones << data
|
45
|
+
end
|
46
|
+
|
47
|
+
if params.has_key?(:zonename)
|
48
|
+
zones = handleZoneName(zones, params)
|
49
|
+
end
|
50
|
+
|
51
|
+
@log.debug "Performing pagination functions on zone list"
|
52
|
+
params.delete(:seqno) # Zones don't have seqno's
|
53
|
+
zones = handlePagination(zones, params) # Perform pagination operations on results
|
54
|
+
|
55
|
+
|
56
|
+
final = {:zones=>zones, :total=>config["zones"].size, :found=>zones.size}
|
57
|
+
|
58
|
+
return final # Return the final results
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
##
|
64
|
+
# Ardtweeno::API#retrievenodes method to filter node data according to REST API request
|
65
|
+
#
|
66
|
+
# * *Args* :
|
67
|
+
# - ++ ->
|
68
|
+
# * *Returns* :
|
69
|
+
# -
|
70
|
+
# * *Raises* :
|
71
|
+
#
|
72
|
+
def retrievenodes(nodeList, params)
|
73
|
+
@log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
|
74
|
+
@log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
|
75
|
+
|
76
|
+
params = params.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
|
77
|
+
|
78
|
+
nodes = Array.new
|
79
|
+
|
80
|
+
nodeList.each do |i|
|
81
|
+
|
82
|
+
data = {:name=>i.node, :key=> i.key, :description=>i.description,
|
83
|
+
:version=>i.version, :sensors=>i.sensors,
|
84
|
+
:packets=>i.packetqueue.size}
|
85
|
+
|
86
|
+
nodes << data
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
if params.has_key?(:name)
|
91
|
+
nodes = handleName(nodes, params)
|
92
|
+
end
|
93
|
+
|
94
|
+
if params.has_key?(:nodekey)
|
95
|
+
nodes = handleNodeKey(nodes, params)
|
96
|
+
end
|
97
|
+
|
98
|
+
if params.has_key?(:version)
|
99
|
+
nodes = handleVersion(nodes, params)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
@log.debug "Performing pagination functions on node list"
|
104
|
+
params.delete(:seqno) # Nodes don't have seqno's
|
105
|
+
nodes = handlePagination(nodes, params) # Perform pagination operations on results
|
106
|
+
|
107
|
+
|
108
|
+
final = {:nodes=>nodes, :total=>nodeList.size, :found=>nodes.size}
|
109
|
+
|
110
|
+
return final # Return the final results
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
##
|
116
|
+
# Ardtweeno::API#retrievepackets method to filter packet data according to REST API request
|
117
|
+
#
|
118
|
+
# * *Args* :
|
119
|
+
# - ++ ->
|
120
|
+
# * *Returns* :
|
121
|
+
# -
|
122
|
+
# * *Raises* :
|
123
|
+
#
|
124
|
+
def retrievepackets(nodeList, params)
|
125
|
+
@log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
|
126
|
+
@log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
|
127
|
+
|
128
|
+
params = params.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
|
129
|
+
|
130
|
+
packetqueue = Array.new
|
131
|
+
|
132
|
+
if params.has_key?(:node)
|
133
|
+
@log.debug "the params has a node key"
|
134
|
+
nodeList.each do |i|
|
135
|
+
@log.debug "Matching #{i.node} with #{params[:node]}"
|
136
|
+
if i.node == params[:node]
|
137
|
+
@log.debug params
|
138
|
+
@log.debug i.node
|
139
|
+
packetqueue = i.packetqueue # Found the node we are interested in,
|
140
|
+
# extract the packets
|
141
|
+
break # Node found break and continue
|
142
|
+
end
|
143
|
+
end
|
144
|
+
else # Aggregate all
|
145
|
+
|
146
|
+
@log.debug "the params does not have a node key"
|
147
|
+
nodeList.each do |i|
|
148
|
+
packetqueue += i.packetqueue
|
149
|
+
packetqueue = packetqueue.sort_by {|x| x.seqNo} # Not exactly ideal.. but it works ;p
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# From most specific to least specific
|
154
|
+
|
155
|
+
if params.has_key?(:seqno)
|
156
|
+
packetqueue = handleSeqNo(packetqueue, params)
|
157
|
+
end
|
158
|
+
|
159
|
+
if params.has_key?(:minute)
|
160
|
+
packetqueue = handleMinute(packetqueue, params)
|
161
|
+
end
|
162
|
+
|
163
|
+
if params.has_key?(:hour)
|
164
|
+
packetqueue = handleHour(packetqueue, params)
|
165
|
+
end
|
166
|
+
|
167
|
+
if params.has_key?(:date)
|
168
|
+
packetqueue = handleDate(packetqueue, params)
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
packets = handlePagination(packetqueue, params) # Perform pagination operations on results
|
173
|
+
|
174
|
+
final = {:packets=>packets, :total=>packetqueue.size, :found=>packets.size}
|
175
|
+
|
176
|
+
return final # Return the final results
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
##
|
182
|
+
# Ardtweeno::API#handleZoneName method to filter according to REST API request
|
183
|
+
#
|
184
|
+
# * *Args* :
|
185
|
+
# - ++ -> Array nodes, Hash params
|
186
|
+
# * *Returns* :
|
187
|
+
# - Array
|
188
|
+
# * *Raises* :
|
189
|
+
#
|
190
|
+
def handleZoneName(theArray, theParams)
|
191
|
+
@log.debug "handleZoneName function called"
|
192
|
+
|
193
|
+
containerArray = Array.new
|
194
|
+
|
195
|
+
theArray.each do |i|
|
196
|
+
# Found the node required
|
197
|
+
if i[:zonename] == theParams[:zonename]
|
198
|
+
containerArray << i
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
if containerArray.size == 0
|
203
|
+
@log.debug "version not found, returning empty array"
|
204
|
+
end
|
205
|
+
|
206
|
+
return containerArray
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
##
|
212
|
+
# Ardtweeno::API#handleVersion method to filter according to REST API request
|
213
|
+
#
|
214
|
+
# * *Args* :
|
215
|
+
# - ++ -> Array nodes, Hash params
|
216
|
+
# * *Returns* :
|
217
|
+
# - Array
|
218
|
+
# * *Raises* :
|
219
|
+
#
|
220
|
+
def handleVersion(theArray, theParams)
|
221
|
+
@log.debug "handleVersion function called"
|
222
|
+
|
223
|
+
containerArray = Array.new
|
224
|
+
|
225
|
+
theArray.each do |i|
|
226
|
+
# Found the node required
|
227
|
+
if i[:version] == theParams[:version]
|
228
|
+
containerArray << i
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
if containerArray.size == 0
|
233
|
+
@log.debug "version not found, returning empty array"
|
234
|
+
end
|
235
|
+
|
236
|
+
return containerArray
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
##
|
242
|
+
# Ardtweeno::API#handleNodeKey method to filter packet data according to REST API request
|
243
|
+
#
|
244
|
+
# * *Args* :
|
245
|
+
# - ++ -> Array nodes, Hash params
|
246
|
+
# * *Returns* :
|
247
|
+
# - Array
|
248
|
+
# * *Raises* :
|
249
|
+
#
|
250
|
+
def handleNodeKey(theArray, theParams)
|
251
|
+
@log.debug "handleNodeKey function called"
|
252
|
+
|
253
|
+
containerArray = Array.new
|
254
|
+
|
255
|
+
theArray.each do |i|
|
256
|
+
# Found the node required
|
257
|
+
if i[:key] == theParams[:nodekey]
|
258
|
+
containerArray << i
|
259
|
+
return containerArray
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
@log.debug "key not found, returning empty array"
|
264
|
+
return containerArray
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
##
|
270
|
+
# Ardtweeno::API#handleName method to filter packet data according to REST API request
|
271
|
+
#
|
272
|
+
# * *Args* :
|
273
|
+
# - ++ -> Array nodes, Hash params
|
274
|
+
# * *Returns* :
|
275
|
+
# - Array
|
276
|
+
# * *Raises* :
|
277
|
+
#
|
278
|
+
def handleName(theArray, theParams)
|
279
|
+
@log.debug "handleName function called"
|
280
|
+
|
281
|
+
containerArray = Array.new
|
282
|
+
|
283
|
+
theArray.each do |i|
|
284
|
+
# Found the node required
|
285
|
+
if i[:name] == theParams[:name]
|
286
|
+
containerArray << i
|
287
|
+
return containerArray
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
@log.debug "name not found, returning empty array"
|
292
|
+
return containerArray
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
##
|
298
|
+
# Ardtweeno::API#handleSeqNo method to filter packet data according to
|
299
|
+
# meet the REST API seqNo request
|
300
|
+
#
|
301
|
+
# * *Args* :
|
302
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
303
|
+
# * *Returns* :
|
304
|
+
# - Array of Ardtweeno::Packet
|
305
|
+
# * *Raises* :
|
306
|
+
#
|
307
|
+
def handleSeqNo(theArray, theParams)
|
308
|
+
@log.debug "handleSeqNo function called"
|
309
|
+
|
310
|
+
containerArray = Array.new
|
311
|
+
|
312
|
+
theArray.each do |i|
|
313
|
+
# Found the packet required
|
314
|
+
if i.seqNo == theParams[:seqno].to_i
|
315
|
+
containerArray << i
|
316
|
+
return containerArray
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
@log.debug "seqNo not found, returning empty array"
|
321
|
+
return containerArray
|
322
|
+
end
|
323
|
+
|
324
|
+
|
325
|
+
##
|
326
|
+
# Ardtweeno::API#handleHour method to filter packet data according to
|
327
|
+
# meet the REST API seqNo request
|
328
|
+
#
|
329
|
+
# * *Args* :
|
330
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
331
|
+
# * *Returns* :
|
332
|
+
# - Array of Ardtweeno::Packet
|
333
|
+
# * *Raises* :
|
334
|
+
#
|
335
|
+
def handleHour(theArray, theParams)
|
336
|
+
@log.debug "handleHour function called"
|
337
|
+
|
338
|
+
containerArray = Array.new
|
339
|
+
|
340
|
+
theArray.each do |i|
|
341
|
+
# Found the packet required
|
342
|
+
if i.hour == theParams[:hour]
|
343
|
+
containerArray << i
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
@log.debug "Returning Packet data after Hour filtering"
|
348
|
+
return containerArray
|
349
|
+
end
|
350
|
+
|
351
|
+
##
|
352
|
+
# Ardtweeno::API#handleMinute method to filter packet data according to
|
353
|
+
# meet the REST API seqNo request
|
354
|
+
#
|
355
|
+
# * *Args* :
|
356
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
357
|
+
# * *Returns* :
|
358
|
+
# - Array of Ardtweeno::Packet
|
359
|
+
# * *Raises* :
|
360
|
+
#
|
361
|
+
def handleMinute(theArray, theParams)
|
362
|
+
@log.debug "handleHour function called"
|
363
|
+
|
364
|
+
containerArray = Array.new
|
365
|
+
|
366
|
+
theArray.each do |i|
|
367
|
+
# Found the packet required
|
368
|
+
if i.minute == theParams[:minute]
|
369
|
+
containerArray << i
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
@log.debug "Returning Packet data after Minute filtering"
|
374
|
+
return containerArray
|
375
|
+
end
|
376
|
+
|
377
|
+
|
378
|
+
##
|
379
|
+
# Ardtweeno::API#handleDate method to filter packet data according to
|
380
|
+
# meet the REST API date request
|
381
|
+
#
|
382
|
+
# * *Args* :
|
383
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
384
|
+
# * *Returns* :
|
385
|
+
# - Array of Ardtweeno::Packet
|
386
|
+
# * *Raises* :
|
387
|
+
#
|
388
|
+
def handleDate(theArray, theParams)
|
389
|
+
@log.debug "handleDate function called"
|
390
|
+
|
391
|
+
containerArray = Array.new
|
392
|
+
|
393
|
+
theArray.each do |i|
|
394
|
+
# Found the packet required
|
395
|
+
if i.date == theParams[:date]
|
396
|
+
containerArray << i
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
@log.debug "Returning Packet data after Date filtering"
|
401
|
+
return containerArray
|
402
|
+
end
|
403
|
+
|
404
|
+
|
405
|
+
##
|
406
|
+
# Ardtweeno::API#handlePagination method to filter packet data according to
|
407
|
+
# meet the REST API pagination request
|
408
|
+
#
|
409
|
+
# * *Args* :
|
410
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
411
|
+
# * *Returns* :
|
412
|
+
# - Array of Ardtweeno::Packet
|
413
|
+
# * *Raises* :
|
414
|
+
#
|
415
|
+
def handlePagination(theArray, theParams)
|
416
|
+
@log.debug "handlePagination function called"
|
417
|
+
|
418
|
+
offsetTransformed = handleOffset(theArray, theParams)
|
419
|
+
lengthTransformed = handleLength(offsetTransformed, theParams)
|
420
|
+
sortTransformed = handleSort(lengthTransformed, theParams)
|
421
|
+
|
422
|
+
final = sortTransformed
|
423
|
+
|
424
|
+
return final # return the Array modified by pagination requests
|
425
|
+
end
|
426
|
+
|
427
|
+
|
428
|
+
##
|
429
|
+
# Ardtweeno::API#handleOffset method to filter according to
|
430
|
+
# the REST API pagination request
|
431
|
+
#
|
432
|
+
# * *Args* :
|
433
|
+
# - ++ -> Array *theArray*, Hash of parameters *theParams*
|
434
|
+
# * *Returns* :
|
435
|
+
# - Array
|
436
|
+
# * *Raises* :
|
437
|
+
#
|
438
|
+
def handleOffset(theArray, theParams)
|
439
|
+
@log.debug "handleOffset function executing"
|
440
|
+
|
441
|
+
if theParams.has_key?(:offset)
|
442
|
+
@log.debug "params hash contains an offset value of #{theParams[:offset]}"
|
443
|
+
modifiedArray = Array.new
|
444
|
+
|
445
|
+
if theParams[:offset].to_i == 0 or theParams[:offset].to_i < 0
|
446
|
+
@log.debug "Offset value is either equal to 0 or less than 0 returning default array"
|
447
|
+
return theArray # Offset value equates to start of array
|
448
|
+
|
449
|
+
elsif theParams[:offset].to_i > theArray.size
|
450
|
+
@log.debug "Offset value is larger than the size of the available array, returning" +
|
451
|
+
" empty array"
|
452
|
+
return [] # The offset can never be satisfied returning empty array
|
453
|
+
|
454
|
+
else
|
455
|
+
@log.debug "theArray size: #{theArray.size}"
|
456
|
+
((theParams[:offset].to_i)..(theArray.size - 1)).step(1) do |i|
|
457
|
+
modifiedArray << theArray[i]
|
458
|
+
end
|
459
|
+
return modifiedArray # Returning the transformed array
|
460
|
+
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
@log.debug "No changes made, returning original array"
|
465
|
+
return theArray # No change, returning original array
|
466
|
+
end
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
##
|
471
|
+
# Ardtweeno::API#handleLength method to filter according to
|
472
|
+
# the REST API pagination request
|
473
|
+
#
|
474
|
+
# * *Args* :
|
475
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
476
|
+
# * *Returns* :
|
477
|
+
# - Array of Ardtweeno::Packet
|
478
|
+
# * *Raises* :
|
479
|
+
#
|
480
|
+
def handleLength(theArray, theParams)
|
481
|
+
@log.debug "handleLength function executing"
|
482
|
+
|
483
|
+
modifiedArray = Array.new
|
484
|
+
|
485
|
+
if theParams.has_key?(:length) and theParams[:length].to_i < 100
|
486
|
+
length = theParams[:length].to_i
|
487
|
+
@log.debug "theParams has a length value of #{length}"
|
488
|
+
else
|
489
|
+
length = 100
|
490
|
+
@log.debug "Defaulting to the default length of #{length}"
|
491
|
+
end
|
492
|
+
|
493
|
+
|
494
|
+
if theArray.size > length
|
495
|
+
@log.debug "Length is smaller than the size of theArray"
|
496
|
+
(0..(length - 1)).step(1) do |i|
|
497
|
+
modifiedArray << theArray[i]
|
498
|
+
end
|
499
|
+
|
500
|
+
@log.debug "Returning transformed array"
|
501
|
+
return modifiedArray # Returning transformed array
|
502
|
+
else
|
503
|
+
@log.debug "No need to perform any operations, returning original array"
|
504
|
+
return theArray # No need to perform any transformation, length pagination request
|
505
|
+
# is larger than the available data, returning original
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
|
510
|
+
##
|
511
|
+
# Ardtweeno::API#handleSort method to filter according to
|
512
|
+
# the REST API pagination request.
|
513
|
+
#
|
514
|
+
# * *Args* :
|
515
|
+
# - ++ -> Array of Ardtweeno::Packet *theArray*, Hash of parameters *theParams*
|
516
|
+
# * *Returns* :
|
517
|
+
# - Array of Ardtweeno::Packet sorted by seqNo values either ascending or
|
518
|
+
# decending
|
519
|
+
# * *Raises* :
|
520
|
+
#
|
521
|
+
def handleSort(theArray, theParams)
|
522
|
+
@log.debug "handleSort function executing"
|
523
|
+
|
524
|
+
if theParams.has_key?(:sort) and theParams[:sort] == "desc"
|
525
|
+
theArray = theArray.sort_by {|x| x.seqNo}
|
526
|
+
return theArray.reverse()
|
527
|
+
|
528
|
+
else
|
529
|
+
return theArray # Order is already ascending, return original array
|
530
|
+
end
|
531
|
+
|
532
|
+
end
|
533
|
+
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
end
|
538
|
+
end # End of API class
|
539
|
+
|
540
|
+
|
541
|
+
# End of Ardtweeno Module
|
542
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
####################################################################################################
|
2
|
+
# @author David Kirwan <davidkirwanirl@gmail.com>
|
3
|
+
# @description Configuration Reader Class for the Ardtweeno system
|
4
|
+
#
|
5
|
+
# @date 07-11-2012
|
6
|
+
####################################################################################################
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'yaml'
|
10
|
+
|
11
|
+
module Ardtweeno
|
12
|
+
|
13
|
+
class ConfigReader
|
14
|
+
class << self
|
15
|
+
|
16
|
+
attr_accessor :data, :log;
|
17
|
+
|
18
|
+
# Loads the database from disk
|
19
|
+
def load(path, options={})
|
20
|
+
@log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
|
21
|
+
@log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
|
22
|
+
|
23
|
+
begin
|
24
|
+
@data = YAML.load(File.open(path))
|
25
|
+
log.debug @data.inspect
|
26
|
+
|
27
|
+
rescue ArgumentError => e
|
28
|
+
log.fatal "Could not parse YAML: #{e.message}"
|
29
|
+
log.fatal e.backtrace
|
30
|
+
exit()
|
31
|
+
end
|
32
|
+
|
33
|
+
return @data
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
# Saves the database to disk
|
38
|
+
def save(newData, path, options={})
|
39
|
+
@log = options[:log] ||= Logger.new(STDOUT)
|
40
|
+
@log.level = Logger::DEBUG
|
41
|
+
|
42
|
+
@data = newData
|
43
|
+
|
44
|
+
begin
|
45
|
+
unless options[:mode] == 'append'
|
46
|
+
f = File.open(path, "w")
|
47
|
+
else
|
48
|
+
f = File.open(path, "a")
|
49
|
+
end
|
50
|
+
|
51
|
+
f.write(@data.to_yaml)
|
52
|
+
f.close
|
53
|
+
rescue Exception => e
|
54
|
+
@log.fatal e.message
|
55
|
+
@log.fatal e.backtrace
|
56
|
+
exit()
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/lib/ardtweeno/db.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
####################################################################################################
|
2
|
+
# @author David Kirwan <davidkirwanirl@gmail.com>
|
3
|
+
# @description API class for the Ardtweeno system
|
4
|
+
#
|
5
|
+
# @date 08-01-2013
|
6
|
+
####################################################################################################
|
7
|
+
|
8
|
+
# Imports
|
9
|
+
require 'rubygems'
|
10
|
+
require 'logger'
|
11
|
+
require 'yaml'
|
12
|
+
require 'json'
|
13
|
+
require 'date'
|
14
|
+
require 'mongo'
|
15
|
+
|
16
|
+
|
17
|
+
module Ardtweeno
|
18
|
+
|
19
|
+
##
|
20
|
+
# Ardtweeno::DB class to handle communication with a MongoDB Database
|
21
|
+
#
|
22
|
+
class DB
|
23
|
+
class << self
|
24
|
+
|
25
|
+
attr_accessor :log, :dbconnector, :auth, :coll
|
26
|
+
|
27
|
+
##
|
28
|
+
# Ardtweeno::DB#new Constructor
|
29
|
+
#
|
30
|
+
# * *Args* :
|
31
|
+
# - ++ -> newNode String, newKey String, options Hash{:description String,
|
32
|
+
# :version String, :sensors Array}
|
33
|
+
# * *Returns* :
|
34
|
+
# -
|
35
|
+
# * *Raises* :
|
36
|
+
#
|
37
|
+
def initialize
|
38
|
+
@log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
|
39
|
+
@log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
|
40
|
+
|
41
|
+
@dbconnector = Mongo::Connection.new(host, port).db(databaseName)
|
42
|
+
@auth = @dbconnector.authenticate(my_user_name, my_password)
|
43
|
+
@coll = @dbconnector.collection(collName)
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|