pdu_ctrl.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #include "pdu_ctrl.h"
  2. #include <QtWidgets/QStackedWidget>
  3. #include <QSettings>
  4. #include <QProcess>
  5. #include <QCoreApplication>
  6. #include <QSql>
  7. #include <QSqlDatabase>
  8. #include <QSqlQuery>
  9. #include <QSqlError>
  10. #include <QTextStream>
  11. #include <QRegularExpression>
  12. #define READ_INI_STR(key, default_value) (settings.contains(key) ? settings.value(key).toString() : default_value)
  13. #define READ_INI_INT(key, default_value) (settings.contains(key) ? settings.value(key).toInt() : default_value)
  14. pdu_Ctrl* pdu_Ctrl::m_instance = nullptr;
  15. QMutex pdu_Ctrl::mutex;
  16. pdu_Ctrl::pdu_Ctrl(QObject *parent) : QObject(parent)
  17. {
  18. m_nModbusSelect=0;
  19. m_nModbusAddress=0;//从机地址
  20. m_nModbusMSType=1;//主从
  21. m_nSaveData=0;
  22. nViewShowType=widgetType_Main;
  23. nSettingType=0;
  24. nChidIndex=0;
  25. nChidPHIndex=0;
  26. m_nAngle=0;
  27. isT_ACType=0;
  28. m_nSaveAngle=0;
  29. memset(nButtonStatus,0,sizeof(nButtonStatus));
  30. //
  31. }
  32. void pdu_Ctrl::slot_setAngel(int nAngle)
  33. {
  34. if(m_pAngleCtrl&&m_pWidget)
  35. {
  36. //m_pAngleCtrl->setPos(size().width()/2, size().height()/2);
  37. //m_pAngleCtrl->transform(width() / 2, height() / 2);
  38. m_pAngleCtrl->setRotation(nAngle);
  39. m_nAngle=nAngle;
  40. if(m_nAngle!=m_nSaveAngle)
  41. {
  42. m_nSaveAngle=m_nAngle;
  43. SetPDUInit();
  44. }
  45. if(nAngle==0||nAngle==180)
  46. {
  47. m_pWidget->resize(m_nWidth, m_nHeight);
  48. if(nAngle==0)
  49. {
  50. m_pAngleCtrl->setPos(0,0);
  51. }else
  52. {
  53. m_pAngleCtrl->setPos(m_pWidget->size().width(),m_pWidget->size().height());
  54. }
  55. //QRect pRect=size();
  56. qDebug()<<"x:"<<m_pWidget->geometry().x()<<"y:"<<m_pWidget->geometry().y()<<"Width:"<<m_pWidget->size().width()<<"Height:"<<m_pWidget->size().height();
  57. }else
  58. {
  59. m_pWidget->resize(m_nHeight,m_nWidth);
  60. if(nAngle==90)
  61. {
  62. m_pAngleCtrl->setPos(m_nWidth,0);
  63. }else
  64. {
  65. m_pAngleCtrl->setPos(0,m_nHeight);
  66. }
  67. qDebug()<<"x:"<<m_pWidget->geometry().x()<<"y:"<<m_pWidget->geometry().y()<<"Width:"<<m_pWidget->size().width()<<"Height:"<<m_pWidget->size().height();
  68. }
  69. m_pView->update();
  70. }
  71. }
  72. void pdu_Ctrl::slot_ClickBtn(int nButton)
  73. {
  74. ButtonDown(nButton,BtnDown_C);
  75. }
  76. void pdu_Ctrl::slot_DClickBtn(int nButton)
  77. {
  78. ButtonDown(nButton,BtnDown_C);
  79. }
  80. void pdu_Ctrl::slot_LClickBtn(int nButton)
  81. {
  82. ButtonDown(nButton,BtnDown_L);
  83. }
  84. void pdu_Ctrl::slot_BtnClear(int nButton)
  85. {
  86. ButtonDown(nButton,BtnDown_Null);
  87. }
  88. void pdu_Ctrl::InitData(Widget *pWidget, QGraphicsProxyWidget *pProxy, QGraphicsView *pView,int nWidth, int nHeight)
  89. {
  90. m_pAngleCtrl=pProxy;
  91. m_pView=pView;
  92. m_pWidget=pWidget;
  93. m_nWidth= nWidth;
  94. m_nHeight = nHeight;
  95. }
  96. void pdu_Ctrl::ReadModbusInit()
  97. {
  98. QSettings settings("/root/run/app/smartPDU.ini",QSettings::IniFormat);
  99. m_nModbusAddress=READ_INI_INT("MODBUS/address",0);
  100. if(m_nModbusAddress>255||m_nModbusAddress<0)
  101. {
  102. m_nModbusAddress=0;
  103. }
  104. m_nModbusMSType=READ_INI_INT("MODBUS/MSType",1);//0:主机,1:从机
  105. qDebug()<<"modbus Config m_nModbusMSType="<<m_nModbusMSType<<" m_nModbusAddress="<<m_nModbusAddress;
  106. QSqlDatabase m_db;
  107. m_db=QSqlDatabase::addDatabase("QSQLITE");
  108. m_db.setDatabaseName(strSqliteAddress);
  109. qDebug()<<"modbus DB="<<strSqliteAddress;
  110. bool isDBOpen=false;
  111. if (m_db.open())
  112. {
  113. qDebug()<<"SQL Open Success";
  114. isDBOpen=true;
  115. }else
  116. {
  117. qDebug()<<"SQL Open Error";
  118. isDBOpen=false;
  119. }
  120. if(isDBOpen)
  121. {
  122. QSqlQuery sql_query(m_db);
  123. QString strSql=QString("SELECT * FROM Table_DeviceInfo where product_id==1;");
  124. //qDebug()<<strSql;
  125. if (sql_query.exec(strSql))
  126. {
  127. while (sql_query.next())
  128. {
  129. m_nModbusAddress = sql_query.value("product_modbus_addr").toInt();
  130. }
  131. }else
  132. {
  133. qDebug()<<sql_query.lastError().text();
  134. }
  135. }
  136. m_db.close();
  137. qDebug()<<"modbus m_nModbusMSType="<<m_nModbusMSType<<" m_nModbusAddress="<<m_nModbusAddress;
  138. ReadPDUinit();
  139. }
  140. void pdu_Ctrl::SaveModbusInit()
  141. {
  142. //QSettings settings("/root/run/app/smartPDU.ini",QSettings::IniFormat);
  143. updateIniValue("/root/run/app/smartPDU.ini","MODBUS","address",QString::number(m_nModbusAddress));
  144. updateIniValue("/root/run/app/smartPDU.ini","MODBUS","MSType",QString::number(m_nModbusMSType));
  145. //settings.setValue("MODBUS/address",m_nModbusAddress);
  146. //settings.setValue("MODBUS/MSType",m_nModbusMSType);//0:主机,1:从机
  147. QSqlDatabase m_db;
  148. m_db=QSqlDatabase::addDatabase("QSQLITE");
  149. m_db.setDatabaseName(strSqliteAddress);
  150. qDebug()<<strSqliteAddress;
  151. bool isDBOpen=false;
  152. if (m_db.open())
  153. {
  154. qDebug()<<"SQL Open Success";
  155. isDBOpen=true;
  156. }else
  157. {
  158. qDebug()<<"SQL Open Error";
  159. isDBOpen=false;
  160. }
  161. if(isDBOpen)
  162. {
  163. QSqlQuery sql_query(m_db);
  164. QString strSql=QString("UPDATE Table_DeviceInfo SET product_modbus_addr = %1 WHERE product_id==1").arg(m_nModbusAddress);
  165. //qDebug()<<strSql;
  166. if (!sql_query.exec(strSql))
  167. {
  168. qDebug()<<sql_query.lastError().text();
  169. }
  170. }
  171. m_db.close();
  172. }
  173. void pdu_Ctrl::ReadPDUinit()
  174. {
  175. qDebug()<<"Read misc/rotate";
  176. QSettings settings("/root/run/app/smartPDU.ini",QSettings::IniFormat);
  177. QString strRotate=READ_INI_STR("misc/rotate","0");
  178. qDebug()<<"misc/rotate="<<strRotate;
  179. if(!strRotate.isEmpty())
  180. {
  181. m_nSaveAngle=strRotate.toInt();
  182. qDebug()<<"InitAngleStr="<<strRotate<<"InitAngleToint="<<m_nSaveAngle;
  183. m_nAngle=m_nSaveAngle;
  184. slot_setAngel(m_nAngle);
  185. }
  186. else
  187. {
  188. qDebug()<<"InitAngleStr="<<strRotate;
  189. }
  190. }
  191. void pdu_Ctrl::SetPDUInit()
  192. {
  193. //QSettings settings("/root/run/app/smartPDU.ini",QSettings::IniFormat);
  194. QString strValue="\""+QString::number(m_nSaveAngle)+"\"";
  195. //settings.setValue("misc/rotate",strValue);
  196. updateIniValue("/root/run/app/smartPDU.ini","misc","rotate",strValue);
  197. }
  198. bool pdu_Ctrl::updateIniValue(const QString &filePath, const QString &section, const QString &key, const QString &newValue)
  199. {
  200. QFile file(filePath);
  201. if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
  202. return false;
  203. }
  204. QTextStream in(&file);
  205. QStringList lines;
  206. bool inTargetSection = false;
  207. QRegularExpression sectionRe(R"(^\[(.*)\]\s*$)");
  208. QRegularExpression keyRe(R"(^\s*([^=]+)\s*=\s*(.*)\s*$)");
  209. // 逐行读取并处理
  210. while (!in.atEnd()) {
  211. QString line = in.readLine();
  212. QRegularExpressionMatch sectionMatch = sectionRe.match(line);
  213. if (sectionMatch.hasMatch()) {
  214. inTargetSection = (sectionMatch.captured(1) == section);
  215. } else if (inTargetSection) {
  216. QRegularExpressionMatch keyMatch = keyRe.match(line);
  217. if (keyMatch.hasMatch() && keyMatch.captured(1).trimmed() == key) {
  218. // 替换目标键的值,保留原格式(如双引号)
  219. line = QString("%1 = %2").arg(key, newValue);
  220. }
  221. }
  222. lines.append(line);
  223. }
  224. // 写回文件
  225. file.resize(0); // 清空原内容
  226. QTextStream out(&file);
  227. for (const QString& line : lines) {
  228. out << line << "\n";
  229. }
  230. file.close();
  231. return true;
  232. }
  233. void pdu_Ctrl::Main_UPBtnSet(BtnDownType nbtnType)
  234. {
  235. qDebug()<<"Main_UPBtnSet";
  236. if(!m_pWidget)
  237. {
  238. return;
  239. }
  240. if(nbtnType==BtnDown_C)
  241. {
  242. qDebug()<<"pageUp";
  243. m_pWidget->pageUp();
  244. }
  245. }
  246. void pdu_Ctrl::Main_FunBtnSet(BtnDownType nbtnType)
  247. {
  248. //中间按钮
  249. qDebug()<<"Main_FunBtnSet";
  250. if (nbtnType==BtnDown_C)
  251. {
  252. //在通道界面是显示三项通道
  253. if(m_pWidget)
  254. {
  255. int nNowIndex=m_pWidget->getShowPagIndex();
  256. qDebug()<<"NowIndex="<<nNowIndex;
  257. switch (nNowIndex) {
  258. case 1:
  259. case 2://CH通道界面如果是三项则切换至三项子通道界面
  260. {
  261. if(isT_ACType==1)
  262. {
  263. lastACIndex=nNowIndex;
  264. m_pWidget->setShowPHIndex(1);
  265. ChangeView(widgetType_Main,4);
  266. }
  267. }
  268. break;
  269. case 4://PH通道界面返回CH通道界面
  270. {
  271. m_pWidget->setShowPHIndex(0);
  272. ChangeView(widgetType_Main,lastACIndex);
  273. }
  274. break;
  275. default://其他界面
  276. //在显示主界面时是旋转窗口
  277. m_nAngle+=90;
  278. m_nAngle==360?m_nAngle=0:m_nAngle;
  279. qDebug()<<"nAngle:"<<m_nAngle;
  280. slot_setAngel(m_nAngle);
  281. qDebug()<<"slot_setAngel";
  282. break;
  283. }
  284. }
  285. }
  286. else
  287. {
  288. qDebug()<<"setSelectStatus";
  289. if(m_pWidget)
  290. {
  291. m_nModbusSelect=0;
  292. m_pWidget->CtrlBtnSet(0,m_nModbusAddress);
  293. m_pWidget->setSelectStatus(m_nModbusSelect);
  294. m_nSaveData=0;
  295. }
  296. //切换到modbus
  297. qDebug()<<"ChangeView";
  298. //nViewShowType=widgetType_Setting;
  299. ChangeView(widgetType_Setting,0);
  300. }
  301. }
  302. void pdu_Ctrl::Main_DownBtnSet(BtnDownType nbtnType)
  303. {
  304. qDebug()<<"Main_DownBtnSet";
  305. if(!m_pWidget)
  306. {
  307. return;
  308. }
  309. if(nbtnType==BtnDown_C)
  310. {
  311. m_pWidget->pageDown();
  312. qDebug()<<"pageDown";
  313. }
  314. }
  315. void pdu_Ctrl::Setting_UPBtnSet(BtnDownType nbtnType)
  316. {
  317. switch (nSettingType) {
  318. case settingMode_Modbus:
  319. {
  320. if(nbtnType==BtnDown_C)
  321. {//切换select
  322. if(m_pWidget)
  323. {
  324. switch (m_nModbusSelect) {
  325. case 0://address
  326. {
  327. m_nModbusAddress+=1;
  328. if(m_nModbusAddress>32)m_nModbusAddress=1;
  329. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusAddress);
  330. }break;
  331. case 1://SAVE
  332. {
  333. //保存切换
  334. m_nSaveData==0?m_nSaveData=1:m_nSaveData=0;
  335. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nSaveData);
  336. }break;
  337. case 2://主从
  338. {
  339. //主从切换
  340. m_nModbusMSType==0?m_nModbusMSType=1:m_nModbusMSType=0;
  341. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusMSType);
  342. }break;
  343. }
  344. }
  345. }else if(nbtnType==BtnDown_L)
  346. {
  347. switch (m_nModbusSelect) {
  348. case 0://address
  349. {
  350. m_nModbusAddress+=5;
  351. if(m_nModbusAddress>255)m_nModbusAddress=0;
  352. if(m_pWidget)m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusAddress);
  353. }break;
  354. }
  355. }
  356. }break;
  357. case settingMode_IP:
  358. {
  359. }break;
  360. default:
  361. break;
  362. }
  363. }
  364. void pdu_Ctrl::Setting_FunBtnSet(BtnDownType nbtnType)
  365. {
  366. switch (nSettingType) {
  367. case settingMode_Modbus:
  368. {
  369. if(nbtnType==BtnDown_C)
  370. {//切换select
  371. if(m_pWidget)
  372. {
  373. if(m_nModbusSelect==1&&m_nSaveData==1)//短按 FUN 保存YES回到前台。
  374. {
  375. SaveModbusInit();
  376. ChangeView(widgetType_Main,0);
  377. }else
  378. {
  379. #ifdef RS485Type
  380. m_nModbusSelect==0?m_nModbusSelect=1:m_nModbusSelect=0;
  381. #endif
  382. m_pWidget->setSelectStatus(m_nModbusSelect);
  383. }
  384. }
  385. }else if(nbtnType==BtnDown_L)
  386. {
  387. ChangeView(widgetType_Main,0);
  388. }
  389. }break;
  390. case settingMode_IP:
  391. {
  392. }break;
  393. default:
  394. break;
  395. }
  396. }
  397. void pdu_Ctrl::Setting_DownBtnSet(BtnDownType nbtnType)
  398. {
  399. switch (nSettingType) {
  400. case settingMode_Modbus:
  401. {
  402. if(nbtnType==BtnDown_C)
  403. {//切换select
  404. if(m_pWidget)
  405. {
  406. switch (m_nModbusSelect) {
  407. case 0://address
  408. {
  409. m_nModbusAddress-=1;
  410. if(m_nModbusAddress<1)m_nModbusAddress=32;
  411. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusAddress);
  412. }break;
  413. case 1://SAVE
  414. {
  415. //保存切换
  416. m_nSaveData==0?m_nSaveData=1:m_nSaveData=0;
  417. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nSaveData);
  418. }break;
  419. case 2://主从
  420. {
  421. //主从切换
  422. m_nModbusMSType==0?m_nModbusMSType=1:m_nModbusMSType=0;
  423. m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusMSType);
  424. }break;
  425. }
  426. }
  427. }else if(nbtnType==BtnDown_L)
  428. {
  429. switch (m_nModbusSelect) {
  430. case 0://address
  431. {
  432. m_nModbusAddress-=5;
  433. if(m_nModbusAddress<0)m_nModbusAddress=255;
  434. if(m_pWidget)m_pWidget->CtrlBtnSet(m_nModbusSelect,m_nModbusAddress);
  435. }break;
  436. }
  437. }
  438. }break;
  439. case settingMode_IP:
  440. {
  441. }break;
  442. default:
  443. break;
  444. }
  445. }
  446. void pdu_Ctrl::ChangeView(WidgetType widgetType, int nIndex)
  447. {
  448. if(!m_pWidget)
  449. {
  450. return;
  451. }
  452. if(widgetType!=nViewShowType)
  453. {
  454. qDebug()<<"Widget->ChangeView";
  455. m_pWidget->ChangeView(widgetType,0);
  456. nViewShowType=widgetType;
  457. }else
  458. {
  459. m_pWidget->ChangeView(widgetType,nIndex);
  460. }
  461. }
  462. void pdu_Ctrl::ResetAll()
  463. {
  464. #ifdef RS485IpType
  465. system("sh /mnt/UDISK/app/recovery.sh");
  466. #else
  467. system("sh /opt/recovery.sh");
  468. #endif
  469. }
  470. void pdu_Ctrl::RestartSys()
  471. {
  472. system("reboot");
  473. }
  474. void pdu_Ctrl::ButtonDown(int nButtonID, BtnDownType nDownType)
  475. {
  476. //规整按钮状态
  477. switch (nButtonID) {
  478. case GPIO_KEY1://1按钮
  479. nButtonStatus[0]=nDownType;
  480. break;
  481. case GPIO_KEY2:
  482. nButtonStatus[1]=nDownType;
  483. break;
  484. case GPIO_KEY3://3按钮
  485. nButtonStatus[2]=nDownType;
  486. break;
  487. default:
  488. break;
  489. }
  490. qDebug()<<"Key1="<<GPIO_KEY1<<" Type1="<<nButtonStatus[0];
  491. qDebug()<<"Key2="<<GPIO_KEY2<<" Type2="<<nButtonStatus[1];
  492. qDebug()<<"Key3="<<GPIO_KEY3<<" Type3="<<nButtonStatus[2];
  493. qDebug()<<"widgetType="<<nViewShowType;
  494. if(nDownType<0)
  495. {
  496. return;
  497. }
  498. //判断多键控制
  499. if(nButtonStatus[0]==2&&nButtonStatus[2]==2)
  500. {//恢复出厂设置
  501. ResetAll();
  502. return;
  503. }
  504. if(nButtonStatus[0]==2&&nButtonStatus[1]==2)
  505. {//重新启动
  506. RestartSys();
  507. return;
  508. }
  509. if(nViewShowType==widgetType_Main)
  510. {
  511. qDebug()<<"widgetType="<<widgetType_Main;
  512. qDebug()<<"nButtonID="<<nButtonID;
  513. switch (nButtonID) {
  514. case GPIO_KEY1://1按钮
  515. Main_UPBtnSet(nDownType);
  516. break;
  517. case GPIO_KEY2:
  518. Main_FunBtnSet(nDownType);
  519. break;
  520. case GPIO_KEY3://3按钮
  521. Main_DownBtnSet(nDownType);
  522. break;
  523. default:
  524. break;
  525. }
  526. }else if(nViewShowType==widgetType_Setting)
  527. {
  528. qDebug()<<"widgetType="<<widgetType_Setting;
  529. qDebug()<<"nButtonID="<<nButtonID;
  530. switch (nButtonID) {
  531. case GPIO_KEY1://1按钮
  532. Setting_UPBtnSet(nDownType);
  533. break;
  534. case GPIO_KEY2:
  535. Setting_FunBtnSet(nDownType);
  536. break;
  537. case GPIO_KEY3://3按钮
  538. Setting_DownBtnSet(nDownType);
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. }