Пример #1
0
        /* Euclidean Distance calculation */
        private double Distance(RssiValue[] unknown, AdjustedFingerprinting data)
        {
            double distance = 0.0;

            for (int i = 0; i < unknown.Length; ++i)
            {
                distance += (unknown[i].Rssi - data.RssiValueAndGateway[i].Rssi) *
                            (unknown[i].Rssi - data.RssiValueAndGateway[i].Rssi);
            }
            return(Math.Sqrt(distance));
        }
        /* sends the command to fetch fingerprinting data from the server*/
        public static List <AdjustedFingerprinting> GetFingerprintings(int environmentId)
        {
            /* If we fetch the fingerprintings of the environment in question.
             * we do not call the server again, return them from the cache */
            if (!fingerprintings.ContainsKey(environmentId))
            {
                /* Fetch the fingerprintings from the server and cache. */
                string json = Get(string.Format($"{GET_FINGERINTING_COMMAND} -env {environmentId}"));
                List <Fingerprinting> listOfFingerprinting = JsonConvert.DeserializeObject <List <Fingerprinting> >(json);

                /* Create a new cache with the environment Id */
                List <AdjustedFingerprinting> adjustedFingerprintings = new List <AdjustedFingerprinting>();
                fingerprintings.Add(environmentId, adjustedFingerprintings);

                /* There is an assumption below that is all the data will come in sorted manner.
                 * The data should be sorted X-axis, Y-axis, timestamp, and gateway Id.
                 * This sorting should be giving a list like:
                 * ------ x_axis -- y_axis -- timestamp -- gateway_id
                 * ------ 40     -- 40     -- 2018-12.. -- 1
                 * ------ 40     -- 40     -- 2018-12.. -- 2
                 * ------ 40     -- 40     -- 2018-12.. -- 3
                 * ------ 40     -- 40     -- 2018-12.. -- 1
                 * ------ 40     -- 40     -- 2018-12.. -- 2
                 * ------ 40     -- 40     -- 2018-12.. -- 3
                 */
                AdjustedFingerprinting adjustedFingerprinting = new AdjustedFingerprinting();

                /* I am going to use this value to get that I completed to process all gateways
                 * for each of the points */
                int firstGateway = listOfFingerprinting[0].GatewayId;

                /* Rssi values fetched by each of the gateways will be stored on this variable.
                 * As soon as the gateway changes, I will be re-initializing this variable */
                RssiValue rssiValue = new RssiValue();

                for (int i = 0; i < listOfFingerprinting.Count; i++)
                {
                    /* gateway changed */
                    if (listOfFingerprinting[i].GatewayId == firstGateway)
                    {
                        /* If the gateway is the first gateway of the list
                         * this means all gateways processed for the corresponding
                         * reference point and it is time to refresh */
                        adjustedFingerprinting = new AdjustedFingerprinting()
                        {
                            /* Get one of the existing points */
                            Coordinates = GetPoint(environmentId,
                                                   listOfFingerprinting[i].Xaxis,
                                                   listOfFingerprinting[i].Yaxis)
                        };
                        adjustedFingerprintings.Add(adjustedFingerprinting);
                    }
                    /* re-initialize the rssiValue variable */
                    rssiValue = new RssiValue()
                    {
                        GatewayId = listOfFingerprinting[i].GatewayId,
                        Rssi      = (listOfFingerprinting[i].Rssi)
                    };
                    adjustedFingerprinting.RssiValueAndGateway.Add(rssiValue);
                }
            }

            return(fingerprintings[environmentId]);
        }