Class: Ansible::ZWave::ValueID

Inherits:
OpenZWave::RemoteValueID show all
Includes:
AnsibleCallback, AnsibleValue
Defined in:
lib/ansible/zwave/zwave_value.rb

Overview

extend the Thrift ValueID interface with some interesting stuff

Constant Summary collapse

@@transceiver =

—— CLASS VARIABLES & METHODS

nil

Constants inherited from OpenZWave::RemoteValueID

OpenZWave::RemoteValueID::FIELDS

Instance Attribute Summary collapse

Attributes included from AnsibleValue

#current_value, #flags, #last_update, #previous_value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AnsibleCallback

#add_callback, #fire_callback, #remove_callback

Methods included from AnsibleValue

[], #as_canonical_value, #get, insert, #matches?, #set, #to_protocol_value, #update

Methods inherited from OpenZWave::RemoteValueID

#struct_fields, #validate

Constructor Details

#initialize(homeid_str, valueid_str) ⇒ ValueID

initialize ValueID by home and value id (both hex strings)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ansible/zwave/zwave_value.rb', line 76

def initialize( homeid_str, valueid_str)
    raise 'both arguments must be strings' if [homeid_str, valueid_str].find{|s| not s.is_a?String}
    
    # wARNING: instance variable naming must be consistent with ozw_types.rb (Thrift interface)
    @_homeId = homeid_str.to_i(16)
    @valueId  = valueid_str.to_i(16)
    # parse all fields
    m_id = OZW_ValueID_id.read([valueid_str.delete(' ')[-8..-1].to_i(16)].pack("N"))
    m_id1 = OZW_ValueID_id1.read([valueid_str.delete(' ')[0..-9].to_i(16)].pack("N"))
    # and store them
    @_nodeId = m_id.node_id
    @_genre = m_id.value_genre
    @_type = m_id.value_type
    @_valueIndex = m_id.value_idx
    @_commandClassId = m_id.cmd_class
    @_instance = m_id1.cmd_class_instance
    # access flags, default R/W
    @flags = {:r => true, :w => true}
    puts "NEW ZWAVE VALUE CREATED: #{self.inspect}" if $DEBUG
    
    if @_homeId > 0 then
        # fill in some useful info so as not to query OpenZWave all the time
        @flags[:readonly] = @@transceiver.manager_send(:IsValueReadOnly, self)
        @flags[:writeonly] = @@transceiver.manager_send(:IsValueWriteOnly, self)
    end
    
    # time of last update
    @last_update = nil
    
    # a boolean flag set to true so as to know all subsequent notifications
    # by OpenZWave regarding this value have been caused by us
    @poll_delayed = false
    
    # dynamic binding to the corresponding OpenZWave data type
    @typename, @typedescr = OpenZWave::ValueTypes[@_type]
    @typemod = Ansible::ZWave.module_eval(@typename)
    raise "unknown/undeclared ZWave type module #{@typename}" unless @typemod.is_a?Module
    # extend this ValueID with type-specific module
    self.extend(@typemod)
    # store this ZWave ValueID in the Ansible database
    AnsibleValue.insert(self)
end

Instance Attribute Details

#valueIdObject (readonly)

—– INSTANCE VARIABLES & METHODS



65
66
67
# File 'lib/ansible/zwave/zwave_value.rb', line 65

def valueId
  @valueId
end

Class Method Details

.get_or_create(homeid_str, valueid_str) ⇒ Object

get existing ValueID object, or else create it



54
55
56
57
58
59
# File 'lib/ansible/zwave/zwave_value.rb', line 54

def ValueID.get_or_create(homeid_str, valueid_str)
    query = AnsibleValue[:_homeId => homeid_str.to_i(16), :valueId => valueid_str.to_i(16)]
    value = (query.is_a?Array and query.size>0 and query[0]) or 
            ValueID.new(homeid_str, valueid_str)
    return value
end

.transceiverObject



48
# File 'lib/ansible/zwave/zwave_value.rb', line 48

def ValueID.transceiver; return @@transceiver; end

.transceiver=(other) ⇒ Object



49
50
51
# File 'lib/ansible/zwave/zwave_value.rb', line 49

def ValueID.transceiver=(other); 
    @@transceiver = other if other.is_a? Ansible::ZWave::ZWave_Transceiver
end

Instance Method Details

#==(other) ⇒ Object

equality checking



68
69
70
71
72
73
# File 'lib/ansible/zwave/zwave_value.rb', line 68

def == (other)
    return (
        other.is_a?(ValueID) and
        (@_homeId == other._homeId) and (@valueId == other.valueId)
    )
end

#explainObject

fetch all available ValueID info from OpenZWave



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ansible/zwave/zwave_value.rb', line 166

def explain
    return(%Q{#{Time.now}
        Value Label: #{ @@transceiver.manager_send(:GetValueLabel, self)}
        Value Help:  #{ @@transceiver.manager_send(:GetValueHelp, self)}
        
        Value Units: #{ @@transceiver.manager_send(:GetValueUnits, self)}
        Value Min:   #{ @@transceiver.manager_send(:GetValueMin, self)}
        Value Max:   #{ @@transceiver.manager_send(:GetValueMax, self)}
        
        Value read-only?  #{ @@transceiver.manager_send(:IsValueReadOnly, self)}
        Value write-only? #{ @@transceiver.manager_send(:IsValueWriteOnly, self)}
        Value set?        #{ @@transceiver.manager_send(:IsValueSet, self)}
})
end

#read_only?Boolean

is this ZWave value read only?

Returns:

  • (Boolean)


121
122
123
# File 'lib/ansible/zwave/zwave_value.rb', line 121

def read_only?
    (defined?@flags) and @flags[:readonly]
end

#read_valueObject

ZWave-specific: read value from the bus



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ansible/zwave/zwave_value.rb', line 133

def read_value()
    return(false) unless respond_to? :read_operation
    result = @@transceiver.manager_send(read_operation, self)
    if result and result.retval then
        puts "#{Time.now} #{self}.read_value() result == #{result.o_value}, Refreshed=#{RefreshedNodes[@_nodeId]}"
        update(result.o_value)
        return(true)
    else
        return(false)
    end
end

#to_sObject

return a reasonable string representation of the ZWave value



161
162
163
# File 'lib/ansible/zwave/zwave_value.rb', line 161

def to_s
    return "#{Time.now} ZWaveValue[n:#{@_nodeId} g:#{@_genre} cc:#{@_commandClassId} i:#{@_instance} vi:#{@_valueIndex} t:#{@_type}]==#{@current_value}(#{@current_value.class})"
end

#write_only?Boolean

is this ZWave value write only?

Returns:

  • (Boolean)


126
127
128
# File 'lib/ansible/zwave/zwave_value.rb', line 126

def write_only?
    (defined?@flags) and @flags[:writeonly]
end

#write_value(new_val) ⇒ Object

ZWave-specific: write new value to OpenZWave new_val must be in protocol form return true if successful, false otherwise



149
150
151
152
153
154
155
156
157
158
# File 'lib/ansible/zwave/zwave_value.rb', line 149

def write_value(new_val)
    return(false) unless respond_to? :write_operation
    if @@transceiver.manager_send(write_operation, self, new_val) then
        # value can also be updated by ValueChanged notification
        update(new_val) 
        return(true)
    else
        return(false)
    end
end