Module: AutomateEm::Utilities

Included in:
DatagramBase, Device::Base, Logic, ModuleCore
Defined in:
lib/automate-em/utilities.rb

Class Method Summary collapse

Class Method Details

.array_to_str(data) ⇒ Object

Converts an array into a raw byte string



155
156
157
# File 'lib/automate-em/utilities.rb', line 155

def array_to_str(data)
	data.pack('c*')
end

.byte_to_hex(data) ⇒ Object

Converts a raw byte string into a hex encoded string



135
136
137
138
139
140
141
142
143
# File 'lib/automate-em/utilities.rb', line 135

def byte_to_hex(data)	# Assumes string - converts from a binary string
	output = ""
	data.each_byte { |c|
		s = c.to_s(16)
		s = "0#{s}" if s.length % 2 > 0
		output << s
	}
	return output
end

.hex_to_byte(data) ⇒ Object

Converts a hex encoded string into a raw byte string



124
125
126
127
128
129
130
# File 'lib/automate-em/utilities.rb', line 124

def hex_to_byte(data)	# Assumes string - converts to binary string
	data.gsub!(/(0x|[^0-9A-Fa-f])*/, "")				# Removes invalid characters
	output = ""
	data = "0#{data}" if data.length % 2 > 0
	data.scan(/.{2}/) { |byte| output << byte.hex}	# Breaks string into an array of characters
	return output
end

.scheduleObject

Schedule events



176
177
178
179
180
181
# File 'lib/automate-em/utilities.rb', line 176

def schedule
			return @schedule unless @schedule.nil?
			@status_lock.synchronize {
				@schedule ||= ScheduleProxy.new
			}
end

.str_to_array(data) ⇒ Object

Converts a string into a byte array



148
149
150
# File 'lib/automate-em/utilities.rb', line 148

def str_to_array(data)
	data.bytes.to_a
end

.task(callback = nil, &block) ⇒ Object

Creates a new threaded task



162
163
164
165
166
167
168
169
170
# File 'lib/automate-em/utilities.rb', line 162

def task(callback = nil, &block)
	EM.defer(nil, callback) do
		begin
			block.call
		rescue => e
			AutomateEm.print_error(System.logger, e, :message => "Error in task")
		end
	end
end