-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstallation.java
More file actions
196 lines (169 loc) · 6.36 KB
/
Copy pathInstallation.java
File metadata and controls
196 lines (169 loc) · 6.36 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
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
package devices.installation;
import devices.configuration.management.Coordinates;
import devices.configuration.management.Location;
import devices.configuration.management.Ownership;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
@AllArgsConstructor
class Installation {
final WorkOrderId workOrderId;
final List<InstallationEvent> events;
private InstallationPhase phase;
private InstallerId assignedInstallerId;
private final Ownership ownership;
private final InstallationContext context;
private DeviceIdentifier deviceIdentifier;
private BootDetails lastRelevantBoot;
private boolean bootConfirmed;
private Coordinates location;
private boolean allMandatorySatAnswered;
static Installation fromWorkOrder(
WorkOrderId workOrderId, InstallationContext context, Ownership ownership) {
Installation installation =
new Installation(
workOrderId,
new ArrayList<>(),
InstallationPhase.INITIAL,
null,
ownership,
context,
null,
null,
false,
null,
false);
installation.events.add(
new InstallationEvent.WorkOrderReceived(workOrderId, context, ownership));
return installation;
}
void assignInstaller(InstallerId installerId, boolean force) {
Objects.requireNonNull(installerId, "InstallerId cannot be null");
if (this.assignedInstallerId != null && !force) {
throw new IllegalStateException("Installer already assigned. Use force=true to reassign.");
}
if (this.assignedInstallerId != null && force) {
this.assignedInstallerId = installerId;
events.add(new InstallationEvent.InstallerReassigned(workOrderId, installerId, true));
} else {
this.assignedInstallerId = installerId;
events.add(new InstallationEvent.InstallerAssigned(workOrderId, installerId));
}
}
void start() {
if (assignedInstallerId == null) {
throw new IllegalStateException("Cannot start installation without assigned installer");
}
if (phase != InstallationPhase.INITIAL) {
throw new IllegalStateException("Installation already started");
}
events.add(new InstallationEvent.InstallationStarted(workOrderId));
}
void assignDevice(DeviceIdentifier newDeviceIdentifier) {
Objects.requireNonNull(newDeviceIdentifier, "DeviceIdentifier cannot be null");
if (this.deviceIdentifier != null && !this.deviceIdentifier.equals(newDeviceIdentifier)) {
// Device swap - invalidate boot confirmation
DeviceIdentifier oldDevice = this.deviceIdentifier;
this.deviceIdentifier = newDeviceIdentifier;
this.lastRelevantBoot = null;
this.bootConfirmed = false;
this.phase = InstallationPhase.DEVICE_ASSIGNED;
events.add(new InstallationEvent.DeviceSwapped(workOrderId, oldDevice, newDeviceIdentifier));
} else if (this.deviceIdentifier == null) {
this.deviceIdentifier = newDeviceIdentifier;
this.phase = InstallationPhase.DEVICE_ASSIGNED;
events.add(new InstallationEvent.DeviceAssigned(workOrderId, newDeviceIdentifier));
}
// If same device - idempotent, no event
}
void registerBootNotification(BootDetails bootDetails) {
Objects.requireNonNull(bootDetails, "BootDetails cannot be null");
if (deviceIdentifier == null) {
throw new IllegalStateException("Cannot register boot without assigned device");
}
boolean requiresConfirmation = isBootConfirmationRequired(bootDetails);
if (requiresConfirmation) {
this.lastRelevantBoot = bootDetails;
this.bootConfirmed = false;
this.phase = InstallationPhase.BOOT_CONFIRMATION_REQUIRED;
events.add(new InstallationEvent.BootNotificationReceived(workOrderId, bootDetails));
}
// If not "relevant" (no hardware/firmware change), ignore silently
}
private boolean isBootConfirmationRequired(BootDetails newBoot) {
if (lastRelevantBoot == null) {
return true; // First boot always requires confirmation
}
// Re-confirm if hardware changed or firmware changed
return lastRelevantBoot.isDifferentHardwareFrom(newBoot)
|| lastRelevantBoot.hasDifferentFirmwareFrom(newBoot);
}
void confirmBoot() {
if (lastRelevantBoot == null) {
throw new IllegalStateException("No boot notification to confirm");
}
if (bootConfirmed) {
return; // Idempotent
}
this.bootConfirmed = true;
this.phase = InstallationPhase.BOOT_CONFIRMED;
events.add(new InstallationEvent.BootConfirmed(workOrderId, lastRelevantBoot));
}
void setLocation(Coordinates coordinates) {
Objects.requireNonNull(coordinates, "Coordinates cannot be null");
if (!Objects.equals(this.location, coordinates)) {
this.location = coordinates;
this.phase = InstallationPhase.LOCATION_SET;
events.add(new InstallationEvent.LocationSet(workOrderId, coordinates));
}
}
void markSatAnswersComplete() {
this.allMandatorySatAnswered = true;
}
void finish() {
if (!canFinish()) {
throw new IllegalStateException(
"Cannot finish installation. Missing: " + getMissingRequirements());
}
this.phase = InstallationPhase.FINISHED;
// Build full location from coordinates (minimal for now, can be extended)
Location fullLocation = null;
if (location != null) {
fullLocation = Location.of(null, null, null, null, null, null, location);
}
events.add(
new InstallationEvent.InstallationFinished(
workOrderId, deviceIdentifier, ownership, fullLocation, lastRelevantBoot));
}
private boolean canFinish() {
return allMandatorySatAnswered && location != null && bootConfirmed;
}
private String getMissingRequirements() {
List<String> missing = new ArrayList<>();
if (!allMandatorySatAnswered) {
missing.add("all mandatory SAT answers");
}
if (location == null) {
missing.add("precise location");
}
if (!bootConfirmed) {
missing.add("boot confirmation");
}
return String.join(", ", missing);
}
InstallationSnapshot toSnapshot() {
return new InstallationSnapshot(
workOrderId,
phase,
assignedInstallerId,
ownership,
context,
deviceIdentifier,
lastRelevantBoot,
bootConfirmed,
location,
allMandatorySatAnswered,
canFinish());
}
}