
/*---------------------------------------------------------------------------------------------------------------

                 _/                                                    _/        _/            _/   
                    _/_/_/    _/  _/_/    _/_/    _/_/_/  _/_/        _/      _/_/_/_/    _/_/_/    
               _/  _/    _/  _/_/      _/    _/  _/    _/    _/      _/        _/      _/    _/     
              _/  _/    _/  _/        _/    _/  _/    _/    _/      _/        _/      _/    _/      
             _/  _/    _/  _/          _/_/    _/    _/    _/      _/_/_/_/    _/_/    _/_/_/       


             Copyright:  inrom Ltd
             Written by: Neil Haran

-------------------------------------------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using TheOwl;

namespace api_test
{
    public partial class Form1 : Form
    {
        TheOwl.EnergyMonitor myOwl = new TheOwl.EnergyMonitor();

        public Form1()
        {
            InitializeComponent();
        }
        
        private void btnInit_Click(object sender, EventArgs e)
        {
            if (!myOwl.Init())
                MessageBox.Show("Init(): Failed");
            else
            {
                btnInit.Enabled = false;
                btnIsAvailable.Enabled = btnQuery.Enabled = btnHistoryEnable.Enabled = 
                    btnHistoryDisable.Enabled = btnHistoryQuery.Enabled = btnFree.Enabled = true;
            }
        }

        private void btnIsAvailable_Click(object sender, EventArgs e)
        {
            MessageBox.Show(myOwl.IsAvailable() ? "The Owl found." : "The Owl unavailable.");
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Sensor[] query = myOwl.Query();
            String msg = String.Format("No usage data received.");
            int slot = 0;

            foreach (Sensor sensor in query)
            {
                if (sensor.valid)
                {
                    msg = String.Format("Usage query successful - first entry:\n    Array slot: {0}\n    Sensor address: {1}\n    Amps: {2}",
                        slot, sensor.addr.ToString(), sensor.currentAmps.ToString());
                    break;
                }
                slot++;
            }
            MessageBox.Show(msg);
        }

        private void btnHistoryEnable_Click(object sender, EventArgs e)
        {
            myOwl.SetCollectHistory(true);
        }

        private void btnHistoryDisable_Click(object sender, EventArgs e)
        {
            myOwl.SetCollectHistory(false);
        }

        private void btnHistoryQuery_Click(object sender, EventArgs e)
        {
            History history = myOwl.QueryHistory();
            String msg;

            if (history.valid)
                msg = String.Format("History query successful:\n    Date-time: {0}\n    Amps: {1}",
                    history.dateTime.ToString(), history.currentAmps.ToString());
            else
                msg = String.Format("No history data received.");

            MessageBox.Show(msg);
        }

        private void btnFree_Click(object sender, EventArgs e)
        {
            myOwl.Free();

            btnInit.Enabled = true;
            btnIsAvailable.Enabled = btnQuery.Enabled = btnHistoryEnable.Enabled =
                btnHistoryDisable.Enabled = btnHistoryQuery.Enabled = btnFree.Enabled = false;
        }
    }
}
