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

this is dart, no underscores pls

parent 3bddd56f
...@@ -18,6 +18,7 @@ class Printer { ...@@ -18,6 +18,7 @@ class Printer {
bool watchTray2; bool watchTray2;
bool watchTray3; bool watchTray3;
int watchInterval; int watchInterval;
PrinterStatus status; PrinterStatus status;
PrinterCounter counter; PrinterCounter counter;
PrinterQueue queue; PrinterQueue queue;
...@@ -43,42 +44,30 @@ class Printer { ...@@ -43,42 +44,30 @@ class Printer {
this.queue, this.queue,
}); });
factory Printer.fromMap(Map<String, dynamic> map) { factory Printer.fromMap(Map<String, dynamic> map) => Printer(
PrinterStatus _printerStatus; id: map['id'] as int,
PrinterCounter _printerCounter; hostname: map['hostname'] as String,
ip: map['ip'] as String,
try { community: map['community'] as String,
_printerStatus = PrinterStatus.fromMap(map['status'] as Map<String, dynamic>); mac: map['mac'] as String,
} catch (e) { deviceId: map['device_id'] as int,
//print('Failed to get printer status'); location: map['location'] as String,
} hasA3: map['has_a3'] as bool,
try { coinOperated: map['coin_operated'] as bool,
_printerCounter = PrinterCounter.fromMap(map['counter'] as Map<String, dynamic>); description: map['description'] as String,
} catch (e) { watchToner: map['watch_toner'] as bool,
//print('Failed to get printer counters'); watchTray1: map['watch_tray1'] as bool,
} watchTray2: map['watch_tray2'] as bool,
watchTray3: map['watch_tray3'] as bool,
return Printer( watchInterval: map['watch_interval'] as int,
id: map['id'] as int, status: map['status'] != null
hostname: map['hostname'] as String, ? PrinterStatus.fromMap(map['status'] as Map<String, dynamic>)
ip: map['ip'] as String, : null,
community: map['community'] as String, counter: map['counter'] != null
mac: map['mac'] as String, ? PrinterCounter.fromMap(map['counter'] as Map<String, dynamic>)
deviceId: map['device_id'] as int, : null,
location: map['location'] as String, queue: map['queue'] as PrinterQueue,
hasA3: map['has_a3'] as bool, );
coinOperated: map['coin_operated'] as bool,
description: map['description'] as String,
watchToner: map['watch_toner'] as bool,
watchTray1: map['watch_tray1'] as bool,
watchTray2: map['watch_tray2'] as bool,
watchTray3: map['watch_tray3'] as bool,
watchInterval: map['watch_interval'] as int,
status: _printerStatus,
counter: _printerCounter,
queue: map['queue'] as PrinterQueue,
);
}
Map<String, dynamic> get toMap => <String, dynamic>{ Map<String, dynamic> get toMap => <String, dynamic>{
'id': id, 'id': id,
......
...@@ -5,9 +5,9 @@ class PrinterStatus { ...@@ -5,9 +5,9 @@ class PrinterStatus {
int tonerM; int tonerM;
int tonerY; int tonerY;
int tonerK; int tonerK;
int tray_1; int tray1;
int tray_2; int tray2;
int tray_3; int tray3;
int total; int total;
PrinterStatus({ PrinterStatus({
...@@ -17,9 +17,9 @@ class PrinterStatus { ...@@ -17,9 +17,9 @@ class PrinterStatus {
this.tonerM, this.tonerM,
this.tonerY, this.tonerY,
this.tonerK, this.tonerK,
this.tray_1, this.tray1,
this.tray_2, this.tray2,
this.tray_3, this.tray3,
}); });
factory PrinterStatus.fromMap(Map<String, dynamic> map) => PrinterStatus( factory PrinterStatus.fromMap(Map<String, dynamic> map) => PrinterStatus(
...@@ -29,9 +29,9 @@ class PrinterStatus { ...@@ -29,9 +29,9 @@ class PrinterStatus {
tonerM: map['toner_m'] as int, tonerM: map['toner_m'] as int,
tonerY: map['toner_y'] as int, tonerY: map['toner_y'] as int,
tonerK: map['toner_k'] as int, tonerK: map['toner_k'] as int,
tray_1: map['tray_1'] as int, tray1: map['tray1'] as int,
tray_2: map['tray_2'] as int, tray2: map['tray2'] as int,
tray_3: map['tray_3'] as int, tray3: map['tray3'] as int,
); );
Map<String, dynamic> get toMap => <String, dynamic>{ Map<String, dynamic> get toMap => <String, dynamic>{
...@@ -41,9 +41,9 @@ class PrinterStatus { ...@@ -41,9 +41,9 @@ class PrinterStatus {
'toner_m': tonerM, 'toner_m': tonerM,
'toner_y': tonerY, 'toner_y': tonerY,
'toner_k': tonerK, 'toner_k': tonerK,
'tray_1': tray_1, 'tray1': tray1,
'tray_2': tray_2, 'tray2': tray2,
'tray_3': tray_3, 'tray3': tray3,
}; };
@override @override
......
...@@ -49,7 +49,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -49,7 +49,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
on<GetPrinterDetails>((GetPrinterDetails event, Emitter<PrintersState> emit) async { on<GetPrinterDetails>((GetPrinterDetails event, Emitter<PrintersState> emit) async {
try { try {
await _getPrinterDetails(event.deviceId); final printer = await _getPrinterDetails(event.deviceId);
_printers.removeWhere((e) => e.deviceId == printer.deviceId);
_printers.add(printer);
emit(PrintersState.result(_printers)); emit(PrintersState.result(_printers));
} on NetworkException catch (e) { } on NetworkException catch (e) {
emit(PrintersState.exception(e)); emit(PrintersState.exception(e));
...@@ -312,7 +314,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -312,7 +314,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
.toList(); .toList();
} }
Future<void> _getPrinterDetails(int deviceId) async { Future<Printer> _getPrinterDetails(int deviceId) async {
_log.finer('_getPrinterDetails:'); _log.finer('_getPrinterDetails:');
return await client.get( return await client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'), Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'),
...@@ -323,6 +325,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -323,6 +325,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
).then( ).then(
(Response response) { (Response response) {
_log.finer(formatResponse(response)); _log.finer(formatResponse(response));
_log.finer(response.body);
if (response.statusCode == 200) { if (response.statusCode == 200) {
var index = _printers.indexWhere((Printer printer) => printer.deviceId == deviceId); var index = _printers.indexWhere((Printer printer) => printer.deviceId == deviceId);
...@@ -341,18 +344,18 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> { ...@@ -341,18 +344,18 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
if (_printers[index].status.tonerK > 100 || _printers[index].status.tonerK < 0) { if (_printers[index].status.tonerK > 100 || _printers[index].status.tonerK < 0) {
_printers[index].status.tonerK = null; _printers[index].status.tonerK = null;
} }
if (_printers[index].status.tray_1 < 0) { if (_printers[index].status.tray1 < 0) {
_printers[index].status.tray_1 = null; _printers[index].status.tray1 = null;
} }
if (_printers[index].status.tray_2 < 0) { if (_printers[index].status.tray2 < 0) {
_printers[index].status.tray_2 = null; _printers[index].status.tray2 = null;
} }
if (_printers[index].status.tray_3 < 0) { if (_printers[index].status.tray3 < 0) {
_printers[index].status.tray_3 = null; _printers[index].status.tray3 = null;
} }
} }
return; return _printers[index];
} else { } else {
throw NetworkException('status code other than 200 received', response.statusCode); throw NetworkException('status code other than 200 received', response.statusCode);
} }
......
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