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



151
152
153
# File 'lib/automate-em/utilities.rb', line 151

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

.byte_to_hex(data) ⇒ Object

Converts a raw byte string into a hex encoded string



131
132
133
134
135
136
137
138
139
# File 'lib/automate-em/utilities.rb', line 131

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



120
121
122
123
124
125
126
# File 'lib/automate-em/utilities.rb', line 120

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



172
173
174
175
176
177
# File 'lib/automate-em/utilities.rb', line 172

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



144
145
146
# File 'lib/automate-em/utilities.rb', line 144

def str_to_array(data)
	data.bytes.to_a
end

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

Creates a new threaded task



158
159
160
161
162
163
164
165
166
# File 'lib/automate-em/utilities.rb', line 158

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