/* * RuggedLayer Capture Adapter Starter * Version: 0.1.0 * SPDX-License-Identifier: MIT * * This is an original, vendor-neutral integration pattern. Bind CapturePort to * an approved device SDK in the application that owns the device integration. * No vendor class, binary, credential, customer data or device claim is part * of this file. */ package com.ruggedlayer.integration import java.time.Instant data class CaptureEvent( val value: String, val symbology: String?, val capturedAt: Instant, ) fun interface CaptureListener { fun onCapture(event: CaptureEvent) } interface CapturePort { fun open() fun start() fun stop() fun close() fun setListener(listener: CaptureListener) } class ScanWorkflow(private val scanner: CapturePort) { private var active = false fun begin(listener: CaptureListener) { if (active) return scanner.open() scanner.setListener(listener) scanner.start() active = true } fun end() { if (!active) return runCatching { scanner.stop() } runCatching { scanner.close() } active = false } } class FakeCapturePort : CapturePort { private var listener: CaptureListener? = null override fun open() = Unit override fun start() = Unit override fun stop() = Unit override fun close() = Unit override fun setListener(listener: CaptureListener) { this.listener = listener } fun emit(value: String, symbology: String? = null) { require(value.isNotBlank()) { "A fake capture must contain a value" } listener?.onCapture(CaptureEvent(value, symbology, Instant.now())) } }