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';
class PrinterDetails {
final String hostname;
final String ip;
final String community;
final String mac;
final String location;
final String description;
final int deviceId;
final bool hasA3;
final bool coinOperated;
String hostname;
String ip;
String community;
String mac;
String location;
String description;
int deviceId;
bool hasA3;
bool coinOperated;
final PrinterWatchOptions watchOptions;
......
......@@ -6,18 +6,20 @@ class PrinterWatchOptions {
final int watchInterval;
PrinterWatchOptions({
this.watchToner,
this.watchTray1,
this.watchTray2,
this.watchTray3,
this.watchInterval,
this.watchToner = true,
this.watchTray1 = true,
this.watchTray2 = true,
this.watchTray3 = true,
this.watchInterval = 5,
});
factory PrinterWatchOptions.fromMap(Map<String, dynamic> map) => PrinterWatchOptions(
watchInterval: map['watch_interval'] as int,
watchToner: map['watch_toner'] as bool,
watchTray1: map['watch_tray1']
);
watchInterval: map['watch_interval'] as int,
watchToner: map['watch_toner'] as bool,
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>{
if (watchToner != null) 'watchToner': watchToner,
......
......@@ -73,8 +73,8 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
on<AddPrinter>((AddPrinter event, Emitter<PrintersState> emit) async {
try {
await _addPrinter(event.deviceId, event.details);
await _getPrinterDetails(event.deviceId);
await _addPrinter(event.details);
await _getPrinterDetails(event.details.deviceId);
emit(PrintersState.result(_printers));
} on NetworkException catch (e) {
emit(PrintersState.exception(e));
......@@ -112,31 +112,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
return super.close();
}
void onAddPrinter(
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 onAddPrinter(PrinterDetails details) => add(AddPrinter(details));
void onDeletePrinter(int deviceId) => add(DeletePrinter(deviceId));
......@@ -196,14 +172,15 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
))));
}
Future<void> _addPrinter(int deviceId, PrinterDetails details) async {
_log.finer('_addPrinter:');
Future<void> _addPrinter(PrinterDetails details) async {
_log.finer('_addPrinter: $details');
return await client
.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: json.encode(details.toMap),
)
......
......@@ -2,12 +2,11 @@ import '../models/enums.dart';
import '../models/printer_details.dart';
class AddPrinter extends PrintersEvent {
final int deviceId;
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
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