@@ -14,9 +14,9 @@ namespace ChromaControl.SDK.OpenRGB;
1414/// <inheritdoc/>
1515public class OpenRGBService : IOpenRGBService , IAsyncDisposable
1616{
17+ private NativeOpenRGBService _service ;
18+ private BlockingCollection < OpenRGBDevice > _devices ;
1719 private readonly OpenRGBManager _manager ;
18- private readonly NativeOpenRGBService _service ;
19- private readonly BlockingCollection < OpenRGBDevice > _devices ;
2020
2121 /// <inheritdoc/>
2222 public bool Started { get ; internal set ; }
@@ -45,6 +45,26 @@ public void UpdateConfiguration(JsonNode config)
4545 _manager . UpdateConfigFile ( config ) ;
4646 }
4747
48+ /// <inheritdoc/>
49+ public async Task Restart ( CancellationToken cancellationToken = default )
50+ {
51+ if ( Started )
52+ {
53+ Started = false ;
54+
55+ _service . DeviceListUpdated -= OnDeviceListUpdated ;
56+ await _service . DisposeAsync ( ) ;
57+ _service = new ( ) ;
58+ _service . DeviceListUpdated += OnDeviceListUpdated ;
59+
60+ _devices = [ ] ;
61+
62+ _manager . Stop ( ) ;
63+
64+ await StartServiceAsync ( cancellationToken ) ;
65+ }
66+ }
67+
4868 /// <inheritdoc/>
4969 public async Task UpdateDeviceListAsync ( CancellationToken cancellationToken = default )
5070 {
@@ -68,6 +88,11 @@ public async Task UpdateDeviceListAsync(CancellationToken cancellationToken = de
6888 /// <inheritdoc/>
6989 public async Task ResizeZoneAsync ( int deviceIndex , int zoneIndex , int size , CancellationToken cancellationToken = default )
7090 {
91+ if ( ! Started )
92+ {
93+ return ;
94+ }
95+
7196 await _service . ResizeZoneAsync ( ( uint ) deviceIndex , ( uint ) zoneIndex , ( uint ) size , cancellationToken ) ;
7297
7398 _service . OnDeviceListUpdated ( _service , new ( ) ) ;
@@ -76,60 +101,110 @@ public async Task ResizeZoneAsync(int deviceIndex, int zoneIndex, int size, Canc
76101 /// <inheritdoc/>
77102 public Task ResizeZoneAsync ( OpenRGBDevice device , int zoneIndex , int size , CancellationToken cancellationToken = default )
78103 {
104+ if ( ! Started )
105+ {
106+ return Task . CompletedTask ;
107+ }
108+
79109 return ResizeZoneAsync ( device . Index , zoneIndex , size , cancellationToken ) ;
80110 }
81111
82112 /// <inheritdoc/>
83113 public Task ResizeZoneAsync ( OpenRGBDevice device , OpenRGBZone zone , int size , CancellationToken cancellationToken = default )
84114 {
115+ if ( ! Started )
116+ {
117+ return Task . CompletedTask ;
118+ }
119+
85120 return ResizeZoneAsync ( device . Index , zone . Index , size , cancellationToken ) ;
86121 }
87122
88123 /// <inheritdoc/>
89124 public async Task UpdateLedsAsync ( int deviceIndex , Color [ ] colors , CancellationToken cancellationToken = default )
90125 {
126+ if ( ! Started )
127+ {
128+ return ;
129+ }
130+
91131 await _service . UpdateLedsAsync ( ( uint ) deviceIndex , colors , cancellationToken ) ;
92132 }
93133
94134 /// <inheritdoc/>
95135 public Task UpdateLedsAsync ( OpenRGBDevice device , Color [ ] colors , CancellationToken cancellationToken = default )
96136 {
137+ if ( ! Started )
138+ {
139+ return Task . CompletedTask ;
140+ }
141+
97142 return UpdateLedsAsync ( device . Index , colors , cancellationToken ) ;
98143 }
99144
100145 /// <inheritdoc/>
101146 public async Task UpdateZoneLedsAsync ( int deviceIndex , int zoneIndex , Color [ ] colors , CancellationToken cancellationToken = default )
102147 {
148+ if ( ! Started )
149+ {
150+ return ;
151+ }
152+
103153 await _service . UpdateZoneLedsAsync ( ( uint ) deviceIndex , ( uint ) zoneIndex , colors , cancellationToken ) ;
104154 }
105155
106156 /// <inheritdoc/>
107157 public Task UpdateZoneLedsAsync ( OpenRGBDevice device , int zoneIndex , Color [ ] colors , CancellationToken cancellationToken = default )
108158 {
159+ if ( ! Started )
160+ {
161+ return Task . CompletedTask ;
162+ }
163+
109164 return UpdateZoneLedsAsync ( device . Index , zoneIndex , colors , cancellationToken ) ;
110165 }
111166
112167 /// <inheritdoc/>
113168 public Task UpdateZoneLedsAsync ( OpenRGBDevice device , OpenRGBZone zone , Color [ ] colors , CancellationToken cancellationToken = default )
114169 {
170+ if ( ! Started )
171+ {
172+ return Task . CompletedTask ;
173+ }
174+
115175 return UpdateZoneLedsAsync ( device . Index , zone . Index , colors , cancellationToken ) ;
116176 }
117177
118178 /// <inheritdoc/>
119179 public async Task UpdateSingleLedAsync ( int deviceIndex , int ledIndex , Color color , CancellationToken cancellationToken = default )
120180 {
181+ if ( ! Started )
182+ {
183+ return ;
184+ }
185+
121186 await _service . UpdateSingleLedAsync ( ( uint ) deviceIndex , ( uint ) ledIndex , color , cancellationToken ) ;
122187 }
123188
124189 /// <inheritdoc/>
125190 public Task UpdateSingleLedAsync ( OpenRGBDevice device , int ledIndex , Color color , CancellationToken cancellationToken = default )
126191 {
192+ if ( ! Started )
193+ {
194+ return Task . CompletedTask ;
195+ }
196+
127197 return UpdateSingleLedAsync ( device . Index , ledIndex , color , cancellationToken ) ;
128198 }
129199
130200 /// <inheritdoc/>
131201 public Task UpdateSingleLedAsync ( OpenRGBDevice device , OpenRGBLed led , Color color , CancellationToken cancellationToken = default )
132202 {
203+ if ( ! Started )
204+ {
205+ return Task . CompletedTask ;
206+ }
207+
133208 return UpdateSingleLedAsync ( device . Index , led . Index , color , cancellationToken ) ;
134209 }
135210
0 commit comments