/* 
 * File:   main.cpp
 * Author: Jan Hudec
 *
 * Created on 25. dubna 2019, 17:49
 */

// generic libraries
#include <cstdlib>
#include <iostream>

// custom libraries
#include "ziehl_tr1200ip_simulator.h"

int main(int argc, char** argv)
{
    printf("\nZiehl TR1200IP Simulator, using libiec61850 library\n");

    ziehl_tr1200ip_simulator sim;
    try
    {
        sim.parse_config("tr1200ip_config.cfg");
        
        printf("Running. Press CTRL+C to quit.\n\n");

        uint64_t time_old = Hal_getTimeInMs();
        //uint64_t time_goose_old = Hal_getTimeInMs();
        bool running = true;

        while (running)
        {
            if (Hal_getTimeInMs() - time_old >= sim.time_treshold)
            {
                sim.generate_temperatures();
                sim.save_temperatures("temp_values.txt");
                time_old = Hal_getTimeInMs();
            }
            
            // async spontaneous GOOSE transmit
            /*if (sim.goose_time_treshold != -1
                && Hal_getTimeInMs() - time_goose_old >= sim.goose_time_treshold)
            {
                sim.retransmit_goose();
                time_goose_old = Hal_getTimeInMs();
            }*/
        }
    }
    catch (const std::exception& e)
    {
        printf("Caught exception: %s\n", e.what());
    }

    return 0;
}