Sha256: 63ae16d591bf1af06c0588297dba91e4cb7aa6b266c577dd2dd10b7b0fffb56d

Contents?: true

Size: 1.79 KB

Versions: 7

Compression:

Stored size: 1.79 KB

Contents

#!/usr/bin/env ruby

require 'bundler/setup'
require 'eventmachine'
require 'em-websocket'
require 'thin'
require 'trollop'

require './config/boot'

opts = Trollop.options do
  version "Smartkiosk::Client #{Smartkiosk::Client::VERSION}"
  banner "Smartkiosk client application"
  opt :log, "Log file to use", type: String
end

Smartkiosk::Client::Logging.destination = opts[:log] if opts[:log]
Smartkiosk::Client::Logging.init

EventMachine.run do
  #
  # Serving front
  #
  Thin::Server.start '0.0.0.0', 3001 do
    map '/assets' do
      run Application.sprockets
    end
    use Smartkiosk::Client::Logging::Middleware
    run Application
  end

  #
  # Serving WebSockets
  #
  @front_orders = EM::Channel.new

  EventMachine.add_periodic_timer 1 do
    @front_orders.push ['state', Terminal.state].to_json
  end

  EventMachine.add_periodic_timer 5 do
    @front_orders.push ['modified_at', Terminal.modified_at].to_json
  end

  Smartware.subscribe do |message|
    if message.key == "modem.accounting"
      message.acknowlege

      begin
        SessionRecord.create(message_id: message.id,
                             started_at: message[0],
                             upstream: message[1],
                             downstream: message[2],
                             time: message[3])
      rescue => e
        Smartkiosk::Client::Logging.logger.error "unable to save session record: #{e}"
        e.backtrace.each { |line| Smartkiosk::Client::Logging.logger.error line }
      end
    else
      @front_orders.push ["smartware.#{message.key}", message.args].to_json
    end
  end

  EventMachine::WebSocket.run(:host => '0.0.0.0', :port => 3002) do |ws|
    ws.onopen {
      sid = @front_orders.subscribe { |msg| ws.send msg }

      ws.onclose {
        @front_orders.unsubscribe(sid)
      }
    }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
smartkiosk-client-0.2.1 bin/smartkiosk-client
smartkiosk-client-0.2 bin/smartkiosk-client
smartkiosk-client-0.1.19 bin/smartkiosk-client
smartkiosk-client-0.1.18 bin/smartkiosk-client
smartkiosk-client-0.1.17 bin/smartkiosk-client
smartkiosk-client-0.1.16 bin/smartkiosk-client
smartkiosk-client-0.1.15 bin/smartkiosk-client