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

write printer connection status into attribute

parent 0d7c0f2c
enum ConnectionStatus {
waiting,
disconnected,
connected,
}
enum Level {
empty,
low,
mid,
full,
}
class PrinterStatus {
int scan;
int copy;
......@@ -8,7 +21,8 @@ class PrinterStatus {
int tray1;
int tray2;
int tray3;
int total;
ConnectionStatus connection;
PrinterStatus({
this.scan,
......@@ -20,6 +34,7 @@ class PrinterStatus {
this.tray1,
this.tray2,
this.tray3,
this.connection = ConnectionStatus.waiting,
});
factory PrinterStatus.fromMap(Map<String, dynamic> map) => PrinterStatus(
......@@ -78,6 +93,8 @@ class PrinterStatus {
return problems;
}
Level get paperLevel => _percentageToLevel(((totalPaper / 3000) * 100).toInt());
List<String> get scanStatus {
var problems = <String>[];
......@@ -128,6 +145,28 @@ class PrinterStatus {
'tray_3': tray3,
};
Level get tonerLevel {
if (tonerK == null) {
return null;
}
if (tonerC == null && tonerM == null && tonerY == null && tonerK != null) {
return _percentageToLevel(tonerK);
}
final toners = [tonerC, tonerM, tonerM, tonerK]..sort((a, b) => a.compareTo(b));
return _percentageToLevel(toners.first);
}
int get totalPaper => (tray1 ?? 0) + (tray2 ?? 0) + (tray3 ?? 0);
@override
String toString() => '[PrinterStatus $toMap]';
Level _percentageToLevel(int percentage) {
if (percentage >= 90) return Level.full;
if (percentage >= 20) return Level.mid;
if (percentage > 0) return Level.low;
return Level.empty;
}
}
......@@ -8,6 +8,7 @@ import 'package:logging/logging.dart';
import '../models/enums.dart';
import '../models/printer.dart';
import '../models/printer_details.dart';
import '../models/printer_status.dart';
import '../models/printer_watch_options.dart';
import '../network_exception.dart';
import '../util.dart';
......@@ -328,39 +329,52 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
_log.finer(response.body);
if (response.statusCode == 200) {
var index = _printers.indexWhere((Printer printer) => printer.deviceId == deviceId);
final printer = Printer.fromMap(json.decode(response.body) as Map<String, dynamic>);
_printers[index] = Printer.fromMap(json.decode(response.body) as Map<String, dynamic>);
if (_printers[index].status != null) {
if (_printers[index].status.tonerC > 100 || _printers[index].status.tonerC < 0) {
_printers[index].status.tonerC = null;
if (printer.status != null) {
if (printer.status.tonerC > 100 || printer.status.tonerC < 0) {
printer.status.tonerC = null;
}
if (_printers[index].status.tonerM > 100 || _printers[index].status.tonerM < 0) {
_printers[index].status.tonerM = null;
if (printer.status.tonerM > 100 || printer.status.tonerM < 0) {
printer.status.tonerM = null;
}
if (_printers[index].status.tonerY > 100 || _printers[index].status.tonerY < 0) {
_printers[index].status.tonerY = null;
if (printer.status.tonerY > 100 || printer.status.tonerY < 0) {
printer.status.tonerY = null;
}
if (_printers[index].status.tonerK > 100 || _printers[index].status.tonerK < 0) {
_printers[index].status.tonerK = null;
if (printer.status.tonerK > 100 || printer.status.tonerK < 0) {
printer.status.tonerK = null;
}
if (_printers[index].status.tray1 < 0) {
_printers[index].status.tray1 = null;
if (printer.status.tray1 < 0) {
printer.status.tray1 = null;
}
if (_printers[index].status.tray2 < 0) {
_printers[index].status.tray2 = null;
if (printer.status.tray2 < 0) {
printer.status.tray2 = null;
}
if (_printers[index].status.tray3 < 0) {
_printers[index].status.tray3 = null;
if (printer.status.tray3 < 0) {
printer.status.tray3 = null;
}
}
return _printers[index];
if (printer.status == null) {
printer.status = PrinterStatus(connection: ConnectionStatus.disconnected);
} else {
printer.status.connection = ConnectionStatus.connected;
}
_printers[index] = printer;
return printer;
} else {
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
).timeout(const Duration(seconds: 10));
).timeout(
const Duration(seconds: 10),
onTimeout: () {
var index = _printers.indexWhere((Printer printer) => printer.deviceId == deviceId);
_printers[index].status = PrinterStatus(connection: ConnectionStatus.disconnected);
return _printers[index];
},
);
}
Future<void> _getPrinters() async {
......
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