7 #include <dpsim-models/Attribute.h>
8 #include <dpsim-models/Logger.h>
9 #include <dpsim-models/Task.h>
10 #include <dpsim/Config.h>
11 #include <dpsim/Definitions.h>
12 #include <dpsim/Interface.h>
13 #include <dpsim/Scheduler.h>
15 #include <readerwriterqueue.h>
19 class InterfaceWorker;
24 typedef std::shared_ptr<Interface> Ptr;
26 using AttributePacket =
struct AttributePacket {
35 enum AttributePacketFlags {
37 PACKET_CLOSE_INTERFACE = 1,
40 Interface(std::shared_ptr<InterfaceWorker> intf,
const String &name =
"",
41 UInt downsampling = 1)
42 : mInterfaceWorker(intf), mName(name), mDownsampling(downsampling),
44 mQueueDpsimToInterface = std::make_shared<
45 moodycamel::BlockingReaderWriterQueue<AttributePacket>>();
46 mQueueInterfaceToDpsim = std::make_shared<
47 moodycamel::BlockingReaderWriterQueue<AttributePacket>>();
55 virtual void pushDpsimAttrsToQueue();
58 virtual void popDpsimAttrsFromQueue(
bool isSync =
false);
61 virtual void syncExports();
65 virtual CPS::Task::List getTasks();
67 void setLogger(CPS::Logger::Log log);
76 std::vector<std::tuple<CPS::AttributeBase::Ptr, UInt, bool, bool>>
79 std::vector<std::tuple<CPS::AttributeBase::Ptr, UInt>> mExportAttrsDpsim;
82 std::shared_ptr<InterfaceWorker> mInterfaceWorker;
83 CPS::Logger::Log mLog;
85 bool mSyncOnSimulationStart;
86 UInt mCurrentSequenceDpsimToInterface = 1;
87 UInt mNextSequenceInterfaceToDpsim = 1;
89 std::atomic<bool> mOpened;
90 std::thread mInterfaceWriterThread;
91 std::thread mInterfaceReaderThread;
93 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
94 mQueueDpsimToInterface;
95 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
96 mQueueInterfaceToDpsim;
99 bool syncOnSimulationStart =
true);
105 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
106 mQueueDpsimToInterface;
107 std::shared_ptr<InterfaceWorker> mInterfaceWorker;
111 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
112 queueDpsimToInterface,
113 std::shared_ptr<InterfaceWorker> intf)
114 : mQueueDpsimToInterface(queueDpsimToInterface),
115 mInterfaceWorker(intf){};
116 void operator()()
const;
121 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
122 mQueueInterfaceToDpsim;
123 std::shared_ptr<InterfaceWorker> mInterfaceWorker;
124 std::atomic<bool> &mOpened;
128 std::shared_ptr<moodycamel::BlockingReaderWriterQueue<AttributePacket>>
129 queueInterfaceToDpsim,
130 std::shared_ptr<InterfaceWorker> intf, std::atomic<bool> &opened)
131 : mQueueInterfaceToDpsim(queueInterfaceToDpsim), mInterfaceWorker(intf),
133 void operator()()
const;
139 : Task(intf.mName +
".Read"), mIntf(intf) {
140 for (
const auto &[attr, _seqId, _blockOnRead, _syncOnStart] :
141 intf.mImportAttrsDpsim) {
142 mModifiedAttributes.push_back(attr);
146 void execute(Real time, Int timeStepCount)
override;
155 : Task(intf.mName +
".Write"), mIntf(intf) {
156 for (
const auto &[attr, _seqId] : intf.mExportAttrsDpsim) {
157 mAttributeDependencies.push_back(attr);
159 mModifiedAttributes.push_back(Scheduler::external);
162 void execute(Real time, Int timeStepCount)
override;
Tasks to be defined by every component.
virtual void syncImports()
Function called by the Simulation to perform interface synchronization.