TimeMs Timer::remainingTime() const { if (!isActive()) { return -1; } auto now = getms(true); return (_next > now) ? (_next - now) : TimeMs(0); }
void InfiniteRadialAnimation::start(TimeMs skip) { const auto now = getms(); if (_workFinished <= now && (_workFinished || !_workStarted)) { _workStarted = std::max(now + _st.sineDuration - skip, TimeMs(1)); _workFinished = 0; } if (!_animation.animating()) { _animation.start(); } }
void cGraphLCDState::SetVolume(int Volume, bool Absolute) { // printf("graphlcd plugin: cGraphLCDState::SetVolume %d %d\n", Volume, Absolute); if (GraphLCDSetup.PluginActive) { mutex.Lock(); volume.value = Volume; if (!first) { volume.lastChange = TimeMs(); mutex.Unlock(); Display.Update(); } else { // first time first = false; mutex.Unlock(); } } }
int main (int argc, char ** argv ) { /// first time inits. pollfds.events = POLLIN; serverAddrInfoGotten = false; /* // Test the replacer char * b = new char[500]; strcpy(b, "Hello world!\n\t\nThinker toys."); unsigned char one41 = 141; char * buf = new char[4]; buf[0] = (unsigned char) 141; buf[1] = '\0'; strcat(b, buf); strcat(b, "Hellow world"); strcat(b, buf); b[14] = 141; int len = strlen(b); std::cout<<"\n"<<b; ReplaceStringKeys(b, len); std::cout<<"\n"; return 0; */ int udpPort = SERVER_PORT_3333; int tcpPort = 4444; int counter = 0; int MAX_COUNTER_SIZE=20; std::cout<<"\nArgc: "<<argc; bool isPi = false; /// Parse inputs. if (argc > 1) { // Parse ports char * portStr = argv[1]; udpPort = atoi(portStr); std::cout<<"\nUsing udp port: "<<udpPort; if (udpPort == 514) isPi = true; } /// Open ports bool ok = BindUdpSocket(udpPort); if (!ok) { std::cout<<"\nUnable to bind UDP socket to port "<<udpPort<<" D:"; return -1; } /// After opening port, try send to pi if specified /* if (argc > 1 && strstr(argv[1], "testpi")) { TestThePi(); }*/ /// Edited /// Set on receiving first message of the batch. Compared to when determining when to start the long sleep. long firstReceiveTime = TimeMs(); /// bool waitingForLastMessage = false; int receivedMessages = 0; /// If true, will try and batch all messages to send. bool batchMessages = true; // std::cout<<"\nStart of loop."; Sleep(500); /// Format with time-stamp std::string dateStr; while(true) { // std::cout<<"\nReady to read?"; // Read timeval t = {0, 1000}; // Check time since last message. if (waitingForLastMessage) { long timeDiff = TimeMs() - firstReceiveTime; if (isPi && batchMessages && timeDiff > 240000) { if (totalReceivedBytes > 0) SendBatchToServer(); } /// If sufficient time has passed since first message (4 minutes), start the long sleep (9 minutes). // std::cout<<"\nTimeMs(): "<<TimeMs()<<" firstReceiveTime: "<<firstReceiveTime<<" diff: "<<TimeMs() - firstReceiveTime; if (timeDiff > 240000) { waitingForLastMessage = false; /// Send acc later std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; Sleep(540000); receivedMessages = 0; } } /// If final message.. send it? if (!ReadyToRead(sockfd)) { if (isPi) ;//Sleep(80); else Sleep(2000); continue; } if (isPi) dateStr = currentDateTime(); // std::cout<<"\nBytes to read"; int flags = 0; // std::cout<<"\nSize of sock Addr: "<<sizeofSockaddr<<" vs "<<sizeof(sockaddr); int bytesRead = recvfrom(sockfd, inputBuffer, INPUT_BUFFER_SIZE-1, flags, &fromAddress, (socklen_t*) &sizeofSockaddr); inputBuffer[bytesRead] = '\0'; // Add null-char so it is printable. // std::cout<<"\nReceived: "<<inputBuffer; if (bytesRead == SOCKET_ERROR) { int error = GetLastError(); std::cout<<"\nError reading socket from given address. ErrorCode: "<<error; continue; } lastReceiveTime = TimeMs(); // std::cout<<"|"; //<<inputBuffer // Flag as waiting for last message. waitingForLastMessage = true; /// First message, initialize stuff. if (receivedMessages == 0) { firstReceiveTime = TimeMs(); if (!isPi) // Clear log file if on server and first message. ClearLogFile(); } ++receivedMessages; if (isPi) { // std::cout<<"\nThis is the PI,"; /// Move the body of the text. int dateStrLen = dateStr.length(); int fullString = bytesRead + dateStrLen; /// Send to the server straight away, first test. if (batchMessages) { // memmove(inputBuffer + dateStrLen, inputBuffer, bytesRead); // memcpy(inputBuffer, dateStr.c_str(), dateStrLen); /// std::cout<<"\nFull string to send: "<<inputBuffer; /// Append into total input. memcpy(totalInputBuffer + totalReceivedBytes, dateStr.c_str(), dateStrLen); totalReceivedBytes += dateStrLen; memcpy(totalInputBuffer + totalReceivedBytes, inputBuffer, fullString); totalReceivedBytes += bytesRead; // memcpy(total) // std::cout<<"\nBatched"; // memcpy(totalInputBuffer + totalReceivedBytes, inputBuffer, fullString); // totalReceivedBytes += fullString; /// Separate messages with \n /// Magic separator 141 between each line. totalInputBuffer[totalReceivedBytes - 1] = 141; // ++totalReceivedBytes; if (totalReceivedBytes > SEND_THRESHOLD) SendBatchToServer(); } else { memmove(inputBuffer + dateStrLen, inputBuffer, bytesRead); memcpy(inputBuffer, dateStr.c_str(), dateStrLen); /// std::cout<<"\nFull string to send: "<<inputBuffer; SendToServer(inputBuffer, fullString); std::cout<<" sending to server"; } } /// SERVER else { /// Replace #000 numbers with chars int newLength = ReplaceStringKeys(inputBuffer, bytesRead); std::fstream file; /// append for now file.open("/opt/gclc/gclc.log", std::ios_base::out | std::ios_base::app); if (file.is_open()) { file.write((char*) inputBuffer, newLength); file.close(); } else { std::cout<<"unable to open file D:"; } std::cout<<"|"; // receivedMessages } // Sleep(50); } /// Free up resources (if we ever exit...) freeaddrinfo(servInfo); return 0; }