Commit 01bd37e3 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

refactor printer models

parent c9adcfb1
export 'src/models/printer.dart';
export 'src/models/printer_base.dart';
export 'src/models/printer_counter.dart';
export 'src/models/printer_details.dart';
export 'src/models/printer_status.dart';
export 'src/models/printer_watch_options.dart';
export 'src/printers/printers_bloc.dart';
export 'src/printers/printers_events.dart';
export 'src/printers/printers_state.dart';
import 'print_queue_task.dart';
import 'printer_base.dart';
import 'printer_counter.dart';
import 'printer_status.dart';
class Printer {
class Printer extends PrinterBase {
final int id;
String hostname;
String ip;
String community;
String mac;
int deviceId;
String location;
bool hasA3;
bool coinOperated;
String description;
bool watchToner;
bool watchTray1;
bool watchTray2;
bool watchTray3;
int watchInterval;
PrinterStatus status;
PrinterCounter counter;
......@@ -25,24 +12,29 @@ class Printer {
Printer({
this.id,
this.hostname,
this.ip,
this.community,
this.mac,
this.deviceId,
this.location,
this.hasA3,
this.coinOperated,
this.description,
this.watchToner,
this.watchTray1,
this.watchTray2,
this.watchTray3,
this.watchInterval,
String hostname,
String ip,
String community,
String mac,
int deviceId,
String location,
bool hasA3,
bool coinOperated,
String description,
this.status,
this.counter,
this.queue,
});
}) : super(
hostname: hostname,
ip: ip,
community: community,
mac: mac,
deviceId: deviceId,
location: location,
hasA3: hasA3,
coinOperated: coinOperated,
description: description,
);
factory Printer.fromMap(Map<String, dynamic> map) => Printer(
id: map['id'] as int,
......@@ -55,11 +47,6 @@ class Printer {
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,
......@@ -69,22 +56,10 @@ class Printer {
queue: map['queue'] != null ? PrintQueueTask.fromMap(map['queue']) : null,
);
@override
Map<String, dynamic> get toMap => <String, dynamic>{
'id': id,
'hostname': hostname,
'ip': ip,
'community': community,
'mac': mac,
'device_id': deviceId,
'location': location,
'has_a3': hasA3,
'coin_operated': coinOperated,
'description': description,
'watch_toner': watchToner,
'watch_tray1': watchTray1,
'watch_tray2': watchTray2,
'watch_tray3': watchTray3,
'watchInterval': watchInterval,
...super.toMap,
'status': status,
'counter': counter,
'queue': queue,
......
import '../models/printer_watch_options.dart';
class PrinterDetails {
class PrinterBase {
String hostname;
String ip;
String community;
String mac;
String location;
String description;
int deviceId;
String location;
bool hasA3;
bool coinOperated;
String description;
final PrinterWatchOptions watchOptions;
PrinterDetails({
PrinterBase({
this.hostname,
this.ip,
this.community,
this.mac,
this.location,
this.description,
this.deviceId,
this.hasA3,
this.coinOperated,
this.watchOptions,
});
PrinterDetails.empty({
this.hostname,
this.ip,
this.community,
this.mac,
this.location,
this.description,
this.deviceId,
this.hasA3,
this.coinOperated,
this.watchOptions,
this.description,
});
factory PrinterDetails.fromMap(Map<String, dynamic> map) => PrinterDetails(
factory PrinterBase.fromMap(Map<String, dynamic> map) => PrinterBase(
hostname: map['hostname'] as String,
ip: map['ip'] as String,
community: map['community'] as String,
......@@ -61,7 +43,6 @@ class PrinterDetails {
if (hasA3 != null) 'has_a3': hasA3,
if (coinOperated != null) 'coin_operated': coinOperated,
if (description != null) 'description': description,
if (watchOptions != null) ...watchOptions.toMap,
};
@override
......
class PrinterWatchOptions {
final bool watchToner;
final bool watchTray1;
final bool watchTray2;
final bool watchTray3;
final int watchInterval;
PrinterWatchOptions({
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'] 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,
if (watchTray1 != null) 'watchTray1': watchTray1,
if (watchTray2 != null) 'watchTray2': watchTray2,
if (watchTray3 != null) 'watchTray3': watchTray3,
if (watchInterval != null) 'watchInterval': watchInterval,
};
@override
String toString() => '[PrinterWatchOptions $toMap]';
}
......@@ -86,7 +86,7 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
}
Future<List<PrintQueueTask>> _getQueue(int deviceId) async {
_log.finer('_getQueue $deviceId');
_log.finer('_getQueue: $deviceId');
return await _client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId/queue'),
headers: {
......
......@@ -7,9 +7,8 @@ import 'package:logging/logging.dart';
import '../models/enums.dart';
import '../models/printer.dart';
import '../models/printer_details.dart';
import '../models/printer_base.dart';
import '../models/printer_status.dart';
import '../models/printer_watch_options.dart';
import '../network_exception.dart';
import '../util.dart';
import 'printers_events.dart';
......@@ -74,7 +73,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
on<AddPrinter>((AddPrinter event, Emitter<PrintersState> emit) async {
try {
await _addPrinter(event.details);
await _getPrinterDetails(event.details.deviceId);
//await _getPrinterDetails(event.details.deviceId);
emit(PrintersState.result(_printers));
} on NetworkException catch (e) {
emit(PrintersState.exception(e));
......@@ -112,11 +111,11 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
return super.close();
}
void onAddPrinter(PrinterDetails details) => add(AddPrinter(details));
void onAddPrinter(PrinterBase details) => add(AddPrinter(details));
void onDeletePrinter(int deviceId) => add(DeletePrinter(deviceId));
void onFilterPrinters(PrinterDetails filter) => add(FilterPrinters(filter));
void onFilterPrinters(PrinterBase filter) => add(FilterPrinters(filter));
void onFuzzyFilterPrinters(String filter) => add(FuzzyFilterPrinters(filter));
......@@ -153,26 +152,20 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}) {
add(UpdatePrinter(
deviceId,
PrinterDetails(
hostname: hostname,
ip: ip,
community: community,
mac: mac,
location: location,
description: description,
deviceId: deviceId,
hasA3: hasA3,
coinOperated: coinOperated,
watchOptions: PrinterWatchOptions(
watchToner: watchToner,
watchTray1: watchTray1,
watchTray2: watchTray2,
watchTray3: watchTray3,
watchInterval: watchInterval,
))));
PrinterBase(
hostname: hostname,
ip: ip,
community: community,
mac: mac,
location: location,
description: description,
deviceId: deviceId,
hasA3: hasA3,
coinOperated: coinOperated,
)));
}
Future<void> _addPrinter(PrinterDetails details) async {
Future<void> _addPrinter(PrinterBase details) async {
_log.finer('_addPrinter: $details');
return await client
.post(
......@@ -188,6 +181,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
_printers.add(details as Printer);
return;
} else {
_log.severe('_addPrinter: ${response.body}');
......@@ -207,6 +201,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
_printers.removeWhere((Printer p) => p.deviceId == deviceId);
return;
} else {
throw NetworkException('status code other than 205 received', response.statusCode);
......@@ -215,7 +210,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
);
}
bool _filterPrinters(PrinterDetails filter) {
bool _filterPrinters(PrinterBase filter) {
_printersFiltered = [];
var filtered = false;
......@@ -293,7 +288,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<Printer> _getPrinterDetails(int deviceId) async {
_log.finer('_getPrinterDetails:');
_log.finer('_getPrinterDetails: $deviceId');
return await client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'),
headers: {
......@@ -309,18 +304,29 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
final printer = Printer.fromMap(json.decode(response.body) as Map<String, dynamic>);
if (printer.status != null) {
if (printer.status.tonerC > 100 || printer.status.tonerC < 0) {
printer.status.tonerC = printer.status.tonerC.abs();
// keep toner levels between 0 and 100
if (printer.status.tonerC > 100) {
printer.status.tonerC = 100;
} else if (printer.status.tonerC < 0) {
printer.status.tonerC = 0;
}
if (printer.status.tonerM > 100 || printer.status.tonerM < 0) {
printer.status.tonerM = printer.status.tonerM.abs();
if (printer.status.tonerM > 100) {
printer.status.tonerM = 100;
} else if (printer.status.tonerM < 0) {
printer.status.tonerM = 0;
}
if (printer.status.tonerY > 100 || printer.status.tonerY < 0) {
printer.status.tonerY = printer.status.tonerY.abs();
if (printer.status.tonerY > 100) {
printer.status.tonerY = 100;
} else if (printer.status.tonerY < 0) {
printer.status.tonerY = 0;
}
if (printer.status.tonerK > 100 || printer.status.tonerK < 0) {
printer.status.tonerK = printer.status.tonerK.abs();
if (printer.status.tonerK > 100) {
printer.status.tonerK = 100;
} else if (printer.status.tonerK < 0) {
printer.status.tonerK = 0;
}
// catch paper levels under 0 to make calculations work properly
if (printer.status.tray1 < 0) {
printer.status.tray1 = 0;
}
......@@ -502,7 +508,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
}
Future<void> _updatePrinterDetails(int deviceId, PrinterDetails details) async {
Future<void> _updatePrinterDetails(int deviceId, PrinterBase details) async {
_log.finer('_updatePrinterDetails: $deviceId $details');
return await client
.put(
......
import '../models/enums.dart';
import '../models/printer_details.dart';
import '../models/printer_base.dart';
class AddPrinter extends PrintersEvent {
final PrinterDetails details;
final PrinterBase details;
AddPrinter(this.details);
......@@ -22,7 +22,7 @@ class DeletePrinter extends PrintersEvent {
}
class FilterPrinters extends PrintersEvent {
final PrinterDetails filter;
final PrinterBase filter;
FilterPrinters(this.filter);
......@@ -79,7 +79,7 @@ class SortPrinters extends PrintersEvent {
class UpdatePrinter extends PrintersEvent {
final int deviceId;
final PrinterDetails details;
final PrinterBase details;
UpdatePrinter(this.deviceId, this.details);
......
......@@ -37,7 +37,7 @@ void main() {
}
},
));
bloc.onFilterPrinters(PrinterDetails.empty(hostname: '43'));
bloc.onFilterPrinters(PrinterBase(hostname: '43'));
});
test('filter printers by ip', () {
......@@ -53,7 +53,7 @@ void main() {
}
},
));
bloc.onFilterPrinters(PrinterDetails.empty(ip: '131.234.234.'));
bloc.onFilterPrinters(PrinterBase(ip: '131.234.234.'));
});
});
......@@ -117,7 +117,7 @@ void main() {
listener.cancel();
}
}));
bloc.onFilterPrinters(PrinterDetails.empty(location: 'BI'));
bloc.onFilterPrinters(PrinterBase(location: 'BI'));
bloc.onSortPrinters(PrinterSortParam.hostname, SortDirection.descending);
});
});
......
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