using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.Text;

public partial class code_arduino : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection conn = null;
        OleDbCommand command = null;
        DateTime now;

        //query string parsing
        string username = Request.QueryString["username"];
        string password = Request.QueryString["password"];
        string datastream_id = Request.QueryString["datastream_id"];
        string value = Request.QueryString["value"];
        
        //replace . with , in value
        if (value.Contains(".")) {
           value = value.Replace(".",",");
        }

        //md5 calculator
        MD5 md5Hasher = MD5.Create();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(password));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        //db connection string
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
        
        //date recording
        now = DateTime.UtcNow;
        now = now.AddHours(+1);

        //check on pass and username
        string sql = "SELECT PasswordMD5 FROM aspnet_Membership WHERE ([PasswordMD5]=@pass AND [myUserName]=@username)";
        try
        {
            conn = new OleDbConnection(another);
            command = new OleDbCommand(sql, conn);
            command.Parameters.AddWithValue("@pass", sBuilder.ToString());
            command.Parameters.AddWithValue("@username", username);
            
            //reading the select
            OleDbDataReader myReader;
            String myresult = null;
            conn.Open();
            myReader = command.ExecuteReader();
            while (myReader.Read())
            {
                myresult = myReader["PasswordMD5"].ToString();
            }
            myReader.Close();

            //if passwordmd5 (from pass AND user) is find
            if (myresult == sBuilder.ToString())
            {
                //insert datapoint
                sql = "INSERT INTO datapoints ([my_datastream],[data],[ip],[myvalue]) VALUES (@datastream,@data,@ip,@myvalue)";
                command = new OleDbCommand(sql, conn);
                command.Parameters.AddWithValue("@datastream", datastream_id);
                command.Parameters.AddWithValue("@data", now.ToString());
                command.Parameters.AddWithValue("@ip", Request.UserHostAddress.ToString());
                command.Parameters.AddWithValue("@myvalue", value);
            }
            else
            {
                // pass and user it isn't find
                Label2.Text = "Wrong Login <br />";
            }            
            int added = 0;
            try
            {
                //if insert is going ok
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record inserted.";
            }
            catch (Exception err)
            {
                //else error 
                Label2.Text = "or: Error inserting record. <br />";
                Label2.Text += "Error type: " + err.Message;
            }
        }
        finally
        {
            if (conn != null)
            {
                //connection closed
                conn.Close();
            }
        }
    }
}
