String Utils::GetRelativeTimeFromTimeStamp(DateTime receivedTime) { DateTime currentTime; //DateTime receivedTime; String relativeTime; int mins = 0; int hours = 0; int days = 0; SystemTime::GetCurrentTime(WALL_TIME, currentTime); //receivedTime = Utils::ConvertUtcTimeToStardardTime(timeStamp); TimeSpan timeDiff = (currentTime.GetTime() - receivedTime.GetTime()); days = timeDiff.GetDays(); hours = timeDiff.GetHours(); mins = timeDiff.GetMinutes(); if(days >= 1) { relativeTime.Format(20, L"%d days ago", days); } else if (hours > 0) relativeTime.Format(20, L"%dhr%dmin ago",hours, mins); else relativeTime.Format(20, L"%dmin ago", mins); return relativeTime; }
core::string DateTime::ToString(const DateTime& dt) { std::ostringstream stream; stream << dt.GetDate().GetDay() << mT("/") << dt.GetDate().GetMonth() << mT("/") << dt.GetDate().GetYear(); stream << "-"; stream << dt.GetTime().GetHour() << mT(":") << dt.GetTime().GetMinute() << mT(":") << dt.GetTime().GetSecond(); return stream.str(); }
// the loop routine runs over and over again forever: void loop() { int result; #ifdef USE_DHT22_SERIAL getTempAndHumidityFromSerial(); #endif // HACK - reset the matrix after every 100 loops - in case 7219s get confused if (loop_count >= MATRIX_RESET_FREQUENCY) { if (WRITE_TO_SERIAL == 0) { m.init(); m.setIntensity(1); } loop_count = 0; } else { loop_count += 1; } double avgValue; int offset; // read local sensors if (millis() - last_reading_millis >= MEASUREMENT_INTERVAL // check if millis has looped back to 0 || (millis() < last_reading_millis)) { total_readings += 1; // not supported on Galileo2 yet //total_cpu_temp += readCPUTemp(); #ifdef USE_BMP085 bmptemp = bmp.readTemperature(); total_bmp085_temp += bmptemp; bmppress = bmp.readPressure(); bmppressK = bmppress / (double)1000; total_bmp085_pressure += bmppressK; Log("bmp085 temp = %.2f press = %.1f\r\n", bmptemp, bmppressK); #endif #ifdef USE_TH02 th02temp = TH02.ReadTemperature(); total_th02_temp += th02temp; th02hum = TH02.ReadHumidity(); total_th02_humidity += th02hum; Log("TH02 temp = %.1fC humidity = %.1f%%\r\n", th02temp, th02hum); #endif last_reading_millis = millis(); } // read online APIs if (weather != nullptr && (millis() - last_weather_millis >= WEATHER_MEASUREMENT_INTERVAL // check if millis has looped back to 0 || (millis() < last_weather_millis))) { result = weather->GetCurrent(); Log("weather->GetCurrent returned %d\r\n", result); if (result == 0) { last_weather_millis = millis(); } } if (weather != nullptr && (millis() - last_forecast_millis >= FORECAST_MEASUREMENT_INTERVAL // check if millis has looped back to 0 || (millis() < last_forecast_millis))) { result = weather->GetForecast(); Log("weather->GetForecast returned %d\r\n", result); if (result == 0) { last_forecast_millis = millis(); } } // fill the data buffer with 00s //memset(matrixData, 0, sizeof(matrixData)); // put date and time in buffer offset = 0; column = 0; if (WRITE_TO_SERIAL >= 1) { // add prefix expected by Arduino code offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "D0:"); } // write time to LED matrix offset += dt.GetTime(matrixData + offset, MATRIX_DATA_LEN - offset, false, false); // add space offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, " "); // write date to LED matrix --> uncomment if you want the date to be displayed //offset += dt.GetDate(matrixData + offset, MATRIX_DATA_LEN - offset); // add space //offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, " "); #ifdef USE_BMP085 // write temperature to LED matrix offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%.1fC ", bmptemp); #endif #ifdef USE_TH02 offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%.1fC %.1f%% ", th02temp, th02hum); #endif #ifdef USE_DHT22_SERIAL offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%sC %s%% ", dht22Temp, dht22Hum); #endif // add space //offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, " "); // write indoor barometer to LED matrix //offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%.1fkPa", bmppressK); // add space //offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, " "); // write current weather from Weather Underground API //TODO - check weather->lastreading time and skip if out-of-date if (weather != nullptr) { offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "Out %dC ", weather->TempC); if (weather->TempC != weather->TempFeelsLikeC) { offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "Feels Like %dC ", weather->TempFeelsLikeC); } if (weather->WindKPH > 5) { offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "Wind %d kph", weather->WindKPH); } } if (WRITE_TO_SERIAL >= 1) { Log("Writing to serial %s\n", matrixData); //HACK - seems that Serial.println isn't sending a CR, causing Arduino to hang matrixData[offset] = '\n'; matrixData[offset + 1] = 0x00; Serial.print(matrixData); } else { m.clear(); printString(matrixData); } // NOTE: the 2nd part of this if statement -- millis < last_upload_millis -- checks if the millisecond // counter has looped around to 0 (which occurs every 70 days) if (!(cosmAPIKey.empty() || cosmFeed.empty())) { if (millis() - last_upload_millis >= COSM_UPLOAD_INTERVAL || (millis() < last_upload_millis)) { if (total_readings > 0) { // not supported on Galileo 2 yet //avgValue = total_cpu_temp / (double)total_readings; //cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmCPUTempDatastream.c_str(), avgValue); #ifdef USE_BMP085 avgValue = total_bmp085_temp / (double)total_readings; cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmTempDatastream.c_str(), avgValue); avgValue = total_bmp085_pressure / (double)total_readings; cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmPressureDatastream.c_str(), avgValue); //TEST - record outside pressure for comparison cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), "PressureOutdoors", weather->PressureMB); total_bmp085_pressure = 0; total_bmp085_temp = 0; #endif #ifdef USE_TH02 avgValue = total_th02_temp / (double)total_readings; cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmTempDatastream.c_str(), avgValue); avgValue = total_th02_humidity / (double)total_readings; cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmHumidityDatastream.c_str(), avgValue); total_th02_humidity = 0; total_th02_temp = 0; #endif #ifdef USE_DHT22_SERIAL if (strlen(dht22Temp) > 0) { cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmTempDatastream.c_str(), stof(dht22Temp)); } if (strlen(dht22Hum) > 0) { cosm.SendToCosm(cosmAPIKey.c_str(), stoi(cosmFeed), cosmHumidityDatastream.c_str(), stof(dht22Hum)); } #endif total_readings = 0; total_cpu_temp = 0; last_upload_millis = millis(); } } } if (WRITE_TO_SERIAL == 1) { // wait 20 seconds for the Arduino to do its thing getTempAndHumidityFromSerial(); delay(20000); getTempAndHumidityFromSerial(); } else if (WRITE_TO_SERIAL == 0) { // wait 1 seconds before scrolling delay(1000); // scroll until the last column is visible column = column - (8 * matrixCount); for (int i = 0; i < column; i++) { m.shiftLeft(false, false); delay(MATRIX_SCROLL_MS); } // delay before clearing the display delay(2000); } if (WRITE_TO_SERIAL == 2) { // write the next forecast to the 2nd display if (weather != nullptr) { if (!weather->ForecastDays[next_forecast].empty()) { offset = sprintf_s(matrixData, MATRIX_DATA_LEN, "D0:Forecast %s - ", weather->ForecastDays[next_forecast].c_str()); // forecast can be wordy, so truncate it if necessary offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%s", weather->Forecasts[next_forecast].substr(0, MATRIX_DATA_LEN - offset - 2).c_str()); Log("Writing to serial1 %s\n", matrixData); //HACK - seems that Serial.println isn't sending a CR, causing Arduino to hang matrixData[offset] = '\n'; matrixData[offset + 1] = 0x00; Serial1.print(matrixData); // advance to next day - its forecast will be displayed the next time through next_forecast += 1; if (next_forecast >= weather->numDaysForecast) { next_forecast = 0; } // give displays time to scroll before moving on delay(15000); } } } else { // every 5th time through the loop, display the forecast if (loop_count % 20 == 1 && weather != nullptr) { for (int day = 0; day < 3; day++) { if (!weather->ForecastDays[day].empty()) { offset = sprintf_s(matrixData, MATRIX_DATA_LEN, "D0:Forecast %s - ", weather->ForecastDays[day].c_str()); // forecast can be wordy, so truncate it if necessary offset += sprintf_s(matrixData + offset, MATRIX_DATA_LEN - offset, "%s", weather->Forecasts[day].substr(0, MATRIX_DATA_LEN - offset - 2).c_str()); if (WRITE_TO_SERIAL == 1) { Log("Writing to serial %s\n", matrixData); //HACK - seems that Serial.println isn't sending a CR, causing Arduino to hang matrixData[offset] = '\n'; matrixData[offset + 1] = 0x00; Serial.print(matrixData); int delayTime = offset / 4; // give Arduino time to read data before moving onto next day //TODO - have Arduino send feedback when it has scrolled through all of the text getTempAndHumidityFromSerial(); delay(delayTime * 1000); } //TODO //else //{ // m.clear(); // printString(matrixData); //} } } } } }