Last active
October 16, 2020 13:11
-
-
Save fthiery/e96649285811650f60c4ca74c4a6a158 to your computer and use it in GitHub Desktop.
Script to start/stop recording on Extron SMP devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import sys | |
import telnetlib | |
class SMP351: | |
commands = ['get_state', 'start_record', 'stop_record'] | |
def __init__(self, args): | |
self.args = args | |
self.connect() | |
self.login() | |
self.run_command() | |
def run_command(self): | |
cmd_name = f'cmd_{args.command}' | |
command = getattr(self, cmd_name) | |
print(f'Calling command {cmd_name}') | |
command() | |
def cmd_start_record(self): | |
print('Start recording') | |
self.send_sis('Y1RCDR', 'RcdrY1') | |
print('Recording started') | |
def cmd_stop_record(self): | |
print('Stop recording') | |
self.send_sis('Y0RCDR', 'RcdrY0') | |
print('Recording stopped') | |
def connect(self): | |
self.host = self.args.device_ip | |
self.conn = telnetlib.Telnet(self.host) | |
def login(self): | |
self.wait_for('Password:') | |
print(f'Connected to {self.host}') | |
self.write(self.args.password, 'Login Administrator') | |
print('Logged in') | |
def wait_for(self, text): | |
out = str(self.conn.read_until(text.encode('ascii'), timeout=2)) | |
if text not in out: | |
print(f'Expected text {text} not found in output {out}, exiting') | |
sys.exit(1) | |
def send_sis(self, text, expect): | |
string = 'W{0!s}\r'.format(text) | |
self.write(string, expect) | |
def write(self, text, expect=None): | |
to_send = text + '\r' | |
print(f'Sending {to_send}') | |
self.conn.write(to_send.encode('ascii')) | |
if expect is not None: | |
self.wait_for(expect) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument( | |
'command', | |
type=str, | |
nargs='?', | |
help='run a command among: %s' % ", ".join(SMP351.commands) | |
) | |
parser.add_argument( | |
'-d', | |
'--device-ip', | |
type=str, | |
default='192.168.43.157', | |
help='Device IP', | |
) | |
parser.add_argument( | |
'-p', | |
'--password', | |
type=str, | |
help='Password (the default password is the device S/N)', | |
required=True, | |
) | |
args = parser.parse_args() | |
e = SMP351(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment