~martijnbraam/pyatem

b3aa00cabb60eb887e4570ce6b50b9db2e7c924b — Martijn Braam a month ago 46d77be master
proxy: add atemswitch commandline tool
2 files changed, 53 insertions(+), 0 deletions(-)

A openswitcher_proxy/cli-switch.py
M openswitcher_proxy/meson.build
A openswitcher_proxy/cli-switch.py => openswitcher_proxy/cli-switch.py +46 -0
@@ 0,0 1,46 @@
#!/usr/bin/env python3
import argparse
import json
import sys

import requests
from requests.auth import HTTPBasicAuth


def switcher(proxy, index, to_source, auto=False, username=None, password=None):
    auth = None
    if username is not None and password is not None:
        auth = HTTPBasicAuth(username, password)

    if to_source.isnumeric():
        to_source = int(to_source)

    if auto:
        r = requests.get(f'{proxy}/program-bus-input', auth=auth)
        try:
            reply = r.json()
            if reply[str(index)]['source'] != to_source:
                data = {'index': index, 'source': to_source}
                requests.post(f'{proxy}/preview-input', data, auth=auth)
                data = {'index': index}
                requests.post(f'{proxy}/auto', data, auth=auth)
            else:
                sys.exit(2)
        except json.decoder.JSONDecodeError:
            sys.exit(1)
    else:
        data = {'index': index, 'source': to_source}
        requests.post(f'{proxy}/program-input', data=data, auth=auth)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--proxy', '-s', required=True)
    parser.add_argument('--to-source', '-t', required=True)
    parser.add_argument('--username', '-u', required=False)
    parser.add_argument('--password', '-p', required=False)
    parser.add_argument('--auto', '-a', required=False, action='store_true')
    parser.add_argument('--index', '-i', type=int, required=False, default=0)
    args = parser.parse_args()

    switcher(args.proxy, args.index, args.to_source, args.auto, args.username, args.password)

M openswitcher_proxy/meson.build => openswitcher_proxy/meson.build +7 -0
@@ 31,3 31,10 @@ proxy_sources = [
]

install_data(proxy_sources, install_dir: moduledir)

install_data(
    'cli-switch.py',
    install_dir: get_option('bindir'),
    install_mode: 'rwxr-xr-x',
    rename: ['atemswitch']
)
\ No newline at end of file