Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
Service.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "Service.h"
4#include <QTimer>
5#include <QSettings>
6#include <QDir>
7#include "RabbitCommonDir.h"
8#include "PluginService.h"
9#include <QLoggingCategory>
10
11Q_DECLARE_LOGGING_CATEGORY(Service)
12
13CService::CService(CPluginService *plugin) : QObject(),
14 m_pPara(nullptr),
15 m_pPlugin(plugin)
16{
17}
18
19CService::~CService()
20{
21 qDebug(Service) << "CService::~CService()";
22}
23
25{
26 int nRet = 0;
27 if(GetParameters())
28 {
29 nRet = LoadConfigure();
30 if(nRet) return nRet;
31 }
32
33 if(!GetParameters() || !GetParameters()->getEnable())
34 {
35 qInfo(Service) << "The service" << m_pPlugin->Name() << "is disable";
36 return -2;
37 } else
38 qInfo(Service) << "The service" << m_pPlugin->Name() << "is start";
39
40 nRet = OnInit();
41 if(0 == nRet)
42 QTimer::singleShot(0, this, SLOT(slotProcess()));
43 if(nRet > 0) return 0;
44 return nRet;
45}
46
47int CService::Clean()
48{
49 int nRet = 0;
50
51 OnClean();
52
53 if(m_pPara)
54 {
55 m_pPara->deleteLater();
56 m_pPara = nullptr;
57 }
58 return nRet;
59}
60
62{
63 int nRet = 0;
64 try{
65 nRet = OnProcess();
66 } catch (std::exception& e) {
67 qCritical(Service) << "OnProcess exception:" << e.what();
68 } catch (...) {
69 qCritical(Service) << "OnProcess exception";
70 }
71
72 // TODO: Ignore error and stop
73 if(nRet < 0)
74 {
75 qCritical(Service) << "OnProcess fail:", nRet;
76 return;
77 }
78 QTimer::singleShot(nRet, this, SLOT(slotProcess()));
79}
80
82{
83 qWarning(Service) << "Need to implement CService::OnProcess()";
84 return 0;
85}
86
87CParameterService* CService::GetParameters()
88{
89 return m_pPara;
90}
91
92int CService::LoadConfigure(const QString &szDir)
93{
94 int nRet = 0;
95 QString szFolder = szDir;
96 QString szFile;
97 if(szFolder.isEmpty())
98 {
99 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
100 QSettings set(szFile, QSettings::IniFormat);
101 szFolder = set.value("Configure/Folder",
102 RabbitCommon::CDir::Instance()->GetDirUserConfig()).toString();
103 szFile = set.value("Configure/File/" + m_pPlugin->Id(),
104 szFolder
105 + QDir::separator()
106 + m_pPlugin->Id()
107 + ".rrs").toString();
108 } else {
109 szFile = szFolder + QDir::separator() + m_pPlugin->Id() + ".rrs";
110 }
111 qInfo(Service) << "Configure file:" << szFile;
112 QDir d;
113 if(d.exists(szFile)){
114 nRet = GetParameters()->Load(szFile);
115 if(nRet)
116 {
117 qInfo(Service) << "Load configure file fail:" << nRet << ":" << szFile;
118 return -1;
119 }
120 }
121 return nRet;
122}
123
124int CService::SaveConfigure(const QString &szDir)
125{
126 int nRet = 0;
127 if(GetParameters())
128 {
129 QString szFolder = szDir;
130 QString szFile;
131 if(szFolder.isEmpty())
132 {
133 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
134 QSettings set(szFile, QSettings::IniFormat);
135 szFolder = set.value("Configure/Folder",
136 RabbitCommon::CDir::Instance()->GetDirUserConfig()).toString();
137 szFile = set.value("Configure/File/" + m_pPlugin->Id(),
138 szFolder
139 + QDir::separator()
140 + m_pPlugin->Id()
141 + ".rrs").toString();
142 } else {
143 szFile = szFolder + QDir::separator() + m_pPlugin->Id() + ".rrs";
144 }
145 nRet = GetParameters()->Save(szFile);
146 }
147 return nRet;
148}
The service plugin interface.
virtual const QString Id() const
ID. Default: Protocol() + "_" + Name()
virtual const QString Name() const =0
This name must be the same as the project name (${PROJECT_NAME}).
virtual int OnInit()=0
Init service.
virtual int Init()
Load parameters, support non-Qt event loop.
Definition Service.cpp:24
virtual void slotProcess()
Support non-Qt event loop.
Definition Service.cpp:61
virtual int OnClean()=0
Clean service.
virtual int OnProcess()
Process service.
Definition Service.cpp:81