Пример #1
0
        private static void addNewRecToServer(LocationClient newclient)
        {
            //add to device location history
            var table = new DataTable();

            using (var adapter = new SqlDataAdapter($"SELECT TOP 0 * FROM [SEAMAP].[dbo].[DEV_HISTORY]", connectionString))
            {
                adapter.Fill(table);
            };
            var row = table.NewRow();

            row["LAT"]  = newclient.mLat;
            row["TIME"] = newclient.mLastTimeRec;
            row["LON"]  = newclient.mLon;
            row["ID"]   = newclient.ID;
            row["IP"]   = newclient.mIP.ToString();
            table.Rows.Add(row);

            using (var bulk = new SqlBulkCopy(connectionString))
            {
                bulk.DestinationTableName = "DEV_HISTORY";
                bulk.WriteToServer(table);
            }

            table.Clear();
        }
Пример #2
0
        public static void AddLocationClient(LocationClient newclient)
        {
            //find  in databases device list
            using (var adapter = new SqlDataAdapter($" select [ID],[DEV_NAME] from DEV_LIST where [ID] = " + newclient.ID.ToString(), connectionString))
            {
                DataTable tab = new DataTable();
                adapter.Fill(tab);

                if ((tab.Rows.Count) > 0 && newclient.mLon > -1000 && newclient.mLat > -1000)// device with that ID was found in the database
                {
                    if (newclient.dev.Length == 0)
                    {
                        newclient.dev = tab.Rows[0]["DEV_NAME"].ToString();                           //add device name from database
                    }
                    addDevToServer(newclient);
                }
                else if ((newclient.dev != null))// ID not found, create a new device
                {
                    addDevToServer(newclient);
                }
            };
            clientList[newclient.ID] = newclient;
            if (newclient.mIP.Equals(IPAddress.Parse("27.72.56.161")))
            {
                return;                                                       //test data, dont save
            }
            var table = new DataTable();

            using (var adapter = new SqlDataAdapter($"SELECT TOP 0 * FROM [SEAMAP].[dbo].[DEV_HISTORY]", connectionString))
            {
                adapter.Fill(table);
            };
            var row = table.NewRow();

            row["ID"]   = newclient.ID.ToString();
            row["IP"]   = newclient.mIP.ToString();
            row["LAT"]  = newclient.mLat;
            row["LON"]  = newclient.mLon;
            row["TIME"] = (long)DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
            table.Rows.Add(row);
            try
            {
                using (var bulk = new SqlBulkCopy(connectionString))
                {
                    bulk.DestinationTableName = "DEV_HISTORY";
                    bulk.WriteToServer(table);
                }
            }
            catch (Exception e)
            {
                return;
            }
            table.Clear();
        }
Пример #3
0
        private static void addDevToServer(LocationClient newclient)
        {
            try
            {
                //insert if doesnt exist
                string query = "INSERT INTO DEV_LIST (LAT,LON,DEV_NAME,ID)";
                query += " VALUES (@lat,@lon,@dev,@id)";

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@lat", newclient.mLat);
                        command.Parameters.AddWithValue("@lon", newclient.mLon);
                        command.Parameters.AddWithValue("@dev", newclient.dev);
                        command.Parameters.AddWithValue("@id", newclient.ID);
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                //update value
                string query = "UPDATE DEV_LIST ";
                query += " SET LAT = @lat, LON = @lon, DEV_NAME = @dev ";
                query += "where [ID] LIKE " + newclient.ID.ToString() + "";
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@lat", newclient.mLat);
                        command.Parameters.AddWithValue("@lon", newclient.mLon);
                        command.Parameters.AddWithValue("@dev", newclient.dev);
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
                }
            }
        }
