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 =
180
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.
-
.last_event ⇒ Object
Returns the value of attribute last_event.
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?(force = false) ⇒ Boolean
- .execute(event) ⇒ Object
- .schedule(callback) ⇒ 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
- #valid_event_interval? ⇒ Boolean
Constructor Details
#initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT) ⇒ Notification
Returns a new instance of Notification.
66 67 68 69 70 71 72 |
# File 'lib/device/notification.rb', line 66 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 |
.last_event ⇒ Object
Returns the value of attribute last_event.
10 11 12 |
# File 'lib/device/notification.rb', line 10 def last_event @last_event 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?(force = false) ⇒ Boolean
51 52 53 54 55 56 |
# File 'lib/device/notification.rb', line 51 def self.create_fiber?(force = false) (! Device::Setting.company_name.empty?) && (! Device::Setting.logical_number.empty?) && (force || self.valid_creation_interval?) && Device::ParamsDat.file["notification_enabled"] == "1" 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 |
.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? unless Device::Notification.current && Device::Notification.current.closed? self.new(*self.config) end end end |
.valid_creation_interval? ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/device/notification.rb', line 58 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/device/notification.rb', line 75 def check # TODO check if should execute this(because of connection exception) if valid_check_interval? && Device::Network.connected? if @fiber.alive? if (notification = @fiber.resume) Notification.execute(NotificationEvent.new(notification)) end else if Device::Notification.create_fiber?(true) self.close @fiber = create_fiber end end @last_check = Time.now end end |
#close ⇒ Object
Close socket and finish Fiber execution
93 94 95 96 97 98 99 |
# File 'lib/device/notification.rb', line 93 def close if closed? true else ! @fiber.resume "close" end end |
#closed? ⇒ Boolean
101 102 103 |
# File 'lib/device/notification.rb', line 101 def closed? ! @fiber.alive? end |
#valid_check_interval? ⇒ Boolean
105 106 107 108 109 110 111 |
# File 'lib/device/notification.rb', line 105 def valid_check_interval? if @last_check (@last_check + self.interval) < Time.now else true end end |
#valid_event_interval? ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/device/notification.rb', line 113 def valid_event_interval? if @last_event (@last_event + Notification.creation_interval) < Time.now else true end end |