Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var socket    = new System.Net.WebSockets.ClientWebSocket();
            var chargerId = "xxxx";   // id of the charger
            var password  = "******";   // any non-empty string
            var endpoint  = "xxxxxx"; //url to websocket server e.g ws://abc.com
            var auth      = Convert.ToBase64String(Encoding.UTF8.GetBytes(chargerId + ":" + password));

            socket.Options.SetRequestHeader("Authorization", "Basic " + auth);
            socket.Options.AddSubProtocol("ocpp1.6");
            var cancellationToken = CancellationToken.None;

            try
            {
                await socket.ConnectAsync(new Uri(endpoint + "/" + chargerId), cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }

            Console.WriteLine(socket.State); // status is opened now

            var sampleAuthReq = new AuthorizeRequest();

            sampleAuthReq.IdTag = "111"; // a correct IdTag predefined on server side

            var newMessageToSend = new TransportLayerMessage
            {
                MessageType = MessageTypes.CALL,
                UniqueId    = GenerateUniqueId(),
                Action      = sampleAuthReq.Action,
                Payload     = sampleAuthReq.ToJson()
            };

            if (socket.State == System.Net.WebSockets.WebSocketState.Open)
            {
                try
                {
                    var keystring = Console.ReadKey();
                    while (keystring.Key == ConsoleKey.S)
                    {
                        await Send(newMessageToSend.ToJson(), socket, cancellationToken);

                        keystring = Console.ReadKey();
                    }
                    Console.WriteLine("finished");
                    Console.ReadKey();
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }