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