Ejemplo n.º 1
0
        private void onServerRequest(object sender, CommandBase command)
        {
            Log.Trace("onServerRequest: " + command?.Command);

            try {
                string cmdType = command.Command;

                if (cmdType == RequestHolderLocation.Name)
                {
                    HolderLocationPeriodDTO holderLocationPeriod = HolderLocationPeriodDTO.FromObject(command.Params);

                    var holderLocationDTO = eventService.GetHolderLocations(holderLocationPeriod).Result;
                    holderLocationDTO.QueryType  = holderLocationPeriod.QueryType;
                    holderLocationDTO.IsHolderIn = holderLocationPeriod.IsHolderIn;

                    var response = new ResponseHolderLocation(holderLocationDTO);
                    comunicator.NotifyAll(response, cancellationToken);
                }
            } catch (Exception e) {
                Log.Warn(e.Message + "\n" + e.StackTrace);
            }
        }
        private async Task processResult(string message)
        {
            if (CS_EventsNotifierSlackBot.Global.GlobalScope.CSEventsListeners.Count > 1)
            {
                return;
            }

            await Task.Delay(1000);

            try {
                Console.WriteLine("Emulator received: " + message);

                CommandBase command = CommandBase.FromJson(message);

                string cmdType = command.Command;

                if (cmdType == RequestHolderLocation.Name)
                {
                    HolderLocationPeriodDTO locationPeriod = HolderLocationPeriodDTO.FromObject(command.Params);

                    string keyToFindLast = locationPeriod.HolderName.Trim().ToLower()
                                           + locationPeriod.HolderMiddlename.Trim().ToLower()
                                           + locationPeriod.HolderSurname.Trim().ToLower()
                                           + locationPeriod.TimePeriod.EndTime?.Date;

                    List <EventInfoDTO> eventsInfo;
                    if (lastGeneratedEvents.ContainsKey(keyToFindLast))
                    {
                        lastGeneratedEvents.TryGetValue(keyToFindLast, out eventsInfo);
                        eventsInfo = eventsInfo.Where(ev => ev.EventTime >= locationPeriod.TimePeriod.StartTime && ev.EventTime <= locationPeriod.TimePeriod.EndTime).ToList();
                    }
                    else
                    {
                        eventsInfo = generateEvents(locationPeriod.TimePeriod);
                        lastGeneratedEvents.Add(keyToFindLast, eventsInfo);
                    }


                    // Emulatig Response from Database
                    var holderLocationDTO = new HolderLocationDTO()
                    {
                        QueryType  = locationPeriod.QueryType,
                        IsHolderIn = locationPeriod.IsHolderIn,
                        TimePeriod = locationPeriod.TimePeriod,
                        HolderInfo = new EventDTO()
                        {
                            CardNumber       = 421449585,
                            HolderType       = "\u041F\u043E\u0441\u0435\u0442\u0438\u0442\u0435\u043B\u044C",                       //Посетитель
                            HolderName       = locationPeriod.HolderName,
                            HolderMiddlename = locationPeriod.HolderMiddlename,
                            HolderSurname    = locationPeriod.HolderSurname,
                            HolderDepartment = "\u041E\u0442\u0434\u0435\u043B \u043A\u0430\u0434\u0440\u043E\u0432", //Отдел кадров
                            HolderTabNumber  = "\u0422\u0430\u0431\u0435\u043B\u044C 845"                             //Табель 845
                        },
                        EventsInfo = eventsInfo
                    };

                    var response = new ResponseHolderLocation(holderLocationDTO);

                    byte[] buffer = Encoding.UTF8.GetBytes(response.ToJson());
                    await webSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, token);
                }
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
        }