ytcore.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "ytcore.h"
  2. #include <stdio.h>
  3. #include "yt_unit_shm.h"
  4. YT_UNIT_SHM *ytShm;
  5. extern bool myLog;
  6. extern bool myDebug;
  7. BpCore::BpCore(QObject *parent) :
  8. QObject(parent)
  9. {
  10. timeoutworking=false;
  11. CHour = QDateTime::currentDateTime().time().hour();
  12. CMin = QDateTime::currentDateTime().time().minute();
  13. TMin = QDateTime::currentDateTime().time().minute();
  14. UskyID = "90001";
  15. UskyIP = "172.16.120.191";
  16. UskyPort = 51211;
  17. LocIP = "192.168.1.100";
  18. GateWay = "192.168.1.1";
  19. NetMask = "255.255.255.0";
  20. MacAddr = "0:A1:E9:40:6D:EA";
  21. timer = new QTimer(this);
  22. connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
  23. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  24. db.setDatabaseName(QString(CONF_DB));
  25. recdb = QSqlDatabase::addDatabase("QSQLITE","rec_db");
  26. recdb.setDatabaseName(QString(REC_DB));
  27. if((!db.open())||(!recdb.open()))
  28. {
  29. system("echo 'Database open failed.\n'");
  30. exit(-1);
  31. }
  32. db_init();
  33. if(!shm_load())
  34. {
  35. system("echo 'Memory load failed.\n'");
  36. exit(-1);
  37. }
  38. printf("shm ok\n");
  39. uServerThread = new UServerThread(this,UskyIP,UskyPort);
  40. logThread = new LogThread(this);
  41. logThread->start();
  42. if(myLog)
  43. logThread->appendLog(QString("[%1] main appliction start\r\n")
  44. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  45. dbThread = new DBThread(this,&db,&recdb);
  46. connect(dbThread,SIGNAL(log(QString)),this,SLOT(log(QString)));
  47. connect(dbThread,SIGNAL(dbg_log(QString)),this,SLOT(dbg_log(QString)));
  48. ytShm->dogTimeList.dogTime[CORE_MAIN].IDX = CORE_MAIN;
  49. ytShm->dogTimeList.dogTime[CORE_MAIN].WAITSEC = 30;
  50. ytShm->dogTimeList.dogTime[CORE_MAIN].ENABLED = 0x01;
  51. dbThread->start();
  52. uServerThread->start();
  53. for(quint8 CommId=0;CommId<4;CommId++)
  54. {
  55. if(ytShm->commList.comm[CommId].ENABLED==0x01)
  56. {
  57. dnThread[CommId] = new DNCommThread(this,CommId,true,QString(ytShm->commList.comm[CommId].PATH));
  58. dnThread[CommId]->setCommType(ytShm->commList.comm[CommId].CommType);
  59. connect(dnThread[CommId],SIGNAL(log(QString)),this,SLOT(log(QString)));
  60. dnThread[CommId]->start();
  61. }
  62. }
  63. }
  64. void BpCore::run()
  65. {
  66. timer->start(500);
  67. printf("start\n");
  68. }
  69. void BpCore::log(QString txt)
  70. {
  71. if(myLog)
  72. logThread->appendLog(txt);
  73. }
  74. void BpCore::dbg_log(QString txt)
  75. {
  76. if(myDebug)
  77. logThread->appendDbgLog(txt);
  78. }
  79. void BpCore::confsql(QString sql)
  80. {
  81. dbThread->appendSQL(1,sql);
  82. }
  83. void BpCore::recsql(QString sql)
  84. {
  85. dbThread->appendSQL(3,sql);
  86. }
  87. void BpCore::time_out()
  88. {
  89. if(timeoutworking)
  90. return;
  91. timeoutworking=true;
  92. QDateTime dt = QDateTime::currentDateTime();
  93. unsigned int t = dt.toTime_t();
  94. ytShm->dogTimeList.dogTime[CORE_MAIN].LASTFEED = t;
  95. QString dataStr = "";
  96. QString cloumn = "";
  97. if(TMin!=(QDateTime::currentDateTime().time().minute())){
  98. TMin=QDateTime::currentDateTime().time().minute();
  99. for(int cid=0;cid<4;cid++){
  100. for(int pid=1;pid<32;pid++){
  101. if(ytShm->sPointList.sPoint[cid][pid].ENABLED==0x01){
  102. dataStr.append(QString(",%1").arg(ytShm->spStatusList.spStatus[cid][pid].REALTIME_VALUE));
  103. }
  104. }
  105. }
  106. //printf("dataStr: %s, t: %d\n",dataStr.toUtf8().data(),t);
  107. QString sql = QString("insert into yt_rec_realdata (Id,CommId,P_0_001,P_0_002,P_0_003,P_0_004,P_0_005,P_0_006,P_0_007,P_0_008,P_0_009,P_0_010,P_0_011,P_0_012,P_0_013,P_0_014,P_0_015,P_0_016,P_0_017,P_0_018,P_0_019,P_0_020,P_0_021,P_0_022,P_0_023,P_0_024,P_0_025,P_0_026,P_0_027,P_0_028,P_0_029,P_0_030,P_0_031, StoreTime)values(NULL,0%1,%2)").arg(dataStr).arg(t);
  108. dbThread->appendSQL(3,sql);
  109. uServerThread->appendAllRealdata();
  110. }
  111. timeoutworking=false;
  112. }
  113. bool BpCore::chk_table(QString tbname, QStringList tables)
  114. {
  115. printf("chk table %s\n",tbname.toUtf8().data());
  116. for(int i=0;i<tables.length();i++)
  117. {
  118. if(strcmp(tables.at(i).toUtf8().data(),tbname.toUtf8().data())==0)
  119. return true;
  120. }
  121. return false;
  122. }
  123. void BpCore::db_init()
  124. {
  125. if(!chk_table("yt_t_syscom",db.tables()))
  126. {
  127. if(db.transaction())
  128. {
  129. db.exec(QString("create table yt_t_syscom (Id integer, UskyID text, UskyIP text, UskyPort integer)"));
  130. db.exec(QString("insert into yt_t_syscom (Id, UskyID, UskyIP, UskyPort) values (0, '%1','%2','%3')").arg(UskyID).arg(UskyIP).arg(UskyPort));
  131. db.commit();
  132. }
  133. }
  134. if(!chk_table("yt_t_locnet",db.tables())){
  135. if(db.transaction())
  136. {
  137. db.exec(QString("create table yt_t_locnet (loc_ip text, gateway text, netmask text, macaddr text)"));
  138. db.exec(QString("insert into yt_t_locnet (loc_ip, gateway, netmask, macaddr) values ('%1','%2','%3','%4')")
  139. .arg(LocIP).arg(GateWay).arg(NetMask).arg(MacAddr));
  140. db.commit();
  141. }
  142. }
  143. if(!chk_table("yt_t_comm",db.tables()))
  144. {
  145. if(db.transaction())
  146. {
  147. db.exec(QString("create table yt_t_comm (Id integer, CommType integer, CommPath text, Enabled integer)"));
  148. for(int i=0;i<4;i++)
  149. {
  150. if(i==0)
  151. db.exec(QString("insert into yt_t_comm (Id, CommType, CommPath, Enabled) values (0,1,'/dev/ttySAC2',1)"));
  152. else
  153. db.exec(QString("insert into yt_t_comm (Id, CommType, CommPath, Enabled) values (%1,1,'/dev/ttyO%2',%3)")
  154. .arg(i).arg(i).arg(0));
  155. }
  156. db.commit();
  157. }
  158. }
  159. if(!chk_table("yt_rec_data",recdb.tables()))
  160. {
  161. if(recdb.transaction())
  162. {
  163. recdb.exec(QString("create table yt_rec_data (Id integer primary key autoincrement, CommId integer, BusAdd integer, AveValue real, StoreTime integer, MaxValue real, MaxTime integer, MinValue real, MinTime integer)"));
  164. recdb.commit();
  165. }
  166. }
  167. if(!chk_table("yt_rec_realdata",recdb.tables()))
  168. {
  169. if(recdb.transaction())
  170. {
  171. recdb.exec(QString("CREATE TABLE yt_rec_realdata (Id integer primary key autoincrement,CommId integer,P_0_001 real,P_0_002 real,P_0_003 real,P_0_004 real,P_0_005 real,P_0_006 real,P_0_007 real,P_0_008 real,P_0_009 real,P_0_010 real,P_0_011 real,P_0_012 real,P_0_013 real,P_0_014 real,P_0_015 real,P_0_016 real,P_0_017 real,P_0_018 real,P_0_019 real,P_0_020 real,P_0_021 real,P_0_022 real,P_0_023 real,P_0_024 real,P_0_025 real,P_0_026 real,P_0_027 real,P_0_028 real,P_0_029 real,P_0_030 real,P_0_031 real,StoreTime integer)"));
  172. recdb.commit();
  173. }
  174. }
  175. }
  176. void BpCore::shm_init()
  177. {
  178. QSqlQuery qry;
  179. QString sql = "select loc_ip, gateway, netmask, macaddr from yt_t_locnet";
  180. // printf("%s\n",sql.toUtf8().data());
  181. qry = db.exec(sql);
  182. if(qry.next()){
  183. LocIP = qry.value(0).toString();
  184. GateWay = qry.value(1).toString();
  185. NetMask = qry.value(2).toString();
  186. MacAddr = qry.value(3).toString();
  187. sprintf(ytShm->unitNet.LocIP,"%s",LocIP.toUtf8().data());
  188. sprintf(ytShm->unitNet.GateWay,"%s",GateWay.toUtf8().data());
  189. sprintf(ytShm->unitNet.NetMask,"%s",NetMask.toUtf8().data());
  190. sprintf(ytShm->unitNet.MacAddr,"%s",MacAddr.toUtf8().data());
  191. ytShm->unitNet.UNSAVED=0x00;
  192. }
  193. qry.clear();
  194. sql = "select Id, CommType, CommPath, Enabled from yt_t_comm";
  195. // printf("%s\n",sql.toUtf8().data());
  196. qry = db.exec(sql);
  197. while(qry.next())
  198. {
  199. quint8 id = qry.value(0).toUInt()&0xff;
  200. quint16 commType = qry.value(1).toUInt()&0xffff;
  201. QString commPath = qry.value(2).toString();
  202. quint8 enabled = ((qry.value(3).toUInt()&0xff)==0x01)?0x01:0x00;
  203. ytShm->commList.comm[id].IDX = id;
  204. ytShm->commList.comm[id].CommType = commType;
  205. sprintf(ytShm->commList.comm[id].PATH,"%s",commPath.toUtf8().data());
  206. ytShm->commList.comm[id].ENABLED = enabled;
  207. ytShm->commList.comm[id].UNSAVED = 0x00;
  208. // printf("ID:%d\t ENABLED:%d\tPATH:%s\n",id,enabled,commPath.toUtf8().data());
  209. }
  210. qry.clear();
  211. qry = db.exec("select Id, SelfId, Key, UpUrl, UskyID, UskyIP, UskyPort, AgboxEnabled from yt_t_syscom");
  212. if(qry.next())
  213. {
  214. UskyID = qry.value(4).toString();
  215. UskyIP = qry.value(5).toString();
  216. UskyPort = qry.value(6).toUInt()&0xffff;
  217. sprintf(ytShm->sysShm.USKYID,"%s",UskyID.toUtf8().data());
  218. sprintf(ytShm->sysShm.USKYSERIP,"%s",UskyIP.toUtf8().data());
  219. ytShm->sysShm.USKYPORT = UskyPort;
  220. }
  221. qry.clear();
  222. sql = "select CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL,SubType from yt_t_point";
  223. // printf("%s\n",sql.toUtf8().data());
  224. qry = db.exec(sql);
  225. while(qry.next())
  226. {
  227. quint8 commId = qry.value(0).toUInt()&0xff;
  228. quint8 busAdd = qry.value(1).toUInt()&0xff;
  229. quint8 enabled = ((qry.value(2).toUInt()&0xff)==0x01)?0x01:0x00;
  230. QString name = qry.value(3).toString();
  231. quint8 pnotype = qry.value(4).toUInt()&0xff;
  232. quint8 pnoalarm = qry.value(5).toUInt()&0xff;
  233. quint8 pnovalue = qry.value(6).toUInt()&0xff;
  234. quint8 pnodly = qry.value(7).toUInt()&0xff;
  235. quint8 pnoad = qry.value(8).toUInt()&0xff;
  236. quint8 offset = qry.value(9).toUInt()&0xff;
  237. quint8 offsetflag = qry.value(10).toUInt()&0xff;
  238. float pnomax = qry.value(11).toFloat();
  239. float pnomin = qry.value(12).toFloat();
  240. float alarmh = qry.value(13).toFloat();
  241. float alarml = qry.value(14).toFloat();
  242. uint subtype = qry.value(15).toUInt();
  243. if(commId<4){
  244. ytShm->sPointList.sPoint[commId][busAdd].COMM_IDX = commId;
  245. ytShm->sPointList.sPoint[commId][busAdd].BUS_ADD = busAdd;
  246. ytShm->sPointList.sPoint[commId][busAdd].ENABLED = enabled;
  247. bzero(ytShm->sPointList.sPoint[commId][busAdd].NAME,sizeof(ytShm->sPointList.sPoint[commId][busAdd].NAME));
  248. sprintf(ytShm->sPointList.sPoint[commId][busAdd].NAME,"%s",name.toUtf8().data());
  249. ytShm->sPointList.sPoint[commId][busAdd].PNO_TYPE = pnotype;
  250. ytShm->sPointList.sPoint[commId][busAdd].PNO_ALARM = pnoalarm;
  251. ytShm->sPointList.sPoint[commId][busAdd].PNO_VALUE = pnovalue;
  252. ytShm->sPointList.sPoint[commId][busAdd].PNO_DLY = pnodly;
  253. ytShm->sPointList.sPoint[commId][busAdd].PNO_AD = pnoad;
  254. ytShm->sPointList.sPoint[commId][busAdd].OFFSET = offset;
  255. ytShm->sPointList.sPoint[commId][busAdd].OFFSET_FLAG = offsetflag;
  256. ytShm->sPointList.sPoint[commId][busAdd].PNO_MAX = pnomax;
  257. ytShm->sPointList.sPoint[commId][busAdd].PNO_MIN = pnomin;
  258. ytShm->sPointList.sPoint[commId][busAdd].ALARM_H = alarmh;
  259. ytShm->sPointList.sPoint[commId][busAdd].ALARM_L = alarml;
  260. ytShm->sPointList.sPoint[commId][busAdd].SUB_TYPE = subtype;
  261. ytShm->sPointList.sPoint[commId][busAdd].UNSAVED = 0x00;
  262. }
  263. }
  264. }
  265. bool BpCore::shm_load()
  266. {
  267. int shmid;
  268. key_t key;
  269. if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
  270. return false;
  271. if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1)
  272. return false;
  273. ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0);
  274. shm_init();
  275. return true;
  276. }