2022-10-22 22:30:40 +01:00
|
|
|
module OBSWS
|
|
|
|
module Events
|
|
|
|
module SUBS
|
|
|
|
NONE = 0
|
2022-10-27 06:45:21 +01:00
|
|
|
GENERAL = 1 << 0
|
|
|
|
CONFIG = 1 << 1
|
|
|
|
SCENES = 1 << 2
|
|
|
|
INPUTS = 1 << 3
|
|
|
|
TRANSITIONS = 1 << 4
|
|
|
|
FILTERS = 1 << 5
|
|
|
|
OUTPUTS = 1 << 6
|
|
|
|
SCENEITEMS = 1 << 7
|
|
|
|
MEDIAINPUTS = 1 << 8
|
|
|
|
VENDORS = 1 << 9
|
|
|
|
UI = 1 << 10
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-27 06:45:21 +01:00
|
|
|
LOW_VOLUME = GENERAL | CONFIG | SCENES | INPUTS | TRANSITIONS | FILTERS | OUTPUTS |
|
2023-07-19 15:11:05 +01:00
|
|
|
SCENEITEMS | MEDIAINPUTS | VENDORS | UI
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-27 06:45:21 +01:00
|
|
|
INPUTVOLUMEMETERS = 1 << 16
|
|
|
|
INPUTACTIVESTATECHANGED = 1 << 17
|
|
|
|
INPUTSHOWSTATECHANGED = 1 << 18
|
|
|
|
SCENEITEMTRANSFORMCHANGED = 1 << 19
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-27 06:45:21 +01:00
|
|
|
HIGH_VOLUME = INPUTVOLUMEMETERS | INPUTACTIVESTATECHANGED | INPUTSHOWSTATECHANGED |
|
2023-07-19 15:11:05 +01:00
|
|
|
SCENEITEMTRANSFORMCHANGED
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-27 06:45:21 +01:00
|
|
|
ALL = LOW_VOLUME | HIGH_VOLUME
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
module Callbacks
|
2023-07-26 18:55:35 +01:00
|
|
|
include Util::String
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2023-07-26 18:55:19 +01:00
|
|
|
def observers
|
|
|
|
@observers ||= []
|
|
|
|
end
|
|
|
|
|
2022-10-22 22:30:40 +01:00
|
|
|
def add_observer(observer)
|
2023-07-28 19:05:28 +01:00
|
|
|
observer = [observer] unless observer.respond_to? :each
|
2023-07-27 14:55:00 +01:00
|
|
|
observer.each { |o| observers << o unless observers.include? o }
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_observer(observer)
|
2023-08-03 14:15:46 +01:00
|
|
|
observer = [observer] unless observer.respond_to? :each
|
|
|
|
observers.reject! { |o| observer.include? o }
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
2023-08-03 14:15:46 +01:00
|
|
|
private def notify_observers(event, data)
|
2023-07-26 18:55:19 +01:00
|
|
|
observers.each do |o|
|
2023-07-27 14:55:00 +01:00
|
|
|
if o.is_a? Method
|
|
|
|
if o.name.to_s == "on_#{snakecase(event)}"
|
|
|
|
data.empty? ? o.call : o.call(data)
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
2023-07-27 14:55:00 +01:00
|
|
|
elsif o.respond_to? "on_#{snakecase(event)}"
|
|
|
|
data.empty? ? o.send("on_#{snakecase(event)}") : o.send("on_#{snakecase(event)}", data)
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-07-27 14:55:00 +01:00
|
|
|
|
2023-08-03 14:15:46 +01:00
|
|
|
alias_method :callbacks, :observers
|
2023-07-27 14:55:00 +01:00
|
|
|
alias_method :register, :add_observer
|
|
|
|
alias_method :deregister, :remove_observer
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class Client
|
2023-07-26 14:37:35 +01:00
|
|
|
include Logging
|
2022-10-22 22:30:40 +01:00
|
|
|
include Callbacks
|
|
|
|
include Mixin::TearDown
|
|
|
|
include Mixin::OPCodes
|
|
|
|
|
|
|
|
def initialize(**kwargs)
|
2022-10-27 06:45:21 +01:00
|
|
|
kwargs[:subs] ||= SUBS::LOW_VOLUME
|
2022-10-22 22:30:40 +01:00
|
|
|
@base_client = Base.new(**kwargs)
|
2023-08-11 22:12:28 +01:00
|
|
|
unless @base_client.identified.state == :identified
|
|
|
|
err_msg = @base_client.identified.error_message
|
|
|
|
logger.error(err_msg)
|
|
|
|
raise OBSWSConnectionError.new(err_msg)
|
|
|
|
end
|
2023-07-26 16:15:43 +01:00
|
|
|
logger.info("#{self} successfully identified with server")
|
2023-08-11 02:22:14 +01:00
|
|
|
rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
|
|
|
|
msg = "#{e.class.name}: #{e.message}"
|
|
|
|
logger.error(msg)
|
|
|
|
raise OBSWSConnectionError.new(msg)
|
|
|
|
else
|
2023-07-26 10:51:38 +01:00
|
|
|
@base_client.updater = ->(op_code, data) {
|
|
|
|
if op_code == Mixin::OPCodes::EVENT
|
2023-07-26 14:37:35 +01:00
|
|
|
logger.debug("received: #{data}")
|
2023-07-26 10:51:38 +01:00
|
|
|
event = data[:eventType]
|
|
|
|
data = data.fetch(:eventData, {})
|
|
|
|
notify_observers(event, Mixin::Data.new(data, data.keys))
|
|
|
|
end
|
|
|
|
}
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
2022-10-25 00:03:43 +01:00
|
|
|
def to_s
|
2023-07-19 15:11:05 +01:00
|
|
|
self.class.name.split("::").last(2).join("::")
|
2022-10-25 00:03:43 +01:00
|
|
|
end
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|