Example #1
0
        /// <summary>
        /// Receives an HTTP request and check if it contains login details and if these details are correct
        /// </summary>
        /// <param name="headers">HTTP request splitted to rows</param>
        /// <returns>True if login details are exist and correct</returns>
        public bool authenticate(string[] headers)
        {
            bool auth = false;

            string[] temp;
            string   basic;

            foreach (string header in headers)
            {
                if (header.ToLower().IndexOf("authorization") >= 0)
                {
                    temp  = header.Split(' ');
                    basic = temp[temp.Length - 1].Trim();

                    byte[] decbasic      = Convert.FromBase64String(basic);
                    string decodedString = Encoding.UTF8.GetString(decbasic);

                    if (decodedString.Equals(PermanentData.auth()))
                    {
                        auth = true;
                    }
                }
            }
            return(auth);
        }
        public async Task MainPageAsync()
        {
            //Initializing GPIO
            gpioController = GpioController.GetDefault();
            if (gpioController != null)
            {
                dout = gpioController.OpenPin(DOUT_PIN);
                clk  = gpioController.OpenPin(SLK_PIN);
                uhl  = new UserHardwareLinker(clk, dout);
                System.Diagnostics.Debug.WriteLine("Connected to Hardware via GPIO.");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("WARNING: Your machine does not support GPIO!");
            }

            //Start listening to messages from clients
            new ConnectionHandler(uhl);

            //Start Web Server
            new WebHandler(uhl);


            //loading permanent data from memory
            Task  loadTask = PermanentData.LoadFromMemoryAsync();
            await loadTask;

            Task putRecordTask = null;

            PermanentData.CurrIP = GetLocalIp();
            if (!PermanentData.Serial.Equals(PermanentData.NULL_SYMBOL))
            {
                putRecordTask = putRecordInDatabase(PermanentData.CurrIP, PermanentData.Serial);
            }
            if (uhl != null)
            {
                uhl.setParameters(PermanentData.Offset, PermanentData.Scale);
            }

            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;

            if (putRecordTask != null)
            {
                await putRecordTask;
            }
        }
