Class: Device::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/device/notification.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
15
DEFAULT_INTERVAL =
10
DEFAULT_STREAM_TIMEOUT =
0

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT) ⇒ Notification

Returns a new instance of Notification.



64
65
66
67
68
69
70
# File 'lib/device/notification.rb', line 64

def initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT)
  @timeout        = timeout
  @stream_timeout = stream_timeout
  @interval       = interval
  Device::Notification.current = self
  @fiber = create_fiber
end

Class Attribute Details

.callbacksObject

Returns the value of attribute callbacks.



8
9
10
# File 'lib/device/notification.rb', line 8

def callbacks
  @callbacks
end

.currentObject

Returns the value of attribute current.



8
9
10
# File 'lib/device/notification.rb', line 8

def current
  @current
end

Instance Attribute Details

#fiberObject (readonly)

Returns the value of attribute fiber.



13
14
15
# File 'lib/device/notification.rb', line 13

def fiber
  @fiber
end

#intervalObject (readonly)

Returns the value of attribute interval.



13
14
15
# File 'lib/device/notification.rb', line 13

def interval
  @interval
end

#last_checkObject (readonly)

Returns the value of attribute last_check.



13
14
15
# File 'lib/device/notification.rb', line 13

def last_check
  @last_check
end

#stream_timeoutObject (readonly)

Returns the value of attribute stream_timeout.



13
14
15
# File 'lib/device/notification.rb', line 13

def stream_timeout
  @stream_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/device/notification.rb', line 13

def timeout
  @timeout
end

Class Method Details

.checkObject



15
16
17
# File 'lib/device/notification.rb', line 15

def self.check
  self.current.check if self.current
end

.configObject



32
33
34
35
36
37
# File 'lib/device/notification.rb', line 32

def self.config
  notification_timeout        = Device::Setting.notification_timeout.empty? ? DEFAULT_TIMEOUT : Device::Setting.notification_timeout.to_i
  notification_interval       = Device::Setting.notification_interval.empty? ? DEFAULT_INTERVAL : Device::Setting.notification_interval.to_i
  notification_stream_timeout = Device::Setting.notification_stream_timeout.empty? ? DEFAULT_STREAM_TIMEOUT : Device::Setting.notification_stream_timeout.to_i
  [notification_timeout, notification_interval, notification_stream_timeout]
end

.execute(event) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/device/notification.rb', line 19

def self.execute(event)
  calls = self.callbacks[event.callback]
  return unless calls
  [:before, :on, :after].each do |moment|
    calls.each{|callback| callback.call(event, moment)}
  end
end

.schedule(callback) ⇒ Object



27
28
29
30
# File 'lib/device/notification.rb', line 27

def self.schedule(callback)
  self.callbacks[callback.description] ||= []
  self.callbacks[callback.description] << callback
end

.setupObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/device/notification.rb', line 47

def self.setup
  NotificationCallback.new "APP_UPDATE", :on => Proc.new { Device::ParamsDat.update_apps(true) }
  NotificationCallback.new "SETUP_DEVICE_CONFIG", :on => Proc.new { Device::ParamsDat.update_apps(true) }
  NotificationCallback.new "RESET_DEVICE_CONFIG", :on => Proc.new { Device::ParamsDat.format! }

  NotificationCallback.new "SYSTEM_UPDATE", :on => Proc.new { |file| }
  NotificationCallback.new "CANCEL_SYSTEM_UPDATE", :on => Proc.new { }
  NotificationCallback.new "TIMEZONE_UPDATE", :on => Proc.new { Device::Setting.cw_pos_timezone = "" }
  NotificationCallback.new "SHOW_MESSAGE", :on => Proc.new { |message, datetime|
    Device::Display.clear
    date = datetime.sub(" ", "-").split("-")
    Device::Display.print_line("#{date[1]}/#{date[0]}/#{date[2]} #{date[3]}", 0)
    Device::Display.print_line("#{message}", 2)
    getc nil
  }
end

.startObject



39
40
41
42
43
44
45
# File 'lib/device/notification.rb', line 39

def self.start
  unless Device::Setting.logical_number.empty? || Device::Setting.company_name.empty? || (! Device::Network.connected?)
    unless Device::Notification.current && Device::Notification.current.closed?
      self.new(*self.config)
    end
  end
end

Instance Method Details

#checkObject

Check if there is any notification



73
74
75
76
77
78
79
80
# File 'lib/device/notification.rb', line 73

def check
  if @fiber.alive? && valid_interval? && Device::Network.connected?
    if (notification = @fiber.resume)
      Notification.execute(NotificationEvent.new(notification))
    end
    @last_check = Time.now
  end
end

#closeObject

Close socket and finish Fiber execution



83
84
85
# File 'lib/device/notification.rb', line 83

def close
  @fiber.resume "close"
end

#closed?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/device/notification.rb', line 87

def closed?
  ! @fiber.alive?
end

#valid_interval?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/device/notification.rb', line 91

def valid_interval?
  if @last_check
    (@last_check + self.interval) < Time.now
  else
    true
  end
end