Skip to content

Commit

Permalink
Extend typing and code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vwout committed Aug 9, 2024
1 parent e5c72f3 commit 0d4ac30
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 43 deletions.
42 changes: 33 additions & 9 deletions libvisca.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ function Visca.set_log_function(func)
end

--- @class PayloadCommand object
--- @field category integer The Visca command category
--- @field command integer The Visca command
--- @field arguments table The command data bytes
Visca.PayloadCommand = {}
Visca.PayloadCommand.__index = Visca.PayloadCommand

Expand Down Expand Up @@ -366,6 +369,9 @@ function Visca.PayloadCommand:as_string()
end

--- @class PayloadReply object
--- @field error_type integer The reply error type - if any
--- @field arguments table The command reply data bytes
--- @field argument_cnt integer The nummer of reply data bytes
Visca.PayloadReply = {}
Visca.PayloadReply.__index = Visca.PayloadReply

Expand Down Expand Up @@ -535,6 +541,10 @@ function Visca.PayloadReply:as_string()
end

--- @class Message Visca Message object
--- @field payload_size number The lenght in bytes of the payload in the message
--- @field seq_nr number Sequential incrementing number of the message
--- @field payload table The raw data in the massage
--- @field message table Structure containing the decoded response object, either a PayloadCommand or PayloadReply
Visca.Message = {}
Visca.Message.__index = Visca.Message

Expand Down Expand Up @@ -607,7 +617,7 @@ end

--- Generate a data bytestring of this message
---
--- @param mode ViscaModes
--- @param mode ViscaModes|integer
--- @return string
function Visca.Message:to_data(mode)
mode = mode or Visca.modes.generic
Expand Down Expand Up @@ -641,7 +651,7 @@ end

--- Generate a human readable representation in hex of this message
---
--- @param mode ViscaModes
--- @param mode ViscaModes|integer
function Visca.Message:as_string(mode)
mode = mode or Visca.modes.generic
local bin_str = self:to_data(mode)
Expand Down Expand Up @@ -716,8 +726,12 @@ Visca.ReplyServer = {

function Visca.ReplyServer.add_listener_for(address, port)
local cnt = (Visca.ReplyServer.clients[port] or 0)
--- @type string|table|nil
local err

if cnt == 0 then
local sock_address, err, num = socket.find_first_address("*", port,
local sock_address, num
sock_address, err, num = socket.find_first_address("*", port,
{family="inet", socket_type="dgram", protocol="udp"})
if sock_address then
local server
Expand Down Expand Up @@ -811,6 +825,8 @@ end

--- @class Transmission The Transmission object tracks responses received on a send message.
--- It stores the response by type (ack, error or completion) and tracks timeout.
--- @field send Message The message send
--- @field send_timestamp number|nil The timestamp in nanseconds at which the message was send, or nil
Visca.Transmission = {}
Visca.Transmission.__index = Visca.Transmission

Expand Down Expand Up @@ -874,13 +890,19 @@ function Visca.Transmission:inquiry_data()
end

--- @class Connection Connection to a Visca camera
--- @field private sock_address unknown The socket address structure of the destination
--- @field public sock_err string The last error obtained from the socket or address detection
--- @field private address string The IP address or DNS of the camera
--- @field private transmission_queue Transmission[] List of Transmission objects
--- @field private callbacks table List of callbacks: [type][id] = function
--- @field private compatibility table List of compatibility settings (key/value)
Visca.Connection = {}
Visca.Connection.__index = Visca.Connection

--- Visca Connection constructor
---
--- @param address string The IP address or DNS of the camera
--- @param port number The Visca control port of the camera
--- @param port integer|nil The Visca control port of the camera
--- @return Connection
function Visca.Connection.new(address, port)
port = port or Visca.default_port
Expand Down Expand Up @@ -913,7 +935,7 @@ function Visca.Connection.new(address, port)
return connection
end

--- @param mode ViscaModes
--- @param mode ViscaModes|integer
function Visca.Connection:set_mode(mode)
if Visca.modes:has_value(mode or Visca.modes.generic) then
self.mode = mode
Expand Down Expand Up @@ -1036,16 +1058,17 @@ function Visca.Connection:__transmissions_process()
local transmit_size = 0
local transmit_data

local remove_transmission = false
for i,t in pairs(self.transmission_queue) do
if t:timed_out() then
self:__exec_callback('timeout', t)
t = nil
remove_transmission = true
elseif t.error or t.completion then
-- Message transaction completed, remove from queue
t = nil
remove_transmission = true
end

if not t then
if remove_transmission or not t then
self.transmission_queue[i] = nil
end
end
Expand Down Expand Up @@ -1601,8 +1624,9 @@ end
--- Connect to a Visca capable camera
---
--- @param address string The IP address or DNS of the camera
--- @param port number The Visca control port of the camera
--- @param port integer|nil The Visca control port of the camera
--- @return Connection
--- @overload fun(addres: string, port: integer|nil): nil,string
function Visca.connect(address, port)
---@type Connection
local connection = Visca.Connection.new(address, port)
Expand Down
2 changes: 1 addition & 1 deletion obs-visca-control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ local function source_signal_processor(source_settings, source_name, signal)
-- In case the stop action applies to a different camera than the scene for which the activate signal is
-- send, the stop action execution is postponed until the scene deactivation, to speed up transition.
for program_scene_name, program_scene_active in pairs(plugin_data.program_scene) do
if program_scene_active and current_program_scene_name ~= program_scene_name then
if program_scene_active and (current_program_scene_name ~= program_scene_name) then
for _, program_source_name, program_source_settings, program_source_is_visible in
get_plugin_settings_from_scene(program_scene_name, camera_id) do
if program_source_settings ~= nil and program_source_is_visible then
Expand Down
13 changes: 10 additions & 3 deletions test/libvisca_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,26 @@ end

module("libvisca_test", lunit.testcase, package.seeall)

--- @type: Visca.Connection
--- @type Connection
local connection

function setup()
--- @diagnostic disable-next-line: cast-local-type
connection = Visca.connect('127.1.2.101')
lunit.assert_true(connection:set_mode(Visca.modes.generic))
lunit.assert_not_nil(connection)
if connection ~= nil then
lunit.assert_true(connection:set_mode(Visca.modes.generic))
end
end

function teardown()
--connection:close()
end

function test_visca_mode()
lunit.assert_equal(0, Visca.modes.generic)
end

function test_set_preset_4()
local set_preset_4 = Visca.Message.new():from_data("\x01\x00\x00\x07\x00\x00\x00\x01\x81\x01\x04\x3f\x02\x03\xff"):dump("set preset 4")
lunit.assert_equal(Visca.payload_types.visca_command, set_preset_4.payload_type, "invalid payload")
Expand Down Expand Up @@ -130,7 +138,6 @@ function test_cam_reset_recall_8_ptzoptics()
end

function test_cam_reset_recall_6_jvc()
connection:set_compatibility(nil)
local _, data = connection:Cam_Preset_Recall(6)
local recv_msg = Visca.Message.new():from_data(data):dump("Cam_Preset_Recall 6 normal")
lunit.assert_not_nil(recv_msg.message.command)
Expand Down
Loading

0 comments on commit 0d4ac30

Please sign in to comment.