Commit b5021e4f authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

update models for easy printer adding

parent 98102948
import '../models/printer_watch_options.dart'; import '../models/printer_watch_options.dart';
class PrinterDetails { class PrinterDetails {
final String hostname; String hostname;
final String ip; String ip;
final String community; String community;
final String mac; String mac;
final String location; String location;
final String description; String description;
final int deviceId; int deviceId;
final bool hasA3; bool hasA3;
final bool coinOperated; bool coinOperated;
final PrinterWatchOptions watchOptions; final PrinterWatchOptions watchOptions;
......
...@@ -6,18 +6,20 @@ class PrinterWatchOptions { ...@@ -6,18 +6,20 @@ class PrinterWatchOptions {
final int watchInterval; final int watchInterval;
PrinterWatchOptions({ PrinterWatchOptions({
this.watchToner, this.watchToner = true,
this.watchTray1, this.watchTray1 = true,
this.watchTray2, this.watchTray2 = true,
this.watchTray3, this.watchTray3 = true,
this.watchInterval, this.watchInterval = 5,
}); });
factory PrinterWatchOptions.fromMap(Map<String, dynamic> map) => PrinterWatchOptions( factory PrinterWatchOptions.fromMap(Map<String, dynamic> map) => PrinterWatchOptions(
watchInterval: map['watch_interval'] as int, watchInterval: map['watch_interval'] as int,
watchToner: map['watch_toner'] as bool, watchToner: map['watch_toner'] as bool,
watchTray1: map['watch_tray1'] watchTray1: map['watch_tray1'] as bool,
); watchTray2: map['watch_tray2'] as bool,
watchTray3: map['watch_tray3'] as bool,
);
Map<String, dynamic> get toMap => <String, dynamic>{ Map<String, dynamic> get toMap => <String, dynamic>{
if (watchToner != null) 'watchToner': watchToner, if (watchToner != null) 'watchToner': watchToner,
......
...@@ -73,8 +73,8 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -73,8 +73,8 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
on<AddPrinter>((AddPrinter event, Emitter<PrintersState> emit) async { on<AddPrinter>((AddPrinter event, Emitter<PrintersState> emit) async {
try { try {
await _addPrinter(event.deviceId, event.details); await _addPrinter(event.details);
await _getPrinterDetails(event.deviceId); await _getPrinterDetails(event.details.deviceId);
emit(PrintersState.result(_printers)); emit(PrintersState.result(_printers));
} on NetworkException catch (e) { } on NetworkException catch (e) {
emit(PrintersState.exception(e)); emit(PrintersState.exception(e));
...@@ -112,31 +112,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -112,31 +112,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
return super.close(); return super.close();
} }
void onAddPrinter( void onAddPrinter(PrinterDetails details) => add(AddPrinter(details));
int deviceId, {
String hostname,
String ip,
String community,
String mac,
String location,
String description,
bool hasA3,
bool coinOperated,
}) {
add(AddPrinter(
deviceId,
PrinterDetails(
hostname: hostname,
ip: ip,
community: community,
mac: mac,
location: location,
description: description,
deviceId: deviceId,
hasA3: hasA3,
coinOperated: coinOperated,
)));
}
void onDeletePrinter(int deviceId) => add(DeletePrinter(deviceId)); void onDeletePrinter(int deviceId) => add(DeletePrinter(deviceId));
...@@ -196,14 +172,15 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -196,14 +172,15 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
)))); ))));
} }
Future<void> _addPrinter(int deviceId, PrinterDetails details) async { Future<void> _addPrinter(PrinterDetails details) async {
_log.finer('_addPrinter:'); _log.finer('_addPrinter: $details');
return await client return await client
.post( .post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers'), Uri.parse('https://astaprint.upb.de/api/v1/admin/printers'),
headers: { headers: {
'X-Api-Key': _token, 'X-Api-Key': _token,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json',
}, },
body: json.encode(details.toMap), body: json.encode(details.toMap),
) )
......
...@@ -2,12 +2,11 @@ import '../models/enums.dart'; ...@@ -2,12 +2,11 @@ import '../models/enums.dart';
import '../models/printer_details.dart'; import '../models/printer_details.dart';
class AddPrinter extends PrintersEvent { class AddPrinter extends PrintersEvent {
final int deviceId;
final PrinterDetails details; final PrinterDetails details;
AddPrinter(this.deviceId, this.details); AddPrinter(this.details);
Map<String, dynamic> get toMap => {'device_id': deviceId, 'details': details.toMap}; Map<String, dynamic> get toMap => details.toMap;
@override @override
String toString() => '[AddPrinter $toMap]'; String toString() => '[AddPrinter $toMap]';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment