-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(composer): add utils to convert all nodes to entities
- Loading branch information
Showing
20 changed files
with
748 additions
and
136 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
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
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
102 changes: 102 additions & 0 deletions
102
packages/scene-composer/src/utils/entityModelUtils/createNodeEntity.spec.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,102 @@ | ||
import { TwinMakerSceneMetadataModule } from '@iot-app-kit/source-iottwinmaker'; | ||
|
||
import { setTwinMakerSceneMetadataModule } from '../../common/GlobalSettings'; | ||
import { defaultNode } from '../../../__mocks__/sceneNode'; | ||
import { KnownComponentType } from '../../interfaces'; | ||
|
||
import { createNodeEntityComponent } from './nodeComponent'; | ||
import { createTagEntityComponent } from './tagComponent'; | ||
import { createOverlayEntityComponent } from './overlayComponent'; | ||
import { createCameraEntityComponent } from './cameraComponent'; | ||
import { createMotionIndicatorEntityComponent } from './motionIndicatorComponent'; | ||
import { createModelRefComponent } from './modelRefComponent'; | ||
import { createNodeEntity } from './createNodeEntity'; | ||
|
||
jest.mock('./nodeComponent', () => ({ | ||
createNodeEntityComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.node' }), | ||
})); | ||
jest.mock('./tagComponent', () => ({ | ||
createTagEntityComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.tag' }), | ||
})); | ||
jest.mock('./overlayComponent', () => ({ | ||
createOverlayEntityComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.overlay' }), | ||
})); | ||
jest.mock('./cameraComponent', () => ({ | ||
createCameraEntityComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.camera' }), | ||
})); | ||
jest.mock('./motionIndicatorComponent', () => ({ | ||
createMotionIndicatorEntityComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.motionIndicator' }), | ||
})); | ||
jest.mock('./modelRefComponent', () => ({ | ||
createModelRefComponent: jest.fn().mockReturnValue({ componentTypeId: '3d.modelRef' }), | ||
})); | ||
|
||
describe('createNodeEntity', () => { | ||
const createSceneEntity = jest.fn(); | ||
const mockMetadataModule: Partial<TwinMakerSceneMetadataModule> = { | ||
createSceneEntity, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
setTwinMakerSceneMetadataModule(mockMetadataModule as unknown as TwinMakerSceneMetadataModule); | ||
}); | ||
|
||
it('should call create entity with only node component', async () => { | ||
await createNodeEntity(defaultNode, 'parent', 'layer'); | ||
|
||
expect(createSceneEntity).toHaveBeenCalledTimes(1); | ||
expect(createSceneEntity).toHaveBeenCalledWith({ | ||
workspaceId: undefined, | ||
entityId: defaultNode.ref, | ||
entityName: defaultNode.name + '_' + defaultNode.ref, | ||
parentEntityId: 'parent', | ||
components: { | ||
Node: { componentTypeId: '3d.node' }, | ||
}, | ||
}); | ||
|
||
expect(createNodeEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createNodeEntityComponent).toHaveBeenCalledWith(defaultNode, 'layer'); | ||
}); | ||
|
||
it('should call create entity to create multiple components', async () => { | ||
const tag = { type: KnownComponentType.Tag, ref: 'tag-ref' }; | ||
const overlay = { type: KnownComponentType.DataOverlay, ref: 'overlay-ref' }; | ||
const camera = { type: KnownComponentType.Camera, ref: 'camera-ref' }; | ||
const motionIndicator = { type: KnownComponentType.MotionIndicator, ref: 'indicator-ref' }; | ||
const modelRef = { type: KnownComponentType.ModelRef, ref: 'modelref-ref' }; | ||
const node = { ...defaultNode, components: [tag, overlay, camera, motionIndicator, modelRef] }; | ||
|
||
await createNodeEntity(node, 'parent', 'layer'); | ||
|
||
expect(createSceneEntity).toHaveBeenCalledTimes(1); | ||
expect(createSceneEntity).toHaveBeenCalledWith({ | ||
workspaceId: undefined, | ||
entityId: defaultNode.ref, | ||
entityName: defaultNode.name + '_' + defaultNode.ref, | ||
parentEntityId: 'parent', | ||
components: { | ||
Node: { componentTypeId: '3d.node' }, | ||
Tag: { componentTypeId: '3d.tag' }, | ||
DataOverlay: { componentTypeId: '3d.overlay' }, | ||
Camera: { componentTypeId: '3d.camera' }, | ||
MotionIndicator: { componentTypeId: '3d.motionIndicator' }, | ||
ModelRef: { componentTypeId: '3d.modelRef' }, | ||
}, | ||
}); | ||
|
||
expect(createNodeEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createNodeEntityComponent).toHaveBeenCalledWith(node, 'layer'); | ||
expect(createTagEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createTagEntityComponent).toHaveBeenCalledWith(tag); | ||
expect(createOverlayEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createOverlayEntityComponent).toHaveBeenCalledWith(overlay); | ||
expect(createCameraEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createCameraEntityComponent).toHaveBeenCalledWith(camera); | ||
expect(createMotionIndicatorEntityComponent).toHaveBeenCalledTimes(1); | ||
expect(createMotionIndicatorEntityComponent).toHaveBeenCalledWith(motionIndicator); | ||
expect(createModelRefComponent).toHaveBeenCalledTimes(1); | ||
expect(createModelRefComponent).toHaveBeenCalledWith(modelRef); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
packages/scene-composer/src/utils/entityModelUtils/createNodeEntity.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,74 @@ | ||
import { | ||
ComponentUpdateRequest, | ||
CreateEntityCommandInput, | ||
CreateEntityCommandOutput, | ||
} from '@aws-sdk/client-iottwinmaker'; | ||
|
||
import { getGlobalSettings } from '../../common/GlobalSettings'; | ||
import { | ||
ICameraComponent, | ||
IDataOverlayComponent, | ||
IModelRefComponent, | ||
IMotionIndicatorComponent, | ||
KnownComponentType, | ||
} from '../../interfaces'; | ||
import { ISceneNodeInternal } from '../../store/internalInterfaces'; | ||
|
||
import { createNodeEntityComponent } from './nodeComponent'; | ||
import { createTagEntityComponent } from './tagComponent'; | ||
import { createOverlayEntityComponent } from './overlayComponent'; | ||
import { createMotionIndicatorEntityComponent } from './motionIndicatorComponent'; | ||
import { createCameraEntityComponent } from './cameraComponent'; | ||
import { createModelRefComponent } from './modelRefComponent'; | ||
|
||
export const createNodeEntity = ( | ||
node: ISceneNodeInternal, | ||
parentRef: string, | ||
layerId: string, | ||
): Promise<CreateEntityCommandOutput> | undefined => { | ||
const sceneMetadataModule = getGlobalSettings().twinMakerSceneMetadataModule; | ||
|
||
const nodecomp = createNodeEntityComponent(node, layerId); | ||
|
||
const createEntity: CreateEntityCommandInput = { | ||
workspaceId: undefined, | ||
entityId: node.ref, | ||
entityName: node.name + '_' + node.ref, | ||
parentEntityId: parentRef, | ||
components: { | ||
Node: nodecomp, | ||
}, | ||
}; | ||
|
||
node.components?.forEach((compToBeCreated) => { | ||
if (compToBeCreated?.type !== KnownComponentType.EntityBinding) { | ||
let comp: ComponentUpdateRequest | undefined = undefined; | ||
switch (compToBeCreated?.type) { | ||
case KnownComponentType.Tag: | ||
comp = createTagEntityComponent(compToBeCreated); | ||
break; | ||
case KnownComponentType.DataOverlay: | ||
comp = createOverlayEntityComponent(compToBeCreated as IDataOverlayComponent); | ||
break; | ||
case KnownComponentType.MotionIndicator: | ||
comp = createMotionIndicatorEntityComponent(compToBeCreated as IMotionIndicatorComponent); | ||
break; | ||
case KnownComponentType.Camera: | ||
comp = createCameraEntityComponent(compToBeCreated as ICameraComponent); | ||
break; | ||
case KnownComponentType.ModelRef: | ||
comp = createModelRefComponent(compToBeCreated as IModelRefComponent); | ||
break; | ||
|
||
default: | ||
throw new Error('Component type not supported'); | ||
} | ||
|
||
if (comp) { | ||
createEntity.components![compToBeCreated.type] = comp; | ||
} | ||
} | ||
}); | ||
|
||
return sceneMetadataModule?.createSceneEntity(createEntity); | ||
}; |
Oops, something went wrong.