main.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <QCoreApplication>
  2. #include "boxshm.h"
  3. #include "dogcore.h"
  4. #include <QFile>
  5. #include <QDir>
  6. #include <QSqlDatabase>
  7. #include <QSqlQuery>
  8. QSqlDatabase db;
  9. AGBoxShm *agBoxShm;
  10. void shm_init(){
  11. QSqlQuery qry;
  12. int nrow = 0;
  13. QString sql = QString("select device_code,device_id,device_type,product_code,device_gateway from yt_t_device");
  14. qry = db.exec(sql);
  15. while (qry.next()) {
  16. if(nrow > 1023)
  17. break;
  18. agBoxShm->device[nrow].Enabled = 0x01;
  19. sprintf(agBoxShm->device[nrow].device_code,"%s",qry.value(0).toString().toUtf8().data());
  20. sprintf(agBoxShm->device[nrow].device_id,"%s",qry.value(1).toString().toUtf8().data());
  21. agBoxShm->device[nrow].device_type = qry.value(2).toInt();
  22. sprintf(agBoxShm->device[nrow].product_code,"%s",qry.value(3).toString().toUtf8().data());
  23. sprintf(agBoxShm->device[nrow].device_gateway,"%s",qry.value(4).toString().toUtf8().data());
  24. nrow++;
  25. }
  26. for(int i=nrow;i<1024;i++){
  27. agBoxShm->device[i].Enabled = 0x00;
  28. }
  29. nrow = 0;
  30. qry.clear();
  31. sql.clear();
  32. sql = QString("select device_type,product_code,attribute_name,attribute_code from yt_t_attribute");
  33. qry = db.exec(sql);
  34. while (qry.next()) {
  35. if(nrow > 299)
  36. break;
  37. agBoxShm->procuctattrbute[nrow].Enabled = 0x01;
  38. agBoxShm->procuctattrbute[nrow].device_type = qry.value(0).toInt();
  39. sprintf(agBoxShm->procuctattrbute[nrow].product_code,"%s",qry.value(1).toString().toUtf8().data());
  40. sprintf(agBoxShm->procuctattrbute[nrow].attribute_name,"%s",qry.value(2).toString().toUtf8().data());
  41. sprintf(agBoxShm->procuctattrbute[nrow].attribute_code,"%s",qry.value(3).toString().toUtf8().data());
  42. nrow++;
  43. }
  44. for(int i=nrow;i<300;i++){
  45. agBoxShm->procuctattrbute[i].Enabled = 0x00;
  46. }
  47. }
  48. bool shm_load(){
  49. int shmid;
  50. key_t key;
  51. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  52. return false;
  53. }
  54. if((shmid=shmget(key,sizeof (AGBoxShm),IPC_CREAT|0666))==-1){
  55. return false;
  56. }
  57. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  58. shm_init();
  59. return true;
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. QCoreApplication a(argc, argv);
  64. QDir path("/opt/agBoxProcess");
  65. if(!path.exists()){
  66. system("mkdir -p /opt/agBoxProcess");
  67. system("mkdir -p /opt/agBoxProcess/log");
  68. }
  69. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  70. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  71. if(!db.open()){
  72. exit(1);
  73. }
  74. if(shm_load()){
  75. DogCore *dogcore = new DogCore(nullptr);
  76. dogcore->start();
  77. }else{
  78. fprintf(stderr,"shm load failed!\n");
  79. exit(1);
  80. }
  81. return a.exec();
  82. }