1
1
use anyhow:: { anyhow, Result } ;
2
- use integrations:: Integration ;
2
+ use integrations:: { Integration , IntegrationEnum , IntegrationsConfigurationEnum , IntoIntegration } ;
3
3
use sdc_core:: types:: { Actions , ExecuteActionReq , Profiles } ;
4
4
use std:: collections:: HashMap ;
5
5
use std:: env;
@@ -15,55 +15,10 @@ mod ws_api;
15
15
16
16
const ACTION_SPLIT_CHARS : [ char ; 2 ] = [ ':' , ':' ] ;
17
17
18
- #[ derive( Debug , serde:: Serialize , serde:: Deserialize ) ]
19
- struct IntegrationConfiguration < T : integrations:: IntegrationConfig > {
20
- name : Option < String > ,
21
- #[ serde( flatten) ]
22
- options : T ,
23
- }
24
-
25
- impl < T : integrations:: IntegrationConfig > IntegrationConfiguration < T > {
26
- async fn as_integration ( & self ) -> integrations:: IntegrationResult {
27
- return self . options . to_integration ( self . name . clone ( ) ) . await ;
28
- }
29
- }
30
-
31
- #[ derive( Debug , serde:: Serialize , serde:: Deserialize ) ]
32
- #[ serde( tag = "type" ) ]
33
- #[ serde( rename_all = "snake_case" ) ]
34
- enum IntegrationsConfiguration {
35
- Hue ( IntegrationConfiguration < integrations:: hue:: IntegrationConfig > ) ,
36
- Homebridge ( IntegrationConfiguration < integrations:: integrations:: homebridge:: IntegrationConfig > ) ,
37
- Airplay ( IntegrationConfiguration < integrations:: airplay:: IntegrationConfig > ) ,
38
- Http ( IntegrationConfiguration < integrations:: http:: IntegrationConfig > ) ,
39
- }
40
-
41
- impl IntegrationsConfiguration {
42
- async fn as_integration ( & self ) -> integrations:: IntegrationResult {
43
- match self {
44
- IntegrationsConfiguration :: Hue ( c) => c. as_integration ( ) . await ,
45
- IntegrationsConfiguration :: Homebridge ( c) => c. as_integration ( ) . await ,
46
- IntegrationsConfiguration :: Airplay ( c) => c. as_integration ( ) . await ,
47
- IntegrationsConfiguration :: Http ( c) => c. as_integration ( ) . await ,
48
- }
49
- }
50
- }
51
-
52
- impl std:: fmt:: Display for IntegrationsConfiguration {
53
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
54
- match self {
55
- IntegrationsConfiguration :: Hue ( _) => write ! ( f, "hue" ) ,
56
- IntegrationsConfiguration :: Homebridge ( _) => write ! ( f, "homebridge" ) ,
57
- IntegrationsConfiguration :: Airplay ( _) => write ! ( f, "airplay" ) ,
58
- IntegrationsConfiguration :: Http ( _) => write ! ( f, "http" ) ,
59
- }
60
- }
61
- }
62
-
63
18
#[ derive( Debug , serde:: Serialize , serde:: Deserialize ) ]
64
19
#[ serde( rename_all = "snake_case" ) ]
65
20
pub struct Config {
66
- integrations : Vec < IntegrationsConfiguration > ,
21
+ integrations : Vec < IntegrationsConfigurationEnum > ,
67
22
profiles : Profiles ,
68
23
}
69
24
@@ -132,7 +87,7 @@ async fn populat_image_cache(config_ref: Arc<Config>, image_cache: ws_api::Image
132
87
}
133
88
134
89
struct IntegrationManager {
135
- integrations : HashMap < String , Box < dyn Integration + Send + Sync > > ,
90
+ integrations : HashMap < String , IntegrationEnum > ,
136
91
rx : Receiver < ExecuteActionReq > ,
137
92
ws_clients : ws_api:: Clients ,
138
93
}
@@ -155,7 +110,7 @@ impl IntegrationManager {
155
110
integration = integration. to_string( ) ,
156
111
"setting up integration"
157
112
) ;
158
- let i = integration. as_integration ( ) . await . map_err ( |err| {
113
+ let i = integration. into_integration ( ) . await . map_err ( |err| {
159
114
error ! ( ?integration, error = ?err, "failed to create integration" ) ;
160
115
anyhow ! (
161
116
"failed to create integration {:?} with {:?}" ,
@@ -175,7 +130,7 @@ impl IntegrationManager {
175
130
return Ok ( ( manager, tx) ) ;
176
131
}
177
132
178
- fn add_integration ( & mut self , integration : Box < dyn Integration + Send + Sync > ) {
133
+ fn add_integration ( & mut self , integration : IntegrationEnum ) {
179
134
self . integrations
180
135
. insert ( integration. name ( ) . to_string ( ) , integration) ;
181
136
}
@@ -238,7 +193,6 @@ impl IntegrationManager {
238
193
match integration_option {
239
194
Some ( integration) => {
240
195
integration
241
- . as_ref ( )
242
196
. execute_action ( action_name. to_string ( ) , options)
243
197
. await ?;
244
198
}
0 commit comments