Last active
May 7, 2016 14:38
-
-
Save ma2shita/8959a512e4ad7d8c166be06fd6b36438 to your computer and use it in GitHub Desktop.
Patlite control ruby library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage; | |
# | |
# Patlite.host = "192.168.254.209" | |
# red = Patlite.new(:red) | |
# p red.state? | |
# #=> :off / :on | |
# if red.off? | |
# red.on | |
# end | |
require "snmp" # NOTE: ref: http://snmplib.rubyforge.org/doc/index.html | |
class Patlite | |
attr_accessor :target | |
@@host = "127.0.0.1" | |
@@secret = "private" | |
@@status = {} | |
# NOTE: ref: http://www.patlite.jp/download/NH-SPL-Manual8158-A.pdf | |
CONTROL_PREFIX = "1.3.6.1.4.1.20440.4.1.5.1.2.1.2" | |
OFFTIMER_PREFIX = "1.3.6.1.4.1.20440.4.1.5.1.2.1.3" | |
MONITOR_PREFIX = "1.3.6.1.4.1.20440.4.1.5.1.2.1.4" | |
TARGET_SUFFIX = {red: "1", yellow: "2", green: "3"} | |
ON = 2 | |
OFF = 1 | |
def initialize(target) | |
raise NameError, "#{target} not found in #{TARGET_SUFFIX.keys}" if ! TARGET_SUFFIX.keys.include?(target) | |
@target = target | |
# NOTE: OID Pre-create for memoize | |
@control_oids = {} | |
@offtimer_oids = {} | |
@monitor_oids = {} | |
TARGET_SUFFIX.each do |k, v| | |
@control_oids[k] = SNMP::ObjectId.new("#{CONTROL_PREFIX}.#{v}") | |
@offtimer_oids[k] = SNMP::ObjectId.new("#{OFFTIMER_PREFIX}.#{v}") | |
@monitor_oids[k] = SNMP::ObjectId.new("#{MONITOR_PREFIX}.#{v}") | |
end | |
end | |
def on ; set ON ; state? ; end | |
def off ; set OFF ; state? ; end | |
def on? ; state?(false) == ON ; end | |
def off? ; state?(false) == OFF ; end | |
def state?(friendly = true) | |
update_status | |
if friendly | |
case true | |
when @@status[@target] == ON | |
:on | |
when @@status[@target] == OFF | |
:off | |
else | |
@@status[@target] | |
end | |
else | |
@@status[@target] | |
end | |
end | |
# NOTE: for accessor to class variables | |
class << self | |
def host ; @@host ; end | |
def host=(value) ; @@host = value ; end | |
def secret ; @@secret ; end | |
def secret=(value) ; @@secret = value ; end | |
end | |
def host ; @@host ; end | |
def secret ; @@secret ; end | |
private | |
def update_status | |
# TODO: @@status cache(memoize) for performance and reduce communication | |
SNMP::Manager.open(Host: @@host, Community: @@secret) do |manager| | |
r = manager.get(@monitor_oids.values) | |
r.varbind_list.each do |i| | |
target = @monitor_oids.find{|k, v| v == i.name}.first | |
@@status[target] = i.value.to_i | |
end | |
end | |
end | |
def set(value, offtimer = 0) # NOTE: OffTimer is count down timer to OFF (unit is sec, 0=disable) | |
SNMP::Manager.open(Host: @@host, Community: @@secret) do |manager| | |
manager.set([ | |
SNMP::VarBind.new(@control_oids[@target], SNMP::Integer.new(value)), | |
SNMP::VarBind.new(@offtimer_oids[@target], SNMP::Integer.new(offtimer)), | |
]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment