11import logging
22from collections .abc import Callable
3- from queue import Queue
43from threading import Event as ThreadEvent
54from threading import Thread
65
76from isar .config .settings import settings
8- from isar .models .events import (
9- EventConflictError ,
10- Events ,
11- EventTimeoutError ,
12- RobotServiceEvents ,
13- StateMachineEvents ,
14- )
7+ from isar .models .events import Event , EventConflictError , Events , EventTimeoutError
158from isar .robot .function_thread import FunctionThread
9+ from isar .storage .uploader import Uploader
1610from robot_interface .models .exceptions .robot_exceptions import (
1711 RobotException ,
1812 RobotRetrieveInspectionException ,
2115from robot_interface .models .mission .mission import Mission
2216from robot_interface .models .mission .task import InspectionTask
2317from robot_interface .robot_interface import RobotInterface
24- from robot_interface .telemetry .mqtt_client import MqttClientInterface
2518
2619
27- def robot_upload_inspection (
28- robot : RobotInterface ,
20+ def fetch_and_upload_inspection (
21+ get_inspection_function : Callable [[ InspectionTask ], Inspection ] ,
2922 logger : logging .Logger ,
23+ upload_function : Callable [[Inspection , Mission ], None ],
3024 task : InspectionTask ,
3125 mission : Mission ,
32- upload_queue : Queue ,
3326) -> None :
3427 try :
35- inspection : Inspection = robot . get_inspection ( task = task )
28+ inspection : Inspection = get_inspection_function ( task )
3629 if task .id != inspection .id :
3730 logger .warning (
3831 f"The id of task ({ task .id } ) "
@@ -45,33 +38,30 @@ def robot_upload_inspection(
4538 return
4639
4740 if not inspection :
48- logger .warning (
49- f"No inspection result data retrieved for task { str (task .id )[:8 ]} "
50- )
41+ logger .error (f"No inspection result data retrieved for task { str (task .id )[:8 ]} " )
42+ return
5143
5244 inspection .metadata .tag_id = task .tag_id
5345 inspection .metadata .analysis_types = task .analysis_types
5446
55- message : tuple [Inspection , Mission ] = (
56- inspection ,
57- mission ,
58- )
59- upload_queue .put (message )
60- logger .info (f"Inspection result: { str (inspection .id )[:8 ]} queued for upload" )
47+ upload_function (inspection , mission )
6148
6249
6350class RobotInspectionService :
6451 def __init__ (
6552 self ,
6653 events : Events ,
6754 robot : RobotInterface ,
68- mqtt_publisher : MqttClientInterface ,
55+ uploader : Uploader ,
6956 ) -> None :
7057 self .logger = logging .getLogger ("uploader" )
71- self .state_machine_events : StateMachineEvents = events .state_machine_events
72- self .robot_service_events : RobotServiceEvents = events .robot_service_events
73- self .mqtt_publisher : MqttClientInterface = mqtt_publisher
74- self .upload_queue : Queue = events .upload_queue
58+ self .upload_task_event : Event [tuple [InspectionTask , Mission ]] = (
59+ events .robot_service_events .request_inspection_upload
60+ )
61+ self .upload_inspection_event : Event [tuple [Inspection , Mission ]] = (
62+ events .upload_event
63+ )
64+ self .uploader : Uploader = uploader
7565 self .robot : RobotInterface = robot
7666 self .upload_inspection_threads : list [FunctionThread ] = []
7767 self .signal_exit : ThreadEvent = ThreadEvent ()
@@ -107,7 +97,7 @@ def _restart_inspection_thread_if_stopped(self) -> None:
10797
10898 def register_and_monitor_inspection_callback (
10999 self ,
110- callback_function : Callable ,
100+ callback_function : Callable [[ Inspection , Mission ], None ] ,
111101 ) -> None :
112102 self .inspection_callback_function = callback_function
113103
@@ -121,18 +111,33 @@ def register_and_monitor_inspection_callback(
121111 def run (self ) -> None :
122112 try :
123113 while not self .signal_exit .wait (0 ):
124- upload_request : tuple [(InspectionTask , Mission )] | None = (
125- self .robot_service_events .request_inspection_upload .consume_event ()
114+
115+ upload_task_request : tuple [(InspectionTask , Mission )] | None = (
116+ self .upload_task_event .consume_event ()
126117 )
127- if upload_request is not None :
118+
119+ if upload_task_request is not None :
128120 self .upload_inspection_threads .append (
129121 FunctionThread (
130- robot_upload_inspection ,
131- self .robot ,
122+ fetch_and_upload_inspection ,
123+ self .robot . get_inspection ,
132124 self .logger ,
133- upload_request [0 ],
134- upload_request [1 ],
135- self .upload_queue ,
125+ self .uploader .upload_inspection ,
126+ upload_task_request [0 ],
127+ upload_task_request [1 ],
128+ )
129+ )
130+
131+ upload_inspection_request : tuple [Inspection , Mission ] | None = (
132+ self .upload_inspection_event .consume_event ()
133+ )
134+
135+ if upload_inspection_request is not None :
136+ self .upload_inspection_threads .append (
137+ FunctionThread (
138+ self .uploader .upload_inspection ,
139+ upload_inspection_request [0 ],
140+ upload_inspection_request [1 ],
136141 )
137142 )
138143
0 commit comments