-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLibraryBorrowMachine.hx
300 lines (244 loc) · 9.93 KB
/
LibraryBorrowMachine.hx
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package contexts;
import haxe.ds.Option;
import haxe.Timer;
import views.ScreenView.ScreenState;
import Data.LibraryCard;
import Data.LoanItem;
import Data.ScannedItem;
import Data.ReceiptItem;
@:publicFields private class State
{
var pinAttemptsRemaining = LibraryBorrowMachine.maxPinAttempts;
var authorizedCard : Null<LibraryCard>;
final scannedItems : Array<ScannedItem> = [];
public function new() {}
}
/**
* Use case implementation.
* @see https://docs.google.com/spreadsheets/d/1TSpjKUhjvP9pMRukt_mInHVbdQWsXHzFjSymQ3VyGmE/edit#gid=2
*/
class LibraryBorrowMachine implements dci.Context
{
public static final maxPinAttempts = 3;
final state : State;
///// Constructor and System Operations /////
public function new(scanner, cardReader, screen, printer, keypad, finishButtons) {
this.scanner = scanner;
this.cardReader = cardReader;
this.screen = screen;
this.printer = printer;
this.keypad = keypad;
this.finishButtons = finishButtons;
this.state = new State();
this.scannedItems = state.scannedItems;
this.library = Data;
}
public function start() {
resetState();
screen.displayWelcome();
cardReader.waitForCardChange();
}
function restart() {
resetState();
screen.displayThankYouMessage();
cardReader.waitForCardChange();
}
function resetState() {
state.scannedItems.splice(0, state.scannedItems.length);
state.pinAttemptsRemaining = maxPinAttempts;
state.authorizedCard = null;
}
///// Roles /////
@role final cardReader : {
function scanRfid(callback : Option<String> -> Void) : Void;
public function waitForCardChange()
self.scanRfid(self.rfidScanned);
function rfidScanned(data : Option<String>) switch data {
case None:
// No card, keep waiting
self.waitForCardChange();
case Some(rfid):
// Create a wait loop, detecting card removal.
self.createWaitLoopForCardRemoval();
// Look up current card in library database, display pin screen if valid.
switch library.card(rfid) {
case None:
screen.displayInvalidCard();
case Some(card):
screen.displayEnterPin();
}
}
function createWaitLoopForCardRemoval() {
final removeCardLoop = new haxe.Timer(50);
removeCardLoop.run = function() {
self.scanRfid(function(data) {
// An "equals" test is required because data is an Enum.
if(data.equals(None)) {
removeCardLoop.stop();
restart();
}
});
}
}
public function validatePin(pin : String) {
self.scanRfid(function(data) switch data {
case Some(rfid):
switch library.card(rfid) {
case None:
screen.displayInvalidCard();
case Some(card):
if(card.pin == pin) {
// PIN ok, authorize card and move to scanning of items.
state.authorizedCard = card;
screen.displayScannedItems();
scanner.waitForItem();
}
else {
state.pinAttemptsRemaining--;
if(state.pinAttemptsRemaining > 0) {
screen.displayEnterPin();
}
else {
screen.displayTooManyInvalidPin();
}
}
}
case None:
// LibraryCard already removed.
});
}
}
@role final scanner : {
function scanRfid(callback : Option<String> -> Void) : Void;
public function waitForItem()
self.scanRfid(self.readLoanItem);
function readLoanItem(rfid : Option<String>) {
if(state.authorizedCard == null) return;
switch rfid {
case Some(rfid) if(!scannedItems.alreadyScanned(rfid)):
switch library.item(rfid) {
case None:
screen.displayInvalidLoanItemMessage();
self.waitForItem();
case Some(item):
self.borrowLoanItem(item);
}
case _:
self.waitForItem();
}
}
function borrowLoanItem(item : LoanItem) {
if(state.authorizedCard == null) return;
switch new BorrowLoanItem(item, state.authorizedCard).borrow() {
case Ok(loan):
// Emulate a short database connection delay
Timer.delay(function() {
scannedItems.addItem(new ReceiptItem(item, loan.returnDate));
screen.displayScannedItems();
self.waitForItem();
}, Std.random(400) + 100);
return;
case InvalidLoanItem:
screen.displayInvalidLoanItemMessage();
case ItemAlreadyBorrowed:
screen.displayAlreadyBorrowedMessage();
case InvalidBorrower:
// LibraryCard is invalid, don't wait for another item.
screen.displayInvalidCard();
return;
}
self.waitForItem();
}
}
@role final scannedItems : {
function iterator() : Iterator<ScannedItem>;
function push(item : ScannedItem) : Int;
function splice(pos : Int, len : Int) : Iterable<ScannedItem>;
var length(default, null) : Int;
public function addItem(item : ScannedItem)
self.push(item);
public function clearItems()
self.splice(0, self.length);
public function alreadyScanned(rfid : String) : Bool
return self.exists(function(scannedItem) return scannedItem.item.rfid == rfid);
}
@role final screen : {
function display(s : ScreenState) : Void;
function displayMessage(state : ScreenState, waitMs : Int, ?thenDisplay : ScreenState) : Void;
public function displayWelcome()
display(Welcome);
public function displayThankYouMessage()
displayMessage(ThankYou, 4000, Welcome);
public function displayEnterPin() {
// Listen to keypad event
keypad.waitForEnterPin();
display(EnterPin({
previousAttemptFailed: state.pinAttemptsRemaining < maxPinAttempts
}));
}
public function displayScannedItems() {
// Listen to finish button events
finishButtons.waitForFinishClick();
display(DisplayBorrowedItems(scannedItems));
}
public function displayTooManyInvalidPin()
display(TooManyInvalidPin);
public function displayInvalidCard()
display(InvalidCard);
public function displayInvalidLoanItemMessage()
displayMessage(InvalidLoanItem, 100, DisplayBorrowedItems(scannedItems));
public function displayAlreadyBorrowedMessage()
displayMessage(ItemAlreadyBorrowed, 3000);
public function displayDontForgetLibraryCard()
display(DontForgetLibraryCard);
}
@role final finishButtons : {
function onFinishWithoutReceiptClicked(callback : Void -> Void, ?pos : haxe.PosInfos) : Void;
function onFinishWithReceiptClicked(callback : Void -> Void, ?pos : haxe.PosInfos) : Void;
public function waitForFinishClick() {
self.onFinishWithoutReceiptClicked(screen.displayDontForgetLibraryCard);
self.onFinishWithReceiptClicked(printer.printReceipt);
}
}
@role final keypad : {
function onPinCodeEntered(callback : String -> Void, ?pos : haxe.PosInfos) : Void;
public function waitForEnterPin() {
self.onPinCodeEntered(cardReader.validatePin);
}
}
@role final printer : {
function print(line : String) : Void;
function cutPaper() : Void;
public function printReceipt() : Void {
var buffer = [Date.now().format("%Y-%m-%d"), ""];
for(scanned in scannedItems) {
buffer.push(scanned.item.title);
buffer.push("Return on " + scanned.returnDate.format("%Y-%m-%d"));
buffer.push("");
}
var timer = new Timer(80);
timer.run = function() {
final current = buffer.pop();
if(current == null) {
timer.stop();
self.cutPaper();
screen.displayDontForgetLibraryCard();
} else {
self.print(current);
}
}
}
}
@role final library : {
var libraryItems(default, null) : Array<LoanItem>;
var libraryCards(default, null) : Array<LibraryCard>;
public function item(rfid : String) : Option<LoanItem> {
final libraryItem = self.libraryItems.find(loanItem -> loanItem.rfid == rfid);
return libraryItem == null ? None : Some(libraryItem);
}
public function card(rfid : String) : Option<LibraryCard> {
final libraryCard = libraryCards.find(libraryCard -> libraryCard.rfid == rfid);
return libraryCard == null ? None : Some(libraryCard);
}
}
}