Skip to content

Commit

Permalink
devices
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappydinoa committed Jul 19, 2018
1 parent 728aeda commit 234b14d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 45 deletions.
29 changes: 16 additions & 13 deletions itachip2ir/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""device"""
"""device.py"""
import socket
from contextlib import closing

Expand All @@ -24,15 +24,15 @@ def _check_ip(self):
sock.settimeout(3)
return sock.connect_ex((self.ipaddress, self.port)) == 0

def add(self, *devices):
def add(self, *args):
"""adds device to devices"""
for device in devices:
for device in args:
device.ipaddress = self.ipaddress
device.port = self.port
self.devices[device.name] = device
if len(devices) > 1:
return devices
return devices[0]
if len(args) > 1:
return args
return args[0]

def send_command(self, device_name, command_name):
"""sends command to device"""
Expand All @@ -49,26 +49,29 @@ def __init__(self, name="", commands={}):
"""init method"""
self.name = name
self.commands = commands
self.generate_methods()
self.generate_functions()

def __repr__(self):
"""repr method"""
return "VirtualDevice(name=%s, commands=%s)" % (self.name, self.commands)

def generate_methods(self):
def generate_functions(self):
"""dynamically generate functions"""
for command_name in self.commands.keys():
def fn():
def func():
"""generated fuction"""
return self.send_command(command_name)
setattr(self, command_name, fn)
fn.__name__ = command_name
setattr(self, command_name, func)
func.__name__ = command_name

def format_message(self, msg):
"""format message"""
if isinstance(msg, bytes):
return msg.decode()
return msg

def format_command(self, command):
"""format command for sending"""
"""format command"""
if not command.endswith("\r"):
return command + "\r"
return command
Expand All @@ -93,7 +96,7 @@ def send_command(self, command_name, byte_size=4096, timeout=3):

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):
for _command in range(repeats):
response = self.send_command(command_name, byte_size, timeout)
return response

Expand Down
76 changes: 45 additions & 31 deletions itachip2ir/exception.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
"""exception.py"""


class iTachException(Exception):
"""iTachException Exception"""

def __init__(self, response):
if not isinstance(response, str):
pass
elif response.startswith("ERR_01"):
message = "Invalid command. Command not found."
"""init method"""
message = self.error_handler(response)
super(iTachException, self).__init__(message)
self.message = message

def error_handler(self, response):
"""error handler"""
if response.startswith("ERR_01"):
response = self.invalid_error("command. Command not found.")
elif response.startswith("ERR_02"):
message = "Invalid module address (does not exist)."
response = self.invalid_error("module address (does not exist).")
elif response.startswith("ERR_03"):
message = "Invalid connector address (does not exist)."
response = self.invalid_error("connector address (does not exist).")
elif response.startswith("ERR_04"):
message = "Invalid ID value."
response = self.invalid_error("ID value.")
elif response.startswith("ERR_05"):
message = "Invalid frequency value"
response = self.invalid_error("frequency value")
elif response.startswith("ERR_06"):
message = "Invalid repeat value."
response = self.invalid_error("repeat value.")
elif response.startswith("ERR_07"):
message = "Invalid offset value."
response = self.invalid_error("offset value.")
elif response.startswith("ERR_08"):
message = "Invalid pulse count."
response = self.invalid_error("pulse count.")
elif response.startswith("ERR_09"):
message = "Invalid pulse data."
response = self.invalid_error("pulse data.")
elif response.startswith("ERR_10"):
message = "Uneven amount of <on|off> statements."
response = "Uneven amount of <on|off> statements."
elif response.startswith("ERR_11"):
message = "No carriage return found."
response = "No carriage return found."
elif response.startswith("ERR_12"):
message = "Repeat count exceeded."
response = "Repeat count exceeded."
elif response.startswith("ERR_13"):
message = "IR command sent to input connector."
response = "IR command sent to input connector."
elif response.startswith("ERR_14"):
message = "Blaster command sent to non-blaster connector."
response = "Blaster command sent to non-blaster connector."
elif response.startswith("ERR_15"):
message = "No carriage return before buffer full."
response = "No carriage return before buffer full."
elif response.startswith("ERR_16"):
message = "No carriage return."
response = "No carriage return."
elif response.startswith("ERR_17"):
message = "Bad command syntax."
response = "Bad command syntax."
elif response.startswith("ERR_18"):
message = "Sensor command sent to non-input connector."
response = "Sensor command sent to non-input connector."
elif response.startswith("ERR_19"):
message = "Repeated IR transmission failure."
response = "Repeated IR transmission failure."
elif response.startswith("ERR_20"):
message = "Above designated IR <on|off> pair limit."
response = "Above designated IR <on|off> pair limit."
elif response.startswith("ERR_21"):
message = "Symbol odd boundary."
response = "Symbol odd boundary."
elif response.startswith("ERR_22"):
message = "Undefined symbol."
response = "Undefined symbol."
elif response.startswith("ERR_23"):
message = "Unknown option."
response = "Unknown option."
elif response.startswith("ERR_24"):
message = "Invalid baud rate setting."
response = self.invalid_error("baud rate setting.")
elif response.startswith("ERR_25"):
message = "Invalid flow control setting."
response = self.invalid_error("flow control setting.")
elif response.startswith("ERR_26"):
message = "Invalid parity setting."
response = self.invalid_error("parity setting.")
elif response.startswith("ERR_27"):
message = "Settings are locked."
message = response
response = "Settings are locked."
return response

def invalid_error(self, message):
"""invalid error"""
return "Invalid " + message
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='itachip2ir',
version='1.3.7',
version='1.3.8',
description='A small Python module for interacting with the Global Cache iTach WF2IR or IP2IR',
long_description=long_description,
url='https://github.com/thehappydinoa/itachip2ir',
Expand Down
17 changes: 17 additions & 0 deletions tests/test_itach.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def test_device(self):
assert device.name == name
assert device.commands == commands

def test_devices(self):
name1 = "device1"
name2 = "device2"
commands = {"test_command": "test_ir"}
itach = iTach(ipaddress="192.168.1.111", port=4998)
device1 = VirtualDevice(
name=name1, commands=commands)
device2 = VirtualDevice(
name=name2, commands=commands)
devices = itach.add(device1, device2)
assert itach.devices[device1.name] == device1
assert itach.devices[device2.name] == device2
assert device1.name == name1
assert device2.name == name2
assert device1.commands == commands
assert device2.commands == commands

def test_exception(self):
with pytest.raises(iTachException):
raise iTachException("ERR_01")
Expand Down

0 comments on commit 234b14d

Please sign in to comment.