Initial commit
This commit is contained in:
59
lib/models/busylight_color.dart
Normal file
59
lib/models/busylight_color.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BusylightColor {
|
||||
final int r;
|
||||
final int g;
|
||||
final int b;
|
||||
final double brightness;
|
||||
|
||||
const BusylightColor({
|
||||
required this.r,
|
||||
required this.g,
|
||||
required this.b,
|
||||
this.brightness = 1.0,
|
||||
});
|
||||
|
||||
factory BusylightColor.fromJson(Map<String, dynamic> json) {
|
||||
// GET /api/color returns { "colors": { r, g, b }, "brightness": 0.3 }
|
||||
final colors = json['colors'] as Map<String, dynamic>? ?? json;
|
||||
return BusylightColor(
|
||||
r: (colors['r'] as num?)?.toInt() ?? 0,
|
||||
g: (colors['g'] as num?)?.toInt() ?? 0,
|
||||
b: (colors['b'] as num?)?.toInt() ?? 0,
|
||||
brightness: (json['brightness'] as num?)?.toDouble() ?? 0.3,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'r': r,
|
||||
'g': g,
|
||||
'b': b,
|
||||
'brightness': brightness,
|
||||
};
|
||||
|
||||
Color toFlutterColor() => Color.fromARGB(255, r, g, b);
|
||||
|
||||
factory BusylightColor.fromFlutterColor(Color color, {double brightness = 1.0}) {
|
||||
return BusylightColor(
|
||||
r: color.red,
|
||||
g: color.green,
|
||||
b: color.blue,
|
||||
brightness: brightness,
|
||||
);
|
||||
}
|
||||
|
||||
BusylightColor copyWith({int? r, int? g, int? b, double? brightness}) {
|
||||
return BusylightColor(
|
||||
r: r ?? this.r,
|
||||
g: g ?? this.g,
|
||||
b: b ?? this.b,
|
||||
brightness: brightness ?? this.brightness,
|
||||
);
|
||||
}
|
||||
|
||||
static const green = BusylightColor(r: 0, g: 255, b: 0);
|
||||
static const red = BusylightColor(r: 255, g: 0, b: 0);
|
||||
static const yellow = BusylightColor(r: 255, g: 200, b: 0);
|
||||
static const white = BusylightColor(r: 255, g: 255, b: 255);
|
||||
static const off = BusylightColor(r: 0, g: 0, b: 0, brightness: 0);
|
||||
}
|
||||
37
lib/models/busylight_status.dart
Normal file
37
lib/models/busylight_status.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
enum BusylightStatus {
|
||||
on,
|
||||
off,
|
||||
available,
|
||||
away,
|
||||
busy,
|
||||
colored;
|
||||
|
||||
String get apiPath {
|
||||
switch (this) {
|
||||
case BusylightStatus.on: return '/api/status/on';
|
||||
case BusylightStatus.off: return '/api/status/off';
|
||||
case BusylightStatus.available: return '/api/status/available';
|
||||
case BusylightStatus.away: return '/api/status/away';
|
||||
case BusylightStatus.busy: return '/api/status/busy';
|
||||
case BusylightStatus.colored: return '/api/status';
|
||||
}
|
||||
}
|
||||
|
||||
String get label {
|
||||
switch (this) {
|
||||
case BusylightStatus.on: return 'On';
|
||||
case BusylightStatus.off: return 'Off';
|
||||
case BusylightStatus.available: return 'Available';
|
||||
case BusylightStatus.away: return 'Away';
|
||||
case BusylightStatus.busy: return 'Busy';
|
||||
case BusylightStatus.colored: return 'Custom';
|
||||
}
|
||||
}
|
||||
|
||||
static BusylightStatus fromString(String value) {
|
||||
return BusylightStatus.values.firstWhere(
|
||||
(e) => e.name == value,
|
||||
orElse: () => BusylightStatus.off,
|
||||
);
|
||||
}
|
||||
}
|
||||
42
lib/models/color_preset.dart
Normal file
42
lib/models/color_preset.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'dart:convert';
|
||||
import 'busylight_color.dart';
|
||||
|
||||
class ColorPreset {
|
||||
final String id;
|
||||
final String name;
|
||||
final BusylightColor color;
|
||||
|
||||
const ColorPreset({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.color,
|
||||
});
|
||||
|
||||
factory ColorPreset.fromJson(Map<String, dynamic> json) {
|
||||
return ColorPreset(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
color: BusylightColor.fromJson(json['color'] as Map<String, dynamic>),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'color': {
|
||||
'r': color.r,
|
||||
'g': color.g,
|
||||
'b': color.b,
|
||||
'brightness': color.brightness,
|
||||
},
|
||||
};
|
||||
|
||||
static List<ColorPreset> listFromJson(String raw) {
|
||||
final list = jsonDecode(raw) as List<dynamic>;
|
||||
return list.map((e) => ColorPreset.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
static String listToJson(List<ColorPreset> presets) {
|
||||
return jsonEncode(presets.map((p) => p.toJson()).toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user