-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHooks.php
More file actions
149 lines (130 loc) · 5.72 KB
/
Copy pathHooks.php
File metadata and controls
149 lines (130 loc) · 5.72 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
<?php
namespace Modules\ExampleShowcase;
use App\Events\Batch\BatchCreated;
use App\Events\BatchStep\StepCompleted;
use App\Events\BatchStep\StepStarted;
use App\Events\Machine\WorkstationStateChanged;
use App\Events\MachineMessageReceived;
use App\Events\Resource\ResourceChanged;
use App\Events\Schedule\WorkOrderScheduled;
use App\Events\User\UserAssignedToLine;
use App\Events\WorkOrder\WorkOrderCompleted;
use App\Events\WorkOrder\WorkOrderCreated;
use App\Events\WorkOrder\WorkOrderUpdated;
use Illuminate\Support\Facades\Log;
/**
* Every event hook OpenMES fires, in one place — PrestaShop-style: one method
* per hook (think hookActionOrderStatusUpdate()). The service provider wires each
* domain event to the matching method here.
*
* IMPORTANT — stay non-intrusive: these bodies only READ the payload and log.
* A module must never mutate core state from inside an event handler; doing so
* turns an observer into a hidden participant in the flow and can corrupt the
* very process it is watching (double counts, re-entrant events, broken
* transactions). Replace the log calls with your own SIDE effects — notify a
* webhook, push to an external ERP, write to your module's own tables — but
* leave the core objects untouched.
*/
class Hooks
{
private function log(string $hook, array $context = []): void
{
Log::channel('daily')->info("[ExampleShowcase] {$hook}", $context);
}
// ── Work orders ──────────────────────────────────────────────────────────
/** Fired right after a work order is created (any source: UI, CSV, ERP API). */
public function onWorkOrderCreated(WorkOrderCreated $e): void
{
$this->log('workOrderCreated', [
'order_no' => $e->workOrder->order_no,
'planned_qty' => $e->workOrder->planned_qty,
'line_id' => $e->workOrder->line_id,
]);
}
/** Fired after an existing work order is edited. `changes` is the dirty set. */
public function onWorkOrderUpdated(WorkOrderUpdated $e): void
{
$this->log('workOrderUpdated', [
'order_no' => $e->workOrder->order_no,
'changed' => array_keys($e->changes),
]);
}
/** Fired the first time a work order reaches DONE (idempotent upstream). */
public function onWorkOrderCompleted(WorkOrderCompleted $e): void
{
$this->log('workOrderCompleted', [
'order_no' => $e->workOrder->order_no,
'produced_qty' => $e->workOrder->produced_qty,
]);
}
// ── Batches & steps ──────────────────────────────────────────────────────
/** Fired when a production batch is created for a work order. */
public function onBatchCreated(BatchCreated $e): void
{
$this->log('batchCreated', ['batch_id' => $e->batch->id]);
}
/** Fired when an operator (or machine) starts a batch step. */
public function onStepStarted(StepStarted $e): void
{
$this->log('stepStarted', [
'batch_step_id' => $e->batchStep->id,
'name' => $e->batchStep->name,
]);
}
/** Fired when a batch step is completed. */
public function onStepCompleted(StepCompleted $e): void
{
$this->log('stepCompleted', [
'batch_step_id' => $e->batchStep->id,
'name' => $e->batchStep->name,
]);
}
// ── Machine / shop-floor ─────────────────────────────────────────────────
/** Fired when the signal pipeline transitions a workstation to a new state. */
public function onWorkstationStateChanged(WorkstationStateChanged $e): void
{
$this->log('workstationStateChanged', [
'workstation' => $e->workstation->code,
'from' => $e->from,
'to' => $e->to,
]);
}
/** Fired for every inbound MQTT message once it has been parsed/logged. */
public function onMachineMessageReceived(MachineMessageReceived $e): void
{
$this->log('machineMessageReceived', [
'topic' => $e->message->topic,
'status' => $e->message->processing_status,
]);
}
// ── Users ────────────────────────────────────────────────────────────────
/** Fired when a user is assigned to a production line. */
public function onUserAssignedToLine(UserAssignedToLine $e): void
{
$this->log('userAssignedToLine', [
'user_id' => $e->user->id,
'line_id' => $e->line->id,
]);
}
// ── Generic CRUD + scheduling ────────────────────────────────────────────
/**
* Fired for every create/update/delete of a curated resource (work orders,
* customers, materials, lines, …). Filter by model class and/or action.
*/
public function onResourceChanged(ResourceChanged $e): void
{
$this->log('resourceChanged', [
'type' => $e->type(),
'id' => $e->model->getKey(),
'action' => $e->action,
]);
}
/** Fired when a work order's placement changes on the planner. */
public function onWorkOrderScheduled(WorkOrderScheduled $e): void
{
$this->log('workOrderScheduled', [
'order_no' => $e->workOrder->order_no,
'changed' => array_keys($e->changes),
]);
}
}