-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathcapability_profile.dart
More file actions
73 lines (56 loc) · 1.84 KB
/
capability_profile.dart
File metadata and controls
73 lines (56 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* esc_pos_utils
* Created by Andrey U.
*
* Copyright (c) 2019-2020. All rights reserved.
* See LICENSE for distribution and usage details.
*/
import 'dart:convert' show json;
import 'package:esc_pos_utils/src/capabilities_json.dart';
// import 'package:flutter/services.dart' show rootBundle;
class CodePage {
CodePage(this.id, this.name);
int id;
String name;
}
class CapabilityProfile {
CapabilityProfile._internal(this.name, this.codePages);
/// Public factory
static Future<CapabilityProfile> load({String name = 'default'}) async {
final content = capabilitiesJson;
Map capabilities = json.decode(content);
var profile = capabilities['profiles'][name];
if (profile == null) {
throw Exception("The CapabilityProfile '$name' does not exist");
}
List<CodePage> list = [];
profile['codePages'].forEach((k, v) {
list.add(CodePage(int.parse(k), v));
});
// Call the private constructor
return CapabilityProfile._internal(name, list);
}
String name;
List<CodePage> codePages;
int getCodePageId(String? codePage) {
if (codePages == null) {
throw Exception("The CapabilityProfile isn't initialized");
}
return codePages.firstWhere((cp) => cp.name == codePage, orElse: () => throw Exception("Code Page '$codePage' isn't defined for this profile")).id;
}
static Future<List<dynamic>> getAvailableProfiles() async {
final content = capabilitiesJson;
Map capabilities = json.decode(content);
var profiles = capabilities['profiles'];
List<dynamic> res = [];
profiles.forEach((k, v) {
res.add({
'key': k,
'vendor': v['vendor'] is String ? v['vendor'] : '',
'model': v['model'] is String ? v['model'] : '',
'description': v['description'] is String ? v['description'] : '',
});
});
return res;
}
}