// Function to update the bulb Intensity based on time. Here we also act as server. // NOTE: select() method might not be required as we use only one server socket here. Can be made much neater void updateBasedOnTime(struct timeEmulate *bulbTime, int level[10], int servSock) { /* * Initialize active sockets - code modified from * www.gnu.org/software/libc/manual/html_node/Server-Example.html * because no other reference was provided... * */ fd_set active_fd_set, read_fd_set; FD_ZERO(&active_fd_set); FD_SET(servSock, &active_fd_set); struct sockaddr_in clientAddr; unsigned int clientLength = sizeof(clientAddr); //Wait for an active connection to arrive read_fd_set = active_fd_set; if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0){ printf("select\n"); //exit(EXIT_FAILURE); }else if(FD_ISSET(servSock, &read_fd_set) && (strcmp(bulb.health,"DAMAGED"))){ int clientSock = accept(servSock, (struct sockaddr_in *)&clientAddr, clientLength); FILE *weatherFile = fdopen(clientSock, "r"); char *climate = extractClimate(weatherFile); int sunriseHour = extractHour(weatherFile); int sunsetHour = extractHour(weatherFile); close(weatherFile); //if it's before sunrise if(((*bulbTime).hour >+ 0) && ((*bulbTime).hour < sunriseHour)){ updateIntensity(level[8], climate); //after sunrise but before midday(ish) }else if(((*bulbTime).hour < sunriseHour+4) && ((*bulbTime).hour >= sunriseHour)){ updateIntensity(level[2], climate); //before sunset and after midday(ish) }else if(((*bulbTime).hour >= sunriseHour+4) && ((*bulbTime).hour < sunsetHour)){ updateIntensity(0, climate); //after sunset and before late night }else if(((*bulbTime).hour >= sunsetHour) && ((*bulbTime).hour < sunsetHour+4)){ updateIntensity(level[5], climate); }else if(((*bulbTime).hour >= sunsetHour+4)){ updateIntensity(level[8], climate); }else{ printf("ERROR: NO UPDATE TO INTENSITY, BAD TIMING\n"); } free(climate); close(weatherFile); printf("SUCCESS: UPDATED BASED ON TIME\n"); } }
bool MSNet::isNight(std::string dateTimeString) { int month=extractMonth(dateTimeString), hour=extractHour(dateTimeString); if(month<3||month>11) { // December, January or February return((hour>=18)&&(hour<8)); } else if(month<6) { // March, April or May return((hour>=19)&&(hour<7)); } else if(month<9) { // June, July or August return((hour>=20)&&(hour<7)); } else { // September, October or November return((hour>=19)&&(hour<7)); } }