コード例 #1
0
        /// <summary>
        /// Sub-function that takes a dataset which holds (a number of) sensormeasurements and saves these to the database.
        /// Note: the MAC-address (SunSpot) or the Unique ID (TelosB) has to be in the database or the measurement will be ignored and not saved tot he database.
        /// </summary>
        /// <param name="Set">DataSet with the table that contains the sensormeasurement.</param>
        /// <returns>The last UID of the inserted row (which means an int >= 1) anything else means the last insert failed.</returns>
        private DataSet AddSensorMeasurements(DataSet Set)
        {
            DataSet returnSet = new DataSet();
            DataSet tempSet = new DataSet();

            double temp;
            int tempint;
            string cmd = "";

            foreach (DataRow row in Set.Tables[0].Rows) //Run through every sensor in the xml-message
            {
                string CheckIfNodeInDB = "call getNodeID('" + row["ID"].ToString() + "');";

                try
                {
                    if(MySQLAllowedConn)
                        tempSet = MySQLConn.Query(CheckIfNodeInDB);
                }
                catch (Exception e_mysql)
                {
                    SocketServer.LogError(e_mysql, "LogServer.txt");
                }

                #region add node to DB
                if (tempSet.Tables[0].Rows.Count < 1)  //If the sensor is in the database, proceed with inserting the new measurements into the database.
                {
                    string addTelosb = "call addSensorToDBTelosb('" + row["ID"] + "'," + "2);";

                    try
                    {
                        //Update the MySQL-database (if it is available)
                        if (MySQLAllowedConn)
                            returnSet = MySQLConn.Query(addTelosb);
                        Console.WriteLine("Query OK");
                    }
                    catch (Exception e_mysql)
                    {
                        Console.WriteLine("Failed to add node to the DB");
                        SocketServer.LogError(e_mysql, "LogServer.txt");
                        //return Set;
                    }
                }
                #endregion

                if (Set.DataSetName == "SensorMeasurements") {
                        int TimeSecs;
                        if (int.TryParse(row["Time"].ToString(), out TimeSecs))
                            row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs);        //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp.

                        //Create the command that we send to the database to insert the new row.
                        cmd = "call addSensorMeasurements('" +
                            row["ID"] + "','" +
                            row["Time"] + "','" +
                            row["Temp"] + "','" +
                            row["Light"] + "'," +
                            ((double.TryParse(row["Humidity"].ToString(), out temp)) ? row["Humidity"] : "null") + "," +
                            ((int.TryParse(row["Power"].ToString(), out tempint)) ? row["Power"] : "null") + "," +
                            ((double.TryParse(row["TiltX"].ToString(), out temp)) ? row["TiltX"] : "null") + "," +
                            ((double.TryParse(row["TiltY"].ToString(), out temp)) ? row["TiltY"] : "null") + "," +
                            ((double.TryParse(row["TiltZ"].ToString(), out temp)) ? row["TiltZ"] : "null") + "," +
                            ((double.TryParse(row["AccX"].ToString(), out temp)) ? row["AccX"] : "null") + "," +
                            ((double.TryParse(row["AccY"].ToString(), out temp)) ? row["AccY"] : "null") + "," +
                            ((double.TryParse(row["AccZ"].ToString(), out temp)) ? row["AccZ"] : "null") + ",'" +
                            ((int.TryParse(row["Button1"].ToString(), out tempint)) ? row["Button1"] : "0") + "'," +
                            ((int.TryParse(row["Button2"].ToString(), out tempint)) ? row["Button2"] : "null") + ",'" +
                            ((double.TryParse(row["LED1"].ToString(), out temp)) ? row["LED1"] : "null") + "','" +
                            ((double.TryParse(row["LED2"].ToString(), out temp)) ? row["LED2"] : "null") + "','" +
                            ((double.TryParse(row["LED3"].ToString(), out temp)) ? row["LED3"] : "null") + "','" +
                            ((double.TryParse(row["LED4"].ToString(), out temp)) ? row["LED4"] : "null") + "','" +
                            ((double.TryParse(row["LED5"].ToString(), out temp)) ? row["LED5"] : "null") + "','" +
                            ((double.TryParse(row["LED6"].ToString(), out temp)) ? row["LED6"] : "null") + "','" +
                            ((double.TryParse(row["LED7"].ToString(), out temp)) ? row["LED7"] : "null") + "','" +
                            ((double.TryParse(row["LED8"].ToString(), out temp)) ? row["LED8"] : "null") + "'," +
                            ((int.TryParse(row["Polling"].ToString(), out tempint)) ? row["Polling"] : "null") + ");";
                            Console.WriteLine("AddSensorMeasurements OK");
                            Console.WriteLine("WSNID is: \n" + row["ID"].ToString());
                }
                    else if (Set.DataSetName == "LocationMessage")
                    {
                            int TimeSecs;
                            if (int.TryParse(row["Time"].ToString(), out TimeSecs))
                                row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs);        //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp.

                            currentID = row["ID"].ToString();
                            Positioning.Point pos = new Positioning.Point(0,0);

                            if (!CL.Exists(ExistsAlgorithms))
                            {
                                CL.Add(new CentroidLocalization(row["ID"].ToString(), MySQLConn));
                                Console.WriteLine("Added new BN to be positioned\n\n\n");
                            }

                            foreach (CentroidLocalization alg in CL)
                            {
                                if (alg.WSNid == row["ID"].ToString())
                                {
                                    pos = alg.ProcessMessage(Convert.ToInt32(row["ANode"].ToString()), Convert.ToInt32(row["RSSI"].ToString()));
                                    Console.WriteLine("Position succesfully calculated, x = {0}, y = {1}", pos.x.ToString(), pos.y.ToString());
                                }
                            }

                            //Create the command that we send to the database to insert the new row.
                            cmd = "call addLocalizationData(" +
                            ((int.TryParse(row["RSSI"].ToString(), out tempint)) ? row["RSSI"] : "null") + "," +

                            pos.x.ToString() + "," +
                            pos.y.ToString() + "," +

                            ((int.TryParse(row["Z"].ToString(), out tempint)) ? row["Z"] : "null") + "," +
                            row["ID"] + ",'" +
                            row["Time"] + "'," +
                            ((int.TryParse(row["ANode"].ToString(), out tempint)) ? row["ANode"] : "null") + ");";
                            Console.WriteLine("Add location OK");
                    }
                    else if (Set.DataSetName == "StatusMessage")
                    {
                            int TimeSecs;
                            if (int.TryParse(row["Time"].ToString(), out TimeSecs))
                                row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs);        //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp.

                            //Create the command that we send to the database to insert the new row.
                            cmd = "call addStatus(" +
                            row["ID"] + ",'" +
                            row["Time"] + "'," +
                            row["Active"] + "," +
                            row["AN"] + "," +
                            ((int.TryParse(row["X"].ToString(), out tempint)) ? row["X"] : "null") + "," +
                            ((int.TryParse(row["Y"].ToString(), out tempint)) ? row["Y"] : "null") + "," +
                            ((int.TryParse(row["SampleRate"].ToString(), out tempint)) ? row["SampleRate"] : "null") + "," +
                            ((int.TryParse(row["LocRate"].ToString(), out tempint)) ? row["LocRate"] : "null") + "," +
                            ((int.TryParse(row["Leds"].ToString(), out tempint)) ? row["Leds"] : "null") + "," +
                            ((int.TryParse(row["Power"].ToString(), out tempint)) ? row["Power"] : "null") + "," +
                            ((int.TryParse(row["Frequency"].ToString(), out tempint)) ? row["Frequency"] : "null") + "," +
                            ((int.TryParse(row["Conn"].ToString(), out tempint)) ? row["Conn"] : "null") + ");";
                            Console.WriteLine("Add Status OK");
                    }

                    try
                    {
                        //Update the MySQL-database (if it is available)
                        if(MySQLAllowedConn)
                            returnSet = MySQLConn.Query(cmd);
                        Console.WriteLine("Query OK");
                    }
                    catch (XmlException xmlex)
                    {
                        Console.WriteLine(xmlex.Message);
                        Console.WriteLine(xmlex.StackTrace);
                        Console.WriteLine("Received an incorrect XML message\n");
                    }
                    catch (Exception e_mysql)
                    {
                        Console.WriteLine(e_mysql.Message);
                        Console.WriteLine(e_mysql.StackTrace);
                        Console.WriteLine("Some field is probably missing");
                        SocketServer.LogError(e_mysql, "LogServer.txt");
                    }
                }

            return returnSet;
        }
