Sha256: f155e26657f5fd95d14517eee4f50b2e67186c893a770c4f49b7dc148c4796c4

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Hotwired
  class DB
    require "sequel"
    require "sqlite3"

    # 类对象初始化函数入口
    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

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

    # 更新数据
    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

    # 查询数据表记录信息
    def old(ip, oid_sysname)
      ip      = Device[ip: ip]
      sysname = Device[oid_sysName: oid_sysname]
      [ip, sysname]
    end

    private
      def create_table
        # 检查是否存在数据表结构,不存在则新建
        @db.create_table? :devices do
          primary_key :id
          String :ip
          String :ptr
          String :model
          String :oid_ifDescr
          Boolean :active
          Time :first_seen
          Time :last_seen
          String :oid_sysName
          String :oid_sysLocation
          String :oid_sysDescr
          String :oid_sysObjectID
        end
        Log.debug "Creating SQLITE DB"
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hotwired-0.0.4 lib/hotwired/db/db.rb
hotwired-0.0.3 lib/hotwired/db/db.rb
hotwired-0.0.2 lib/hotwired/db/db.rb
hotwired-0.0.1 lib/hotwired/db/db.rb