Class: Device::Notification
- Inherits:
-
Object
- Object
- Device::Notification
- Defined in:
- lib/device/notification.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
20- DEFAULT_INTERVAL =
10- DEFAULT_STREAM_TIMEOUT =
0- DEFAULT_CREATION_INTERVAL =
3600
Class Attribute Summary collapse
-
.callbacks ⇒ Object
Returns the value of attribute callbacks.
-
.creation_interval ⇒ Object
Returns the value of attribute creation_interval.
-
.current ⇒ Object
Returns the value of attribute current.
-
.last_creation ⇒ Object
Returns the value of attribute last_creation.
Instance Attribute Summary collapse
-
#fiber ⇒ Object
readonly
Returns the value of attribute fiber.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_check ⇒ Object
readonly
Returns the value of attribute last_check.
-
#stream_timeout ⇒ Object
readonly
Returns the value of attribute stream_timeout.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
- .check ⇒ Object
- .config ⇒ Object
- .create_fiber? ⇒ Boolean
- .execute(event) ⇒ Object
- .schedule(callback) ⇒ Object
- .setup ⇒ Object
- .start ⇒ Object
- .valid_creation_interval? ⇒ Boolean
Instance Method Summary collapse
-
#check ⇒ Object
Check if there is any notification.
-
#close ⇒ Object
Close socket and finish Fiber execution.
- #closed? ⇒ Boolean
-
#initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT) ⇒ Notification
constructor
A new instance of Notification.
- #valid_check_interval? ⇒ Boolean
Constructor Details
#initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT) ⇒ Notification
Returns a new instance of Notification.
80 81 82 83 84 85 86 |
# File 'lib/device/notification.rb', line 80 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
.callbacks ⇒ Object
Returns the value of attribute callbacks.
10 11 12 |
# File 'lib/device/notification.rb', line 10 def callbacks @callbacks end |
.creation_interval ⇒ Object
Returns the value of attribute creation_interval.
10 11 12 |
# File 'lib/device/notification.rb', line 10 def creation_interval @creation_interval end |
.current ⇒ Object
Returns the value of attribute current.
10 11 12 |
# File 'lib/device/notification.rb', line 10 def current @current end |
.last_creation ⇒ Object
Returns the value of attribute last_creation.
10 11 12 |
# File 'lib/device/notification.rb', line 10 def last_creation @last_creation end |
Instance Attribute Details
#fiber ⇒ Object (readonly)
Returns the value of attribute fiber.
16 17 18 |
# File 'lib/device/notification.rb', line 16 def fiber @fiber end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
16 17 18 |
# File 'lib/device/notification.rb', line 16 def interval @interval end |
#last_check ⇒ Object (readonly)
Returns the value of attribute last_check.
16 17 18 |
# File 'lib/device/notification.rb', line 16 def last_check @last_check end |
#stream_timeout ⇒ Object (readonly)
Returns the value of attribute stream_timeout.
16 17 18 |
# File 'lib/device/notification.rb', line 16 def stream_timeout @stream_timeout end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
16 17 18 |
# File 'lib/device/notification.rb', line 16 def timeout @timeout end |
Class Method Details
.check ⇒ Object
18 19 20 |
# File 'lib/device/notification.rb', line 18 def self.check self.current.check if self.current end |
.config ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/device/notification.rb', line 35 def self.config self.creation_interval = Device::Setting.notification_socket_timeout.empty? ? DEFAULT_CREATION_INTERVAL : Device::Setting.notification_socket_timeout.to_i 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 |
.create_fiber? ⇒ Boolean
68 69 70 |
# File 'lib/device/notification.rb', line 68 def self.create_fiber? (! Device::Setting.company_name.empty?) && (! Device::Setting.logical_number.empty?) && self.valid_creation_interval? end |
.execute(event) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/device/notification.rb', line 22 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
30 31 32 33 |
# File 'lib/device/notification.rb', line 30 def self.schedule(callback) self.callbacks[callback.description] ||= [] self.callbacks[callback.description] << callback end |
.setup ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/device/notification.rb', line 51 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 { |, 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("#{}", 2) getc(0) } end |
.start ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/device/notification.rb', line 43 def self.start if create_fiber? && Device::Network.connected? == Device::Network::SUCCESS unless Device::Notification.current && Device::Notification.current.closed? self.new(*self.config) end end end |
.valid_creation_interval? ⇒ Boolean
72 73 74 75 76 77 78 |
# File 'lib/device/notification.rb', line 72 def self.valid_creation_interval? if @last_creation (@last_creation + self.creation_interval) < Time.now else true end end |
Instance Method Details
#check ⇒ Object
Check if there is any notification
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/device/notification.rb', line 89 def check if valid_check_interval? && Device::Network.connected? == Device::Network::SUCCESS if @fiber.alive? if (notification = @fiber.resume) Notification.execute(NotificationEvent.new(notification)) end @last_check = Time.now if Device::Notification.create_fiber? self.close @fiber = create_fiber end end end end |
#close ⇒ Object
Close socket and finish Fiber execution
105 106 107 108 109 110 111 |
# File 'lib/device/notification.rb', line 105 def close if closed? true else ! @fiber.resume "close" end end |
#closed? ⇒ Boolean
113 114 115 |
# File 'lib/device/notification.rb', line 113 def closed? ! @fiber.alive? end |
#valid_check_interval? ⇒ Boolean
117 118 119 120 121 122 123 |
# File 'lib/device/notification.rb', line 117 def valid_check_interval? if @last_check (@last_check + self.interval) < Time.now else true end end |