feat: em增加采集数据方法
This commit is contained in:
parent
1dbb07ddcb
commit
1925e3e5d6
|
@ -0,0 +1,19 @@
|
|||
import socket
|
||||
from rest_framework.exceptions import ParseError
|
||||
|
||||
def get_tyy_data(*args):
|
||||
sc = socket.socket()
|
||||
try:
|
||||
sc.connect((args[0], int(args[1])))
|
||||
except Exception:
|
||||
raise ParseError("无法连接到采集器")
|
||||
sc.send(b"R")
|
||||
resp = sc.recv(1024)
|
||||
if len(resp) < 8:
|
||||
raise ParseError("设备未启动")
|
||||
json_data = resp[5:-4]
|
||||
json_str = json_data.decode('utf-8')
|
||||
return "str", json_str
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_tyy_data())
|
|
@ -72,3 +72,7 @@ class EInspectSerializer(CustomModelSerializer):
|
|||
model = EInspect
|
||||
fields = "__all__"
|
||||
read_only_fields = EXCLUDE_FIELDS
|
||||
|
||||
|
||||
class CdSerializer(serializers.Serializer):
|
||||
method = serializers.CharField(label="方法名")
|
|
@ -1,6 +1,6 @@
|
|||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from apps.em.views import EquipmentViewSet, EcheckRecordViewSet, EInspectViewSet, EcateViewSet
|
||||
from apps.em.views import EquipmentViewSet, EcheckRecordViewSet, EInspectViewSet, EcateViewSet, CdView
|
||||
|
||||
API_BASE_URL = 'api/em/'
|
||||
HTML_BASE_URL = 'em/'
|
||||
|
@ -12,4 +12,5 @@ router.register('einspect', EInspectViewSet, basename='einspect')
|
|||
router.register('ecate', EcateViewSet, basename='ecate')
|
||||
urlpatterns = [
|
||||
path(API_BASE_URL, include(router.urls)),
|
||||
path(API_BASE_URL + 'cd/', CdView.as_view()),
|
||||
]
|
||||
|
|
|
@ -4,7 +4,7 @@ from drf_yasg import openapi
|
|||
from django.utils import timezone
|
||||
from apps.em.models import Equipment, EcheckRecord, EInspect, Ecate
|
||||
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
|
||||
from apps.em.serializers import EquipmentSerializer, EcheckRecordSerializer, EInspectSerializer, EcateSerializer
|
||||
from apps.em.serializers import EquipmentSerializer, EcheckRecordSerializer, EInspectSerializer, EcateSerializer, CdSerializer
|
||||
from apps.em.filters import EquipFilterSet
|
||||
from rest_framework.exceptions import ParseError
|
||||
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin
|
||||
|
@ -17,6 +17,9 @@ from rest_framework.response import Response
|
|||
from django.conf import settings
|
||||
from django.db.models import Count, Case, When, IntegerField
|
||||
from apps.enp.services import get_last_envdata
|
||||
from rest_framework.views import APIView
|
||||
from apps.utils.mixins import MyLoggingMixin
|
||||
import importlib
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
@ -167,3 +170,20 @@ class EInspectViewSet(CustomModelViewSet):
|
|||
serializer_class = EInspectSerializer
|
||||
select_related_fields = ["equipment", "inspect_user"]
|
||||
filterset_fields = ["equipment"]
|
||||
|
||||
|
||||
class CdView(MyLoggingMixin, APIView):
|
||||
|
||||
@swagger_auto_schema(request_body=CdSerializer)
|
||||
def post(self, request, format=None):
|
||||
"""执行采集数据方法
|
||||
|
||||
执行采集数据方法
|
||||
"""
|
||||
method = request.data.get("method")
|
||||
m = method.split("(")[0]
|
||||
args = method.split("(")[1].split(")")[0].split(",")
|
||||
module, func = m.rsplit(".", 1)
|
||||
m = importlib.import_module(module)
|
||||
f = getattr(m, func)
|
||||
return Response(f(*args))
|
Loading…
Reference in New Issue