|
|
@@ -0,0 +1,56 @@
|
|
|
+package com.zksy.common.utils;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.util.function.BooleanSupplier;
|
|
|
+
|
|
|
+/** 采集入库后的独立操作边界。失败不能被解释为采集失败或设备恢复正常。 */
|
|
|
+public final class CollectionSideEffects {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(CollectionSideEffects.class);
|
|
|
+
|
|
|
+ private CollectionSideEffects() { }
|
|
|
+
|
|
|
+ @FunctionalInterface
|
|
|
+ public interface Operation {
|
|
|
+ void run() throws Exception;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean run(String deviceCode, String stage, Operation action) {
|
|
|
+ try {
|
|
|
+ action.run();
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("采集后处理失败,继续其他操作: deviceCode={}, stage={}", deviceCode, stage, e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** null 表示评估失败;与已评估、未报警的 false 区分。 */
|
|
|
+ public static Boolean evaluate(String deviceCode, String stage, BooleanSupplier action) {
|
|
|
+ try {
|
|
|
+ return action.getAsBoolean();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("报警评估失败,保留原状态: deviceCode={}, stage={}", deviceCode, stage, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 有确定报警即可置 1;只有所有指标均评估正常才允许清零。 */
|
|
|
+ public static Integer alarmStatus(Boolean... results) {
|
|
|
+ boolean incomplete = results.length == 0;
|
|
|
+ for (Boolean result : results) {
|
|
|
+ if (Boolean.TRUE.equals(result)) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ incomplete |= result == null;
|
|
|
+ }
|
|
|
+ return incomplete ? null : Integer.valueOf(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void requireSaved(boolean saved) {
|
|
|
+ if (!saved) {
|
|
|
+ throw new IllegalStateException("数据库操作未成功:影响行数为0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|