Class: Subduino::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/subduino/store.rb

Class Method Summary collapse

Class Method Details

.add_csv_to_store(csv, stamp = false) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/subduino/store.rb', line 39

def add_csv_to_store(csv, stamp = false)
  # Log.info "[STORE] CSV #{Time.now.to_i}"
  csv.split(",").each do |d|
    comm, value = d.split(":")
    write(comm, value, stamp)
  end
end

.add_to_store(key, val = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/subduino/store.rb', line 29

def add_to_store(key, val=nil)
  if val
    write key, val
  else
    key.each do |k,v|
      write k, v
    end
  end
end

.allObject



62
63
64
# File 'lib/subduino/store.rb', line 62

def all
  redis.keys
end

.countObject



53
54
55
# File 'lib/subduino/store.rb', line 53

def count
  redis.dbsize
end

.flushObject



57
58
59
60
# File 'lib/subduino/store.rb', line 57

def flush
  redis.flushdb
  #redis.flushall
end

.read(key) ⇒ Object



16
17
18
# File 'lib/subduino/store.rb', line 16

def read(key)
  redis.get key.to_s
end

.read_hist(key, period, offset = 0, max = 100) ⇒ Object

SORT key [BY pattern] [LIMIT start count] [GET pattern]

ASC|DESC
ALPHA
STORE dstkey


49
50
51
# File 'lib/subduino/store.rb', line 49

def read_hist(key, period, offset = 0, max = 100)
  redis.sort key, { :order => 'desc', :limit => [offset, max] }
end

.redisObject



12
13
14
# File 'lib/subduino/store.rb', line 12

def redis
  @redis ||= Redis.new(:timeout => 0) rescue false
end

.timestampObject



20
21
22
# File 'lib/subduino/store.rb', line 20

def timestamp
  Time.now.to_i.to_s
end

.write(k, v, stamp = false) ⇒ Object



24
25
26
27
# File 'lib/subduino/store.rb', line 24

def write(k, v, stamp = false)
  return unless redis #.connected?
  stamp ?  redis.rpush("log:inputs:#{k}", "#{timestamp}:#{v}") : redis.set("now:inputs:#{k}", v)
end