void EnginioBaseModelPrivate::fullQueryReset(const QJsonArray &data) { delete _replyConnectionConntext; _replyConnectionConntext = new QObject(); q->beginResetModel(); _data = data; _attachedData.initFromArray(_data); syncRoles(); _canFetchMore = _canFetchMore && _data.count() && (queryData(QtCloudServicesConstants::limit).toDouble() <= _data.count()); q->endResetModel(); }
void benchmark() { struct timeval starttime, endtime, difftime; int i; gettimeofday(&starttime, NULL); printf("gruuu..\n"); // receiving 5MB of data for (i = 0; i < 16384; i++) { if (i % 100 == 0) printf("measuring %08d\r"); queryData(); } printf("\n"); gettimeofday(&endtime, NULL); timersub(&endtime,&starttime,&difftime); printf("Received %d bytes in %d.%d seconds\n", 16384*1*EP_SIZE, difftime.tv_sec, difftime.tv_usec); printf("Transfer speed: %f kbytes/sec\n", 1*16384*EP_SIZE/1024.0/(difftime.tv_sec+difftime.tv_usec/1e6)); }
main() { /* variable: studentList -> curr list * student -> as a tmp variable to save curr info * commands -> combine command you want to exe * id -> analyze id of student in cmd * name -> analyze name of student in cmd * cmd -> analyze sub command in commande,split with * flags -> analyze command */ tLinkList studentList; tStudent student; char commands[1000],id[10],name[20], *cmd,flag; int pos; /* init list with my self define function then print program UI*/ initLinkList(&studentList,dataCmp,handle); printUI(); /* loop to deal with commands*/ while(TRUE) { scanf("%s",commands); cmd=strtok(commands," "); /* loop to deal with command, split with */ do { /*analyze curr command to get id and name*/ flag=cmd[0]; if(strchr(cmd,',') !=NULL) { pos=strchr(cmd,',')-cmd; strncpy(id,cmd+2,pos-1); id[pos-2]='\0'; strcpy(name,cmd+pos+1); name[strlen(name)-1]='\0'; student.id = atoi(id); strcpy(student.name,name); } else { strcpy(id,cmd+2); id[strlen(id)-1]='\0'; student.id = atoi(id); } /* both upper and lower can be executed*/ switch(toupper(flag)) { case 'I' : insertData(&studentList,&student,sizeof(tStudent)); break; case 'D' : if(deleteData(&studentList,&student)==SUCCESS) printf("%s deletes successfully!\n",id); else printf("%s not exists in the list!\n",id); break; case 'U' : updateData(&studentList,&student,&student.id,sizeof(tStudent)); break; case 'Q' : /* query and print the data*/ handle(queryData(&studentList,&student.id)); break; case 'P' : iterativeLinkList(&studentList); break; case 'C' : system("cls"); printUI(); break; case 'E' : exit(0); break; default: printf("command error ,please try again !\n"); } cmd=strtok(NULL," "); }while(cmd != NULL); } system("pause"); }
void MainWindow::realtimeDataSlot() { QVector<Qstring> deviceNums[10000]; QVector<double> timeStamps[10000]; QVector<double> tempVals[10000]; QSqlDatabase accounts_db = QSqlDatabase::addDatabase("QSQLITE"); accounts_db.setDatabaseName(dbaddr); if (!accounts_db.open()) { qDebug() << "Could not open database file:"; qDebug() << accounts_db.lastError(); return; } else { qDebug("Connected to the database"); } Qstring temp = Qstring("select Data, Timestamp, DeviceID from sensorData ORDER BY Timestamp;"); qDebug() << "Query:"<< temp; QSqlQuery query(temp); if( !query.exec() ) qDebug() << query.lastError(); else qDebug( "Qeuried successfully" ); //count the current number of rows in database ordered by timestamp int cur_last_row; if(query.last()) cur_last_row = query.at(); qDebug()<<"current last row: "<<cur_last_row; if(cur_last_row > prev_last_row) { /*Before adding data, check to see if new devices are added * and add a curve for the device **/ // if db is not specified, then default database of this program is choosen QSqlQuery dev_query("SELECT DISTINCT DeviceID FROM Client_IP ORDER BY Timestamp;"); if( !dev_query.exec() ) qDebug() << dev_query.lastError(); else qDebug( "Qeury executed successfully" ); // find the number of distinct devices int cur_num_dev; if(dev_query.last()) cur_num_dev = dev_query.at() ;// dev_num counts from "zero" qDebug( "number of distinct devices: " ); qDebug() << cur_num_dev; if(cur_num_dev>num_dev){ for(int i=num_dev; i<cur_num_dev+1; i++){ dev_query.seek(i); dev_id[i]=dev_query.value(0).toString(); qDebug() << dev_id[i]; // add the add_graph function addCurve(ui->qtplot,i); } num_dev=cur_num_dev; } /* for(int i=prev_last_row; i<cur_last_row+1;i++){ query.seek(i); x[i]=query.value(0).toDouble(); y[i]=query.value(1).toDouble(); qDebug()<<x[i]<<y[i]; a[i]=query.value(2).toString(); qDebug()<<a[i]; // add the current data to the corresponding curve addData(ui->qtplot,a[i],x[i],y[i]); sleep(1); } */ QString tempdev= query.value(2).toString(); tempVals[prev_last_row]=query.value(0).toDouble(); timeStamps[prev_last_row]=query.value(1).toDouble(); qDebug()<<x[prev_last_row]<<y[prev_last_row]; deviceNums[prev_last_row]=tempdev; //dviceNums.push_back(query.value(1).toString()); queryData(ui->qtplot,deviceNums[prev_last_row],timeStamps[prev_last_row],tempVals[prev_last_row]); prev_last_row +=1; qDebug()<<"previous last row: "<<prev_last_row; } //count_graph = deviceNums.length(); /* data = 0; static double lastPointKey = 0; double value0; double key=QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;; if (key-lastPointKey >= 1.4) // at most add point every 14 ms { if(checkqry) if(qry->next()) { data = qry->value(2).toDouble(); qDebug() << "value:"<< qry->value(2).toDouble(); // timestamp = qry->value(1).toInt(); // qDebug() << "timestamp:"<< qry->value(1).toInt(); // if(init_time==0) // { // init_time = timestamp; // } } else { qry->~QSqlQuery(); close_qry(); accounts_db.close(); QSqlDatabase::removeDatabase("sData" ); } // key= timestamp-init_time; */ // add data to lines: //ui->customPlot->graph(count_graph)->addData(timeStamps[count_graph], tempVals[count_graph]); // set data of dots: // ui->customPlot->graph(1)->clearData(); //ui->customPlot->graph(count_graph)->addData(timeStamps[count_graph], tempVals[count_graph]); //ui->customPlot->graph(1)->addData(key, value0); // remove data of lines that's outside visible range: // ui->customPlot->graph(0)->removeDataBefore(key-15); }
void * DevicePolling(void * host_number) // thread { unsigned char poll_en = 0; unsigned int time_poll = 30000; unsigned char destroy = 0; int host = (int) host_number; unsigned char trying_time = 0, dev_disconnect_try_time = 3; float_struct_t my_float; if (host < 0 || host >= DEV_HOST_NUMBER) { printf("Host number not valid.\r\nIt should be greater or equal zero and lester than %d.\r\n", DEV_HOST_NUMBER); printf("Thread exiting.\r\n"); pthread_exit(NULL); } printf("Thread: %d start with host: %d.\n", (int)polling_thread[host], host); while(1) { if (pthread_mutex_trylock(&device_control_access) == 0) { poll_en = dev_host[host].polling_control.enable; time_poll = dev_host[host].polling_control.time_poll_ms * 1000; destroy = dev_host[host].polling_control.destroy; pthread_mutex_unlock(&device_control_access); } else { printf("Thread: %d. host: %d. Fail to access device control.\n", (int)polling_thread[host], host); usleep(1000); } if (destroy) { printf("Thread: %d. host: %d. Destroying.\n", (int)polling_thread[host], host); pthread_exit(NULL); } if (poll_en) { //while (pthread_mutex_trylock(&device_control_access) != 0) //usleep(1000); if (dev_host[host].type != DEV_UNKNOWN) // already known device type { trying_time = 0; while (pthread_mutex_trylock(&serial_access) != 0) { usleep(1000); trying_time ++; if (trying_time > 10) break; } if (trying_time > 10) { #if DEVICE_DEBUG printf("Thread: %d. host: %d. Fail to access serial port.\n", (int)polling_thread[host], host); #endif //pthread_mutex_unlock(&device_control_access); usleep(time_poll); continue; } else { RaspiExt_Pin_Hostx_Active(host + 1); if (queryData(&dev_host[host])) { #if DEVICE_DEBUG printf("Thread: %d. host: %d. Got data from device.\n", (int)polling_thread[host], host); DeviceInfo(&dev_host[host]); #endif dev_disconnect_try_time = 3; } else { #if DEVICE_DEBUG printf("Thread: %d. host: %d. No device here.\n", (int)polling_thread[host], host); #endif if (dev_disconnect_try_time == 0) { // TODO: unregister this device unsigned char reg_id = dev_host[host].number | dev_host[host].type; printf("Thread: %d. host: %d. Unregister device %X.\n", (int)polling_thread[host], host, reg_id); UnRegisterID(®_id); if (IS_MY_THESIS(DEV_TYPE_MASK(dev_host[host].type))) { if (dev_host[host].data != NULL) memset(dev_host[host].data, 0, sizeof(struct ThesisData)); } else { if (dev_host[host].data != NULL) memset(dev_host[host].data, 0, getTypeLength(dev_host[host].type)); } dev_host[host].type = DEV_UNKNOWN; dev_host[host].number = DEV_NUMBER_UNKNOWN; } else { dev_disconnect_try_time--; } } RaspiExt_Pin_Hostx_Inactive(host + 1); } pthread_mutex_unlock(&serial_access); switch (dev_host[host].type) { case DEV_SENSOR_TEMPERATURE: RaspiExt_LED_Hostx_Config(LED_MODE_TOGGLE, 1000, host + 1); if (IS_BIG_ENDIAN_BYTE_ORDER(dev_host[host].data_type)) { my_float.f_byte[0] = dev_host[host].data[3]; my_float.f_byte[1] = dev_host[host].data[2]; my_float.f_byte[2] = dev_host[host].data[1]; my_float.f_byte[3] = dev_host[host].data[0]; } else { my_float.f_byte[0] = dev_host[host].data[0]; my_float.f_byte[1] = dev_host[host].data[1]; my_float.f_byte[2] = dev_host[host].data[2]; my_float.f_byte[3] = dev_host[host].data[3]; } printf("Thread: %d. host: %d. Temperature: %0.3f.\n", (int)polling_thread[host], host, my_float.f); // adjust time polling if (pthread_mutex_trylock(&device_control_access) == 0) { dev_host[host].polling_control.time_poll_ms = 50; } else { printf("Thread: %d. host: %d. Fail to access device control.\n", (int)polling_thread[host], host); } // save to shared memory break; case DEV_SENSOR_ULTRA_SONIC: RaspiExt_LED_Hostx_Config(LED_MODE_TOGGLE, 100, host + 1); my_float.f_byte[0] = dev_host[host].data[3]; my_float.f_byte[1] = dev_host[host].data[2]; my_float.f_byte[2] = dev_host[host].data[1]; my_float.f_byte[3] = dev_host[host].data[0]; printf("Thread: %d. host: %d. Distance: %0.3f.\n", (int)polling_thread[host], host, my_float.f); // put to db #if DATABASE //// put this device into database //if (DB_IsExist_sensors(dev_host[host].number, dev_host[host].type, UltraSonic_name, //UltraSonic_code, UltraSonic_symbol, 30, 10, UltraSonic_unit) == 0) // not exist //{ //DB_Record_sensors(dev_host[host].number, dev_host[host].type, UltraSonic_name, //UltraSonic_code, UltraSonic_symbol, 30, 10, UltraSonic_unit); //} //if (DB_IsExist_sensor_types(dev_host[host].type, UltraSonic_name, UltraSonic_description) == 0) // not exist //{ //DB_Record_sensor_types(dev_host[host].type, UltraSonic_name, UltraSonic_description); //} ////DB_Record_sensor_values(dev_host[host].type, UltraSonic_name, UltraSonic_description); DB_Record_sensor_values_short(my_float.f, millis()/1000); #endif // adjust time polling // save to shared memory break; case DEV_SENSOR_LIGTH: RaspiExt_LED_Hostx_Config(LED_MODE_TOGGLE, 50, host + 1); break; case DEV_RF: break; case DEV_BLUETOOTH: break; case DEV_BUZZER: break; case DEV_SENSOR_GAS: RaspiExt_LED_Hostx_Config(LED_MODE_TOGGLE, 50, host + 1); break; case DEV_SIM900: break; case DEV_MY_THESIS: RaspiExt_LED_Hostx_Config(LED_MODE_TOGGLE, 1000, host + 1); break; default: #if DEVICE_DEBUG printf("Thread: %d. host: %d. Unknown device type.\n", (int)polling_thread[host], host); #endif dev_host[host].type = DEV_UNKNOWN; dev_host[host].number = DEV_NUMBER_UNKNOWN; break; } } else // unknown device type { RaspiExt_LED_Hostx_Config(LED_MODE_OFF, 50, host + 1); #if DEVICE_DEBUG printf("Thread: %d. host: %d. Unknown device, identifying.\n", (int)polling_thread[host], host); #endif // query broadcast id to identify what it is // adjust time polling to 500 ms time_poll = 500000; trying_time = 0; while (pthread_mutex_trylock(&serial_access) != 0) { usleep(1000); trying_time ++; if (trying_time > 10) { break; } } if (trying_time > 10) { #if DEVICE_DEBUG printf("Thread: %d. host: %d. Fail to access serial port.\n", (int)polling_thread[host], host); #endif usleep(time_poll); continue; } else { #if DEVICE_DEBUG //dev_host[host].type = DEV_SENSOR_ULTRA_SONIC; //dev_host[host].number = 0x01; #endif RaspiExt_Pin_Hostx_Active(host + 1); if (queryData(&dev_host[host])) { #if DEVICE_DEBUG printf("Thread: %d. host: %d. Got data from device.\n", (int)polling_thread[host], host); #endif // TODO: register new id unsigned char reg_id = dev_host[host].type | dev_host[host].number; if (RegisterID(®_id) != 0) { printf("Thread: %d. host: %d. Fail to register new device.\n", (int)polling_thread[host], host); } else { printf("Thread: %d. host: %d. Registered new device %X.\n", (int)polling_thread[host], host, reg_id); } dev_host[host].type = DEV_TYPE_MASK(reg_id); dev_host[host].number = DEV_NUMBER_MASK(reg_id); sendControl(dev_host[host]); } else { #if DEVICE_DEBUG printf("Thread: %d. host: %d. No device here.\n", (int)polling_thread[host], host); #endif } RaspiExt_Pin_Hostx_Inactive(host + 1); } pthread_mutex_unlock(&serial_access); } usleep(time_poll); //pthread_mutex_unlock(&device_control_access); }// query device } }