using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SanShared;
using Tinkerforge;

namespace TempCAN
{
    /// <summary>
    /// 
    /// </summary>
    public class TinkerForgeTemperatur : ITemperature
    {
        private static string HOST = "localhost";
        private static int PORT = 4223;
        private static string UID = "dW3";
        double temperatur;
        bool erfolg = true;

        /// <summary>
        /// 
        /// </summary>
        public TinkerForgeTemperatur()
        {
            IPConnection ipcon = new IPConnection();
            BrickletTemperature t = new BrickletTemperature(UID, ipcon);

            ipcon.Connect(HOST, PORT);
            short temp;
            try
            {
                temp = t.GetTemperature();
                
            }
            catch(Tinkerforge.TimeoutException)
            {
                temp = 100;
                erfolg = false;
            }
            temperatur = (temp / 100.0);
            ipcon.Disconnect();
            t = null;
            ipcon = null;
        }

        /// <summary>
        /// 
        /// </summary>
        public double GetTemperatur(out string message)
        {
            message = "";
            if (!erfolg) message = "Es konnte keine Verbindung mit der TemperaturSystem aufgebaut werden";
            return temperatur;
        }
    }
}
