refactor some classes into their own files and fill base __init__.py with imports
This commit is contained in:
@@ -19,33 +19,7 @@ import warnings
|
|||||||
import logging
|
import logging
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
from pyasic.errors import APIError, APIWarning
|
||||||
class APIError(Exception):
|
|
||||||
def __init__(self, *args):
|
|
||||||
if args:
|
|
||||||
self.message = args[0]
|
|
||||||
else:
|
|
||||||
self.message = None
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if self.message:
|
|
||||||
return f"{self.message}"
|
|
||||||
else:
|
|
||||||
return "Incorrect API parameters."
|
|
||||||
|
|
||||||
|
|
||||||
class APIWarning(Warning):
|
|
||||||
def __init__(self, *args):
|
|
||||||
if args:
|
|
||||||
self.message = args[0]
|
|
||||||
else:
|
|
||||||
self.message = None
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if self.message:
|
|
||||||
return f"{self.message}"
|
|
||||||
else:
|
|
||||||
return "Incorrect API parameters."
|
|
||||||
|
|
||||||
|
|
||||||
class BaseMinerAPI:
|
class BaseMinerAPI:
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ from typing import Union
|
|||||||
from passlib.handlers.md5_crypt import md5_crypt
|
from passlib.handlers.md5_crypt import md5_crypt
|
||||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
|
|
||||||
from pyasic.API import BaseMinerAPI, APIError
|
from pyasic.errors import APIError
|
||||||
|
from pyasic.API import BaseMinerAPI
|
||||||
from pyasic.settings import PyasicSettings
|
from pyasic.settings import PyasicSettings
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,3 +11,51 @@
|
|||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
from pyasic.API.bmminer import BMMinerAPI
|
||||||
|
from pyasic.API.bosminer import BOSMinerAPI
|
||||||
|
from pyasic.API.btminer import BTMinerAPI
|
||||||
|
from pyasic.API.cgminer import CGMinerAPI
|
||||||
|
from pyasic.API.unknown import UnknownAPI
|
||||||
|
|
||||||
|
from pyasic.config import MinerConfig
|
||||||
|
|
||||||
|
from pyasic.data import (
|
||||||
|
MinerData,
|
||||||
|
BraiinsOSError,
|
||||||
|
InnosiliconError,
|
||||||
|
WhatsminerError,
|
||||||
|
X19Error,
|
||||||
|
)
|
||||||
|
|
||||||
|
from pyasic.errors import APIError, APIWarning
|
||||||
|
|
||||||
|
from pyasic.miners import get_miner
|
||||||
|
from pyasic.miners.base import AnyMiner
|
||||||
|
from pyasic.miners.miner_factory import MinerFactory
|
||||||
|
from pyasic.miners.miner_listener import MinerListener
|
||||||
|
|
||||||
|
from pyasic.network import MinerNetwork
|
||||||
|
|
||||||
|
from pyasic.settings import PyasicSettings
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"BMMinerAPI",
|
||||||
|
"BOSMinerAPI",
|
||||||
|
"BTMinerAPI",
|
||||||
|
"CGMinerAPI",
|
||||||
|
"UnknownAPI",
|
||||||
|
"MinerConfig",
|
||||||
|
"MinerData",
|
||||||
|
"BraiinsOSError",
|
||||||
|
"InnosiliconError",
|
||||||
|
"WhatsminerError",
|
||||||
|
"X19Error",
|
||||||
|
"APIError",
|
||||||
|
"APIWarning",
|
||||||
|
"get_miner",
|
||||||
|
"AnyMiner",
|
||||||
|
"MinerFactory",
|
||||||
|
"MinerListener",
|
||||||
|
"MinerNetwork",
|
||||||
|
"PyasicSettings",
|
||||||
|
]
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ class MinerConfig:
|
|||||||
temp_mode: Literal["auto", "manual", "disabled"] = "auto"
|
temp_mode: Literal["auto", "manual", "disabled"] = "auto"
|
||||||
temp_target: float = 70.0
|
temp_target: float = 70.0
|
||||||
temp_hot: float = 80.0
|
temp_hot: float = 80.0
|
||||||
temp_dangerous: float = 10.0
|
temp_dangerous: float = 100.0
|
||||||
|
|
||||||
minimum_fans: int = None
|
minimum_fans: int = None
|
||||||
fan_speed: Literal[tuple(range(101))] = None # noqa - Ignore weird Literal usage
|
fan_speed: Literal[tuple(range(101))] = None # noqa - Ignore weird Literal usage
|
||||||
|
|||||||
41
pyasic/errors/__init__.py
Normal file
41
pyasic/errors/__init__.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Copyright 2022 Upstream Data Inc
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
class APIError(Exception):
|
||||||
|
def __init__(self, *args):
|
||||||
|
if args:
|
||||||
|
self.message = args[0]
|
||||||
|
else:
|
||||||
|
self.message = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
if self.message:
|
||||||
|
return f"{self.message}"
|
||||||
|
else:
|
||||||
|
return "Incorrect API parameters."
|
||||||
|
|
||||||
|
|
||||||
|
class APIWarning(Warning):
|
||||||
|
def __init__(self, *args):
|
||||||
|
if args:
|
||||||
|
self.message = args[0]
|
||||||
|
else:
|
||||||
|
self.message = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
if self.message:
|
||||||
|
return f"{self.message}"
|
||||||
|
else:
|
||||||
|
return "Incorrect API parameters."
|
||||||
@@ -22,7 +22,7 @@ import toml
|
|||||||
|
|
||||||
from pyasic.miners.base import BaseMiner
|
from pyasic.miners.base import BaseMiner
|
||||||
from pyasic.API.bosminer import BOSMinerAPI
|
from pyasic.API.bosminer import BOSMinerAPI
|
||||||
from pyasic.API import APIError
|
from pyasic.errors import APIError
|
||||||
|
|
||||||
from pyasic.data.error_codes import BraiinsOSError
|
from pyasic.data.error_codes import BraiinsOSError
|
||||||
from pyasic.data import MinerData
|
from pyasic.data import MinerData
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from typing import Union
|
|||||||
|
|
||||||
from pyasic.API.btminer import BTMinerAPI
|
from pyasic.API.btminer import BTMinerAPI
|
||||||
from pyasic.miners.base import BaseMiner
|
from pyasic.miners.base import BaseMiner
|
||||||
from pyasic.API import APIError
|
from pyasic.errors import APIError
|
||||||
|
|
||||||
from pyasic.data import MinerData
|
from pyasic.data import MinerData
|
||||||
from pyasic.data.error_codes import WhatsminerError
|
from pyasic.data.error_codes import WhatsminerError
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from typing import Union
|
|||||||
|
|
||||||
from pyasic.API.cgminer import CGMinerAPI
|
from pyasic.API.cgminer import CGMinerAPI
|
||||||
from pyasic.miners.base import BaseMiner
|
from pyasic.miners.base import BaseMiner
|
||||||
from pyasic.API import APIError
|
from pyasic.errors import APIError
|
||||||
from pyasic.config import MinerConfig
|
from pyasic.config import MinerConfig
|
||||||
|
|
||||||
from pyasic.data import MinerData
|
from pyasic.data import MinerData
|
||||||
|
|||||||
@@ -13,3 +13,4 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from .S9 import CGMinerS9
|
from .S9 import CGMinerS9
|
||||||
|
from .T9 import CGMinerT9
|
||||||
|
|||||||
@@ -91,18 +91,10 @@ class BaseMiner(ABC):
|
|||||||
async def fault_light_off(self) -> bool:
|
async def fault_light_off(self) -> bool:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# async def send_file(self, src, dest):
|
|
||||||
# async with (await self._get_ssh_connection()) as conn:
|
|
||||||
# await asyncssh.scp(src, (conn, dest))
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def check_light(self) -> bool:
|
async def check_light(self) -> bool:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# @abstractmethod
|
|
||||||
async def get_board_info(self):
|
|
||||||
return None
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def get_config(self) -> MinerConfig:
|
async def get_config(self) -> MinerConfig:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from pyasic.data import MinerData
|
|||||||
from pyasic.data.error_codes import InnosiliconError
|
from pyasic.data.error_codes import InnosiliconError
|
||||||
from pyasic.settings import PyasicSettings
|
from pyasic.settings import PyasicSettings
|
||||||
from pyasic.config import MinerConfig
|
from pyasic.config import MinerConfig
|
||||||
from pyasic.API import APIError
|
from pyasic.errors import APIError
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import warnings
|
import warnings
|
||||||
|
|||||||
@@ -32,7 +32,9 @@ from pyasic.miners._backends.bosminer_old import ( # noqa - Ignore _module impo
|
|||||||
|
|
||||||
from pyasic.miners.unknown import UnknownMiner
|
from pyasic.miners.unknown import UnknownMiner
|
||||||
|
|
||||||
from pyasic.API import APIError
|
from pyasic.errors import APIError
|
||||||
|
|
||||||
|
from pyasic.misc import Singleton
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
@@ -59,6 +61,7 @@ MINER_CLASSES = {
|
|||||||
"Default": BMMinerT9,
|
"Default": BMMinerT9,
|
||||||
"BMMiner": BMMinerT9,
|
"BMMiner": BMMinerT9,
|
||||||
"Hiveon": HiveonT9,
|
"Hiveon": HiveonT9,
|
||||||
|
"CGMiner": CGMinerT9,
|
||||||
},
|
},
|
||||||
"ANTMINER S17": {
|
"ANTMINER S17": {
|
||||||
"Default": BMMinerS17,
|
"Default": BMMinerS17,
|
||||||
@@ -252,15 +255,6 @@ MINER_CLASSES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Singleton(type):
|
|
||||||
_instances = {}
|
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
|
||||||
if cls not in cls._instances:
|
|
||||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
||||||
return cls._instances[cls]
|
|
||||||
|
|
||||||
|
|
||||||
class MinerFactory(metaclass=Singleton):
|
class MinerFactory(metaclass=Singleton):
|
||||||
"""A factory to handle identification and selection of the proper class of miner"""
|
"""A factory to handle identification and selection of the proper class of miner"""
|
||||||
|
|
||||||
@@ -637,9 +631,7 @@ class MinerFactory(metaclass=Singleton):
|
|||||||
else:
|
else:
|
||||||
# make sure the command succeeded
|
# make sure the command succeeded
|
||||||
if data["STATUS"][0]["STATUS"] not in ("S", "I"):
|
if data["STATUS"][0]["STATUS"] not in ("S", "I"):
|
||||||
# this is an error
|
return False, data["STATUS"][0]["Msg"]
|
||||||
if data["STATUS"][0]["STATUS"] not in ("S", "I"):
|
|
||||||
return False, data["STATUS"][0]["Msg"]
|
|
||||||
return True, None
|
return True, None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -14,14 +14,7 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from pyasic.misc import Singleton
|
||||||
class Singleton(type):
|
|
||||||
_instances = {}
|
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
|
||||||
if cls not in cls._instances:
|
|
||||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
||||||
return cls._instances[cls]
|
|
||||||
|
|
||||||
|
|
||||||
class _MinerListener:
|
class _MinerListener:
|
||||||
|
|||||||
22
pyasic/misc/__init__.py
Normal file
22
pyasic/misc/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Copyright 2022 Upstream Data Inc
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
class Singleton(type):
|
||||||
|
_instances = {}
|
||||||
|
|
||||||
|
def __call__(cls, *args, **kwargs):
|
||||||
|
if cls not in cls._instances:
|
||||||
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
||||||
|
return cls._instances[cls]
|
||||||
@@ -37,7 +37,9 @@ class MinerNetwork:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, ip_addr: Union[str, None] = None, mask: Union[str, int, None] = None
|
self,
|
||||||
|
ip_addr: Union[str, List[str], None] = None,
|
||||||
|
mask: Union[str, int, None] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.network = None
|
self.network = None
|
||||||
self.ip_addr = ip_addr
|
self.ip_addr = ip_addr
|
||||||
@@ -63,18 +65,14 @@ class MinerNetwork:
|
|||||||
if self.network:
|
if self.network:
|
||||||
return self.network
|
return self.network
|
||||||
|
|
||||||
|
# if there is no IP address passed, default to 192.168.1.0
|
||||||
|
if not self.ip_addr:
|
||||||
|
self.ip_addr = "192.168.1.0"
|
||||||
if "-" in self.ip_addr:
|
if "-" in self.ip_addr:
|
||||||
self.network = MinerNetworkRange(self.ip_addr)
|
self.network = MinerNetworkRange(self.ip_addr)
|
||||||
elif isinstance(self.ip_addr, list):
|
elif isinstance(self.ip_addr, list):
|
||||||
self.network = MinerNetworkRange(self.ip_addr)
|
self.network = MinerNetworkRange(self.ip_addr)
|
||||||
else:
|
else:
|
||||||
# if there is no IP address passed, default to 192.168.1.0
|
|
||||||
if not self.ip_addr:
|
|
||||||
default_gateway = "192.168.1.0"
|
|
||||||
# if we do have an IP address passed, use that
|
|
||||||
else:
|
|
||||||
default_gateway = self.ip_addr
|
|
||||||
|
|
||||||
# if there is no subnet mask passed, default to /24
|
# if there is no subnet mask passed, default to /24
|
||||||
if not self.mask:
|
if not self.mask:
|
||||||
subnet_mask = "24"
|
subnet_mask = "24"
|
||||||
@@ -84,7 +82,7 @@ class MinerNetwork:
|
|||||||
|
|
||||||
# save the network and return it
|
# save the network and return it
|
||||||
self.network = ipaddress.ip_network(
|
self.network = ipaddress.ip_network(
|
||||||
f"{default_gateway}/{subnet_mask}", strict=False
|
f"{self.ip_addr}/{subnet_mask}", strict=False
|
||||||
)
|
)
|
||||||
|
|
||||||
logging.debug(f"Setting MinerNetwork: {self.network}")
|
logging.debug(f"Setting MinerNetwork: {self.network}")
|
||||||
|
|||||||
@@ -14,14 +14,7 @@
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from pyasic.misc import Singleton
|
||||||
class Singleton(type):
|
|
||||||
_instances = {}
|
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
|
||||||
if cls not in cls._instances:
|
|
||||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
||||||
return cls._instances[cls]
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
Reference in New Issue
Block a user