mirror of
https://github.com/onyx-and-iris/vban-cmd-python.git
synced 2024-11-15 17:10:46 +00:00
add _keepupdated
added _keepupdated for public packet, now running in worker2 thread. edit readme to reflect change
This commit is contained in:
parent
b933397b6c
commit
2d857a4cb1
@ -18,7 +18,7 @@ git clone https://github.com/onyx-and-iris/vban-cmd-python
|
|||||||
cd vban-cmd-python
|
cd vban-cmd-python
|
||||||
```
|
```
|
||||||
|
|
||||||
Just the wrapper:
|
Just the interface:
|
||||||
```
|
```
|
||||||
pip install .
|
pip install .
|
||||||
```
|
```
|
||||||
@ -42,11 +42,7 @@ only applies to `sendtext`:
|
|||||||
|
|
||||||
#### Use with a context manager:
|
#### Use with a context manager:
|
||||||
It is advised to use this code with a context manager.
|
It is advised to use this code with a context manager.
|
||||||
Parameter coverage is not as extensive for the RT Packet Service as with the Remote API so defined below are the
|
Parameter coverage is not as extensive for the RT Packet Service as with the Remote API.
|
||||||
classes/methods available through this interface.
|
|
||||||
|
|
||||||
Ideally, calling a background thread to poll for new RT packets ensures up-to-date parameters (see examples gui).
|
|
||||||
For simple scripts you may refer to the example below.
|
|
||||||
|
|
||||||
### Example 1
|
### Example 1
|
||||||
```python
|
```python
|
||||||
|
@ -25,6 +25,8 @@ class VbanCmd(abc.ABC):
|
|||||||
self._bps = kwargs['bps']
|
self._bps = kwargs['bps']
|
||||||
self._channel = kwargs['channel']
|
self._channel = kwargs['channel']
|
||||||
self._delay = kwargs['delay']
|
self._delay = kwargs['delay']
|
||||||
|
self._ratelimiter = kwargs['ratelimiter']
|
||||||
|
self._sync = kwargs['sync']
|
||||||
self._bps_opts = \
|
self._bps_opts = \
|
||||||
[0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250,
|
[0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250,
|
||||||
38400, 57600, 115200, 128000, 230400, 250000, 256000, 460800,921600,
|
38400, 57600, 115200, 128000, 230400, 250000, 256000, 460800,921600,
|
||||||
@ -49,17 +51,21 @@ class VbanCmd(abc.ABC):
|
|||||||
is_error = []
|
is_error = []
|
||||||
self.ready_to_read, self.ready_to_write, in_error = select.select(is_readable, is_writable, is_error, 60)
|
self.ready_to_read, self.ready_to_write, in_error = select.select(is_readable, is_writable, is_error, 60)
|
||||||
self._public_packet = None
|
self._public_packet = None
|
||||||
|
self.running = True
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._rt_packet_socket.bind((socket.gethostbyname(socket.gethostname()), self._port))
|
self._rt_packet_socket.bind((socket.gethostbyname(socket.gethostname()), self._port))
|
||||||
worker = Thread(target=self._send_register_rt, daemon=True)
|
worker = Thread(target=self._send_register_rt, daemon=True)
|
||||||
worker.start()
|
worker.start()
|
||||||
self._public_packet = self._get_rt()
|
self._public_packet = self._get_rt()
|
||||||
|
if self._sync:
|
||||||
|
worker2 = Thread(target=self._keepupdated, daemon=True)
|
||||||
|
worker2.start()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _send_register_rt(self):
|
def _send_register_rt(self):
|
||||||
if self._rt_register_socket in self.ready_to_write:
|
if self._rt_register_socket in self.ready_to_write:
|
||||||
while True:
|
while self.running:
|
||||||
self._rt_register_socket.sendto(
|
self._rt_register_socket.sendto(
|
||||||
self._register_rt_header.header + bytes(1), (socket.gethostbyname(self._ip), self._port)
|
self._register_rt_header.header + bytes(1), (socket.gethostbyname(self._ip), self._port)
|
||||||
)
|
)
|
||||||
@ -101,11 +107,17 @@ class VbanCmd(abc.ABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def public_packet(self):
|
def public_packet(self):
|
||||||
return self._public_packet or self._get_rt()
|
return self._public_packet
|
||||||
@public_packet.setter
|
@public_packet.setter
|
||||||
def public_packet(self, val):
|
def public_packet(self, val):
|
||||||
self._public_packet = val
|
self._public_packet = val
|
||||||
|
|
||||||
|
def _keepupdated(self):
|
||||||
|
while self.running:
|
||||||
|
packet = self._get_rt()
|
||||||
|
if not packet.__eq__(self.public_packet):
|
||||||
|
self.public_packet = packet
|
||||||
|
|
||||||
def _get_rt(self):
|
def _get_rt(self):
|
||||||
def fget():
|
def fget():
|
||||||
data = False
|
data = False
|
||||||
@ -122,6 +134,7 @@ class VbanCmd(abc.ABC):
|
|||||||
)
|
)
|
||||||
count = int.from_bytes(self._text_header.framecounter, 'little') + 1
|
count = int.from_bytes(self._text_header.framecounter, 'little') + 1
|
||||||
self._text_header.framecounter = count.to_bytes(4, 'little')
|
self._text_header.framecounter = count.to_bytes(4, 'little')
|
||||||
|
sleep(self._ratelimiter)
|
||||||
|
|
||||||
def sendtext(self, cmd):
|
def sendtext(self, cmd):
|
||||||
self.set_rt(cmd)
|
self.set_rt(cmd)
|
||||||
@ -149,6 +162,8 @@ class VbanCmd(abc.ABC):
|
|||||||
self.set_rt('Command', 'Restart', 1)
|
self.set_rt('Command', 'Restart', 1)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
self.running = False
|
||||||
|
sleep(0.2)
|
||||||
self._rt_register_socket.close()
|
self._rt_register_socket.close()
|
||||||
self._sendrequest_string_socket.close()
|
self._sendrequest_string_socket.close()
|
||||||
self._rt_packet_socket.close()
|
self._rt_packet_socket.close()
|
||||||
@ -167,7 +182,7 @@ def _make_remote(kind: NamedTuple) -> VbanCmd:
|
|||||||
def init(self, **kwargs):
|
def init(self, **kwargs):
|
||||||
defaultkwargs = {
|
defaultkwargs = {
|
||||||
'ip': None, 'port': 6990, 'streamname': 'Command1', 'bps': 0,
|
'ip': None, 'port': 6990, 'streamname': 'Command1', 'bps': 0,
|
||||||
'channel': 0, 'delay': 0.001
|
'channel': 0, 'delay': 0.001, 'ratelimiter': 0.035, 'sync': True
|
||||||
}
|
}
|
||||||
kwargs = defaultkwargs | kwargs
|
kwargs = defaultkwargs | kwargs
|
||||||
VbanCmd.__init__(self, **kwargs)
|
VbanCmd.__init__(self, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user