예제 #1
0
        public async void PostItemHttpTask(Measurement.Measurement measure)
        {
            string EventWebApi = "https://localhost:44354/";

            // Measurement.Measurement newMeasure = new Measurement.Measurement(DateTime.Now, "D5.16", "Ozon", 9.12, "Rasp v.1.0");

            using (HttpClient client = new HttpClient())
            {
                string newItemJson = JsonConvert.SerializeObject(measure);
                Console.WriteLine("Serialized object: " + newItemJson);
                var content = new StringContent(newItemJson, Encoding.UTF8, "application/json");
                client.BaseAddress = new Uri(EventWebApi);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                IEnumerable <string> headers;
                headers = client.DefaultRequestHeaders.GetValues("Accept");
                foreach (var VARIABLE in headers)
                {
                    Console.WriteLine(VARIABLE);
                }
                var response = client.PostAsync("api/measurements", content).Result;
                if (response.IsSuccessStatusCode)
                {
                    var responseEvent = client.GetAsync("api/measurements" + measure).Result;
                    if (responseEvent.IsSuccessStatusCode)
                    {
                        //var Event = responseEvent.Content.ReadAsStreamAsync<Event>().Result;
                        //   string saveEvent = await responseEvent.Content.ReadAsStringAsync<Event>().Result;
                    }
                }
            }

            //return await GetItemsHttpTask();
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World receiver ready press enter!");
            Console.ReadLine();
            PostService postMeasure = new PostService();

            Measurement.Measurement measurement;


            UdpClient receiveUdp = new UdpClient(11111);

            IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 0);

            Console.WriteLine("ready to receive data");

            try
            {
                while (true)
                {
                    Byte[] receiveBytes = receiveUdp.Receive(ref receiveEndPoint);
                    string receivedData = Encoding.ASCII.GetString(receiveBytes);

                    Console.WriteLine("This message was sent from " +
                                      receiveEndPoint.Address.ToString() +
                                      " on their port number " +
                                      receiveEndPoint.Port.ToString());

                    Console.WriteLine(receivedData);

                    string[] txtSplit = receivedData.Split(" ");
                    for (int i = 0; i < txtSplit.Length; i++)
                    {
                        Console.WriteLine(txtSplit[i]);
                    }

                    measurement = new Measurement.Measurement(Convert.ToDateTime(txtSplit[0] + " " + txtSplit[1]), txtSplit[2], txtSplit[3], Convert.ToDouble(txtSplit[4]), txtSplit[5], receiveEndPoint.Port.ToString(), receiveEndPoint.Address.ToString(), txtSplit[8]);
                    postMeasure.PostItemHttpTask(measurement);
                    Thread.Sleep(3000);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: Danielddwp/UDP20
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World press enter to start prepare sending environment!");
            Console.ReadLine();
            Random rvalue = new Random();

            Measurement.Measurement measure;

            UdpClient udpSender = new UdpClient(0);

            udpSender.EnableBroadcast = true;
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, 11111);

            Console.WriteLine("ready to start press enter again");
            Console.ReadLine();
            string dateToday = DateTime.Now.Date.ToString();

            while (true)
            {
                measure = new Measurement.Measurement(10, 15, 20);

                byte[] sendBytes = Encoding.ASCII.GetBytes(measure.ToString());

                try
                {
                    Console.WriteLine("sending :" + measure.ToString());
                    udpSender.Send(sendBytes, sendBytes.Length, endPoint);
                    Thread.Sleep(3000);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }