Class: Lignite::SystemCommands
- Inherits:
-
Object
- Object
- Lignite::SystemCommands
- Extended by:
- Logger
- Defined in:
- lib/lignite/system_commands.rb
Instance Method Summary collapse
- #handlers(odata) ⇒ Object
-
#initialize(conn = Connection.create) ⇒ SystemCommands
constructor
A new instance of SystemCommands.
-
#load_op(oname, odata) ⇒ Object
oname LIST_FILES.
- #load_yml ⇒ Object
- #param_handler(oparam) ⇒ Object
-
#return_handler(oparam) ⇒ Object
the handler is a lambda returning a pair: a parsed value and the rest of the input.
Methods included from Logger
Methods included from Bytes
#f32, #hexdump, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8
Constructor Details
#initialize(conn = Connection.create) ⇒ SystemCommands
Returns a new instance of SystemCommands.
8 9 10 11 |
# File 'lib/lignite/system_commands.rb', line 8 def initialize(conn = Connection.create) = MessageSender.new(conn) load_yml end |
Instance Method Details
#handlers(odata) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lignite/system_commands.rb', line 54 def handlers(odata) oparams = odata["params"] param_handlers = [] return_handlers = [] oparams.each do |p| if p["dir"] == "in" param_handlers << param_handler(p) else return_handlers << return_handler(p) end end [param_handlers, return_handlers] end |
#load_op(oname, odata) ⇒ Object
oname LIST_FILES
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 |
# File 'lib/lignite/system_commands.rb', line 22 def load_op(oname, odata) ovalue = odata["value"] param_handlers, return_handlers = handlers(odata) osym = oname.downcase.to_sym self.class.send(:define_method, osym) do |*args| logger.debug "called #{osym} with #{args.inspect}" if args.size != param_handlers.size raise ArgumentError, "expected #{param_handlers.size} arguments, got #{args.size}" end bytes = u8(ovalue) bytes += param_handlers.zip(args).map do |h, a| # h.call(a) would have self = Op instead of #<Op> instance_exec(a, &h) end.join("") logger.debug "sysop to execute: #{bytes.inspect}" reply = .system_command_with_reply(bytes) # TODO: parse it with return_handlers replies = return_handlers.map do |h| parsed, reply = h.call(reply) parsed end raise "Unparsed reply #{reply.inspect}" unless reply.empty? # A single reply is returned as a scalar, not an array replies.size == 1 ? replies.first : replies end end |
#load_yml ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/lignite/system_commands.rb', line 13 def load_yml fname = File.("../../../data/sysops.yml", __FILE__) op_hash = YAML.load_file(fname)["sysops"] op_hash.each do |oname, odata| load_op(oname, odata) end end |
#param_handler(oparam) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lignite/system_commands.rb', line 68 def param_handler(oparam) case oparam["type"] when "U8" ->(x) { u8(x) } when "U16" ->(x) { u16(x) } when "U32" ->(x) { u32(x) } when "BYTES" ->(x) { x } when "ZBYTES" ->(x) { x + u8(0) } else raise end end |
#return_handler(oparam) ⇒ Object
the handler is a lambda returning a pair: a parsed value and the rest of the input
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lignite/system_commands.rb', line 87 def return_handler(oparam) case oparam["type"] when "U8" ->(i) { [unpack_u8(i[0, 1]), i[1..-1]] } when "U16" ->(i) { [unpack_u16(i[0, 2]), i[2..-1]] } when "U32" ->(i) { [unpack_u32(i[0, 4]), i[4..-1]] } when "BYTES" ->(i) { [i, ""] } else raise end end |