Add websocket support
This commit is contained in:
16 files changed
+1042
-168
No files matched your search
@@ -9,8 +9,6 @@ import '../services/busylight_service.dart';
|
||||
|
||||
const _kHostKey = 'busylight_host';
|
||||
const _kDefaultHost = 'http://igox-busylight.local';
|
||||
const _kPollIntervalKey = 'busylight_poll_interval';
|
||||
const _kDefaultPollInterval = 1.0; // seconds
|
||||
|
||||
final sharedPreferencesProvider = FutureProvider<SharedPreferences>(
|
||||
(_) => SharedPreferences.getInstance(),
|
||||
@@ -21,11 +19,6 @@ final deviceHostProvider = StateProvider<String>((ref) {
|
||||
return prefs?.getString(_kHostKey) ?? _kDefaultHost;
|
||||
});
|
||||
|
||||
final pollIntervalProvider = StateProvider<double>((ref) {
|
||||
final prefs = ref.watch(sharedPreferencesProvider).valueOrNull;
|
||||
return prefs?.getDouble(_kPollIntervalKey) ?? _kDefaultPollInterval.toDouble();
|
||||
});
|
||||
|
||||
// ── Service ──────────────────────────────────────────────────────────────────
|
||||
|
||||
final busylightServiceProvider = Provider<BusylightService>((ref) {
|
||||
@@ -33,33 +26,10 @@ final busylightServiceProvider = Provider<BusylightService>((ref) {
|
||||
return BusylightService(baseUrl: host);
|
||||
});
|
||||
|
||||
// ── Startup snapshot ─────────────────────────────────────────────────────────
|
||||
// Loads status + color (which includes brightness) in 2 parallel calls.
|
||||
// No separate GET /api/brightness needed — the color response already has it.
|
||||
|
||||
class BusylightSnapshot {
|
||||
final BusylightStatus status;
|
||||
final BusylightColor color;
|
||||
|
||||
const BusylightSnapshot({
|
||||
required this.status,
|
||||
required this.color,
|
||||
});
|
||||
|
||||
double get brightness => color.brightness;
|
||||
}
|
||||
|
||||
final busylightSnapshotProvider = FutureProvider<BusylightSnapshot>((ref) async {
|
||||
final service = ref.watch(busylightServiceProvider);
|
||||
final results = await Future.wait([
|
||||
service.getStatus(),
|
||||
service.getColor(),
|
||||
]);
|
||||
return BusylightSnapshot(
|
||||
status: results[0] as BusylightStatus,
|
||||
color: results[1] as BusylightColor,
|
||||
);
|
||||
});
|
||||
final busylightSnapshotProvider =
|
||||
StateNotifierProvider<LiveStatusNotifier, AsyncValue<BusylightUpdate>>(
|
||||
(ref) => LiveStatusNotifier(ref.watch(busylightServiceProvider)),
|
||||
);
|
||||
|
||||
// ── State notifiers ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -67,25 +37,21 @@ class BusylightStateNotifier extends StateNotifier<AsyncValue<BusylightStatus>>
|
||||
BusylightStateNotifier(this._service, BusylightStatus? initial)
|
||||
: super(initial != null
|
||||
? AsyncValue.data(initial)
|
||||
: const AsyncValue.loading()) {
|
||||
if (initial == null) refresh();
|
||||
}
|
||||
: const AsyncValue.loading());
|
||||
|
||||
final BusylightService _service;
|
||||
|
||||
Future<void> refresh() async {
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(_service.getStatus);
|
||||
}
|
||||
int _revision = 0;
|
||||
|
||||
Future<void> setStatus(BusylightStatus status) async {
|
||||
// Keep current value visible during the API call — no loading state
|
||||
final revision = ++_revision;
|
||||
final result = await AsyncValue.guard(() => _service.setStatus(status));
|
||||
state = result;
|
||||
if (mounted && revision == _revision) state = result;
|
||||
}
|
||||
|
||||
/// Update state locally without making an API call (e.g. for `colored`)
|
||||
void setLocalStatus(BusylightStatus status) {
|
||||
_revision++;
|
||||
state = AsyncValue.data(status);
|
||||
}
|
||||
}
|
||||
@@ -120,7 +86,7 @@ final brightnessProvider = StateNotifierProvider<BrightnessNotifier, double>(
|
||||
final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull;
|
||||
return BrightnessNotifier(
|
||||
ref.watch(busylightServiceProvider),
|
||||
snapshot?.brightness ?? 0.3,
|
||||
snapshot?.color.brightness ?? 0.3,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -149,51 +115,47 @@ final colorProvider = StateNotifierProvider<ColorNotifier, BusylightColor>(
|
||||
},
|
||||
);
|
||||
|
||||
// ── Background polling ────────────────────────────────────────────────────────
|
||||
// Periodically pulls status + color from the device and silently updates state.
|
||||
|
||||
class PollingNotifier extends StateNotifier<void> {
|
||||
PollingNotifier(this._ref) : super(null) {
|
||||
_start();
|
||||
class LiveStatusNotifier extends StateNotifier<AsyncValue<BusylightUpdate>> {
|
||||
LiveStatusNotifier(
|
||||
this._service, {
|
||||
this.reconnectDelay = const Duration(seconds: 2),
|
||||
}) : super(const AsyncValue.loading()) {
|
||||
_connect();
|
||||
}
|
||||
|
||||
final Ref _ref;
|
||||
Timer? _timer;
|
||||
final BusylightService _service;
|
||||
final Duration reconnectDelay;
|
||||
Timer? _reconnectTimer;
|
||||
StreamSubscription<BusylightUpdate>? _subscription;
|
||||
|
||||
void _start() {
|
||||
final intervalSeconds = _ref.read(pollIntervalProvider);
|
||||
final intervalMillis = (intervalSeconds * 1000).toInt();
|
||||
_timer?.cancel();
|
||||
if (intervalMillis <= 0) return;
|
||||
_timer = Timer.periodic(Duration(milliseconds: intervalMillis), (_) => _poll());
|
||||
void _connect() {
|
||||
if (!mounted) return;
|
||||
_subscription = _service.watchStatus().listen(
|
||||
(update) {
|
||||
if (!mounted) return;
|
||||
state = AsyncValue.data(update);
|
||||
},
|
||||
onError: _disconnected,
|
||||
onDone: () => _disconnected(
|
||||
const BusylightException('Live status connection closed'),
|
||||
StackTrace.current,
|
||||
),
|
||||
cancelOnError: true,
|
||||
);
|
||||
}
|
||||
|
||||
void restart() => _start();
|
||||
|
||||
Future<void> _poll() async {
|
||||
try {
|
||||
final service = _ref.read(busylightServiceProvider);
|
||||
final results = await Future.wait([
|
||||
service.getStatus(),
|
||||
service.getColor(),
|
||||
]);
|
||||
final status = results[0] as BusylightStatus;
|
||||
final color = results[1] as BusylightColor;
|
||||
_ref.read(busylightStatusProvider.notifier).setLocalStatus(status);
|
||||
_ref.read(colorProvider.notifier).silentSet(color);
|
||||
_ref.read(brightnessProvider.notifier).silentSet(color.brightness);
|
||||
} catch (_) {
|
||||
// Silently ignore poll errors — connection issues are shown on manual refresh
|
||||
}
|
||||
void _disconnected(Object error, StackTrace stackTrace) {
|
||||
if (!mounted) return;
|
||||
state = AsyncValue<BusylightUpdate>.error(error, stackTrace)
|
||||
.copyWithPrevious(state);
|
||||
_reconnectTimer?.cancel();
|
||||
_reconnectTimer = Timer(reconnectDelay, _connect);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
_reconnectTimer?.cancel();
|
||||
_subscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
final pollingProvider = StateNotifierProvider<PollingNotifier, void>(
|
||||
(ref) => PollingNotifier(ref),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user