fix pre-commit findings

This commit is contained in:
Robert Resch 2022-05-17 22:18:56 +02:00
parent e81ed17aa3
commit 50d31344c8
11 changed files with 205 additions and 188 deletions

View file

@ -2,7 +2,7 @@
import asyncio import asyncio
import json import json
import ssl import ssl
from typing import Any, MutableMapping, Union from typing import Any, MutableMapping
from cachetools import TTLCache from cachetools import TTLCache
from gmqtt import Client, Subscription from gmqtt import Client, Subscription
@ -60,7 +60,7 @@ class HelperBot:
data_decoded = str(payload.decode()) data_decoded = str(payload.decode())
if topic_split[10] in self._commands: if topic_split[10] in self._commands:
self._commands[topic_split[10]].add_response(data_decoded) self._commands[topic_split[10]].add_response(data_decoded)
except Exception: except Exception: # pylint: disable=broad-except
_LOGGER.error( _LOGGER.error(
"An exception occurred during handling message.", exc_info=True "An exception occurred during handling message.", exc_info=True
) )

View file

@ -1,3 +1,4 @@
"""Web image module."""
import logging import logging
import os import os

View file

@ -31,6 +31,7 @@ def _add_routes(
if not module.__name__.startswith(plugin_module_name): if not module.__name__.startswith(plugin_module_name):
return return
assert module.__file__ is not None
if module.__file__.endswith("__init__.py"): if module.__file__.endswith("__init__.py"):
sub_app = web.Application() sub_app = web.Application()
else: else:
@ -53,6 +54,7 @@ def _add_routes(
def _import_plugins(module: ModuleType) -> None: def _import_plugins(module: ModuleType) -> None:
assert module.__file__ is not None
for file in glob(join(dirname(module.__file__), "**/*.py"), recursive=True): for file in glob(join(dirname(module.__file__), "**/*.py"), recursive=True):
if not isfile(file) or file == module.__file__: if not isfile(file) or file == module.__file__:
continue continue
@ -77,6 +79,7 @@ def add_plugins(app: web.Application) -> None:
def get_success_response(data: Any) -> Response: def get_success_response(data: Any) -> Response:
"""Get success response with provided data."""
body = { body = {
"code": RETURN_API_SUCCESS, "code": RETURN_API_SUCCESS,
"data": data, "data": data,

View file

@ -33,14 +33,12 @@ async def _handle_dim_devmanager(request: Request) -> Response:
if bot["company"] == "eco-ng" and bot["mqtt_connection"]: if bot["company"] == "eco-ng" and bot["mqtt_connection"]:
retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid) retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid)
body = retcmd body = retcmd
logging.debug(f"Send Bot - {json_body}") logging.debug("Send Bot - %s", json_body)
logging.debug(f"Bot Response - {body}") logging.debug("Bot Response - %s", body)
return web.json_response(body) return web.json_response(body)
# No response, send error back # No response, send error back
logging.error( logging.error("No bots with DID: %s connected to MQTT", json_body["toId"])
"No bots with DID: {} connected to MQTT".format(json_body["toId"])
)
body = {"id": randomid, "errno": ERR_COMMON, "ret": "fail"} body = {"id": randomid, "errno": ERR_COMMON, "ret": "fail"}
return web.json_response(body) return web.json_response(body)

View file

@ -31,35 +31,32 @@ async def _handle_devmanager_bot_command(request: Request) -> Response:
if bot["company"] == "eco-ng": if bot["company"] == "eco-ng":
retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid) retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid)
body = retcmd body = retcmd
logging.debug(f"Send Bot - {json_body}") logging.debug("Send Bot - %s", json_body)
logging.debug(f"Bot Response - {body}") logging.debug("Bot Response - %s", body)
return web.json_response(body)
else:
# No response, send error back
logging.error(
"No bots with DID: {} connected to MQTT".format(json_body["toId"])
)
body = {
"id": randomid,
"errno": 500,
"ret": "fail",
"debug": "wait for response timed out",
}
return web.json_response(body) return web.json_response(body)
else: # No response, send error back
if "td" in json_body: # Seen when doing initial wifi config logging.error("No bots with DID: %s connected to MQTT", json_body["toId"])
if json_body["td"] == "PollSCResult": body = {
body = {"ret": "ok"} "id": randomid,
return web.json_response(body) "errno": 500,
"ret": "fail",
"debug": "wait for response timed out",
}
return web.json_response(body)
if json_body["td"] == "HasUnreadMsg": # EcoVacs Home if "td" in json_body: # Seen when doing initial wifi config
body = {"ret": "ok", "unRead": False} if json_body["td"] == "PollSCResult":
return web.json_response(body) body = {"ret": "ok"}
return web.json_response(body)
if json_body["td"] == "PreWifiConfig": # EcoVacs Home if json_body["td"] == "HasUnreadMsg": # EcoVacs Home
body = {"ret": "ok"} body = {"ret": "ok", "unRead": False}
return web.json_response(body) return web.json_response(body)
if json_body["td"] == "PreWifiConfig": # EcoVacs Home
body = {"ret": "ok"}
return web.json_response(body)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
logging.error("Unexpected exception occurred", exc_info=True) logging.error("Unexpected exception occurred", exc_info=True)

