// Define a maintenance workflow
WorkflowRegistry.register({
name: "TurbineMaintenanceWorkflow",
description: "Coordinates the maintenance process for a wind turbine",
steps: [
{
id: "initiate",
type: "start",
next: "assessUrgency"
},
{
id: "assessUrgency",
type: "decision",
decision: async (context) => {
// Get the turbine
const turbine = await WindTurbine.fetch(context.turbineId);
// Get the latest sensor readings
const readings = await SensorReading.filter({
turbine: { id: turbine.id }
}).orderBy({ timestamp: "DESC" }).limit(1).fetch();
// Assess urgency based on sensor readings
if (readings.length > 0 && readings[0].vibration > 10) {
return "urgent";
} else {
return "routine";
}
},
branches: {
urgent: "scheduleEmergencyMaintenance",
routine: "scheduleRoutineMaintenance"
}
},
{
id: "scheduleEmergencyMaintenance",
type: "task",
task: async (context) => {
// Create an emergency maintenance record
const record = await MaintenanceRecord.make({
turbine: { id: context.turbineId },
type: "EMERGENCY",
status: "SCHEDULED",
priority: "HIGH",
scheduledDate: new Date(), // Immediate
description: "Emergency maintenance required due to high vibration"
}).save();
// Update the context with the record ID
context.maintenanceRecordId = record.id;
return context;
},
next: "assignTechnician"
},
{
id: "scheduleRoutineMaintenance",
type: "task",
task: async (context) => {
// Create a routine maintenance record
const record = await MaintenanceRecord.make({
turbine: { id: context.turbineId },
type: "ROUTINE",
status: "SCHEDULED",
priority: "NORMAL",
scheduledDate: addDays(new Date(), 7), // Schedule for next week
description: "Routine maintenance"
}).save();
// Update the context with the record ID
context.maintenanceRecordId = record.id;
return context;
},
next: "assignTechnician"
},
{
id: "assignTechnician",
type: "human_task",
assignee: "maintenance_manager",
form: "TechnicianAssignmentForm",
timeout: 86400, // 24 hours in seconds
next: "performMaintenance"
},
{
id: "performMaintenance",
type: "human_task",
assignee: (context) => context.technicianId,
form: "MaintenanceChecklistForm",
timeout: 259200, // 3 days in seconds
next: "verifyMaintenance"
},
{
id: "verifyMaintenance",
type: "task",
task: async (context) => {
// Get the maintenance record
const record = await MaintenanceRecord.fetch(context.maintenanceRecordId);
// Update the record status
record.status = "COMPLETED";
record.completionDate = new Date();
record.completionNotes = context.maintenanceNotes;
// Save the updated record
await record.save();
// Get the turbine
const turbine = await WindTurbine.fetch(context.turbineId);
// Update the turbine status
turbine.status = "OPERATIONAL";
turbine.lastMaintenanceDate = new Date();
// Save the updated turbine
await turbine.save();
// Emit a maintenance completed event
EventBus.emit("MAINTENANCE_COMPLETED", {
turbineId: context.turbineId,
maintenanceRecordId: context.maintenanceRecordId,
completionDate: new Date()
});
return context;
},
next: "complete"
},
{
id: "complete",
type: "end"
}
]
});