Class: Plc::Raspberrypi::RaspberrypiPlcServer

Inherits:
Object
  • Object
show all
Defined in:
lib/plc/raspberrypi/raspberrypi_plc_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ RaspberrypiPlcServer

Returns a new instance of RaspberrypiPlcServer.



43
44
45
46
# File 'lib/plc/raspberrypi/raspberrypi_plc_server.rb', line 43

def initialize config = {}
  @port = config[:port] || 5555
  @plc = RaspberrypiPlc.new config
end

Class Method Details

.launch(config = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/plc/raspberrypi/raspberrypi_plc_server.rb', line 33

def launch config={}
  @server ||= begin
    server = new config
    server.run
    server
  end
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/plc/raspberrypi/raspberrypi_plc_server.rb', line 48

def run
  puts "launching respberrypi plc ... "
  @plc.run
  puts "done launching"

  Thread.new do
    server = TCPServer.open @port
    loop do
      Thread.start(server.accept) do |socket|
        while line = socket.gets
          begin
            r = @plc.execute_console_commands line
            socket.puts r
          rescue => e
            socket.puts "E0 #{e}\r\n"
          end
        end
      end
    end
  end

end