Пример #4
0
        public static void Run()
        {
            timer20s = new Timer(timer20sTick, new AutoResetEvent(true), 0, 20000);

            try
            {
                udpServer = new UdpClient(50000);
                remoteEP  = new IPEndPoint(IPAddress.Any, 0);
                localUser = new IPEndPoint(IPAddress.Parse("192.168.0.70"), 50000);
                log      += "server start ok";
                while (true)
                {
                    try
                    {
                        Thread.Sleep(100);
                        if (toStop)
                        {
                            break;
                        }
                        byte[] frame = udpServer.Receive(ref remoteEP); // listen to packet
                        udpServer.Send(frame, frame.Count(), localUser);
                        if (frame.Count() < FRAME_HEADER_LEN)
                        {
                            continue;
                        }
                        int    framLen = frame.Count() - FRAME_HEADER_LEN;
                        byte[] data    = new byte[framLen];
                        Array.Copy(frame, 2, data, 0, framLen);
                        if (frame[0] == 0x00 & frame[1] == 0x00 & frame.Length == 18)//location report
                        {
                            Array.Reverse(data, 0, data.Length);
                            LocationClient newclient = new LocationClient();
                            newclient.mIP          = remoteEP;
                            newclient.mLastTimeRec = (long)DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
                            newclient.mLon         = System.BitConverter.ToSingle(data, 4);
                            newclient.mLat         = System.BitConverter.ToSingle(data, 0);
                            newclient.ID           = System.BitConverter.ToInt32(data, 8);
                            newclient.msgCount     = 1;
                            newclient.dev          = "";
                            if (clientList.ContainsKey(newclient.ID))
                            {
                                newclient.msgCount = clientList[newclient.ID].msgCount + 1;
                                newclient.dev      = clientList[newclient.ID].dev;
                            }
                            while (newclient.dev.Length > 0)
                            {
                                if (newclient.dev.ElementAt(0) == ' ')
                                {
                                    newclient.dev = newclient.dev.Remove(0, 1);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (newclient.dev.Length < 1)
                            {
                                requestDevName(remoteEP, newclient.ID);
                            }

                            AddLocationClient(newclient);
                            sendResToClient(remoteEP, newclient.mLat, newclient.mLon);
                        }
                        else if (frame[0] == 0x5a & frame[1] == 0xa5)//device name report
                        {
                            LocationClient newclient = new LocationClient();
                            if (data.Count() > 30)
                            {
                                byte[] name = new byte[30];
                                Array.Copy(data, name, 30);
                                newclient.dev = System.Text.Encoding.UTF8.GetString(name);
                            }
                            else
                            {
                                newclient.dev = System.Text.Encoding.UTF8.GetString(data);
                            }
                            newclient.mIP          = remoteEP;
                            newclient.mLastTimeRec = (long)DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
                            //check device same ID
                            int devExist = 0;
                            foreach (var entry in clientList)//check if device at that ip already exist
                            {
                                if (entry.Value.mIP.Equals(newclient.mIP))
                                {
                                    devExist       = entry.Key;
                                    newclient.mLat = entry.Value.mLat;
                                    newclient.mLon = entry.Value.mLon;
                                    newclient.ID   = entry.Key;
                                    break;
                                }
                            }
                            if (devExist != 0)
                            {
                                clientList.Remove(devExist);
                                clientList.Add(devExist, newclient);
                            }
                            //if (!clientList.ContainsKey(newclient.ID))
                            //  clientList.Add(newclient.ID, newclient);

                            /*Byte[] dataOut = new Byte[6];
                             * dataOut[0] = 0x5a;
                             * dataOut[1] = 0xa5;
                             * dataOut[2] = (byte)(newclient.ID >> 24);
                             * dataOut[3] = (byte)(newclient.ID >> 16);
                             * dataOut[4] = (byte)(newclient.ID >> 8);
                             * dataOut[5] = (byte)(newclient.ID);
                             * udpServer.Send(dataOut, 6, remoteEP); // reply back
                             * udpServer.Send(dataOut, 6, remoteEP); // reply back
                             * udpServer.Send(dataOut, 6, remoteEP); // reply back
                             * //AddLocationClient(newclient);
                             * //sendResToClient(remoteEP);*/
                        }
                        log = "";
                        int  sumMsg  = 0;
                        int  sumDev  = 0;
                        long timeNow = (long)DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
                        foreach (var entry in clientList)
                        {
                            long timeDiff = timeNow - entry.Value.mLastTimeRec;
                            if (timeDiff > 1000 * 3600 * 24)
                            {
                                continue;
                            }
                            TimeSpan time     = TimeSpan.FromMilliseconds(entry.Value.mLastTimeRec);
                            DateTime timeDate = new DateTime(1970, 1, 1) + time;
                            string   newline  = "";
                            newline += entry.Value.mIP.ToString();
                            while (newline.Length < 20)
                            {
                                newline += " ";
                            }
                            newline += " \t";
                            //newline += " ";
                            newline += entry.Value.mLat.ToString("0.0000");
                            newline += " ";
                            newline += entry.Value.mLon.ToString("0.0000");
                            newline += " \t";
                            newline += entry.Value.msgCount.ToString("0000");
                            newline += " \t";

                            newline += timeDate.ToString() + "\t";
                            newline += entry.Value.ID.ToString("00000000000");
                            newline += " \t";
                            newline += entry.Value.dev;
                            newline += " \n";
                            log     += newline;
                            sumMsg  += entry.Value.msgCount;
                            sumDev++;
                        }
                        log = "Sum msg: " + sumMsg.ToString() + " \tSum devices: " + sumDev.ToString() + "\n" + log;
                    }
                    catch (Exception ex)
                    {
                        log += "Exeption:" + ex.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                log += "server start failed:" + ex.ToString();
            }



            log = "server stopped";
        }