detector.py 7.85 KB
Newer Older
Wei Shoulin's avatar
c3  
Wei Shoulin committed
1
2
3
import grpc

from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
4
5
from csst_dfs_commons.models.common import from_proto_model_list
from csst_dfs_commons.models.facility import Detector, DetectorStatus
Wei Shoulin's avatar
c3  
Wei Shoulin committed
6
7
from csst_dfs_proto.facility.detector import detector_pb2, detector_pb2_grpc

Wei Shoulin's avatar
Wei Shoulin committed
8
from ..common.service import grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
9
10
11
12
from ..common.utils import *

class DetectorApi(object):
    def __init__(self):
Wei Shoulin's avatar
Wei Shoulin committed
13
14
        self.stub_class = detector_pb2_grpc.DetectorSrvStub
        self.stub = None
Wei Shoulin's avatar
c3  
Wei Shoulin committed
15

Wei Shoulin's avatar
Wei Shoulin committed
16
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    def find(self, **kwargs):
        ''' retrieve detector records from database

        parameter kwargs:
            module_id: [str]
            key: [str]

        return: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.Find.with_call(detector_pb2.FindDetectorReq(
                module_id = get_parameter(kwargs, "module_id"),
                key = get_parameter(kwargs, "key")
            ),metadata = get_auth_headers())

            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
33
                return Result.ok_data(data=from_proto_model_list(Detector,resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
34
35
36
37
            else:
                return Result.error(message = str(resp.error.detail))

        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
38
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
39

Wei Shoulin's avatar
Wei Shoulin committed
40
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    def get(self, **kwargs):
        '''  fetch a record from database

        parameter kwargs:
            no : [str] 

        return csst_dfs_common.models.Result
        '''
        try:
            no = get_parameter(kwargs, "no")
            resp, _ =  self.stub.Get.with_call(detector_pb2.GetDetectorReq(
                no = no
            ),metadata = get_auth_headers())

            if not resp.record.no:
                return Result.error(message=f"no:{no} not found")  

Wei Shoulin's avatar
Wei Shoulin committed
58
            return Result.ok_data(data=Detector().from_proto_model(resp.record))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
59
60
           
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
61
            return Result.error(message="%s:%s" % (e.code().value, e.details()))   
Wei Shoulin's avatar
c3  
Wei Shoulin committed
62

Wei Shoulin's avatar
Wei Shoulin committed
63
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    def update(self, **kwargs):
        ''' update a detector by no

        parameter kwargs:
            no : [str],
            detector_name : [str],
            module_id : [str],
            filter_id : [str]

        return csst_dfs_common.models.Result
        '''
        try:
            no = get_parameter(kwargs, "no")
            result_get = self.get(no=no)
            if not result_get.success:
                return result_get

            record = detector_pb2.Detector(
                no = no,
                detector_name = get_parameter(kwargs, "detector_name", result_get.data.detector_name),
                module_id = get_parameter(kwargs, "module_id", result_get.data.module_id),
                filter_id = get_parameter(kwargs, "filter_id", result_get.data.filter_id)
            )            
            resp,_ = self.stub.Update.with_call(
                detector_pb2.UpdateDetectorReq(record=record),
                metadata = get_auth_headers()
            )
            if resp.success:
                return Result.ok_data()
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
96
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
97

Wei Shoulin's avatar
Wei Shoulin committed
98
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    def delete(self, **kwargs):
        ''' delete a detector by no

        parameter kwargs:
            no : [str]

        return csst_dfs_common.models.Result
        '''
        no = get_parameter(kwargs, "no")

        try:
            resp,_ = self.stub.Delete.with_call(
                detector_pb2.DeleteDetectorReq(no=no),
                metadata = get_auth_headers()
            )
            if resp.success:
                return Result.ok_data()
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
119
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
120

Wei Shoulin's avatar
Wei Shoulin committed
121
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
122
123
    def write(self, **kwargs):
        ''' insert a detector record into database
Wei Shoulin's avatar
Wei Shoulin committed
124

Wei Shoulin's avatar
c3  
Wei Shoulin committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        parameter kwargs:
            no : [str],
            detector_name : [str],
            module_id : [str],
            filter_id : [str]
        return csst_dfs_common.models.Result
        '''   

        rec = detector_pb2.Detector(
            no = get_parameter(kwargs, "no"),
            detector_name = get_parameter(kwargs, "detector_name"),
            module_id = get_parameter(kwargs, "module_id"),
            filter_id = get_parameter(kwargs, "filter_id")
        )
        req = detector_pb2.WriteDetectorReq(record = rec)
        try:
            resp,_ = self.stub.Write.with_call(req, metadata = get_auth_headers())
            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
143
                return Result.ok_data(data=Detector().from_proto_model(resp.record))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
144
145
146
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
147
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
148

Wei Shoulin's avatar
Wei Shoulin committed
149
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    def find_status(self, **kwargs):
        ''' retrieve a detector status's from database

        parameter kwargs:
            detector_no: [str]
            status_occur_time: (begin,end)
            limit: limits returns the number of records,default 0:no-limit

        return: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.FindStatus.with_call(detector_pb2.FindStatusReq(
                detector_no = get_parameter(kwargs, "detector_no"),
                status_begin_time = get_parameter(kwargs, "status_occur_time", [None, None])[0],
                status_end_time = get_parameter(kwargs, "status_occur_time", [None, None])[1],                
                limit = get_parameter(kwargs, "limit", 0)
            ),metadata = get_auth_headers())

            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
169
                return Result.ok_data(data=from_proto_model_list(DetectorStatus,resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
170
171
172
173
            else:
                return Result.error(message = str(resp.error.detail))

        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
174
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
175

Wei Shoulin's avatar
Wei Shoulin committed
176
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    def get_status(self, **kwargs):
        '''  fetch a record from database

        parameter kwargs:
            id : [int] 

        return csst_dfs_common.models.Result
        '''
        try:
            id = get_parameter(kwargs, "id")
            resp, _ =  self.stub.GetStatus.with_call(detector_pb2.GetStatusReq(
                id = id
            ),metadata = get_auth_headers())

Wei Shoulin's avatar
Wei Shoulin committed
191
            if resp.record == 0:
Wei Shoulin's avatar
c3  
Wei Shoulin committed
192
193
                return Result.error(message=f"id:{id} not found")  

Wei Shoulin's avatar
Wei Shoulin committed
194
            return Result.ok_data(data=DetectorStatus().from_proto_model(resp.record))
Wei Shoulin's avatar
Wei Shoulin committed
195

Wei Shoulin's avatar
c3  
Wei Shoulin committed
196
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
197
            return Result.error(message="%s:%s" % (e.code().value, e.details()))   
Wei Shoulin's avatar
c3  
Wei Shoulin committed
198
    
Wei Shoulin's avatar
Wei Shoulin committed
199
    @grpc_channel
Wei Shoulin's avatar
c3  
Wei Shoulin committed
200
201
    def write_status(self, **kwargs):
        ''' insert a detector status into database
Wei Shoulin's avatar
Wei Shoulin committed
202

Wei Shoulin's avatar
c3  
Wei Shoulin committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        parameter kwargs:
            detector_no : [str],
            status : [str],
            status_time : [str]
        return csst_dfs_common.models.Result
        '''   

        rec = detector_pb2.DetectorStatus(
            id = 0,
            detector_no = get_parameter(kwargs, "detector_no"),
            status = get_parameter(kwargs, "status"),
            status_time = get_parameter(kwargs, "status_time")
        )
        req = detector_pb2.WriteStatusReq(record = rec)
        try:
            resp,_ = self.stub.WriteStatus.with_call(req, metadata = get_auth_headers())
            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
220
                return Result.ok_data(data=DetectorStatus().from_proto_model(resp.record))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
221
222
223
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
224
            return Result.error(message="%s:%s" % (e.code().value, e.details()))