MotionLib  1.0.0
SamBuCa motion library
PlatformBase.cpp
1 /*
2 ** Copyright (C) 2022 CERN
3 **
4 ** This software is provided 'as-is', without any express or implied
5 ** warranty. In no event will the authors be held liable for any damages
6 ** arising from the use of this software.
7 **
8 ** Permission is granted to anyone to use this software for any purpose,
9 ** including commercial applications, and to alter it and redistribute it
10 ** freely, subject to the following restrictions:
11 **
12 ** 1. The origin of this software must not be misrepresented; you must not
13 ** claim that you wrote the original software. If you use this software
14 ** in a product, an acknowledgment in the product documentation would be
15 ** appreciated but is not required.
16 ** 2. Altered source versions must be plainly marked as such, and must not be
17 ** misrepresented as being the original software.
18 ** 3. This notice may not be removed or altered from any source distribution.
19 **
20 ** Created on: 2022-07-25T15:31:43
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "PlatformBase.hpp"
25 
26 #include <logger/Logger.hpp>
27 
28 #include "Exception.hpp"
29 #include "device/DeviceStore.hpp"
30 
31 namespace smc {
32 namespace internal {
33 static const std::string s_loggerCat{"smc:platform"};
34 
35 void PlatformBase::setNotificationWorker(const NotificationWorker::Shared &worker)
36 {
37  m_notificationWorker = worker;
38 }
39 
40 void PlatformBase::stop() {}
41 
42 void PlatformBase::notify(const DeviceId &id)
43 {
44  NotificationWorker::Shared notificationWorker{m_notificationWorker.lock()};
45  if (notificationWorker)
46  {
47  try
48  {
49  notificationWorker->post([id](const DeviceStore::Shared &store) {
50  if (!store)
51  return;
52  DeviceBase::Shared device = store->getDevice(id);
53  if (!device)
54  logger::warning(s_loggerCat) << "no such device: " << id;
55  else
56  device->updateSignal(device);
57  });
58  }
59  catch (std::exception &ex)
60  {
61  logger::warning(s_loggerCat)
62  << "failed to post notification: " << ex.what();
63  }
64  }
65 }
66 
67 void PlatformBase::notify(std::set<DeviceId> &&notifications)
68 {
69  NotificationWorker::Shared notificationWorker{m_notificationWorker.lock()};
70  if (notificationWorker && !notifications.empty())
71  {
72  std::shared_ptr<std::set<DeviceId>> list{
73  std::make_shared<std::set<DeviceId>>(std::move(notifications))};
74 
75  try
76  {
77  notificationWorker->post([list](const DeviceStore::Shared &store) {
78  if (!store)
79  return;
80  for (DeviceId id : *list)
81  {
82  try
83  {
84  DeviceBase::Shared device = store->getDevice(id);
85  device->updateSignal(device);
86  }
87  catch (Exception &ex)
88  {
89  logger::warning(s_loggerCat) << ex.what();
90  }
91  }
92  });
93  }
94  catch (std::exception &ex)
95  {
96  logger::warning(s_loggerCat)
97  << "failed to post notification: " << ex.what();
98  }
99  }
100 }
101 
102 } // namespace internal
103 } // namespace smc
main motion-lib namespace
Definition: Client.cpp:30