1+ use std:: collections:: BTreeMap ;
12use std:: sync:: Arc ;
23
34use async_compression:: tokio:: write:: GzipEncoder ;
@@ -11,6 +12,7 @@ use shvrpc::metamethod::MetaMethod;
1112use shvrpc:: rpcmessage:: { RpcError , RpcErrorCode } ;
1213use shvrpc:: { RpcMessage , RpcMessageMetaTags } ;
1314use tokio:: io:: { AsyncBufRead , AsyncReadExt , AsyncSeekExt , AsyncWriteExt } ;
15+ use tokio:: sync:: RwLockReadGuard ;
1416use tokio_stream:: wrappers:: ReadDirStream ;
1517use tokio_util:: io:: ReaderStream ;
1618
@@ -20,11 +22,12 @@ use crate::getlog::getlog_handler;
2022use crate :: journalrw:: { journal_entries_to_rpcvalue, GetLog2Params , Log2Header , Log2Reader } ;
2123use crate :: pushlog:: pushlog_impl;
2224use crate :: sites:: SubHpInfo ;
23- use crate :: { ClientCommandSender , HpConfig , State , MAX_JOURNAL_DIR_SIZE_DEFAULT } ;
25+ use crate :: { AlarmWithTimestamp , ClientCommandSender , HpConfig , State , MAX_JOURNAL_DIR_SIZE_DEFAULT } ;
2426
2527// History site node methods
2628const METH_GET_LOG : & str = "getLog" ;
2729const METH_ALARM_TABLE : & str = "alarmTable" ;
30+ const METH_STATE_ALARM_TABLE : & str = "stateAlarmTable" ;
2831const METH_ALARM_LOG : & str = "alarmLog" ;
2932const METH_PUSH_LOG : & str = "pushLog" ;
3033
@@ -48,6 +51,16 @@ const META_METHOD_ALARM_TABLE: MetaMethod = MetaMethod {
4851 description : "" ,
4952} ;
5053
54+ const META_METHOD_STATE_ALARM_TABLE : MetaMethod = MetaMethod {
55+ name : METH_STATE_ALARM_TABLE ,
56+ flags : 0 ,
57+ access : shvrpc:: metamethod:: AccessLevel :: Read ,
58+ param : "RpcValue" ,
59+ result : "RpcValue" ,
60+ signals : & [ ( "statealarmmod" , Some ( "Null" ) ) ] ,
61+ description : "" ,
62+ } ;
63+
5164const META_METHOD_ALARM_LOG : MetaMethod = MetaMethod {
5265 name : METH_ALARM_LOG ,
5366 flags : 0 ,
@@ -688,14 +701,33 @@ async fn getlog_handler_rq(
688701 Ok ( result)
689702}
690703
691- async fn alarmtable_handler (
704+ trait AlarmGetter {
705+ async fn alarm_getter ( f : & State ) -> RwLockReadGuard < ' _ , BTreeMap < String , Vec < AlarmWithTimestamp > > > ;
706+ }
707+
708+ struct CommonAlarm ;
709+ impl AlarmGetter for CommonAlarm {
710+ async fn alarm_getter ( f : & State ) -> RwLockReadGuard < ' _ , BTreeMap < String , Vec < AlarmWithTimestamp > > > {
711+ f. alarms . read ( ) . await
712+ }
713+ }
714+
715+ struct StateAlarm ;
716+ impl AlarmGetter for StateAlarm {
717+ async fn alarm_getter ( f : & State ) -> RwLockReadGuard < ' _ , BTreeMap < String , Vec < AlarmWithTimestamp > > > {
718+ f. state_alarms . read ( ) . await
719+ }
720+ }
721+
722+ async fn alarmtable_handler < Getter : AlarmGetter > (
692723 site_path : & str ,
693724 app_state : AppState < State > ,
694725) -> RpcRequestResult {
695726 if !app_state. sites_data . read ( ) . await . sites_info . contains_key ( site_path) {
696727 return Err ( RpcError :: new ( RpcErrorCode :: InvalidParam , format ! ( "Wrong alarmTable path: {site_path}" ) ) ) ;
697728 }
698- match app_state. alarms . read ( ) . await . get ( site_path) {
729+
730+ match Getter :: alarm_getter ( & app_state) . await . get ( site_path) {
699731 Some ( alarms_for_site) => Ok ( alarms_for_site. clone ( ) . into ( ) ) ,
700732 None => Ok ( Vec :: < RpcValue > :: new ( ) . into ( ) ) ,
701733 }
@@ -725,7 +757,8 @@ async fn history_request_handler(
725757 match method {
726758 METH_LS => Ok ( children. into ( ) ) ,
727759 METH_GET_LOG => getlog_handler_rq ( path, & param, app_state) . await ,
728- METH_ALARM_TABLE => alarmtable_handler ( path, app_state) . await ,
760+ METH_ALARM_TABLE => alarmtable_handler :: < CommonAlarm > ( path, app_state) . await ,
761+ METH_STATE_ALARM_TABLE => alarmtable_handler :: < StateAlarm > ( path, app_state) . await ,
729762 METH_ALARM_LOG => alarmlog_handler ( path, & param, app_state) . await ,
730763 METH_PUSH_LOG => pushlog_handler ( path, param, app_state) . await ,
731764 _ => Err ( rpc_error_unknown_method ( method) ) ,
@@ -815,7 +848,7 @@ pub(crate) async fn methods_getter(
815848 }
816849
817850 if sites_data. typeinfos . get ( & path) . is_some_and ( Result :: is_ok) {
818- Some ( MetaMethods :: from ( & [ & META_METHOD_GET_LOG , & META_METHOD_ALARM_TABLE , & META_METHOD_ALARM_LOG ] ) )
851+ Some ( MetaMethods :: from ( & [ & META_METHOD_GET_LOG , & META_METHOD_ALARM_TABLE , & META_METHOD_STATE_ALARM_TABLE , & META_METHOD_ALARM_LOG ] ) )
819852 } else {
820853 Some ( MetaMethods :: from ( & [ & META_METHOD_GET_LOG , & META_METHOD_ALARM_LOG ] ) )
821854 }
0 commit comments