View file

@ -29,23 +29,23 @@ async def _handle_lg_log(request: Request) -> Response:
botdetails = bot_get(did) botdetails = bot_get(did)
if botdetails: if botdetails:
if not "cmdName" in json_body: if "cmdName" not in json_body:
if "td" in json_body: if "td" in json_body:
json_body["cmdName"] = json_body["td"] json_body["cmdName"] = json_body["td"]
if not "toId" in json_body: if "toId" not in json_body:
json_body["toId"] = did json_body["toId"] = did
if not "toType" in json_body: if "toType" not in json_body:
json_body["toType"] = botdetails["class"] json_body["toType"] = botdetails["class"]
if not "toRes" in json_body: if "toRes" not in json_body:
json_body["toRes"] = botdetails["resource"] json_body["toRes"] = botdetails["resource"]
if not "payloadType" in json_body: if "payloadType" not in json_body:
json_body["payloadType"] = "x" json_body["payloadType"] = "x"
if not "payload" in json_body: if "payload" not in json_body:
# json_body["payload"] = "" # json_body["payload"] = ""
if json_body["td"] == "GetCleanLogs": if json_body["td"] == "GetCleanLogs":
json_body["td"] = "q" json_body["td"] = "q"
@ -56,17 +56,17 @@ async def _handle_lg_log(request: Request) -> Response:
if bot["company"] == "eco-ng": if bot["company"] == "eco-ng":
retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid) retcmd = await bumper.mqtt_helperbot.send_command(json_body, randomid)
body = retcmd body = retcmd
logging.debug(f"Send Bot - {json_body}") logging.debug("Send Bot - %s", json_body)
logging.debug(f"Bot Response - {body}") logging.debug("Bot Response - %s", body)
logs = [] logs = []
logsroot = ET.fromstring(retcmd["resp"]) logsroot = ET.fromstring(retcmd["resp"])
if logsroot.attrib["ret"] == "ok": if logsroot.attrib["ret"] == "ok":
for l in logsroot: for log_line in logsroot:
cleanlog = { cleanlog = {
"ts": l.attrib["s"], "ts": log_line.attrib["s"],
"area": l.attrib["a"], "area": log_line.attrib["a"],
"last": l.attrib["l"], "last": log_line.attrib["l"],
"cleanType": l.attrib["t"], "cleanType": log_line.attrib["t"],
# imageUrl allows for providing images of cleanings, something to look into later # imageUrl allows for providing images of cleanings, something to look into later
# "imageUrl": "https://localhost:8007", # "imageUrl": "https://localhost:8007",
} }
@ -78,15 +78,13 @@ async def _handle_lg_log(request: Request) -> Response:
else: else:
body = {"ret": "ok", "logs": []} body = {"ret": "ok", "logs": []}
logging.debug(f"lg logs return: {json.dumps(body)}") logging.debug("lg logs return: %s", json.dumps(body))
return web.json_response(body) return web.json_response(body)
else:
# No response, send error back # No response, send error back
logging.error( logging.error("No bots with DID: %s connected to MQTT", json_body["toId"])
"No bots with DID: {} connected to MQTT".format(json_body["toId"]) except Exception: # pylint: disable=broad-except
) logging.error("An unknown exception occurred", exc_info=True)
except Exception as e:
logging.exception(f"{e}")
body = {"id": randomid, "errno": ERR_COMMON, "ret": "fail"} body = {"id": randomid, "errno": ERR_COMMON, "ret": "fail"}
return web.json_response(body) return web.json_response(body)

