Esempio n. 1
0
        public TicketDto AddCommand(CommandTicketDto commandTicket)
        {
            var ticket = GetByTicketId(commandTicket.CommandId);

            if (ticket == null)
            {
                throw new BadRequestException("Ticket não cadastrado");
            }
            ticket.CommandTickets.Add(commandTicket);
            //Init websocket
            _eventHubService.ACommandQueueWasUpdatedEvent(this, new PayloadUpdateCommandQueue(ticket.Serial, 1, commandTicket.UpdatedAt));
            _eventHubService.ACommandStatusWasUpdatedEvent(this, new PayloadUpdateCommandStatus(ticket.Serial, commandTicket.Command, commandTicket.UpdatedAt.ToString(), commandTicket.StatusCommand.ToString()));
            _eventHubService.AComunicationStatusWasUpdatedEvent(this, new PayloadUpdateComunicationStatus(ticket.Serial, commandTicket.CommunicationStatus, commandTicket.UpdatedAt.ToString()));
            //end Websocket
            return(_mapper.Map <Ticket, TicketDto>(_ticketRepository.Update(_mapper.Map <TicketDto, Ticket>(ticket))));;
        }
Esempio n. 2
0
        public void RelayOn(string serial, string token)
        {
            var meter    = _meterService.GetExistingMeter(serial);
            var meterDto = _mapper.Map <Meter, MeterDto>(meter);
            var client   = token != null?_servicesUtils.GetCurrentClientType(token) : ClientType.ALL;

            if (meter.Modem == null)
            {
                throw new BadRequestException("O medidor informado não possui modem");
            }
            var relayOnCommandTicket = new CommandTicketDto
            {
                CommandId           = Guid.NewGuid(),
                CommunicationStatus = CommunicationStatus.NO_INFORMATION,
                Attempt             = 0,
                Status = Status.Waiting
            };
            var relayStatusCommandTicket = new CommandTicketDto
            {
                CommandId           = Guid.NewGuid(),
                CommunicationStatus = CommunicationStatus.NO_INFORMATION,
                Attempt             = 0,
                Status = Status.Waiting
            };
            var ticket = new TicketDto
            {
                User         = client.ToString(),
                Serial       = meter.Smc == null ? meter.Serial : meter.Smc.Serial,
                TicketId     = Guid.NewGuid(),
                CommandType  = CommandType.RelayOn,
                Interval     = 18,
                TicketStatus = TicketStatus.Waiting
            };
            ForwarderMessage  relayOnForwarderMessage;
            ForwarderMessage  relayStatusForwarderMessage;
            CommandDeviceType commandDeviceType;

            if (meter.Smc != null)
            {
                var smcMiddlewareService =
                    _middlewareProviderService.GetSmcMiddlewareServiceByManufacturerName(meter.MeterModel.Manufacturer
                                                                                         .Name);
                relayOnForwarderMessage =
                    smcMiddlewareService.MakeMeterWithSmcRelayOnCommandForForwarder(meterDto, relayOnCommandTicket);
                relayStatusForwarderMessage =
                    smcMiddlewareService.MakeMeterWithSmcRelayStatusCommandForForwarder(meterDto,
                                                                                        relayStatusCommandTicket);
                commandDeviceType = CommandDeviceType.Smc;
            }
            else
            {
                var meterMiddlewareService =
                    _middlewareProviderService.GetMeterMiddlewareServiceByManufacturerName(meter.MeterModel.Manufacturer
                                                                                           .Name);
                relayOnForwarderMessage =
                    meterMiddlewareService.MakeMeterRelayOnCommandForForwarder(meterDto, relayOnCommandTicket);
                relayStatusForwarderMessage =
                    meterMiddlewareService.MakeMeterRelayStatusCommandForForwarder(meterDto, relayStatusCommandTicket);
                commandDeviceType = CommandDeviceType.Meter;
            }
            var forwarderMessageArray = new[] { relayOnForwarderMessage, relayStatusForwarderMessage };

            ticket.CommandTickets.Add(relayOnCommandTicket);
            ticket.CommandTickets.Add(relayStatusCommandTicket);
            var ticketSaved = _ticketService.Create(ticket);

            _threadService.AddTicketToThread(ticketSaved, forwarderMessageArray, PriorityValue.MediumPriority,
                                             commandDeviceType);
            // Init websocket
            if (ticketSaved != null)
            {
                _eventHubService.ACommandQueueWasUpdatedEvent(this, new PayloadUpdateCommandQueue(ticket.Serial, 2, DateTime.Now.ToString()));
                _eventHubService.ACommandStatusWasUpdatedEvent(this, new PayloadUpdateCommandStatus(ticket.Serial, ticket.CommandTickets[0].Command, DateTime.Now.ToString().ToString(), ticket.CommandTickets[0].StatusCommand.ToString()));
                _eventHubService.AComunicationStatusWasUpdatedEvent(this, new PayloadUpdateComunicationStatus(ticket.Serial, ticket.CommandTickets[0].CommunicationStatus, DateTime.Now.ToString()));
                _eventHubService.ARelayStateWasChangedEvent(this, new PayloadUpdateRelayState(meterDto.Serial, meterDto.UpdatedAt, meterDto.AccountantStatus));
            }
            // fim websocket
            // _threadService.AddCommand(command);
        }