Skip to content

Commit a8fa782

Browse files
committed
Use adapter for beam mode
1 parent d9fcf08 commit a8fa782

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
lines changed

src/alice/dip/LhcInfoObj.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,6 @@ public String getBeamMode() {
317317
return last.value;
318318
}
319319

320-
/**
321-
* Get the beam mode as a key suitable for enum conversion usage in protobuf
322-
* @return Beam mode string with spaces replaced by underscores, or "UNKNOWN" if beam mode is null
323-
*/
324-
public String getBeamModeAsKey() {
325-
String bm = getBeamMode();
326-
if (bm == null) {
327-
return "UNKNOWN";
328-
}
329-
bm = bm.replace(" ", "_");
330-
return bm;
331-
}
332-
333320
public String getStableBeamStartStr() {
334321

335322
long t = getStableBeamStart();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
package alice.dip.adapters;
15+
16+
import alice.dip.enums.BeamModeEnum;
17+
18+
/**
19+
* Adapter class to convert between string representations of beam modes and the BeamModeEnum.
20+
*/
21+
public class BeamModeProtoAdapter {
22+
23+
/**
24+
* Returns the enum constant matching the given string, or UNKNOWN if not found.
25+
* Accepts both space and underscore separated names, case-insensitive.
26+
* @param beamMode The beam mode string to convert.
27+
* @return The corresponding BeamModeEnum constant, or UNKNOWN if not recognized.
28+
*/
29+
public static BeamModeEnum fromStringToEnum(String beamMode) {
30+
if (beamMode == null || beamMode.trim().isEmpty()) {
31+
return BeamModeEnum.UNKNOWN;
32+
}
33+
for (BeamModeEnum value : BeamModeEnum.values()) {
34+
if (value.label.equalsIgnoreCase(beamMode)) {
35+
return value;
36+
}
37+
}
38+
return BeamModeEnum.UNKNOWN;
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
package alice.dip.enums;
15+
16+
/**
17+
* Java enum matching the BeamMode values from DIP service and common.proto
18+
* @enum BeamModeEnum
19+
*/
20+
public enum BeamModeEnum {
21+
UNKNOWN("UNKNOWN"),
22+
SETUP("SETUP"),
23+
ABORT("ABORT"),
24+
INJECTION_PROBE_BEAM("INJECTION PROBE BEAM"),
25+
INJECTION_SETUP_BEAM("INJECTION SETUP BEAM"),
26+
INJECTION_PHYSICS_BEAM("INJECTION PHYSICS BEAM"),
27+
PREPARE_RAMP("PREPARE RAMP"),
28+
RAMP("RAMP"),
29+
FLAT_TOP("FLAT TOP"),
30+
SQUEEZE("SQUEEZE"),
31+
ADJUST("ADJUST"),
32+
STABLE_BEAMS("STABLE BEAMS"),
33+
LOST_BEAMS("LOST BEAMS"),
34+
UNSTABLE_BEAMS("UNSTABLE BEAMS"),
35+
BEAM_DUMP_WARNING("BEAM DUMP WARNING"),
36+
BEAM_DUMP("BEAM DUMP"),
37+
RAMP_DOWN("RAMP DOWN"),
38+
CYCLING("CYCLING"),
39+
RECOVERY("RECOVERY"),
40+
INJECT_AND_DUMP("INJECT AND DUMP"),
41+
CIRCULATE_AND_DUMP("CIRCULATE AND DUMP"),
42+
NO_BEAM("NO BEAM");
43+
44+
public final String label;
45+
46+
private BeamModeEnum(String label) {
47+
this.label = label;
48+
}
49+
}

src/alice/dip/kafka/BeamModeEventsKafkaProducer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import org.apache.kafka.common.serialization.ByteArraySerializer;
1717
import org.apache.kafka.common.serialization.IntegerSerializer;
1818

19+
import alice.dip.adapters.BeamModeProtoAdapter;
1920
import alice.dip.AliDip2BK;
20-
import alice.dip.LhcInfoObj;
21+
import alice.dip.enums.BeamModeEnum;
2122
import alice.dip.kafka.dto.Common;
2223
import alice.dip.kafka.dto.Events;
24+
import alice.dip.LhcInfoObj;
2325

2426
/**
2527
* Kafka producer for LHC Beam Mode events, serialized using Protocol Buffers.
@@ -44,12 +46,15 @@ public BeamModeEventsKafkaProducer(String bootstrapServers) {
4446
* @param timestamp - event timestamp at which the beam mode change event was received from DIP
4547
*/
4648
public void sendEvent(Integer fillNumber, LhcInfoObj fill, long timestamp) {
49+
String beamModeStr = fill.getBeamMode();
50+
BeamModeEnum beamMode = BeamModeProtoAdapter.fromStringToEnum(beamModeStr);
51+
4752
Common.BeamInfo beamInfo = Common.BeamInfo.newBuilder()
4853
.setStableBeamsStart(fill.getStableBeamStart())
4954
.setStableBeamsEnd(fill.getStableBeamStop())
5055
.setFillNumber(fill.fillNo)
5156
.setFillingSchemeName(fill.LHCFillingSchemeName)
52-
.setBeamMode(Common.BeamMode.valueOf(fill.getBeamModeAsKey()))
57+
.setBeamMode(Common.BeamMode.valueOf(beamMode.label))
5358
.setBeamType(fill.beamType)
5459
.build();
5560

0 commit comments

Comments
 (0)