ResponseDHT CheeseDHT::get() { delay(100); float h = dht->readHumidity(); delay(100); float t = dht->readTemperature(); ResponseDHT resp2 = {(int)t, (int)h, CheeseTime::Current(), millis()}; if (!IsValidResponse(resp2) && IsValidResponse(respPrevious)) return respPrevious; respPrevious = resp2; return resp2; }
BOOL KDnsRecorder::RecordRecv( PROTO_TYPE pt, DWORD uLocalIp, USHORT uLocalPort, DWORD uRemoteIP, USHORT uRemotePort, PVOID pData, DWORD uDataLen ) { DNS_HEADER* pDns = NULL; string strHostName; list<DWORD> uAddrList; BOOL bResult = FALSE; DWORD nAnsPos = 0; DWORD nAnsCount = 0; do { if( uDataLen <= sizeof(DNS_HEADER) ) break; if( uRemotePort != 53 ) break; pDns = (DNS_HEADER*)pData; if( !IsValidResponse(pDns) ) break; if( m_sendDnsTansID.find( pDns->id ) == m_sendDnsTansID.end() ) break; m_sendDnsTansID.erase( pDns->id ); nAnsPos = GetHost( pData, uDataLen, strHostName ); if( nAnsPos == 0 || nAnsPos >= uDataLen ) break; nAnsCount = ntohs(pDns->ans_count); if( !GetHostAddr( pData, uDataLen, nAnsPos, nAnsCount, uAddrList ) ) break; m_hostIpList[strHostName] = uAddrList; //remove { struct sockaddr_in a; cout << strHostName <<endl; list<DWORD> &ipList = uAddrList; cout << "-------------------------------"<<endl; for( list<DWORD>::iterator ipItem = ipList.begin(); ipItem != ipList.end(); ipItem++ ) { a.sin_addr.s_addr=(*ipItem); //working without ntohl cout << "\t"<< inet_ntoa(a.sin_addr) << endl; } } bResult = TRUE; } while (FALSE); return bResult; }
String CheeseDHT::toJSON() { ResponseDHT resp = get(); return !IsValidResponse(resp) ? "{\"pin\":" + String(pin) + ",\"name\":\"" + name + "\",\"error\":\"Read failed\"}" : "{\"pin\":" + String(pin) + ",\"name\":\"" + name + "\",\"t\":" + String(resp.temperature) + ",\"h\":" + String(resp.humidity) + ",\"date\":" + resp.date + ", \"seconds_ago\":"+String((millis()-resp.millis)/1000)+"}"; }