Class: KNX::ObjectServer::Datagram

Inherits:
Struct
  • Object
show all
Defined in:
lib/knx/object_server/datagram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data = nil) ⇒ Datagram

Returns a new instance of Datagram.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/knx/object_server/datagram.rb', line 22

def initialize(raw_data = nil)
    super(Header.new, ConnectionHeader.new, ObjectHeader.new)
    # These values are unique to the KNX Object Server
    self.knx_header.version = 0x20
    self.knx_header.request_type = 0xF080
    @data = []

    if raw_data
        self.knx_header.read(raw_data[0..5])
        self.connection.read(raw_data[6..9])
        self.header.read(raw_data[10..15])

        # Check for error
        if self.header.item_count == 0
            @error_code = raw_data[16].getbyte(0)
            @error = Errors[@error_code]
        else
            @error_code = 0
            @error = :no_error

            # Read the response
            index = 16
            self.header.item_count.times do
                next_index = index + 4
                item = StatusItem.new
                item.read(raw_data[index...next_index])

                index = next_index + item.value_length
                item.value = raw_data[next_index...index]

                @data << item
            end
        end
    end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection

Returns:

  • (Object)

    the current value of connection



21
22
23
# File 'lib/knx/object_server/datagram.rb', line 21

def connection
  @connection
end

#dataObject (readonly)

Returns the value of attribute data.



59
60
61
# File 'lib/knx/object_server/datagram.rb', line 59

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



59
60
61
# File 'lib/knx/object_server/datagram.rb', line 59

def error
  @error
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



59
60
61
# File 'lib/knx/object_server/datagram.rb', line 59

def error_code
  @error_code
end

#headerObject

Returns the value of attribute header

Returns:

  • (Object)

    the current value of header



21
22
23
# File 'lib/knx/object_server/datagram.rb', line 21

def header
  @header
end

#knx_headerObject

Returns the value of attribute knx_header

Returns:

  • (Object)

    the current value of knx_header



21
22
23
# File 'lib/knx/object_server/datagram.rb', line 21

def knx_header
  @knx_header
end

Instance Method Details

#add_action(index, data: nil, **options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/knx/object_server/datagram.rb', line 79

def add_action(index, data: nil, **options)
    req = RequestItem.new
    req.id = index.to_i
    req.command = Commands[options[:command]] || :set_value
    if not data.nil?
        if data == true || data == false
            data = data ? 1 : 0
        end

        if data.is_a? String
            req.value = data
        else
            req.value = String.new
            req.value << data
        end
    end
    @data << req
    self
end

#error?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/knx/object_server/datagram.rb', line 62

def error?
    @error_code != 0
end

#to_binary_sObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/knx/object_server/datagram.rb', line 66

def to_binary_s
    self.header.item_count = @data.length if @data.length > 0
    resp = String.new "#{self.connection.to_binary_s}#{self.header.to_binary_s}"

    @data.each do |item|
        resp << item.to_binary_s
    end

    self.knx_header.request_length = resp.length + 6
    resp.prepend self.knx_header.to_binary_s
    resp
end