time_ref time_copy(time_ref original) { time_ref result = new_time(); if(!result) return FAILURE; result->clock_time = original->clock_time; result->milliseconds = original->milliseconds; result->seconds = original->seconds; result->minutes = original->minutes; result->hours = original->hours; result->days = original->days; return result; }
void *crzpt_cpp_make_plan(int time, short live, char *data) { static unsigned int g_pidx_mark = 0; unsigned int pidx = __sync_fetch_and_add(&g_pidx_mark, 1); x_printf(D, "pidx %d time %d live %d\n", pidx, time, live); if ((time >= ONE_DAY_TIMESTAMP) || ((live != 0) && (live != 1)) ){ x_perror("error plan parameter!"); goto FAIL_NEW_PLAN; } /*ok*/ struct crzpt_time_node *p_time = NULL; struct crzpt_plan_node *p_plan = NULL; /*new*/ p_plan = calloc( 1, sizeof(struct crzpt_plan_node) ); if (p_plan == NULL){ x_perror("new plan"); goto FAIL_NEW_PLAN; } /*set*/ p_plan->pidx = pidx; p_plan->live = live; /*add*/ p_time = find_time( time ); if (p_time == NULL){ p_time = new_time( time ); if (p_time == NULL){ free(p_plan); x_perror("new time"); goto FAIL_NEW_PLAN; } add_time(p_time); } X_LOCK( &g_plan_lock ); p_time->nubs++; p_plan->next = &p_time->pl_head; p_plan->prev = p_time->pl_head.prev; p_time->pl_head.prev->next = p_plan; p_time->pl_head.prev = p_plan; char temp[64] = {0}; sprintf(temp, "%d", pidx); g_crzpt_settings.conf->store_insert(temp, strlen(temp), data, strlen(data));//FIXME X_UNLOCK( &g_plan_lock ); return p_plan; FAIL_NEW_PLAN: return NULL; }
static void Realize (Widget gw, XtValueMask *valueMask, XSetWindowAttributes *attrs) { ClockWidget w = (ClockWidget)gw; if (w->clock.backing_store != Always + WhenMapped + NotUseful) { attrs->backing_store = w->clock.backing_store; *valueMask |= CWBackingStore; } if (w->clock.transparent) { attrs->background_pixel = w->clock.minute; *valueMask |= CWBackPixel; *valueMask &= ~CWBackPixmap; } XtCreateWindow( gw, (unsigned)InputOutput, (Visual *)CopyFromParent, *valueMask, attrs ); if (!w->clock.transparent) Resize (gw); new_time ((XtPointer) gw, NULL); }
time_ref new_time_from_string(uint8_t * string) { time_ref result = 0; result = new_time(); if(!result) return FAILURE; uint16_t cur_ptr = 0; result->days = read_number_from_string(&string[cur_ptr], 3); cur_ptr += 3; result->hours = read_number_from_string(&string[cur_ptr], 2); cur_ptr += 2; result->minutes = read_number_from_string(&string[cur_ptr], 2); cur_ptr += 2; result->seconds = read_number_from_string(&string[cur_ptr], 2); cur_ptr += 2; result->milliseconds = read_number_from_string(&string[cur_ptr], 3); cur_ptr += 3; result->clock_time = read_number_from_string(&string[cur_ptr], 2); cur_ptr += 2; return result; }
event_ref new_timer_event(uint8_t timer, uint16_t period, uint8_t repeat, uint16_t repeat_count) { event_ref e = 0; action_ref check_action = 0; node_ref period_arg = 0; node_ref trigger_arg = 0; time_ref time_trigger = 0; time_ref time_period = 0; for(;;) { // Create a new time period time_period = new_time(); if(!time_period) break; // Set up the period if(timer == CLOCK_TIME) { time_period->clock_time = period; } else if (timer == MILLISECONDS) { time_period->milliseconds = period; } else if (timer == SECONDS) { time_period->seconds = period; } else if (timer == MINUTES) { time_period->minutes = period; } else if (timer == HOURS) { time_period->hours = period; } else if (timer == DAYS) { time_period->days = period; } // Create a new time trigger time_trigger = new_time(); if(!time_trigger) break; // Set up the trigger = global + period add_time_to_time(time_trigger, global_time()); add_time_to_time(time_trigger, time_period); // Create a new event e = new_event(); if(!e) break; // Create a new action check_action = new_action(); if(!check_action) break; // Set its function action_set_func(check_action, &check_time_args); // Create a trigger arg trigger_arg = new_node(time_trigger, &time_delete); if(!trigger_arg) break; // Create the period arg period_arg = new_node(time_period, &time_delete); if(!period_arg) break; node_append(period_arg, trigger_arg); action_set_args(check_action, period_arg); event_set_check(e, check_action); event_set_repeat(e, repeat, repeat_count); return e; } event_delete(&e); node_delete(&period_arg); node_delete(&trigger_arg); action_delete(&check_action); time_delete(&time_period); time_delete(&time_trigger); return FAILURE; }
/***************************************************************************** * Function - RunSubTask * DESCRIPTION: * *****************************************************************************/ void LoggingCtrl::RunSubTask() { mRunRequestedFlag = false; MpcTime new_time(true); U32 new_timestamp = new_time.GetSecondsSince1Jan1970(); U32 new_hour = new_timestamp/3600; U32 old_hour = mpHourLogTimestamp->GetValue()/3600; if (old_hour == 0) { // Special handling first time after log data clear (or flash boot) MarkHourLogInvalid(mpPumpedVolume72hLog->GetMaxSize()); UpdateDayLog(); mpHourLogTimestamp->SetValue(new_timestamp); mFirstTimeToRun = false; } if (mFirstTimeToRun == true) { if (new_hour > old_hour) { // Check/update loggings for the time elapsed during power down if (new_hour - old_hour > mpPumpedVolume72hLog->GetMaxSize()) { old_hour = new_hour - mpPumpedVolume72hLog->GetMaxSize(); } UpdateHourLog(); // Fill old hour accumulators in log MarkHourLogInvalid(new_hour-old_hour-1); while (new_hour > old_hour) { old_hour++; if (old_hour%24 == mpDayLoggingStartHour->GetValue()) { // Time for day logging has passed UpdateDayLog(); } } mpHourLogTimestamp->SetValue(new_timestamp); } mFirstTimeToRun = false; } UpdateRunningAccumulators(); if (new_hour > old_hour && mLogDataSimulation == false) { // The clock hour has increased. Move hour accumulators to log UpdateHourLog(); if (new_hour%24 == mpDayLoggingStartHour->GetValue()) { // Time for day logging has passed UpdateTodayAverages(24); UpdateDayLog(); } UpdateTodayAverages((new_hour-mpDayLoggingStartHour->GetValue()) % 24); mpHourLogTimestamp->SetValue(new_timestamp); } else if (new_hour < old_hour) { // The clock must have been set back, e.g. daylight saving period gone // Just prepare to trig the logging on next clock hour mpHourLogTimestamp->SetValue(new_timestamp); } // Special test function: if (mpFillSimulatedLogData.IsUpdated() && GeniAppTestMode::GetInstance()->GetTestMode() == true) { mLogDataSimulation = true; FillValuesForTest(); } else if (mLogDataSimulation == true && GeniAppTestMode::GetInstance()->GetTestMode() == false) { // Disable simulation if test mode is disabled mLogDataSimulation = false; // These function calls will destroy the simulated data UpdateHourLog(); MarkHourLogInvalid(mpPumpedVolume72hLog->GetMaxSize()); UpdateDayLog(); UpdateDayLog(); // Twice to get rid of yesterday log as well } }
void main(void) { // Have to init the clock first init_clock(); // These initialize variables // Mostly just zeroing them okay_to_transmit = FALSE; server_wants_header = FALSE; inside_non_blocking_interrupt = FALSE; last_connect_time = 0; check_in_period = 0; setup_button_pressed = FALSE; setup_button_time_trigger = 0; setup_button_time_duration = 0; main_mode = MAIN_MODE_INIT; init_internal_wattage_sensor(); init_temperature_sensor(); init_audio_sensor(); init_light_sensor(); uint8_t itor = 0; for (itor = 0; itor < NUMBER_OF_AUX_PORTS; itor++) { aux_sensor[itor] = 0; } // These initialize functions and interrupts init_timer(); init_time(); init_leds(); init_relay(); init_uart(); init_transmits(); init_buttons(); init_roving(&roving_call_back); setup_button_time_trigger = new_time(); setup_button_time_duration = new_time(); time_set_seconds(setup_button_time_duration, 2); _enable_interrupts(); // confirm roving works while (!enter_command_mode()) { reset_roving(); wait(500); } init_adc(); init_pll(); // This is for the check in // If we are disconnected, we wait 5 seconds before trying to reconnect last_connect_time = new_time(); check_in_period = new_time(); time_set_seconds(check_in_period, 5); // And go!!! set_led_anim(led_start); // MAIN LOOP while(1) { handle_roving_input(); // Check if it a long hold if(setup_button_pressed) { // If the button is pressed down if(time_cmp(global_time(), setup_button_time_trigger) >= 0) { // If enough time has passed if(in_setup_mode()) { leave_setup_mode(); } else { start_setup_mode(); } // Only want to check this once setup_button_pressed = FALSE; } } // MAIN BEHAVIOR // There is setup_mode and main_mode. // Setup mode is for configuring WiFi SSID and PASS // Main mode is for sampling if (in_setup_mode()) { if(main_mode != MAIN_MODE_SETUP) { stop_sampling(); set_led_anim(led_setup_start); main_mode = MAIN_MODE_SETUP; } do_setup(); } else if (in_main_mode()) { //regular mode if(main_mode != MAIN_MODE_SAMPLE) { set_led_anim(led_main_start); start_sampling(); main_mode = MAIN_MODE_SAMPLE; } if(!is_associated()) { associate(); } else if (!have_dhcp()) { get_dhcp(); } else { if(is_connected()) set_led_anim(led_main_connected); else set_led_anim(led_main_assoc); if(!is_connected() && (time_cmp(global_time(), last_connect_time) >= 0) ) { // If we are not connected, and enough time has passed, connect connect(); add_time_to_time(last_connect_time, check_in_period); } if(server_wants_header) { led_ping(); exit_command_mode(); transmit_header(); wait(100); } if(okay_to_transmit && have_data_to_transmit()) { led_ping(); exit_command_mode(); transmit_data(); } } } else { main_mode = MAIN_MODE_INIT; set_led_anim(led_error); stop_sampling(); } } }
/***************************************************************************** * Function - UpdateAccumulatedUSDCounter * DESCRIPTION: Update counter accumulators * *****************************************************************************/ void UserDefinedCounterCtrl::UpdateAccumulatedUSDCounter(int use_counter) { MpcTime new_time(true); U32 new_timestamp = new_time.GetSecondsSince1Jan1970(); U32 new_hour = new_timestamp/3600; U32 old_hour = mpHourLogTimestamp[use_counter]->GetValue()/3600; if(old_hour ==0) { mpHourLogTimestamp[use_counter]->SetValue(new_timestamp); } if( mpRawUserDefinedCntPulses[0].IsUpdated()) { U8 new_pulses = (U8)mpRawUserDefinedCntPulses[use_counter]->GetValue(); } if (mpRawUserDefinedCntPulses[use_counter]->GetQuality() == DP_AVAILABLE) { U8 delta_pulses = 0; U8 new_pulses = (U8)mpRawUserDefinedCntPulses[use_counter]->GetValue(); mpTotalUserDefinedCount[use_counter]->SetQuality(DP_AVAILABLE); if (mPulsesReady[use_counter] == false) { // Wait until pulse input is alive if (new_pulses != 0) { mPulsesReady[use_counter] = true; mOldPulses[use_counter] = new_pulses; } } else { float total_usdcnt = mpTotalUserDefinedCount[use_counter]->GetValue(); float pulse_usdcnt_ratio = mpPulseUSDCntRatioDisplay[use_counter]->GetValue(); // float today_cnt[3]; // Update pulses and calculate energy increment delta_pulses = new_pulses - mOldPulses[use_counter]; if (delta_pulses > 40) { // Some kind of start up or illegal situation delta_pulses = 0; } //today_cnt- Start if (new_hour > old_hour) { if (new_hour%24 ==0) { yesterday[use_counter] = total_today_cnt[use_counter]; mpYesterdayCounter[use_counter]->SetValue(yesterday[use_counter]); total_today_cnt[use_counter] = 0; mpTodayCounter[use_counter]->SetValue(total_today_cnt[use_counter]); } mpHourLogTimestamp[use_counter]->SetValue(new_timestamp); } else if (new_hour < old_hour) { // The clock must have been set back, e.g. daylight saving period gone // Just prepare to trig the logging on next clock hour mpHourLogTimestamp[use_counter]->SetValue(new_timestamp); } today_cnt[use_counter] = (((mIncrement[use_counter]) * pulse_usdcnt_ratio) + delta_pulses)/pulse_usdcnt_ratio; // total_today_cnt[use_counter] = total_today_cnt[use_counter]+ today_cnt[use_counter]+ mpTodayCounter[use_counter]->GetValue(); total_today_cnt[use_counter] = today_cnt[use_counter]+ mpTodayCounter[use_counter]->GetValue(); mpTodayCounter[use_counter]->SetValue(total_today_cnt[use_counter]); //today_cnt-End mOldPulses[use_counter] = new_pulses; mIncrement[use_counter] = (((total_usdcnt + mIncrement[use_counter]) * pulse_usdcnt_ratio) + delta_pulses)/pulse_usdcnt_ratio; mIncrement[use_counter] = modff(mIncrement[use_counter], &total_usdcnt); mpTotalUserDefinedCount[use_counter]->SetValue((U32)total_usdcnt); } } else { // No User defined counter pulses available. mPulsesReady[use_counter] = false; mpTotalUserDefinedCount[use_counter]->SetValue(0); mpTotalUserDefinedCount[use_counter]->SetQuality(DP_NEVER_AVAILABLE); // mpTodayCounter[use_counter]->SetValue(0); mpTodayCounter[use_counter]->SetQuality(DP_NEVER_AVAILABLE); // mpYesterdayCounter[use_counter]->SetValue(0); mpYesterdayCounter[use_counter]->SetQuality(DP_NEVER_AVAILABLE); } }