Keyple Service C++ Library - 3.4.1
Component of the Keyple C++ middleware
CardInsertionPassiveMonitoringJobAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/core/service/CardInsertionPassiveMonitoringJobAdapter.hpp"
15
16#include <memory>
17#include <string>
18
19#include "keyple/core/plugin/ReaderIOException.hpp"
20#include "keyple/core/plugin/TaskCanceledException.hpp"
21#include "keyple/core/plugin/spi/reader/observable/state/insertion/CardInsertionWaiterBlockingSpi.hpp"
22#include "keyple/core/plugin/spi/reader/observable/state/insertion/WaitForCardInsertionBlockingSpi.hpp"
23#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
24
25namespace keyple {
26namespace core {
27namespace service {
28
29using keyple::core::plugin::ReaderIOException;
30using keyple::core::plugin::TaskCanceledException;
31using keyple::core::plugin::spi::reader::observable::state::insertion::
32 CardInsertionWaiterBlockingSpi;
33using keyple::core::plugin::spi::reader::observable::state::insertion::
34 WaitForCardInsertionBlockingSpi;
35using keyple::core::util::cpp::exception::RuntimeException;
36
38
39const std::string CardInsertionPassiveMonitoringJobAdapter::JOB_ID
40 = "INSERTION_PASSIVE";
41
42CardInsertionPassiveMonitoringJobAdapter::CardInsertionPassiveMonitoringJob ::
43 CardInsertionPassiveMonitoringJob(
44 std::shared_ptr<AbstractObservableStateAdapter> monitoringState,
45 CardInsertionPassiveMonitoringJobAdapter* parent)
46: Job("CardInsertionPassiveMonitoringJob")
47, mMonitoringState(monitoringState)
48, mParent(parent)
49{
50}
51
52void
53CardInsertionPassiveMonitoringJobAdapter::CardInsertionPassiveMonitoringJob::
54 execute()
55{
56 try {
57 mParent->mLogger->trace(
58 "[fsmJob=%, reader=%] Starting monitoring job process\n",
59 JOB_ID,
60 mParent->getReader()->getName());
61
62 const auto& cardInsertion
63 = std::dynamic_pointer_cast<CardInsertionWaiterBlockingSpi>(
64 mParent->mReaderSpi);
65 const auto& waitForCard
66 = std::dynamic_pointer_cast<WaitForCardInsertionBlockingSpi>(
67 mParent->mReaderSpi);
68 if (cardInsertion != nullptr) {
69 cardInsertion->waitForCardInsertion();
70 } else if (waitForCard != nullptr) {
71 waitForCard->waitForCardInsertion();
72 }
73 mMonitoringState->onEvent(InternalEvent::CARD_INSERTED);
74
75 } catch (const ReaderIOException& e) {
76 /* Just warn as it can be a disconnection of the reader. */
77 mParent->mLogger->warn(
78 "[fsmJob=%, reader=%] Failed to process card insertion event "
79 "[reason=%]\n",
80 JOB_ID,
81 mParent->getReader()->getName(),
82 e.getMessage());
83
84 } catch (const TaskCanceledException& e) {
85 mParent->mLogger->warn(
86 "[fsmJob=%, reader=%] Monitoring job process cancelled "
87 "[reason=%]\n",
88 JOB_ID,
89 mParent->getReader()->getName(),
90 e.getMessage());
91
92 } catch (const RuntimeException& e) {
93 mParent->getReader()
94 ->getObservationExceptionHandler()
95 ->onReaderObservationError(
96 mParent->getReader()->getPluginName(),
97 mParent->getReader()->getName(),
98 std::make_shared<RuntimeException>(e));
99 }
100}
101
102CardInsertionPassiveMonitoringJobAdapter::
103 CardInsertionPassiveMonitoringJobAdapter(
104 ObservableLocalReaderAdapter* reader)
105: AbstractMonitoringJobAdapter(reader)
106, mReaderSpi(reader->getObservableReaderSpi())
107{
108}
109
110std::shared_ptr<Job>
111CardInsertionPassiveMonitoringJobAdapter::getMonitoringJob(
112 std::shared_ptr<AbstractObservableStateAdapter> monitoringState)
113{
114 return std::make_shared<CardInsertionPassiveMonitoringJob>(
115 monitoringState, this);
116}
117
118void
119CardInsertionPassiveMonitoringJobAdapter::stop()
120{
121 mLogger->trace(
122 "[fsmJob=%, reader=%] Stopping monitoring job process\n",
123 JOB_ID,
124 getReader()->getName());
125
126 const auto& cardInsertion
127 = std::dynamic_pointer_cast<CardInsertionWaiterBlockingSpi>(mReaderSpi);
128 const auto& waitForCard
129 = std::dynamic_pointer_cast<WaitForCardInsertionBlockingSpi>(
130 mReaderSpi);
131 if (cardInsertion != nullptr) {
132 cardInsertion->stopWaitForCardInsertion();
133 } else if (waitForCard != nullptr) {
134 waitForCard->stopWaitForCardInsertion();
135 }
136
137 mLogger->trace(
138 "[fsmJob=%, reader=%] Monitoring job process stopped\n",
139 JOB_ID,
140 getReader()->getName());
141}
142
143} /* namespace service */
144} /* namespace core */
145} /* namespace keyple */
ObservableLocalReaderAdapter::InternalEvent InternalEvent