コード例 #2
0
    private void positioning()
    {
                #if !UNITY_EDITOR
                #if UNITY_IOS
        Positioning.Point point = null;


        int      archornum = 0;
        double[] data      = new double[mybeacons.Count * 3];
        double   outx      = 0;
        double   outy      = 0;

        int i = 0;

        foreach (var item in mybeacons)
        {
            if (item.BSObject == null)
            {
                continue;
            }
            data [i * 3]     = item.BSObject.transform.position.x;
            data [i * 3 + 1] = item.BSObject.transform.position.z;
            data [i * 3 + 2] = (double)item.range;
            i++;
        }
        archornum = i;

        if (archornum >= 3)
        {
            bool ret = calcPos(data, archornum, out outx, out outy);
            if (ret)
            {
                point   = new Positioning.Point();
                point.x = outx;
                point.y = outy;
            }
        }
#endif
#else
        Positioning.Node targetNode = new Positioning.Node(@"target");
        foreach (var item in mybeacons)
        {
            if (item.BSObject == null)
            {
                continue;
            }
            Positioning.AnchorNode beaconNode =
                new Positioning.AnchorNode(item.IDString(),
                                           item.BSObject.transform.position.x,
                                           item.BSObject.transform.position.z,
                                           item.strength,
                                           (double)item.range);
            targetNode.Anchors.Add(beaconNode);
        }
        //Positioning.Point point = Positioning.ExtendedTrilateration.CalculatePosition (targetNode, null, null, false);
        Positioning.Point point = Positioning.MinMaxExtended.CalculatePosition(targetNode, null, null, false);
        //Positioning.Point point = Positioning.MinMax.CalculatePosition (targetNode, null, null, false);
//				//Positioning.Point point = Positioning.ClusterTrilateration.CalculatePosition (targetNode, null, null, false);
#endif

//
//
        if (point != null)
        {
            var location = new Vector3((float)point.x, 0f, (float)point.y);

            //Debug.Log ("target position :(" + point.x + "," + point.y + ")");
            showTargetAt(location, true);
            //return location;
        }
        else
        {
            var location = new Vector3(0f, 0f, 0f);
            showTargetAt(location, false);
            //return location;
        }
    }