This commit is contained in:
2025-09-24 10:53:28 +08:00
commit f8e4df77fb
856 changed files with 140098 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
#ifndef __UT_SERVICE_APPLICATION_HPP__
#define __UT_SERVICE_APPLICATION_HPP__
#include <unitree/common/service/base/service_base.hpp>
#define SERVICE_REGISTER(SERVICE) \
class SERVICE##Register \
{ \
public: \
SERVICE##Register() \
{ \
unitree::common::ServiceApplication::Instance()->RegistService<SERVICE>(); \
} \
static SERVICE##Register mRegister; \
}; \
SERVICE##Register SERVICE##Register::mRegister = SERVICE##Register();
namespace unitree
{
namespace common
{
class ServiceApplication
{
public:
static ServiceApplication* Instance()
{
static ServiceApplication inst;
return &inst;
}
template<typename SERVICE>
void RegistService()
{
mServicePtr = ServicePtr(new SERVICE());
}
void Init(const std::string& configFileName);
void Start();
void Stop();
private:
ServiceApplication();
protected:
ServicePtr mServicePtr;
};
}
}
#endif//__UT_SERVICE_APPLICATION_HPP__

View File

@@ -0,0 +1,104 @@
#ifndef __UT_SERVICE_BASE_HPP__
#define __UT_SERVICE_BASE_HPP__
#include <unitree/common/service/base/service_config.hpp>
namespace unitree
{
namespace common
{
class ServiceBase
{
public:
ServiceBase();
virtual ~ServiceBase();
virtual void Parse(const std::string& configFileName);
virtual void Init();
virtual void Register();
virtual void Start();
virtual void Wait();
virtual void Stop();
protected:
/*
* Parse config content
*/
virtual void ParseConfigContent(const std::string& content);
/*
* Get field: ServiceName
*/
const std::string& GetServiceName() const;
/*
* Get field: CpuIds
*/
const std::string& GetCpuIds() const;
/*
* Get top-level field/value
*/
const Any& GetGlobalParameter(const std::string& name) const;
/*
* Get top-level field: Parameter
*/
bool HasParameter(const std::string& name) const;
/*
* Get top-level field: Parameter
*/
const JsonMap& GetParameter() const;
/*
* Get field from top-level field: Parameter
*/
const Any& GetParameter(const std::string& name) const;
/*
* Get field from top-level field: Parameter
*/
template<typename T>
const T& GetParameter(const std::string& name) const
{
return mConfig.GetParameter<T>(name);
}
/*
* Get field from top-level field: Parameter
*/
template<typename T>
T GetNumberParameter(const std::string& name) const
{
return mConfig.GetNumberParameter<T>(name);
}
/*
* Get field from top-level field: Parameter
*/
template<typename T>
T GetParameter(const std::string& name, const T& defValue) const
{
return mConfig.GetParameter<T>(name, defValue);
}
template<typename T>
T GetNumberParameter(const std::string& name, const T& defValue) const
{
return mConfig.GetNumberParameter<T>(name, defValue);
}
private:
ServiceConfig mConfig;
};
typedef std::shared_ptr<ServiceBase> ServicePtr;
}
}
#endif//__UT_SERVICE_BASE_HPP__

View File

@@ -0,0 +1,37 @@
#ifndef __UT_SERVICE_CONFIG_HPP__
#define __UT_SERVICE_CONFIG_HPP__
#include <unitree/common/service/base/service_decl.hpp>
namespace unitree
{
namespace common
{
class ServiceConfig : public common::JsonConfig
{
public:
ServiceConfig();
virtual ~ServiceConfig();
ServiceConfig(const std::string& configFileName);
virtual void Parse(const std::string& configFileName);
virtual void ParseContent(const std::string& content);
//top-level field: ServiceName
const std::string& GetServiceName() const;
//top-level field: CpuIds
const std::string& GetCpuIds() const;
protected:
std::string mCpuIds;
std::string mServiceName;
};
typedef std::shared_ptr<ServiceConfig> ServiceConfigPtr;
}
}
#endif//__UT_SERVICE_CONFIG_HPP__

View File

@@ -0,0 +1,10 @@
#ifndef __UT_SERVICE_DECL_HPP__
#define __UT_SERVICE_DECL_HPP__
#include <unitree/common/thread/thread.hpp>
#include <unitree/common/time/time_tool.hpp>
#include <unitree/common/json/json.hpp>
#include <unitree/common/json/json_config.hpp>
#include <unitree/common/log/log.hpp>
#endif//__UT_SERVICE_DECL_HPP__

View File

@@ -0,0 +1,85 @@
#ifndef __UT_DDS_SERVICE_HPP__
#define __UT_DDS_SERVICE_HPP__
#include <unitree/common/service/base/service_base.hpp>
#include <unitree/common/service/base/service_config.hpp>
#include <unitree/common/dds/dds_easy_model.hpp>
namespace unitree
{
namespace common
{
class DdsService : public ServiceBase
{
public:
DdsService()
{
mLogger = GetLogger("/unitree/service/dds_service");
mQuit = false;
}
virtual ~DdsService()
{}
virtual void Register()
{}
virtual void Init()
{
mModel.Init(AnyCast<JsonMap>(GetGlobalParameter("DdsParameter")));
LOG_INFO(mLogger, "parse init dds model success.");
}
virtual void Start()
{}
virtual void Wait()
{
while (!mQuit) { sleep(1); }
}
virtual void Stop()
{
mQuit = true;
}
public:
virtual void Parse(const std::string& configFileName)
{
ServiceBase::Parse(configFileName);
LOG_INFO(mLogger, "parse config success. filename:", configFileName);
}
protected:
template<typename MSG>
void RegistTopicMessageHandler(const std::string& topic, const DdsMessageHandler& handler)
{
mModel.SetTopic<MSG>(topic, handler);
LOG_INFO(mLogger, "regist topic reader callback. topic:", topic);
}
template<typename MSG>
void RegistTopic(const std::string& topic)
{
mModel.SetTopic<MSG>(topic);
LOG_INFO(mLogger, "regist topic. topic:", topic);
}
/*
* Write message to topic
*/
template<typename MSG>
void WriteMessage(const std::string& topic, const MSG& message)
{
mModel.WriteMessage<MSG>(topic, message);
}
private:
bool mQuit;
DdsEasyModel mModel;
Logger* mLogger;
};
}
}
#endif//__UT_DDS_SERVICE_HPP__