Пример #1
0
        public async Task Start()
        {
            if (State.IsOn)
            {
                await State.Ready("on");

                return;
            }
            _readyState.IsOff = true;
            if (_lifeTimer != null)
            {
                throw new InvalidOperationException("start life timer exist");
            }

            State.IsOn = EventEmitter.State.PendingSymbol;

            try
            {
                if (_memory == null)
                {
                    _memory = new MemoryCard(_options.Name, _loggerFactory.CreateLogger <MemoryCard>(), _loggerFactory);
                }
                try
                {
                    await _memory.Load();
                }
                catch (Exception exception)
                {
                    _logger.LogError(exception, "start memory load failed.");
                }
                await InitPuppet();

                await Puppet.Start();

                InitPuppetEventBridge(Puppet);
                //TODO: IO component
                if (!string.IsNullOrWhiteSpace(_options.IoToken))
                {
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "start failed.");
                EmitError(exception);
                try
                {
                    await Stop();
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "start stop failed.");
                    EmitError(e);
                }
            }
            OnHeartbeat((data) => MemoryCheck());

            _readyState.IsOn = true;
            EmitStart();
        }
Пример #2
0
        protected void InitPuppetEventBridge(Puppet puppet)
        {
            _logger.LogInformation("init puppet event bridge");
            _ = puppet.OnDong(payload => EmitDong(payload.Data))
                .OnError(payload => EmitError(payload.Exception))
                .OnHeartbeat(payload => EmitHearbeat(payload.Data))
                .OnFriendship(async payload =>
            {
                var friendship = Friendship.Load(payload.FriendshipId);
                await friendship.Ready();
                _ = EmitFriendship(friendship);
                _ = friendship.Contact.Emit("friendship", friendship);
            })
                .OnLogin(async payload =>
            {
                var contact = ContactSelf.Load(payload.ContactId);
                await contact.Ready();
                _ = EmitLogin(contact);
            })
                .OnLogout(async payload =>
            {
                var contact = ContactSelf.Load(payload.ContactId);
                await contact.Ready();
                _ = EmitLogout(contact, payload.Data);
            })
                .OnMessage(async payload =>
            {
                var message = Message.Load(payload.MessageId);
                await message.Ready;
                _        = EmitMessage(message);
                var room = message.Room;
                if (room != null)
                {
                    _ = room.EmitMessage(message);
                }
            }).OnReady(payload =>
            {
                _ = Emit("ready");
                _readyState.IsOn = true;
            })
                .OnRoomInvite(payload =>
            {
                var roomInvitation = RoomInvitation.Load(payload.RoomInvitationId);
                _ = EmitRoomInvite(roomInvitation);
            })
                .OnRoomJoin(async payload =>
            {
                var room = Room.Load(payload.RoomId);
                await room.Sync();

                var inviteeList = payload
                                  .InviteeIdList
                                  .Select(Contact.Load)
                                  .ToList();
                await Task.WhenAll(inviteeList.Select(c => c.Ready()));

                var inviter = Contact.Load(payload.InviterId);
                await inviter.Ready();
                var date = payload.Timestamp.TimestampToDateTime();

                _ = EmitRoomJoin(room, inviteeList, inviter, date);
                _ = room.EmitJoin(inviteeList, inviter, date);
            })
                .OnRoomLeave(async payload =>
            {
                var room = Room.Load(payload.RoomId);
                await room.Sync();

                var leaverList = payload.RemoverIdList.Select(Contact.Load).ToList();
                await Task.WhenAll(leaverList.Select(c => c.Ready()));

                var remover = Contact.Load(payload.RemoverId);
                await remover.Ready();

                var date = payload.Timestamp.TimestampToDateTime();

                _ = EmitRoomLeave(room, leaverList, remover, date);
                _ = room.EmitLeave(leaverList, remover, date);

                var selftId = Puppet.SelfId;
                if (!string.IsNullOrEmpty(selftId) && payload.RemoverIdList.Contains(selftId))
                {
                    await Puppet.RoomPayloadDirty(payload.RoomId);
                    await Puppet.RoomMemberPayloadDirty(payload.RoomId);
                }
            }).OnRoomTopic(async payload =>
            {
                var room = Room.Load(payload.RoomId);
                await room.Sync();

                var changer = Contact.Load(payload.ChangerId);
                await changer.Ready();
                var date = payload.Timestamp.TimestampToDateTime();

                _ = EmitRoomTopic(room, payload.NewTopic, payload.OldTopic, changer, date);
            })
                .OnScan(payload => _ = EmitScan(payload.Data ?? "", payload.Status, payload.Data));
        }
Пример #3
0
        public async Task Reset()
        {
            await Puppet.Stop();

            await Puppet.Start();
        }