forked from microsoft/coyote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeMachine.cs
486 lines (419 loc) · 16.5 KB
/
CoffeeMachine.cs
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Coyote.Samples.Common;
using Microsoft.Coyote.Specifications;
namespace Microsoft.Coyote.Samples.CoffeeMachineTasks
{
/// <summary>
/// This async interface is provided by the CoffeeMachine.
/// </summary>
internal interface ICoffeeMachine
{
/// <summary>
/// Initialize the coffee machine, checking the Sensors to make sure we're good to go.
/// </summary>
/// <param name="sensors">The persistent sensor state.</param>
/// <returns>True if everything looks good, false if we cannot make coffee at this time.</returns>
Task<bool> InitializeAsync(ISensors sensors);
/// <summary>
/// Make a coffee. This is a long running async operation.
/// </summary>
/// <param name="shots">The number of espresso shots.</param>
/// <returns>An async task that can be controlled by Coyote tester containing an optional string error message.</returns>
Task<string> MakeCoffeeAsync(int shots);
/// <summary>
/// Reboot the coffee machine!
/// </summary>
/// <returns>An async task.</returns>
Task TerminateAsync();
}
/// <summary>
/// Implementation of the ICoffeeMachine interface.
/// </summary>
internal class CoffeeMachine : ICoffeeMachine
{
private readonly object SyncObject = new object();
private bool Initialized;
private ISensors Sensors;
private bool Heating;
private double? WaterLevel;
private double? HopperLevel;
private bool? DoorOpen;
private double? PortaFilterCoffeeLevel;
private double? WaterTemperature;
private int ShotsRequested;
private double PreviousShotCount;
private bool RefillRequired;
private string Error;
private bool Halted;
private TaskCompletionSource<bool> ShotCompleteSource;
private readonly LogWriter Log = LogWriter.Instance;
public async Task<bool> InitializeAsync(ISensors sensors)
{
this.Log.WriteLine("initializing...");
lock (this.SyncObject)
{
this.Sensors = sensors;
this.RegisterSensorEvents(false);
this.RegisterSensorEvents(true);
}
await this.CheckSensors();
this.Initialized = !this.RefillRequired && string.IsNullOrEmpty(this.Error);
return this.Initialized;
}
public async Task<string> MakeCoffeeAsync(int shots)
{
if (!this.Initialized)
{
throw new Exception("Please make sure InitializeAsync returns true.");
}
if (this.Halted)
{
return "Ignoring MakeCoffeeAsync on halted Coffee machine";
}
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.BusyEvent());
if (!this.RefillRequired && !this.Halted)
{
// Make sure water is hot enough.
await this.StartHeatingWater();
}
this.Log.WriteLine($"Coffee requested, shots={shots}");
this.ShotsRequested = shots;
// Grind beans until porta filter is full. Turn on shot button for desired time dump the
// grinds, while checking for error conditions, e.g. out of water or coffee beans.
if (!this.RefillRequired && !this.Halted)
{
await this.GrindBeans();
}
if (!this.RefillRequired && !this.Halted)
{
await this.MakeShotsAsync();
}
await this.CleanupAsync();
if (this.Halted)
{
return "<halted>";
}
return this.Error;
}
public async Task CheckSensors()
{
this.Log.WriteLine("checking initial state of sensors...");
// When this state machine starts it has to figure out the state of the sensors.
if (!await this.Sensors.GetPowerSwitchAsync())
{
// Coffee machine was off, so this is the easy case, simply turn it on!
await this.Sensors.SetPowerSwitchAsync(true);
}
// Make sure grinder, shot maker and water heater are off.
await this.Sensors.SetGrinderButtonAsync(false);
await this.Sensors.SetShotButtonAsync(false);
await this.Sensors.SetWaterHeaterButtonAsync(false);
// Need to check water and hopper levels and if the porta filter
// has coffee in it we need to dump those grinds.
await this.CheckWaterLevelAsync();
await this.CheckHopperLevelAsync();
await this.CheckPortaFilterCoffeeLevelAsync();
await this.CheckDoorOpenAsync();
}
private async Task CheckWaterLevelAsync()
{
this.WaterLevel = await this.Sensors.GetWaterLevelAsync();
this.Log.WriteLine("Water level is {0} %", (int)this.WaterLevel.Value);
if ((int)this.WaterLevel.Value <= 0)
{
this.OnRefillRequired("is out of water");
}
}
private async Task CheckHopperLevelAsync()
{
this.HopperLevel = await this.Sensors.GetHopperLevelAsync();
this.Log.WriteLine("Hopper level is {0} %", (int)this.HopperLevel.Value);
if ((int)this.HopperLevel.Value == 0)
{
this.OnRefillRequired("out of coffee beans");
}
}
private async Task CheckPortaFilterCoffeeLevelAsync()
{
this.PortaFilterCoffeeLevel = await this.Sensors.GetPortaFilterCoffeeLevelAsync();
if (this.PortaFilterCoffeeLevel > 0)
{
// Dump these grinds because they could be old, we have no idea how long
// the coffee machine was off (no real time clock sensor).
this.Log.WriteLine("Dumping old smelly grinds!");
await this.Sensors.SetDumpGrindsButtonAsync(true);
}
}
private async Task CheckDoorOpenAsync()
{
this.DoorOpen = await this.Sensors.GetReadDoorOpenAsync();
if (this.DoorOpen.Value != false)
{
this.Log.WriteLine("Cannot safely operate coffee machine with the door open!");
this.OnError();
}
}
private async Task StartHeatingWater()
{
if (!this.Halted)
{
// Start heater and keep monitoring the water temp till it reaches 100!
this.Log.WriteLine("Warming the water to 100 degrees");
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.BusyEvent());
await this.MonitorWaterTemperature();
}
else
{
this.Log.WriteLine("Ignoring StartHeatingWater on a Halted Coffee machine");
}
}
private async Task OnWaterHot()
{
this.Log.WriteLine("Coffee machine water temperature is now 100");
if (this.Heating)
{
this.Heating = false;
// Turn off the heater so we don't overheat it!
await this.Sensors.SetWaterHeaterButtonAsync(false);
this.Log.WriteLine("Turning off the water heater");
}
this.OnReady();
}
private async Task MonitorWaterTemperature()
{
while (!this.IsBroken)
{
this.WaterTemperature = await this.Sensors.GetWaterTemperatureAsync();
if (this.WaterTemperature.Value >= 100)
{
await this.OnWaterHot();
break;
}
else
{
if (!this.Heating)
{
this.Heating = true;
// Turn on the heater and wait for WaterHotEvent.
this.Log.WriteLine("Turning on the water heater");
await this.Sensors.SetWaterHeaterButtonAsync(true);
}
}
this.Log.WriteLine("Coffee machine is warming up ({0} degrees)...", this.WaterTemperature);
await Task.Delay(TimeSpan.FromSeconds(0.1));
}
}
private void OnReady()
{
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.IdleEvent());
this.Log.WriteLine("Coffee machine is ready to make coffee (green light is on)");
}
private async Task GrindBeans()
{
// Grind beans until porta filter is full.
this.Log.WriteLine("Grinding beans...");
// Turn on the grinder!
await this.Sensors.SetGrinderButtonAsync(true);
// We now receive a stream of PortaFilterCoffeeLevelChanged events so we keep monitoring
// the porta filter till it is full, and the bean level in case we get empty.
await this.MonitorPortaFilter();
}
private async Task MonitorPortaFilter()
{
while (this.PortaFilterCoffeeLevel < 100 && !this.RefillRequired && !this.IsBroken)
{
await Task.Delay(TimeSpan.FromSeconds(0.1));
}
}
private async Task OnHopperEmpty()
{
await this.Sensors.SetGrinderButtonAsync(false);
this.OnRefillRequired("out of coffee beans");
}
private Task MakeShotsAsync()
{
// Pour the shots.
this.Log.WriteLine("Making shots...");
// First we assume user placed a new cup in the machine, and so the shot count is zero.
this.PreviousShotCount = 0;
// Wait for shots to be completed.
return this.MonitorShotsAsync();
}
private async Task MonitorShotsAsync()
{
try
{
while (!this.IsBroken)
{
this.Log.WriteLine("Shot count is {0}", this.PreviousShotCount);
// So we can wait for async event to come back from the sensors.
var completion = new TaskCompletionSource<bool>();
this.ShotCompleteSource = completion;
// Request another shot!
await this.Sensors.SetShotButtonAsync(true);
if (!this.IsBroken)
{
await completion.Task;
if (!this.IsBroken)
{
this.PreviousShotCount++;
if (this.PreviousShotCount >= this.ShotsRequested && !this.IsBroken)
{
this.Log.WriteLine("{0} shots completed and {1} shots requested!", this.PreviousShotCount, this.ShotsRequested);
if (this.PreviousShotCount > this.ShotsRequested)
{
Specification.Assert(false, "Made the wrong number of shots");
}
break;
}
}
}
}
}
catch (OperationCanceledException)
{
// Cancelled.
}
}
private Task CleanupAsync()
{
// Dump the grinds.
this.Log.WriteLine("Dumping the grinds!");
return this.Sensors.SetDumpGrindsButtonAsync(true);
}
private void OnRefillRequired(string message)
{
this.Error = message;
this.RefillRequired = true;
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.IdleEvent());
this.Log.WriteError(message);
}
private void OnError()
{
this.Error = "Coffee machine needs fixing!";
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.IdleEvent());
this.Log.WriteError(this.Error);
}
public async Task TerminateAsync()
{
this.Halted = true;
this.Log.WriteLine("Coffee Machine Terminating...");
var sensors = this.Sensors;
if (sensors != null)
{
await sensors.SetPowerSwitchAsync(false);
}
var src = this.ShotCompleteSource;
if (src != null)
{
src.TrySetCanceled();
}
// Stop listening to the sensors.
this.RegisterSensorEvents(false);
Specification.Monitor<LivenessMonitor>(new LivenessMonitor.IdleEvent());
this.Log.WriteWarning("#################################################################");
this.Log.WriteWarning("# Coffee Machine Halted #");
this.Log.WriteWarning("#################################################################");
this.Log.WriteLine(string.Empty);
}
private void RegisterSensorEvents(bool register)
{
if (register)
{
this.Sensors.HopperEmpty += this.OnHopperEmpty;
this.Sensors.PortaFilterCoffeeLevelChanged += this.OnPortaFilterCoffeeLevelChanged;
this.Sensors.ShotComplete += this.OnShotComplete;
this.Sensors.WaterEmpty += this.OnWaterEmpty;
this.Sensors.WaterHot += this.OnWaterHot;
this.Sensors.WaterTemperatureChanged += this.OnWaterTemperatureChanged;
}
else
{
this.Sensors.HopperEmpty -= this.OnHopperEmpty;
this.Sensors.PortaFilterCoffeeLevelChanged -= this.OnPortaFilterCoffeeLevelChanged;
this.Sensors.ShotComplete -= this.OnShotComplete;
this.Sensors.WaterEmpty -= this.OnWaterEmpty;
this.Sensors.WaterHot -= this.OnWaterHot;
this.Sensors.WaterTemperatureChanged -= this.OnWaterTemperatureChanged;
}
}
private void OnWaterTemperatureChanged(object sender, double level)
{
}
private void OnWaterHot(object sender, bool value)
{
if (!this.IsBroken)
{
Task.Run(this.OnWaterHot);
}
}
private void OnWaterEmpty(object sender, bool e)
{
if (!this.IsBroken)
{
// Turn off the water pump.
Task.Run(async () =>
{
await this.Sensors.SetShotButtonAsync(false);
});
this.OnRefillRequired("Water is empty!");
}
}
private void OnShotComplete(object sender, bool value)
{
if (!this.IsBroken && this.ShotCompleteSource != null)
{
try
{
this.ShotCompleteSource.SetResult(value);
}
catch (InvalidOperationException)
{
// Cancelled.
}
}
}
private void OnPortaFilterCoffeeLevelChanged(object sender, double level)
{
if (!this.IsBroken)
{
if (level >= 100)
{
this.PortaFilterCoffeeLevel = level;
this.Log.WriteLine("PortaFilter is full");
}
else
{
if (level != this.PortaFilterCoffeeLevel)
{
this.PortaFilterCoffeeLevel = level;
this.Log.WriteLine("PortaFilter is {0} % full", (int)level);
}
}
}
Task.Run(this.UpdatePortaFilterLevelAsync);
}
private Task UpdatePortaFilterLevelAsync()
{
if (this.PortaFilterCoffeeLevel >= 100)
{
return this.Sensors.SetGrinderButtonAsync(false);
}
return Task.CompletedTask;
}
private void OnHopperEmpty(object sender, bool value)
{
if (!this.IsBroken)
{
var nowait = this.OnHopperEmpty();
}
}
private bool IsBroken
{
get { return this.Halted || !string.IsNullOrEmpty(this.Error); }
}
}
}