smartkiosk-client 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,6 +57,10 @@ class Application
57
57
  json(:state => Terminal.state)
58
58
  end
59
59
 
60
+ post '/terminal/recalibrate' do
61
+ Terminal.recalibrate
62
+ end
63
+
60
64
  get '/terminal/test_connection' do
61
65
  begin
62
66
  uri = URI.parse(Terminal.config.host)
@@ -0,0 +1,3 @@
1
+ class SessionRecord < ActiveRecord::Base
2
+
3
+ end
@@ -68,6 +68,11 @@ class Terminal
68
68
  Smartguard::Client.reboot_async
69
69
  end
70
70
 
71
+ def self.recalibrate
72
+ Smartware.user_interface.delete_calibration
73
+ Smartware.user_interface.restart_ui
74
+ end
75
+
71
76
  #
72
77
  # CONDITON
73
78
  #
@@ -5,6 +5,7 @@ class PingWorker
5
5
 
6
6
  sidekiq_options :retry => false, :queue => :pings
7
7
 
8
+ DAY = 24 * 3600
8
9
  SEMAPHORE = ConnectionPool.new(:size => 1, :timeout => 5) { true }
9
10
 
10
11
  def perform
@@ -14,16 +15,49 @@ class PingWorker
14
15
 
15
16
  Sidekiq::Logging.logger.info "Requesting with updated_at: #{Terminal.providers_updated_at}"
16
17
 
18
+ sessions_per_day = {}
19
+ sessions = SessionRecord.all
20
+
21
+ sessions.each do |session|
22
+ next if session.time == 0
23
+
24
+ session_start = session.started_at
25
+ session_start -= session_start % DAY
26
+ session_end = session.started_at + session.time
27
+
28
+ session_start.step(session_end - 1, DAY) do |day|
29
+ day_time = [ day + DAY - 1, session.started_at + session.time ].min -
30
+ [ day, session.started_at ].max
31
+
32
+ fraction = day_time / session.time
33
+
34
+ sessions_per_day[day] = {
35
+ upstream: session.upstream * fraction,
36
+ downstream: session.downstream * fraction,
37
+ time: session.time * fraction
38
+ }
39
+ end
40
+ end
41
+
42
+ sessions.each do |day, charges|
43
+ Sidekiq::Logging.logger.info "reporting #{charges.inspect} on #{day}"
44
+ end
45
+
17
46
  begin
18
47
  response = RestClient::Request.execute(
19
48
  :method => :post,
20
49
  :url => "#{Terminal.config.host}/terminal_pings",
21
50
  :timeout => 40,
22
51
  :open_timeout => 60,
23
- :payload => {
52
+ :headers => {
53
+ "Content-Type" => "application/json",
54
+ "Accept" => "application/json"
55
+ },
56
+ :payload => JSON.dump(
24
57
  :terminal => Terminal.keyword,
25
- :terminal_ping => condition
26
- }
58
+ :terminal_ping => condition,
59
+ :sessions => sessions_per_day
60
+ )
27
61
  )
28
62
  rescue Exception => e
29
63
  Sidekiq::Logging.logger.warn e.to_s
@@ -39,6 +73,10 @@ class PingWorker
39
73
 
40
74
  Sidekiq::Logging.logger.info "Response: #{response.inspect}"
41
75
 
76
+ ActiveRecord::Base.transaction do
77
+ sessions.each &:destroy
78
+ end
79
+
42
80
  #
43
81
  # PROFILE
44
82
  #
@@ -42,8 +42,19 @@ EventMachine.run do
42
42
  @front_orders.push ['modified_at', Terminal.modified_at].to_json
43
43
  end
44
44
 
45
- Smartware.subscribe do |key, *value|
46
- @front_orders.push ["smartware.#{key}", value].to_json
45
+ Smartware.subscribe do |message|
46
+ if message.key == "modem.accounting"
47
+ message.acknowlege
48
+
49
+ p message
50
+
51
+ SessionRecord.create(started_at: message[0],
52
+ upstream: message[1],
53
+ downstream: message[2],
54
+ time: message[3])
55
+ else
56
+ @front_orders.push ["smartware.#{message.key}", message.args].to_json
57
+ end
47
58
  end
48
59
 
49
60
  EventMachine::WebSocket.run(:host => '0.0.0.0', :port => 3002) do |ws|
@@ -0,0 +1,12 @@
1
+ class CreateSessionRecords < ActiveRecord::Migration
2
+ def change
3
+ create_table :session_records do |t|
4
+ t.integer :started_at
5
+ t.integer :upstream
6
+ t.integer :downstream
7
+ t.integer :time
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -2,6 +2,6 @@ require 'sinatra/base'
2
2
 
3
3
  module Smartkiosk
4
4
  class Client < Sinatra::Base
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
  end
7
7
  end
@@ -49,7 +49,7 @@ Gem::Specification.new do |gem|
49
49
  gem.add_dependency 'rubyzip', '0.9.9'
50
50
  gem.add_dependency 'pry', '0.9.10'
51
51
 
52
- gem.add_dependency 'smartware', '0.4.4'
52
+ gem.add_dependency 'smartware', '0.4.9'
53
53
  gem.add_dependency 'smartguard', '0.4.3'
54
54
 
55
55
  gem.add_dependency 'sidekiq', '2.6.5'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartkiosk-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-12 00:00:00.000000000 Z
13
+ date: 2013-02-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -435,7 +435,7 @@ dependencies:
435
435
  requirements:
436
436
  - - '='
437
437
  - !ruby/object:Gem::Version
438
- version: 0.4.4
438
+ version: 0.4.9
439
439
  type: :runtime
440
440
  prerelease: false
441
441
  version_requirements: !ruby/object:Gem::Requirement
@@ -443,7 +443,7 @@ dependencies:
443
443
  requirements:
444
444
  - - '='
445
445
  - !ruby/object:Gem::Version
446
- version: 0.4.4
446
+ version: 0.4.9
447
447
  - !ruby/object:Gem::Dependency
448
448
  name: smartguard
449
449
  requirement: !ruby/object:Gem::Requirement
@@ -538,6 +538,7 @@ files:
538
538
  - app/models/provider.rb
539
539
  - app/models/receipt.rb
540
540
  - app/models/receipt_template.rb
541
+ - app/models/session_record.rb
541
542
  - app/models/terminal.rb
542
543
  - app/uploaders/icon_uploader.rb
543
544
  - app/views/banners.haml
@@ -577,6 +578,7 @@ files:
577
578
  - db/migrate/20130106181844_create_phone_ranges.rb
578
579
  - db/migrate/20130108110546_create_promotions.rb
579
580
  - db/migrate/20130110025256_create_banners.rb
581
+ - db/migrate/20130216114539_create_session_records.rb
580
582
  - init.rb
581
583
  - lib/patches/sidekiq.rb
582
584
  - lib/pinger.rb
@@ -608,7 +610,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
608
610
  version: '0'
609
611
  segments:
610
612
  - 0
611
- hash: -233556075953690001
613
+ hash: 2149702848177027716
612
614
  required_rubygems_version: !ruby/object:Gem::Requirement
613
615
  none: false
614
616
  requirements:
@@ -617,7 +619,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
617
619
  version: '0'
618
620
  segments:
619
621
  - 0
620
- hash: -233556075953690001
622
+ hash: 2149702848177027716
621
623
  requirements: []
622
624
  rubyforge_project:
623
625
  rubygems_version: 1.8.24