@@ -13,7 +13,7 @@ mod utils;
1313
1414use crate :: app_errors:: AppResult ;
1515use crate :: cache:: intraday_chart_cache:: IntradayChartCache ;
16- use crate :: config:: config:: Config ;
16+ use crate :: config:: config:: { Config , DataConfig } ;
1717use crate :: entities:: init_db_coon;
1818use crate :: service:: command:: tauri_command:: {
1919 add_stock_info, create_group, delete_all_transaction_records, delete_graphic_by_group_id,
@@ -35,12 +35,14 @@ use std::collections::HashMap;
3535use std:: { env, panic} ;
3636use std:: sync:: atomic:: { AtomicBool } ;
3737use std:: sync:: { Arc , LazyLock , Mutex } ;
38+ use std:: time:: Duration ;
3839use log4rs:: append:: console:: ConsoleAppender ;
3940use log4rs:: append:: file:: FileAppender ;
4041use log4rs:: encode:: pattern:: PatternEncoder ;
4142use log4rs:: filter:: threshold:: ThresholdFilter ;
42- use tauri:: { AppHandle , Manager } ;
43+ use tauri:: { Manager } ;
4344use tokio:: sync:: mpsc;
45+ use tokio:: sync:: mpsc:: Receiver ;
4446use tokio:: task:: JoinHandle ;
4547
4648///是否需要实时更新
@@ -118,23 +120,14 @@ pub async fn get_close_prices(
118120#[ tokio:: main]
119121async fn main ( ) {
120122 init_app ( ) . await ;
121- //todo 下面这俩更新如果出错了在前端页面是没有提醒的,如果想要emit一个错误事件到前端的话,有可能存在前端页面还没渲染完就emit了,就收不到了;
122- // 如果弄一个本地变量保存错误信息,前端页面渲染完成后主动来invoke读取这个变量判断是否出错应该可以,但是目前懒得搞。
123- tokio:: spawn ( async {
124- info ! ( "update stock info start" ) ;
125- if let Err ( e) = service:: curd:: update_all_day_k ( ) . await {
126- error ! ( "update stock info failed:{}" , e)
127- }
128- // update().await;
129- } ) ;
130- tokio:: spawn ( async {
131- info ! ( "update position start" ) ;
132- if let Err ( e) = service:: curd:: update_all_position ( ) . await {
133- error ! ( "update position failed:{}" , e)
134- }
135- } ) ;
123+
124+ let config = Config :: load ( ) . await ;
125+ let data_config = config. data_config . clone ( ) ;
126+ let ( send, recv) = mpsc:: channel :: < ( ) > ( 1 ) ;
127+ init_data_server_daily_position ( recv, data_config) . await ;
128+
136129 let state = MyState :: new ( ) . await ;
137- info ! ( "ui start" ) ;
130+ info ! ( "tauri start... " ) ;
138131 tauri:: Builder :: default ( )
139132 . plugin ( tauri_plugin_single_instance:: init ( |app, args, cwd| {
140133 error ! ( "the app is already running, args: {:?}, cwd: {}" , args, cwd) ;
@@ -147,40 +140,29 @@ async fn main() {
147140 . manage ( state)
148141 . manage ( IntradayChartCache :: new ( ) )
149142 // .manage(Mutex::new(Config::load().await))
150- . manage ( Mutex :: new ( Config :: load ( ) . await ) )
143+ . manage ( Mutex :: new ( config) )
144+ . manage ( send)
151145 . plugin ( tauri_plugin_shell:: init ( ) )
152146 . plugin ( tauri_plugin_fs:: init ( ) )
153147 . plugin ( tauri_plugin_window_state:: Builder :: default ( ) . build ( ) )
154- . setup ( |app| {
155- let app_handle: & AppHandle = app. app_handle ( ) ;
156- let config = app_handle. state :: < Mutex < Config > > ( ) ;
157- let data_config = config. lock ( ) . unwrap ( ) . data_config . clone ( ) ;
158- let ( send, recv) = mpsc:: channel :: < ( ) > ( 1 ) ;
159- app. manage ( send) ;
160- tokio:: spawn ( async move {
161- if let Err ( e) = start_data_server ( & data_config, recv) . await {
162- error ! ( "start data server error:{}" , e) ;
163- }
164- } ) ;
165- Ok ( ( ) )
166- } )
167- // .setup(|app|{
168- // info!("{:?}",app.app_handle().path().app_config_dir().unwrap()); //"C:\\Users\\Xbss\\AppData\\Roaming\\com.xbss.trade-tip"
169- // let main_window = app.get_webview_window("main").unwrap();
170- // // main_window.as_ref().window().restore_state(StateFlags::all())
171- // let tool_window = app.get_webview_window("tool").unwrap();
148+ // .setup(|app| {
149+ // let app_handle: &AppHandle = app.app_handle();
150+ // let config = app_handle.state::<Mutex<Config>>();
151+ // let data_config = config.lock().unwrap().data_config.clone();
152+ // let (send,recv) = mpsc::channel::<()>(1);
153+ // app.manage(send);
172154 // tokio::spawn(async move{
173- // // let mut result = main_window.restore_state(StateFlags::all());
174- // // info!("restore main window state result:{:?}",result);
175- // // if result.is_err(){
176- // // error!("restore main window state error:{}",result.err().unwrap().to_string());
177- // // };
178- // main_window.show().unwrap();
179- // // result = tool_window.restore_state(StateFlags::all());
180- // // if result.is_err(){
181- // // error!("restore tool window state error:{}",result.err().unwrap().to_string());
182- // // };
155+ // tokio::spawn(async move{
156+ // info!("update daily info start...");
157+ // if let Err(e) = service::curd::update_all_day_k(data_config.use_ak_share).await{
158+ // error!("update daily info failed:{}",e)
159+ // };
160+ // });
161+ // if let Err(e) = start_data_server(&data_config,recv).await{
162+ // error!("start data server error:{}",e);
163+ // };
183164 // });
165+ // info!("setup ok.");
184166 // Ok(())
185167 // })
186168 . invoke_handler ( tauri:: generate_handler![
@@ -285,24 +267,44 @@ async fn init_logger() {
285267 }
286268 } ) ) ;
287269}
288- // async fn update() {
289- // match service::curd::update_all_day_k().await {
290- // Ok(_) => {
291- // info!("更新日线数据成功");
292- // NOTICE
293- // .lock()
294- // .unwrap()
295- // .replace("更新日线数据成功".to_string());
296- // }
297- // Err(e) => {
298- // error!("更新日线数据失败:{}", e);
299- // NOTICE
300- // .lock()
301- // .unwrap()
302- // .replace(format!("更新日线数据失败:{}", e.to_string()));
303- // }
304- // };
305- // }
270+ async fn init_data_server_daily_position ( recv : Receiver < ( ) > , data_config : DataConfig ) {
271+ let can_handle_futures = data_config. use_ak_share ;
272+ tokio:: spawn ( async move {
273+ if let Err ( e) = start_data_server ( & data_config, recv) . await {
274+ error ! ( "start data server error:{}" , e) ;
275+ } ;
276+ } ) ;
277+ //todo 下面这俩更新如果出错了在前端页面是没有提醒的,如果想要emit一个错误事件到前端的话,有可能存在前端页面还没渲染完就emit了,就收不到了;
278+ // 如果弄一个本地变量保存错误信息,前端页面渲染完成后主动来invoke读取这个变量判断是否出错应该可以,但是目前懒得搞。
279+ tokio:: spawn ( async move {
280+ info ! ( "update daily info start..." ) ;
281+ if let Err ( e) = service:: curd:: update_all_day_k ( can_handle_futures, false ) . await {
282+ error ! ( "update daily info failed:{:?}." , e)
283+ }
284+ info ! ( "update daily info end." ) ;
285+ // match service::curd::update_all_day_k(can_handle_futures).await {
286+ // Ok(failure) => {
287+ // if failure {
288+ // error!("first update daily info failed, 20s later will retry again.");
289+ // sleep(Duration::from_secs(20)).await;
290+ // match service::curd::update_all_day_k(can_handle_futures).await{
291+ // Ok(failure) =>{
292+ //
293+ // }
294+ // Err(e) => {error!("second update stock info failed:{:?}",e)}
295+ // }
296+ // }
297+ // }
298+ // Err(e) => {error!("update stock info failed:{:?}",e)}
299+ // }
300+ } ) ;
301+ tokio:: spawn ( async {
302+ info ! ( "update position start" ) ;
303+ if let Err ( e) = service:: curd:: update_all_position ( ) . await {
304+ error ! ( "update position failed:{}" , e)
305+ }
306+ } ) ;
307+ }
306308// ///判断是否开市,先发起一个请求,然后sleep 1秒,再发起一个请求,如果两个请求的返回值不一样,则证明开市了,否则证明闭市了。
307309// async fn judge_market_open() {
308310// let response1 = REQUEST.get().unwrap().get("https://qt.gtimg.cn/q=sz159992").await.unwrap();
0 commit comments