Skip to content

Commit

Permalink
Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappydinoa committed Jul 18, 2018
1 parent e43e789 commit 07f8a7b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
25 changes: 14 additions & 11 deletions itachip2ir/device.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""device"""
from contextlib import closing
import socket
import ipaddress
import socket
from contextlib import closing

from .exception import iTachException


class iTach(object):
Expand Down Expand Up @@ -61,33 +63,34 @@ def format_command(self, command):
def format_command_name(self, command_name):
"""format command in commands for sending"""
command = self.commands[command_name]
return self.format_command(command)
return self.format_command(command).encode()

def send_command(self, command_name, byte_size=4096, timeout=3):
"""send command from commands"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.ip_address, self.port))
sock.settimeout(timeout)
sock.sendall(self.format_command_name(command_name))
msg = sock.recv(byte_size)
sock.close()
return msg
except socket.error as error:
print(repr(error))
return error
return iTachException(error)
finally:
sock.close()

def send_commands(self, command_name, repeats, byte_size=4096, timeout=3):
"""send command multiple times from command from commands"""
for x in range(repeats):
self.send_command(command_name, byte_size, timeout)
response = self.send_command(command_name, byte_size, timeout)
return response


if __name__ == "__main__":
blueray_commands = {
"toggle_power": "test"
"toggle_power": "get_NET"
}
itach = iTach(ip_address="localhost", port=4998)
itach = iTach(ip_address="192.168.1.111", port=4998)
blueray = itach.add(VirtualDevice(
name="blueray", commands=blueray_commands))
print(blueray.send_command("toggle_power"))
57 changes: 57 additions & 0 deletions itachip2ir/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class iTachException(Exception):
def __init__(self, response):
if response.startswith("ERR_01"):
message = "Invalid command. Command not found."
elif response.startswith("ERR_02"):
message = "Invalid module address (does not exist)."
elif response.startswith("ERR_03"):
message = "Invalid connector address (does not exist)."
elif response.startswith("ERR_04"):
message = "Invalid ID value."
elif response.startswith("ERR_05"):
message = "Invalid frequency value"
elif response.startswith("ERR_06"):
message = "Invalid repeat value."
elif response.startswith("ERR_07"):
message = "Invalid offset value."
elif response.startswith("ERR_08"):
message = "Invalid pulse count."
elif response.startswith("ERR_09"):
message = "Invalid pulse data."
elif response.startswith("ERR_10"):
message = "Uneven amount of <on|off> statements."
elif response.startswith("ERR_11"):
message = "No carriage return found."
elif response.startswith("ERR_12"):
message = "Repeat count exceeded."
elif response.startswith("ERR_13"):
message = "IR command sent to input connector."
elif response.startswith("ERR_14"):
message = "Blaster command sent to non-blaster connector."
elif response.startswith("ERR_15"):
message = "No carriage return before buffer full."
elif response.startswith("ERR_16"):
message = "No carriage return."
elif response.startswith("ERR_17"):
message = "Bad command syntax."
elif response.startswith("ERR_18"):
message = "Sensor command sent to non-input connector."
elif response.startswith("ERR_19"):
message = "Repeated IR transmission failure."
elif response.startswith("ERR_20"):
message = "Above designated IR <on|off> pair limit."
elif response.startswith("ERR_21"):
message = "Symbol odd boundary."
elif response.startswith("ERR_22"):
message = "Undefined symbol."
elif response.startswith("ERR_23"):
message = "Unknown option."
elif response.startswith("ERR_24"):
message = "Invalid baud rate setting."
elif response.startswith("ERR_25"):
message = "Invalid flow control setting."
elif response.startswith("ERR_26"):
message = "Invalid parity setting."
elif response.startswith("ERR_27"):
message = "Settings are locked."
message = response

0 comments on commit 07f8a7b

Please sign in to comment.