Example #3
0
        /// <summary>
        /// This method is activated every time a message has received from the user.
        /// </summary>
        /// <param name="data">The first row of the HTTP request query.</param>
        /// <param name="sender">the HTTPServer object that handles the current connection.</param>
        public async Task http_OnDataRecived(string data, Windows.Storage.Streams.DataWriter writer)
        {
            string[] headers = data.Split('\n');

            //Parse the message to extract the data from the request
            Dictionary <string, string> fields = getQuery(headers[0]);

            //Check if the request contains a login header with a valid user and password. If not, send '401 Unauthorized' response to the client.
            if (!authenticate(headers))
            {
                System.Diagnostics.Debug.WriteLine("@@Unauthorized");
                await http.Send(CreateHTTP.Code401_Unauthorized(), writer);

                return;
            }

            //get page
            string html = await HTTPServer.getHTMLAsync(PAGE);


            //fill the textboxes if the current data
            html = HTTPServer.HTMLInputFill(html, "text", "name", PermanentData.Devname);
            html = HTTPServer.HTMLInputFill(html, "text", "serial", PermanentData.Serial);
            html = HTTPServer.HTMLInputFill(html, "text", "man_offset", PermanentData.Offset.ToString());
            html = HTTPServer.HTMLInputFill(html, "text", "man_scale", PermanentData.Scale.ToString());

            //some browsers might ask for the page's icon.
            if (data.IndexOf("favicon.ico") >= 0)
            {
                await http.Send(CreateHTTP.Code204_NoContent(), writer);

                return;
            }

            double hardwtask = -100;

            try
            {
                /*determine which operation the client requested and do it.*/

                if (fields.Keys.Contains("chname"))
                {
                    //change name
                    System.Diagnostics.Debug.WriteLine("got: " + fields["name"]);
                    PermanentData.Devname = WebUtility.UrlDecode(fields["name"]);
                    html = HTTPServer.HTMLRewrite(html, "span", "name_feedback", "Your device's name was changed to  " + PermanentData.Devname);
                }
                if (fields.Keys.Contains("chpass"))
                {
                    //change password
                    string currpass = WebUtility.UrlDecode(fields["curr_pass"]);
                    string newpass  = WebUtility.UrlDecode(fields["password"]);
                    string confirm  = WebUtility.UrlDecode(fields["confirm"]);

                    if (!newpass.Equals(confirm))
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "chpass_feedback", "Passwords do not match");
                    }
                    else if (!currpass.Equals(PermanentData.Password))
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "chpass_feedback", "Password incorrect");
                    }
                    else if (HTTPServer.passwordValidation(newpass, MIN_PASS_LENGTH, MAX_PASS_LENGTH) < 0)
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "chpass_feedback", "Password invalid<br/>The password should contain only letters and numbers and underscore ('_')<br/>Password must be 4-12 digits long.");
                    }
                    else
                    {
                        PermanentData.Password = newpass;
                        html = HTTPServer.HTMLRewrite(html, "span", "chpass_feedback", "Your password has changed successfully");
                    }
                }
                if (fields.Keys.Contains("register"))
                {
                    //set serial number
                    string serial = WebUtility.UrlDecode(fields["serial"]);
                    if (HTTPServer.passwordValidation(serial) < 0)
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "serial_feedback", "Serial invalid<br/>The Serial should contain only letters and numbers and underscore ('_')");
                    }
                    else
                    {
                        string ip = GetLocalIp();
                        await putRecordInDatabase(ip, serial);

                        PermanentData.Serial = serial;
                        PermanentData.CurrIP = ip;
                        html = HTTPServer.HTMLRewrite(html, "span", "serial_feedback", "Your device's serial is now  " + PermanentData.Serial);
                    }
                }

                if (fields.Keys.Contains("soffset") || fields.Keys.Contains("sscale"))
                {
                    //weighing something for calibration
                    if (uhl != null)
                    {
                        hardwtask = uhl.getRawWeight((int)(UserHardwareLinker.WEIGH_AVG * UserHardwareLinker.CALIB_FACTOR));
                        if (fields.Keys.Contains("soffset"))
                        {
                            nullweight = hardwtask;
                            System.Diagnostics.Debug.WriteLine("nullweight: " + nullweight);
                            html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "The sensor returned value: " + nullweight);
                        }
                        if (fields.Keys.Contains("sscale"))
                        {
                            rawWeight = hardwtask;
                            System.Diagnostics.Debug.WriteLine("rawWeight: " + rawWeight);
                            html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "The sensor returned value: " + rawWeight);
                        }
                    }
                    else
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "Error: Your device does not have the sufficient hardware requerments.<br/>Operation did not complete.");
                    }
                }
                if (fields.Keys.Contains("calibrate"))
                {
                    //calibrate
                    try
                    {
                        if (uhl != null)
                        {
                            knownWeight = float.Parse(fields["known"]);
                            if (uhl.Scale != 0)
                            {
                                uhl.setParameters(nullweight, rawWeight, knownWeight);
                                html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "OK! the device's parameters are:<br />OFFSET: " + uhl.Offset + "<br />SCALE: " + uhl.Scale);
                                PermanentData.Scale  = uhl.Scale;
                                PermanentData.Offset = uhl.Offset;
                            }
                            else
                            {
                                html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "the SCALE cannot be zero.");
                            }
                        }
                        else
                        {
                            html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "Error: Your device does not have the sufficient hardware requerments.<br/>Operation did not complete.");
                        }
                    }
                    catch
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "Operation failed.");
                    }
                }
                if (fields.Keys.Contains("man_calib"))
                {
                    //manual calibration
                    if (uhl != null)
                    {
                        float offset = float.Parse(fields["man_offset"]);
                        float scale  = float.Parse(fields["man_scale"]);
                        if (scale != 0)
                        {
                            uhl.setParameters(offset, scale);
                            html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "OK! the device's parameters are:<br />OFFSET: " + uhl.Offset + "<br />SCALE: " + uhl.Scale);
                            PermanentData.Scale  = uhl.Scale;
                            PermanentData.Offset = uhl.Offset;
                        }
                        else
                        {
                            html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "the SCALE cannot be zero.");
                        }
                    }
                    else
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "Error: Your device does not have the sufficient hardware requerments.<br/>Operation did not complete.");
                    }
                }
                if (fields.Keys.Contains("best_calib"))
                {
                    //calibration using default values
                    if (uhl != null)
                    {
                        double offset = PermanentData.BEST_OFFSET;
                        double scale  = PermanentData.BEST_SCALE;
                        uhl.setParameters(offset, scale);
                        html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "OK! the device's parameters are:<br />OFFSET: " + uhl.Offset + "<br />SCALE: " + uhl.Scale);
                        PermanentData.Scale  = uhl.Scale;
                        PermanentData.Offset = uhl.Offset;
                    }
                    else
                    {
                        html = HTTPServer.HTMLRewrite(html, "span", "calibration_feedback", "Error: Your device does not have the sufficient hardware requerments.<br/>Operation did not complete.");
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            html = HTTPServer.HTMLInputFill(html, "text", "name", PermanentData.Devname);
            html = HTTPServer.HTMLInputFill(html, "text", "serial", PermanentData.Serial);
            html = HTTPServer.HTMLInputFill(html, "text", "man_offset", PermanentData.Offset.ToString());
            html = HTTPServer.HTMLInputFill(html, "text", "man_scale", PermanentData.Scale.ToString());

            string response = CreateHTTP.Code200_Ok(html);

            Task writeTask = PermanentData.WriteToMemoryAsync();

            Task sendTask = http.Send(response, writer);

            await writeTask;
            await sendTask;

            System.Diagnostics.Debug.WriteLine("HTTP: page sent back to user");
        }