[Update] Controller: Validate and handle IP address setting in setOscIpAddress method.
[Update] Utils: Implement isValidIpAddress function to check IP address validity.
This commit is contained in:
@@ -6,7 +6,7 @@ import re
|
|||||||
from device_manager import device_manager
|
from device_manager import device_manager
|
||||||
from config import config
|
from config import config
|
||||||
from model import model
|
from model import model
|
||||||
from utils import removeLog, printLog, errorLogging, isConnectedNetwork
|
from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
@@ -1085,9 +1085,28 @@ class Controller:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setOscIpAddress(data, *args, **kwargs) -> dict:
|
def setOscIpAddress(data, *args, **kwargs) -> dict:
|
||||||
config.OSC_IP_ADDRESS = data
|
if isValidIpAddress(data) is False:
|
||||||
model.setOscIpAddress(config.OSC_IP_ADDRESS)
|
return {
|
||||||
return {"status":200, "result":config.OSC_IP_ADDRESS}
|
"status":400,
|
||||||
|
"result":{
|
||||||
|
"message":"Invalid IP address",
|
||||||
|
"data": config.OSC_IP_ADDRESS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
model.setOscIpAddress(data)
|
||||||
|
config.OSC_IP_ADDRESS = data
|
||||||
|
return {"status":200, "result":config.OSC_IP_ADDRESS}
|
||||||
|
except Exception:
|
||||||
|
model.setOscIpAddress(config.OSC_IP_ADDRESS)
|
||||||
|
return {
|
||||||
|
"status":400,
|
||||||
|
"result":{
|
||||||
|
"message":"Cannot set IP address",
|
||||||
|
"data": config.OSC_IP_ADDRESS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getOscPort(*args, **kwargs) -> dict:
|
def getOscPort(*args, **kwargs) -> dict:
|
||||||
|
|||||||
@@ -7,14 +7,22 @@ from logging.handlers import RotatingFileHandler
|
|||||||
|
|
||||||
from ctranslate2 import get_supported_compute_types
|
from ctranslate2 import get_supported_compute_types
|
||||||
import requests
|
import requests
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
def isConnectedNetwork(url="http://www.google.com", timeout=3):
|
def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
|
||||||
try:
|
try:
|
||||||
response = requests.get(url, timeout=timeout)
|
response = requests.get(url, timeout=timeout)
|
||||||
return response.status_code == 200
|
return response.status_code == 200
|
||||||
except requests.RequestException:
|
except requests.RequestException:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def isValidIpAddress(ip_address: str) -> bool:
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(ip_address)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
def getBestComputeType(device, device_index) -> str:
|
def getBestComputeType(device, device_index) -> str:
|
||||||
compute_types = get_supported_compute_types(device, device_index)
|
compute_types = get_supported_compute_types(device, device_index)
|
||||||
compute_types = set(compute_types)
|
compute_types = set(compute_types)
|
||||||
|
|||||||
Reference in New Issue
Block a user