DeviceDocumentPlugin.hpp
1 #pragma once
2 #include <Device/Node/DeviceNode.hpp>
3 
4 #include <Explorer/DeviceList.hpp>
5 #include <Explorer/DocumentPlugin/NodeUpdateProxy.hpp>
6 #include <Explorer/Explorer/DeviceExplorerModel.hpp>
7 #include <Explorer/Listening/ListeningHandler.hpp>
8 
9 #include <score/plugins/documentdelegate/plugin/DocumentPlugin.hpp>
10 
11 #include <ossia/detail/hash_map.hpp>
12 
13 #include <score_plugin_deviceexplorer_export.h>
14 
15 #include <thread>
16 #include <verdigris>
17 
18 namespace ossia::net
19 {
20 struct network_context;
21 using network_context_ptr = std::shared_ptr<network_context>;
22 }
23 
24 UUID_METADATA(
25  , score::DocumentPluginFactory, Explorer::DeviceDocumentPlugin,
26  "6e610e1f-9de2-4c36-90dd-0ef570002a21")
27 
28 namespace Explorer
29 {
30 class SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceDocumentPlugin final
32  , public Nano::Observer
33 {
34  W_OBJECT(DeviceDocumentPlugin)
35  SCORE_SERIALIZE_FRIENDS
36 
37  MODEL_METADATA_IMPL_HPP(DeviceDocumentPlugin)
38 public:
39  explicit DeviceDocumentPlugin(const score::DocumentContext& ctx, QObject* parent);
40 
41  virtual ~DeviceDocumentPlugin();
42  template <typename Impl>
43  DeviceDocumentPlugin(const score::DocumentContext& ctx, Impl& vis, QObject* parent)
44  : score::SerializableDocumentPlugin{ctx, vis, parent}
45  {
46  init();
47  vis.writeTo(*this);
48  }
49 
50  void init();
51 
52  Device::Node& rootNode() { return m_rootNode; }
53  const Device::Node& rootNode() const { return m_rootNode; }
54 
55  Device::DeviceList& list() { return m_list; }
56 
57  const Device::DeviceList& list() const { return m_list; }
58 
59  // TODO make functions that take a parameter and call
60  // list().device(...).TheRelevantMethod
61 
62  Device::Node createDeviceFromNode(const Device::Node&);
63 
64  // If the output is different that the input,
65  // it means that the node has changes and the output should
66  // be used.
67  std::optional<Device::Node> loadDeviceFromNode(const Device::Node&);
68 
69  void setConnection(bool);
70 
71  Explorer::ListeningHandler& listening() const;
72 
73  DeviceExplorerModel& explorer() const { return *m_explorer; }
74 
75  void setupConnections(Device::DeviceInterface&, bool enabled);
76 
77  void reconnect(const QString&);
78 
79  const ossia::net::network_context_ptr& networkContext() const noexcept
80  {
81  return m_asioContext;
82  }
83 
84 private:
85  void initDevice(Device::DeviceInterface&);
86  void on_valueUpdated(const State::Address& addr, const ossia::value& v);
87 
88  Device::Node m_rootNode;
89  Device::DeviceList m_list;
90  std::atomic_bool m_processMessages{};
91  std::thread m_asioThread;
92  ossia::net::network_context_ptr m_asioContext;
93 
94  mutable std::unique_ptr<Explorer::ListeningHandler> m_listening;
95  DeviceExplorerModel* m_explorer{};
96  ossia::hash_map<Device::DeviceInterface*, std::vector<QMetaObject::Connection>>
97  m_connections;
98 
99  void asyncConnect(Device::DeviceInterface& newdev);
100  void timerEvent(QTimerEvent* event) override;
101 
102 public:
103  NodeUpdateProxy updateProxy{*this};
104 };
105 }
Definition: DeviceInterface.hpp:66
The DeviceList class.
Definition: DeviceList.hpp:26
Definition: ListeningHandler.hpp:21
Reimplement to instantiate document plug-ins.
Definition: DocumentPluginCreator.hpp:25
Document plug-in with serializable data.
Definition: SerializableDocumentPlugin.hpp:28
Base toolkit upon which the software is built.
Definition: Application.cpp:90
The Address struct.
Definition: Address.hpp:58
Definition: DocumentContext.hpp:18