#include <stdio.h>
#include <stdbool.h>

#include "ip_connection.h"
#include "bricklet_ptc.h"

#define host "localhost"
#define port 4223
#define uid "woz"

__declspec(dllexport) double temp_ptc() {
    
	
	// Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object
    PTC ptc;
    ptc_create(&ptc, uid, &ipcon);

    // Connect to brickd
    if(ipcon_connect(&ipcon, host, port) < 0) {
        fprintf(stderr, "Could not connect\n");
        return 1;
    }
    // Don't use device before ipcon is connected

    // Get current temperature (unit is °C/100)
    int32_t temperature;
    if(ptc_get_temperature(&ptc, &temperature) < 0) {
        fprintf(stderr, "Could not get temperature, probably timeout\n");
        return 1;
    }

    ptc_destroy(&ptc);
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

    return temperature/100.0;
}
