Class: Hotwired::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/hotwired/db/db.rb,
lib/hotwired/db/model.rb

Defined Under Namespace

Classes: Device

Instance Method Summary collapse

Constructor Details

#initializeDB

类对象初始化函数入口



9
10
11
12
13
14
# File 'lib/hotwired/db/db.rb', line 9

def initialize
  Log.debug "Initialize Hotwired object ... "
  @db = Sequel.sqlite(CFG.db, max_connections: 1, pool_timeout: 60)
  create_table # unless @db.table_exists?(:devices)
  require_relative "model"
end

Instance Method Details

#add(record) ⇒ Object

新建数据库记录



17
18
19
20
21
22
23
# File 'lib/hotwired/db/db.rb', line 17

def add(record)
  # 添加时间戳属性
  record[:first_seen] = record[:last_seen] = Time.now.utc
  record[:active]     = true
  Log.debug "adding: #{record}"
  Device.new(record).save
end

#old(ip, oid_sysname) ⇒ Object

查询数据表记录信息



35
36
37
38
39
# File 'lib/hotwired/db/db.rb', line 35

def old(ip, oid_sysname)
  ip      = Device[ip: ip]
  sysname = Device[oid_sysName: oid_sysname]
  [ip, sysname]
end

#update(record, where) ⇒ Object

更新数据



26
27
28
29
30
31
32
# File 'lib/hotwired/db/db.rb', line 26

def update(record, where)
  # 更新数据入库时间
  record[:last_seen] = Time.now.utc
  record[:active]    = true
  Log.debug "updating (where: #{where}): #{record}"
  Device[where.first.to_sym => where.last].update(record)
end