View file

@ -16,6 +16,7 @@ async def _handle_neng_has_unread_message(_: Request) -> Response:
async def handle_neng_get_share_msgs(_: Request) -> Response: async def handle_neng_get_share_msgs(_: Request) -> Response:
"""Return shared messages."""
# EcoVacs Home # EcoVacs Home
body = {"code": 0, "data": {"hasNext": False, "msgs": []}} body = {"code": 0, "data": {"hasNext": False, "msgs": []}}
@ -46,6 +47,7 @@ async def handle_neng_get_share_msgs(_: Request) -> Response:
async def handle_neng_get_list(_: Request) -> Response: async def handle_neng_get_list(_: Request) -> Response:
"""Get messages."""
# EcoVacs Home # EcoVacs Home
body = {"code": 0, "data": {"hasNext": False, "msgs": []}} body = {"code": 0, "data": {"hasNext": False, "msgs": []}}

View file

@ -4,6 +4,9 @@ import logging
from typing import Iterable from typing import Iterable
from aiohttp import web from aiohttp import web
from aiohttp.web_exceptions import HTTPInternalServerError
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aiohttp.web_routedef import AbstractRouteDef from aiohttp.web_routedef import AbstractRouteDef
from bumper.models import RETURN_API_SUCCESS, EcoVacsHomeProducts from bumper.models import RETURN_API_SUCCESS, EcoVacsHomeProducts
@ -22,7 +25,7 @@ class PimPlugin(WebserverPlugin):
web.route( web.route(
"*", "*",
"/pim/product/getProductIotMap", "/pim/product/getProductIotMap",
self.handle_getProductIotMap, _handle_get_product_iot_map,
), ),
web.route( web.route(
"*", "*",
@ -32,83 +35,93 @@ class PimPlugin(WebserverPlugin):
web.route( web.route(
"*", "*",
"/pim/product/getConfignetAll", "/pim/product/getConfignetAll",
self.handle_getConfignetAll, _handle_get_confignet_all,
), ),
web.route( web.route(
"*", "*",
"/pim/product/getConfigGroups", "/pim/product/getConfigGroups",
self.handle_getConfigGroups, _handle_get_config_groups,
), ),
web.route( web.route(
"*", "*",
"/pim/dictionary/getErrDetail", "/pim/dictionary/getErrDetail",
self.handle_getErrDetail, _handle_get_err_detail,
), ),
web.route( web.route(
"*", "*",
"/pim/product/software/config/batch", "/pim/product/software/config/batch",
self.handle_product_config_batch, _handle_product_config_batch,
), ),
] ]
async def handle_getProductIotMap(self, request):
try:
body = {
"code": RETURN_API_SUCCESS,
"data": EcoVacsHomeProducts,
}
return web.json_response(body)
except Exception as e: async def _handle_get_product_iot_map(_: Request) -> Response:
logging.exception(f"{e}") """Get product iot map."""
try:
body = {
"code": RETURN_API_SUCCESS,
"data": EcoVacsHomeProducts,
}
return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error("An exception occurred during handling request.", exc_info=True)
raise HTTPInternalServerError
async def handle_getConfignetAll(self, request):
try:
body = confignetAllResponse
return web.json_response(body)
except Exception as e: async def _handle_get_confignet_all(_: Request) -> Response:
logging.exception(f"{e}") """Get config net all."""
try:
body = confignetAllResponse
return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error("An exception occurred during handling request.", exc_info=True)
raise HTTPInternalServerError
async def handle_getConfigGroups(self, request):
try:
body = configGroupsResponse
return web.json_response(body)
except Exception as e: async def _handle_get_config_groups(_: Request) -> Response:
logging.exception(f"{e}") """Get config groups."""
try:
body = configGroupsResponse
return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error("An exception occurred during handling request.", exc_info=True)
raise HTTPInternalServerError
async def handle_getErrDetail(self, request):
try:
body = {
"code": -1,
"data": [],
"msg": "This errcode's detail is not exists",
}
return web.json_response(body)
except Exception as e: async def _handle_get_err_detail(_: Request) -> Response:
logging.exception(f"{e}") """Get error details."""
try:
body = {
"code": -1,
"data": [],
"msg": "This errcode's detail is not exists",
}
return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error("An exception occurred during handling request.", exc_info=True)
raise HTTPInternalServerError
async def handle_product_config_batch(self, request):
try:
json_body = json.loads(await request.text())
data = []
for pid in json_body["pids"]:
for productConfig in productConfigBatch:
if pid == productConfig["pid"]:
data.append(productConfig)
continue
# not found in productConfigBatch async def _handle_product_config_batch(request: Request) -> Response:
# some devices don't have any product configuration """Handle product config batch."""
data.append({"cfg": {}, "pid": pid}) try:
json_body = json.loads(await request.text())
data = []
for pid in json_body["pids"]:
for product_config in productConfigBatch:
if pid == product_config["pid"]:
data.append(product_config)
continue
body = {"code": 200, "data": data, "message": "success"} # not found in productConfigBatch
return web.json_response(body) # some devices don't have any product configuration
data.append({"cfg": {}, "pid": pid})
except Exception as e: body = {"code": 200, "data": data, "message": "success"}
logging.exception(f"{e}") return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error("An exception occurred during handling request.", exc_info=True)
raise HTTPInternalServerError
confignetAllResponse = { confignetAllResponse = {

View file

@ -1,9 +1,11 @@
"""Users plugin module.""" """Users plugin module."""
import json import json
import logging import logging
from typing import Iterable from typing import Any, Iterable, Mapping
from aiohttp import web from aiohttp import web
from aiohttp.abc import Request
from aiohttp.web_response import Response
from aiohttp.web_routedef import AbstractRouteDef from aiohttp.web_routedef import AbstractRouteDef
from bumper import bumper_announce_ip from bumper import bumper_announce_ip
@ -22,98 +24,99 @@ class UsersPlugin(WebserverPlugin):
web.route( web.route(
"*", "*",
"/users/user.do", "/users/user.do",
self._handle_usersapi, _handle_usersapi,
), ),
] ]
async def _handle_usersapi(self, request):
if not request.method == "GET": # Skip GET for now
try:
body = {} async def _handle_usersapi(request: Request) -> Response:
postbody = {} if not request.method == "GET": # Skip GET for now
if request.content_type == "application/x-www-form-urlencoded": try:
postbody = await request.post()
else: body = {}
postbody = json.loads(await request.text()) postbody: Mapping[str, Any]
if request.content_type == "application/x-www-form-urlencoded":
postbody = await request.post()
else:
postbody = json.loads(await request.text())
todo = postbody["todo"] todo = postbody["todo"]
if todo == "FindBest": if todo == "FindBest":
service = postbody["service"] service = postbody["service"]
if service == "EcoMsgNew": if service == "EcoMsgNew":
srvip = bumper_announce_ip srvip = bumper_announce_ip
srvport = 5223 srvport = 5223
logging.info( logging.info(
"Announcing EcoMsgNew Server to bot as: {}:{}".format( "Announcing EcoMsgNew Server to bot as: %s:%d",
srvip, srvport srvip,
) srvport,
) )
msgserver = {"ip": srvip, "port": srvport, "result": "ok"} msgserver = json.dumps(
msgserver = json.dumps(msgserver) {"ip": srvip, "port": srvport, "result": "ok"}
msgserver = msgserver.replace( )
" ", "" # bot seems to be very picky about having no spaces, only way was with text
) # bot seems to be very picky about having no spaces, only way was with text msgserver = msgserver.replace(" ", "")
return web.json_response(text=msgserver) return web.json_response(text=msgserver)
elif service == "EcoUpdate": if service == "EcoUpdate":
srvip = "47.88.66.164" # EcoVacs Server srvip = "47.88.66.164" # EcoVacs Server
srvport = 8005 srvport = 8005
logging.info( logging.info(
"Announcing EcoUpdate Server to bot as: {}:{}".format( "Announcing EcoMsgNew Server to bot as: %s:%d",
srvip, srvport srvip,
) srvport,
) )
body = {"result": "ok", "ip": srvip, "port": srvport} body = {"result": "ok", "ip": srvip, "port": srvport}
elif todo == "loginByItToken": elif todo == "loginByItToken":
if "userId" in postbody: if "userId" in postbody:
if check_authcode(postbody["userId"], postbody["token"]): if check_authcode(postbody["userId"], postbody["token"]):
body = { body = {
"resource": postbody["resource"], "resource": postbody["resource"],
"result": "ok", "result": "ok",
"todo": "result", "todo": "result",
"token": postbody["token"], "token": postbody["token"],
"userId": postbody["userId"], "userId": postbody["userId"],
} }
else: # EcoVacs Home LoginByITToken else: # EcoVacs Home LoginByITToken
loginToken = loginByItToken(postbody["token"]) login_token = loginByItToken(postbody["token"])
if not loginToken == {}: if not login_token:
body = { body = {
"resource": postbody["resource"], "resource": postbody["resource"],
"result": "ok", "result": "ok",
"todo": "result", "todo": "result",
"token": loginToken["token"], "token": login_token["token"],
"userId": loginToken["userid"], "userId": login_token["userid"],
} }
else: else:
body = {"result": "fail", "todo": "result"} body = {"result": "fail", "todo": "result"}
elif todo == "GetDeviceList": elif todo == "GetDeviceList":
body = { body = {
"devices": db_get().table("bots").all(), "devices": db_get().table("bots").all(),
"result": "ok", "result": "ok",
"todo": "result", "todo": "result",
} }
elif todo == "SetDeviceNick": elif todo == "SetDeviceNick":
bot_set_nick(postbody["did"], postbody["nick"]) bot_set_nick(postbody["did"], postbody["nick"])
body = {"result": "ok", "todo": "result"} body = {"result": "ok", "todo": "result"}
elif todo == "AddOneDevice": elif todo == "AddOneDevice":
bot_set_nick(postbody["did"], postbody["nick"]) bot_set_nick(postbody["did"], postbody["nick"])
body = {"result": "ok", "todo": "result"} body = {"result": "ok", "todo": "result"}
elif todo == "DeleteOneDevice": elif todo == "DeleteOneDevice":
bot_remove(postbody["did"]) bot_remove(postbody["did"])
body = {"result": "ok", "todo": "result"} body = {"result": "ok", "todo": "result"}
return web.json_response(body) return web.json_response(body)
except Exception: # pylint: disable=broad-except
logging.error(
"An exception occurred during handling request.", exc_info=True
)
except Exception as e: # Return fail for GET
logging.exception(f"{e}") body = {"result": "fail", "todo": "result"}
return web.json_response(body)
# Return fail for GET
body = {"result": "fail", "todo": "result"}
return web.json_response(body)

View file

@ -29,14 +29,15 @@ class CampaignPlugin(WebserverPlugin):
async def _handle_home_page_alert(_: Request) -> Response: async def _handle_home_page_alert(_: Request) -> Response:
nextAlert = convert_to_millis((datetime.now() + timedelta(hours=12)).timestamp())
return get_success_response( return get_success_response(
{ {
"clickSchemeUrl": None, "clickSchemeUrl": None,
"clickWebUrl": None, "clickWebUrl": None,
"hasCampaign": "N", "hasCampaign": "N",
"imageUrl": None, "imageUrl": None,
"nextAlertTime": nextAlert, "nextAlertTime": convert_to_millis(
(datetime.now() + timedelta(hours=12)).timestamp()
),
"serverTime": get_current_time_as_millis(), "serverTime": get_current_time_as_millis(),
} }
) )

View file

@ -1,3 +1,4 @@
"""Message plugin module."""
from typing import Iterable from typing import Iterable
from aiohttp import web from aiohttp import web