-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
473 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
...ages/adapter-tcp/src/event-handlers/command-event-handlers/locate-device.event-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
...domain-event-handlers/lock-device-when-device-is-connected-event-handler.event-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { DomainException } from '@agnoc/toolkit'; | ||
import type { PackerServerConnectionHandler } from '../../packet-server.connection-handler'; | ||
import type { DomainEventHandler, DeviceConnectedDomainEvent } from '@agnoc/domain'; | ||
import { PacketConnection } from '../../aggregate-roots/packet-connection.aggregate-root'; | ||
import type { DomainEventHandler, DeviceConnectedDomainEvent, ConnectionRepository, Connection } from '@agnoc/domain'; | ||
|
||
export class LockDeviceWhenDeviceIsConnectedEventHandler implements DomainEventHandler { | ||
readonly forName = 'DeviceConnectedDomainEvent'; | ||
|
||
constructor(private readonly connectionManager: PackerServerConnectionHandler) {} | ||
constructor(private readonly connectionRepository: ConnectionRepository) {} | ||
|
||
async handle(event: DeviceConnectedDomainEvent): Promise<void> { | ||
const [connection] = this.connectionManager.findConnectionsByDeviceId(event.aggregateId); | ||
const connections = await this.connectionRepository.findByDeviceId(event.aggregateId); | ||
|
||
if (!connection) { | ||
if (connections.length === 0) { | ||
throw new DomainException(`Unable to find a connection for the device with id ${event.aggregateId.value}`); | ||
} | ||
|
||
const connection = connections.find((connection: Connection): connection is PacketConnection => | ||
PacketConnection.isPacketConnection(connection), | ||
); | ||
|
||
if (!connection) { | ||
return; | ||
} | ||
|
||
await connection.send('DEVICE_CONTROL_LOCK_REQ', {}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...domain-event-handlers/set-device-connected-when-connection-device-changed.domain-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { | ||
DomainEventHandler, | ||
ConnectionRepository, | ||
DeviceRepository, | ||
ConnectionDeviceChangedDomainEvent, | ||
} from '@agnoc/domain'; | ||
|
||
export class SetDeviceAsConnectedWhenConnectionDeviceAddedDomainEventHandler implements DomainEventHandler { | ||
readonly forName = 'ConnectionDeviceChangedDomainEvent'; | ||
|
||
constructor( | ||
private readonly connectionRepository: ConnectionRepository, | ||
private readonly deviceRepository: DeviceRepository, | ||
) {} | ||
|
||
async handle(event: ConnectionDeviceChangedDomainEvent): Promise<void> { | ||
if (event.currentDeviceId) { | ||
const connections = await this.connectionRepository.findByDeviceId(event.currentDeviceId); | ||
const device = await this.deviceRepository.findOneById(event.currentDeviceId); | ||
|
||
// This is a hack to only mark the device as connected if there is more than one connection. | ||
// Here we should check that the connections are from the same ip address. | ||
if (connections.length > 1 && device && !device.isConnected) { | ||
device.setAsConnected(); | ||
|
||
await this.deviceRepository.saveOne(device); | ||
} | ||
} | ||
|
||
// TODO: handle device disconnection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PacketConnection } from '../aggregate-roots/packet-connection.aggregate-root'; | ||
import type { PacketConnectionProps } from '../aggregate-roots/packet-connection.aggregate-root'; | ||
import type { PacketEventBus } from '../packet.event-bus'; | ||
import type { Factory } from '@agnoc/toolkit'; | ||
import type { PacketFactory } from '@agnoc/transport-tcp'; | ||
|
||
export class PacketConnectionFactory implements Factory<PacketConnection> { | ||
constructor(private readonly packetEventBus: PacketEventBus, private readonly packetFactory: PacketFactory) {} | ||
|
||
create(props: PacketConnectionProps): PacketConnection { | ||
return new PacketConnection(this.packetFactory, this.packetEventBus, props); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.