16
16
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
18
18
use anyhow:: Result ;
19
+ use async_std:: future:: pending;
19
20
use futures:: { select, FutureExt } ;
21
+ use log:: { error, info} ;
20
22
21
23
mod adc;
22
24
mod backlight;
@@ -50,17 +52,11 @@ use regulators::Regulators;
50
52
use setup_mode:: SetupMode ;
51
53
use system:: System ;
52
54
use temperatures:: Temperatures ;
53
- use ui:: { setup_display, Ui , UiResources } ;
55
+ use ui:: { message , setup_display, Display , Ui , UiResources } ;
54
56
use usb_hub:: UsbHub ;
55
57
use watchdog:: Watchdog ;
56
58
57
- #[ async_std:: main]
58
- async fn main ( ) -> Result < ( ) > {
59
- env_logger:: init ( ) ;
60
-
61
- // Show a splash screen very early on
62
- let display = setup_display ( ) ;
63
-
59
+ async fn init ( ) -> Result < ( Ui , HttpServer , Option < Watchdog > ) > {
64
60
// The BrokerBuilder collects topics that should be exported via the
65
61
// MQTT/REST APIs.
66
62
// The topics are also used to pass around data inside the tacd.
@@ -122,9 +118,6 @@ async fn main() -> Result<()> {
122
118
// in the web interface.
123
119
journal:: serve ( & mut http_server. server ) ;
124
120
125
- // Expose the display as a .png on the web server
126
- ui:: serve_display ( & mut http_server. server , display. screenshooter ( ) ) ;
127
-
128
121
// Set up the user interface for the hardware display on the TAC.
129
122
// The different screens receive updates via the topics provided in
130
123
// the UiResources struct.
@@ -153,7 +146,19 @@ async fn main() -> Result<()> {
153
146
// and expose the topics via HTTP and MQTT-over-websocket.
154
147
bb. build ( & mut http_server. server ) ;
155
148
156
- log:: info!( "Setup complete. Handling requests" ) ;
149
+ Ok ( ( ui, http_server, watchdog) )
150
+ }
151
+
152
+ async fn run (
153
+ ui : Ui ,
154
+ mut http_server : HttpServer ,
155
+ watchdog : Option < Watchdog > ,
156
+ display : Display ,
157
+ ) -> Result < ( ) > {
158
+ // Expose the display as a .png on the web server
159
+ ui:: serve_display ( & mut http_server. server , display. screenshooter ( ) ) ;
160
+
161
+ info ! ( "Setup complete. Handling requests" ) ;
157
162
158
163
// Run until the user interface, http server or (if selected) the watchdog
159
164
// exits (with an error).
@@ -172,3 +177,32 @@ async fn main() -> Result<()> {
172
177
173
178
Ok ( ( ) )
174
179
}
180
+
181
+ #[ async_std:: main]
182
+ async fn main ( ) -> Result < ( ) > {
183
+ env_logger:: init ( ) ;
184
+
185
+ // Show a splash screen very early on
186
+ let display = setup_display ( ) ;
187
+
188
+ match init ( ) . await {
189
+ Ok ( ( ui, http_server, watchdog) ) => run ( ui, http_server, watchdog, display) . await ,
190
+ Err ( e) => {
191
+ // Display a detailed error message on stderr (and thus in the journal) ...
192
+ error ! ( "Failed to initialize tacd: {e}" ) ;
193
+
194
+ // ... and a generic message on the LCD, as it can not fit a lot of detail.
195
+ display. clear ( ) ;
196
+ display. with_lock ( |target| {
197
+ message (
198
+ target,
199
+ "tacd failed to start!\n \n Check log for info.\n Waiting for watchdog\n to restart tacd." ,
200
+ ) ;
201
+ } ) ;
202
+
203
+ // Wait forever (or more likely until the systemd watchdog timer hits)
204
+ // to give the user a chance to actually see the error message.
205
+ pending ( ) . await
206
+ }
207
+ }
208
+ }
0 commit comments