void CHttpThread::convertToBinary(QByteArray& array, const CConfigurationValue& confValue) { convertToBinary(array, confValue.getIdSensor()); convertToBinary(array, confValue.getConfigurationType()); convertToBinary(array, confValue.getData()); }
void CHttpThread::convertToBinary(QByteArray& array, const std::shared_ptr<CConfigurationResponse>& confResp) { convertToBinary(array, confResp->getStatus()); convertToBinary(array, confResp->getIdRequestPackage()); convertToBinary(array, confResp->getCurrentConfiguration()); }
void CHttpThread::convertToBinary(QByteArray& array, const std::shared_ptr<CServerRequest>& reqs) { convertToBinary(array, reqs->getRequestsSize()); std::for_each(reqs->getRequests().begin(), reqs->getRequests().end(), [&](const CRequest& req){ convertToBinary(array, req); }); }
void CHttpThread::convertToBinary(QByteArray& array, const CConfiguration& conf) { convertToBinary(array, conf.getConfigurationsSize()); std::for_each(conf.getConfigurations().begin(), conf.getConfigurations().end(), [&](const CConfigurationValue& value){ convertToBinary(array, value); }); }
bool CHttpThread::convertToBinary(QByteArray& array, const CProtocol& protocol) { array.reserve(protocol.getSize()); convertToBinary(array, protocol.getVersion()); convertToBinary(array, protocol.getSize()); convertToBinary(array, protocol.getIdConcentrator()); convertToBinary(array, protocol.getIdPackage()); convertToBinary(array, protocol.getType()); switch(protocol.getType()){ case MONITOR_DATA: convertToBinary(array, std::dynamic_pointer_cast<CMonitorData>(protocol.getMessage())); break; case CONFIGURATION_RESPONSE: convertToBinary(array, std::dynamic_pointer_cast<CConfigurationResponse>(protocol.getMessage())); break; case SERVER_REQUEST: convertToBinary(array, std::dynamic_pointer_cast<CServerRequest>(protocol.getMessage())); break; case SERVER_MONITOR_RESPONSE: // typ protokolu server -> concentrator return false; break; } convertToBinary(array, protocol.getCRC()); return true; }
void CHttpThread::convertToBinary(QByteArray& array, const std::shared_ptr<CMonitorData>& monitorData) { convertToBinary(array, monitorData->getSendTime()); convertToBinary(array, monitorData->getSensorAmount()); convertToBinary(array, monitorData->getSensorsDataSize()); std::for_each(monitorData->getSensorsData().begin(), monitorData->getSensorsData().end(), [&](const CSensorData& value){ convertToBinary(array, value); }); }
int main(int argc, const char * argv[]) { int threshold; int row,col,maxcolor; char* pgmFile="/Users/oo/Desktop/Eclipse_Projects/CS464_P1_PatternRecog/TestImages/square_and_circle_1024_1024.pgm"; int* image; //read image data into one array from pgm file image = getImage(pgmFile, &row, &col, &maxcolor); //Step 1: calculate and print the histoGram data int* histo = getHistoGram((int*)image, row*col); //Step 2: calculate the threshold value via Ostu algo. threshold = otsuThreshold(histo,maxcolor); //Step 3: convert the grey image into binary image int* binaryImage = convertToBinary((int*)image, row*col, threshold); //Step 4: connectivity analysis int* connectImage = connectAnalysis(binaryImage, row,col); //Step5: pattern analysis, can differentiate circle and square only patternAnalysis(connectImage, row, col); //release the dynamic memory allocated free(histo); free(binaryImage); free(connectImage); free(image); return 0; }
void CHttpThread::convertToBinary(QByteArray& array, const CSensorData& sensorData) { convertToBinary(array, sensorData.getIdData()); convertToBinary(array, sensorData.getIdSensor()); convertToBinary(array, sensorData.getTimeStamp()); convertToBinary(array, sensorData.getSensorState()); convertToBinary(array, sensorData.getDangerLevel()); convertToBinary(array, sensorData.getData()); }
void CHttpThread::sendHttp(const CProtocol& protocol) { //LOG_PROTOCOL(protocol); QByteArray postData; // konwertuj protokol do postaci binarnej tablicy QByteArray if (!convertToBinary(postData, protocol)){ // nieprawidlowy format protokolu LOG_ERROR("Sending protocol error. idPackage:", protocol.getIdPackage()); DConnectionResult res(new CConnectionResult(protocol, EConnectionStatus::OUTPUT_PROTOCOL_FORMAT_ERROR)); resultsQueue.push(res); } else { //convertToProtocolDebug(postData); uint16_t crc = NUtil::CCryptography::crc16(postData.constData(), postData.size()); postData.replace(postData.size() - sizeof(crc), sizeof(crc), reinterpret_cast<char*>(&crc), sizeof(crc)); // tworzy tymczasowa petle komunikatow QEventLoop eventLoop; // dla sygnalu QNetworkAccessManager::finished wywolaj QEventLoop::quit QNetworkAccessManager mgr; QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); // HTTP const std::string url = NEngine::CConfigurationFactory::getInstance()->getServerUrl(); QUrl qUrl(url.c_str()); QNetworkRequest req(qUrl); // typ MIME req.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream")); // wyslij post'a std::shared_ptr<QNetworkReply> reply(mgr.post(req, postData)); eventLoop.exec(); // czekaj QEventLoop::quit (czyli QNetworkAccessManager::finished) if (reply->error() == QNetworkReply::NoError) { //success LOG_DEBUG("Protocol has been sent successfully. idPackage:", protocol.getIdPackage()); CByteWrapper wrapper(reply->readAll()); // wyslanie potwierdzenia zmiany konfiguracji - jesli zmiana odbyla sie bez problemow nie zwraca danych if (wrapper.getSize() > 0) { if (!wrapper.isCRCValid()) { LOG_ERROR("Received protocol error - CRC. idPackage:", protocol.getIdPackage()); DConnectionResult res(new CConnectionResult(protocol, EConnectionStatus::CRC_ERROR)); resultsQueue.push(res); } else { std::shared_ptr<CProtocol> responseProtocol = convertToProtocol(wrapper); // przekonwertuj do struktury if (!responseProtocol) { // blad struktury protokolu LOG_ERROR("Received protocol error. idPackage:", protocol.getIdPackage()); DConnectionResult res(new CConnectionResult(protocol, EConnectionStatus::INPUT_PROTOCOL_FORMAT_ERROR)); resultsQueue.push(res); } else { LOG_DEBUG("Protocol has been received successfully. idPackage:", responseProtocol->getIdPackage()); DConnectionResult res(new CConnectionResult(protocol, responseProtocol, EConnectionStatus::NONE)); resultsQueue.push(res); } } } } else { LOG_ERROR("Protocol sending error. idPackage:", protocol.getIdPackage(), ". Error: ", reply->errorString().toStdString()); DConnectionResult res(new CConnectionResult(protocol, EConnectionStatus::CONNECTION_ERROR)); resultsQueue.push(res); } } }
void CHttpThread::convertToBinary(QByteArray& array, const CData& data) { convertToBinary(array, data.getType()); switch(data.getType()){ case EValueType::INT_8: convertToBinary(array, data.getValue().vInt8); break; case EValueType::UINT_8: convertToBinary(array, data.getValue().vUInt8); break; case EValueType::INT_16: convertToBinary(array, data.getValue().vInt16); break; case EValueType::UINT_16: convertToBinary(array, data.getValue().vUInt16); break; case EValueType::INT_32: convertToBinary(array, data.getValue().vInt32); break; case EValueType::UINT_32: convertToBinary(array, data.getValue().vUInt32); break; case EValueType::INT_64: convertToBinary(array, data.getValue().vInt64); break; case EValueType::UINT_64: convertToBinary(array, data.getValue().vUInt64); break; case EValueType::FLOAT_32: convertToBinary(array, data.getValue().vFloat32); break; case EValueType::DOUBLE_64: convertToBinary(array, data.getValue().vDouble64); break; case EValueType::VOID: convertToBinary(array, data.getValue().vVoid8); break; } }
void CHttpThread::convertToBinary(QByteArray& array, const CRequest& req) { convertToBinary(array, req.getIdSensor()); convertToBinary(array, req.getConfigurationType()); }
QString TestRunnerMobile::getServerIP() { // Get device IP (ifconfig.txt was created when connecting) QFile ifconfigFile{ _workingFolder + "/ifconfig.txt" }; if (!ifconfigFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::critical(0, "Internal error: " + QString(__FILE__) + ":" + QString::number(__LINE__), "Could not open 'ifconfig.txt'"); exit(-1); } QTextStream stream(&ifconfigFile); QString line = ifconfigFile.readLine(); while (!line.isNull()) { // The device IP is in the line following the "wlan0" line if (line.left(6) == "wlan0 ") { break; } line = ifconfigFile.readLine(); } // The following line looks like this "inet addr:192.168.0.15 Bcast:192.168.0.255 Mask:255.255.255.0" // Extract the address and mask line = ifconfigFile.readLine(); QStringList lineParts = line.split(':'); if (lineParts.size() < 4) { QMessageBox::critical(0, "Internal error: " + QString(__FILE__) + ":" + QString::number(__LINE__), "IP address line not in expected format: " + line + "(check that device WIFI is on)"); return NETWORK_NOT_FOUND; } qint64 deviceIP = convertToBinary(lineParts[1].split(' ')[0]); qint64 deviceMask = convertToBinary(lineParts[3].split(' ')[0]); qint64 deviceSubnet = deviceMask & deviceIP; // The device needs to be on the same subnet as the server // To find which of our IPs is the server - choose the 1st that is on the same subnet // If more than one found then report an error QString serverIP; QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < interfaces.count(); i++) { QList<QNetworkAddressEntry> entries = interfaces.at(i).addressEntries(); for (int j = 0; j < entries.count(); j++) { if (entries.at(j).ip().protocol() == QAbstractSocket::IPv4Protocol) { qint64 hostIP = convertToBinary(entries.at(j).ip().toString()); qint64 hostMask = convertToBinary(entries.at(j).netmask().toString()); qint64 hostSubnet = hostMask & hostIP; if (hostSubnet == deviceSubnet) { if (!serverIP.isNull()) { QMessageBox::critical(0, "Internal error: " + QString(__FILE__) + ":" + QString::number(__LINE__), "Cannot identify server IP (multiple interfaces on device submask)"); return QString("CANNOT IDENTIFY SERVER IP"); } else { union { uint32_t ip; uint8_t bytes[4]; } u; u.ip = hostIP; serverIP = QString::number(u.bytes[3]) + '.' + QString::number(u.bytes[2]) + '.' + QString::number(u.bytes[1]) + '.' + QString::number(u.bytes[0]); } } } } } ifconfigFile.close(); return serverIP; }