コード例 #1
0
        public void  Emulate(EmulationConfig config, List <Encoding.EncodedAvlData> encodedData, CancellationToken token)
        {
            switch (config.SourceOfImeIs)
            {
            case SourceOfIMEIs.FromLocalList:
                config.IMEIs = IMEIs;
                if (config.BoxNumber > 100)
                {
                    config.BoxNumber = 100;
                }
                break;

            case SourceOfIMEIs.FromDatabase:
                try
                {
                    GetLog("Requete pour ramener la liste des  IMEIs a partir de la base de données ... ", LogType.Info);
                }
                catch (Exception e)
                {
                    GetLog("Il ya un problème quelque part avec la base de données :" + e.Message, LogType.Error);

                    Console.WriteLine(e);
                    return;
                    // throw;
                }
                break;

            case SourceOfIMEIs.OneIMEI:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Parallel.For((long)0, config.BoxNumber, async(index, state) =>
            {
                try
                {
                    var i1 = index;
                    foreach (var data in encodedData.GroupBy(x => x.SocketNumber))
                    {
                        try
                        {
                            token.ThrowIfCancellationRequested();
                            await RunEmulation(config, config.IMEIs[i1], data, token);
                        }
                        catch (OperationCanceledException e)
                        {
                            UpdateStartBtn?.Invoke(false);
                            state.Stop();
                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    GetLog(e.Message, LogType.Error);
                }
            });
        }
コード例 #2
0
        private async Task RunEmulation(EmulationConfig config, string i1, IGrouping <string, Encoding.EncodedAvlData> data, CancellationToken token)
        {
            // initialiser le client
            var _client = new Client(config.IpAddress, config.Port);

            _client.OnDisconnected += (obj, args) =>
            {
                GetLog($"Le serveur  {config.IpAddress} : {config.Port} est déconnecté...", LogType.Error);
                //return;
            };

            try
            {
                // tentative de connection au serveur
                await _client.ConnectAsync(token);

                _client.IsConnected = true;
                GetLog($"Etablissement de la connexion  au serveur  {config.IpAddress} : {config.Port} ...", LogType.Info);
            }
            catch (Exception e)
            {
                GetLog(e.Message, LogType.Error);
                return;
            }
            // authentification par l'envoi de l'IMEI
            Byte[] imei = BitConverter.GetBytes((ushort)15).Reverse().Concat(System.Text.Encoding.ASCII.GetBytes(i1)).ToArray();

            await _client.SendAsync(imei, token);

            var bt = await _client.ReceiveAsync(token);

            if (bt[0] == 0x001)
            {
                GetLog($"Envoie de {data.Count()} trames  IEMI :{i1} ... ", LogType.Info);
                // envoi des trames AVL
                foreach (var encodedAvlData in data)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    await _client.SendAsync(encodedAvlData.Data, token);

                    var aqt = await _client.ReceiveAsync(token);

                    Console.WriteLine(aqt[3]);
                    Thread.Sleep((int)config.SleepPeriod);
                }
                _client.CloseStream();
                _client.Disconnect();
            }
        }