move productIotMap into own file

This commit is contained in:
Robert Resch 2022-05-17 23:14:14 +02:00
parent 9b85d45407
commit f09cacda9e
5 changed files with 1756 additions and 1744 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,21 +1,23 @@
"""Appsvr plugin module.""" """Appsvr plugin module."""
import copy
import json import json
import logging import logging
from typing import Iterable from typing import Any, Iterable
from aiohttp import web from aiohttp import web
from aiohttp.web_exceptions import HTTPInternalServerError from aiohttp.web_exceptions import HTTPInternalServerError
from aiohttp.web_request import Request from aiohttp.web_request import Request
from aiohttp.web_response import Response from aiohttp.web_response import Response
from aiohttp.web_routedef import AbstractRouteDef from aiohttp.web_routedef import AbstractRouteDef
from amqtt.session import Session
import bumper
from bumper.db import db_get, token_by_authcode, user_add_oauth from bumper.db import db_get, token_by_authcode, user_add_oauth
from bumper.models import include_EcoVacsHomeProducts_info
from .. import WebserverPlugin from .. import WebserverPlugin
from .pim import get_product_iot_map
# pylint: disable=no-self-use
class AppsvrPlugin(WebserverPlugin): class AppsvrPlugin(WebserverPlugin):
"""Appsvr plugin.""" """Appsvr plugin."""
@ -26,21 +28,22 @@ class AppsvrPlugin(WebserverPlugin):
web.route( web.route(
"*", "*",
"/appsvr/app.do", "/appsvr/app.do",
self._handle_appsvr_app, _handle_appsvr_app,
), ),
web.route( web.route(
"*", "*",
"/appsvr/service/list", "/appsvr/service/list",
self._handle_appsvr_service_list, _handle_appsvr_service_list,
), ),
web.route( web.route(
"*", "*",
"/appsvr/oauth_callback", "/appsvr/oauth_callback",
self._handle_appsvr_oauth_callback, _handle_appsvr_oauth_callback,
), ),
] ]
async def _handle_appsvr_app(self, request: Request) -> Response:
async def _handle_appsvr_app(request: Request) -> Response:
if request.method == "GET": if request.method == "GET":
# Skip GET for now # Skip GET for now
return web.json_response({"result": "fail", "todo": "result"}) return web.json_response({"result": "fail", "todo": "result"})
@ -58,7 +61,7 @@ class AppsvrPlugin(WebserverPlugin):
devices = [] devices = []
for bot in bots: for bot in bots:
if bot["class"] != "": if bot["class"] != "":
device = include_EcoVacsHomeProducts_info(bot) device = _include_product_iot_map_info(bot)
# Happens if the bot isn't on the EcoVacs Home list # Happens if the bot isn't on the EcoVacs Home list
if device is not None: if device is not None:
devices.append(device) devices.append(device)
@ -76,7 +79,49 @@ class AppsvrPlugin(WebserverPlugin):
raise HTTPInternalServerError raise HTTPInternalServerError
async def _handle_appsvr_service_list(self, _: Request) -> Response:
def _include_product_iot_map_info(bot: dict[str, Any]) -> dict[str, Any]:
result = copy.deepcopy(bot)
for botprod in get_product_iot_map():
if botprod["classid"] == result["class"]:
result["UILogicId"] = botprod["product"]["UILogicId"]
result["ota"] = botprod["product"]["ota"]
result["icon"] = botprod["product"]["iconUrl"]
result["model"] = botprod["product"]["model"]
result["pip"] = botprod["product"]["_id"]
result["deviceName"] = botprod["product"]["name"]
result["materialNo"] = botprod["product"]["materialNo"]
result["product_category"] = (
"DEEBOT"
if botprod["product"]["name"].startswith("DEEBOT")
else "UNKNOWN"
)
# bot["updateInfo"] = {
# "changeLog": "",
# "needUpdate": False
# }
# bot["service"] = {
# "jmq": "jmq-ngiot-eu.dc.ww.ecouser.net",
# "mqs": "api-ngiot.dc-as.ww.ecouser.net"
# }
result["status"] = (
1 if bot["mqtt_connection"] or bot["xmpp_connection"] else 0
)
# mqtt_connection is not always set correctly, therefore workaround until fixed properly
session: Session
for (session, _) in bumper.mqtt_server.broker._sessions.values():
did = session.client_id.split("@")[0]
if did == bot["did"] and session.transitions.state == "connected":
result["status"] = 1
break
return result
async def _handle_appsvr_service_list(_: Request) -> Response:
try: try:
# original urls comment out as they are sub sub domain, which the current certificate is not valid # original urls comment out as they are sub sub domain, which the current certificate is not valid
# using url, where the certs is valid # using url, where the certs is valid
@ -108,7 +153,8 @@ class AppsvrPlugin(WebserverPlugin):
raise HTTPInternalServerError raise HTTPInternalServerError
async def _handle_appsvr_oauth_callback(self, request: Request) -> Response:
async def _handle_appsvr_oauth_callback(request: Request) -> Response:
try: try:
token = token_by_authcode(request.query["code"]) token = token_by_authcode(request.query["code"])
oauth = user_add_oauth(token["userid"]) oauth = user_add_oauth(token["userid"])

View file

@ -1 +1,13 @@
"""Api pim module plugin.""" """Api pim module plugin."""
import json
import os
from typing import Any
def get_product_iot_map() -> tuple[Any]:
"""Get product iot map."""
with open(
os.path.join(os.path.dirname(__file__), "productIotMap.json"),
encoding="utf-8",
) as file:
return (json.load(file),)

View file

@ -10,9 +10,11 @@ from aiohttp.web_request import Request
from aiohttp.web_response import Response 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
from bumper.web.plugins import WebserverPlugin from bumper.web.plugins import WebserverPlugin
from . import get_product_iot_map
class ProductPlugin(WebserverPlugin): class ProductPlugin(WebserverPlugin):
"""Product plugin.""" """Product plugin."""
@ -49,7 +51,7 @@ async def _handle_get_product_iot_map(_: Request) -> Response:
try: try:
body = { body = {
"code": RETURN_API_SUCCESS, "code": RETURN_API_SUCCESS,
"data": EcoVacsHomeProducts, "data": get_product_iot_map(),
} }
return web.json_response(body) return web.json_response(body)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except

File diff suppressed because it is too large Load diff