Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,849 changes: 3,849 additions & 0 deletions TiaCodegen-ts/package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions TiaCodegen-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "tia-codegen-ts",
"version": "1.0.0",
"description": "TypeScript port of TiaCodeGen C# library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest --testPathPattern=tests",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.12.7",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.5"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/tests/**/*.test.ts"]
}
}
99 changes: 99 additions & 0 deletions TiaCodegen-ts/src/Blocks/Block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { CodeBlock } from './CodeBlock';
import { KopCodeHelper } from '../CodeGen/KopCodeHelper';

export class Block {
name: string;
number: number = 0;
private codeBlock: CodeBlock;
title: string | undefined;
titleEnglish: string | undefined;
comment: string | undefined;
commentEnglish: string | undefined;
author: string | undefined;
blockInterface: string;

constructor(name: string, title: string, codeBlock: CodeBlock) {
this.name = name;
this.title = title;
this.codeBlock = codeBlock;

this.blockInterface = `
<Interface>
<Sections xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v2">
<Section Name="Input" />
<Section Name="Output" />
<Section Name="InOut" />
<Section Name="Temp" />
<Section Name="Constant" />
<Section Name="Return">
<Member Name="Ret_Val" Datatype="Void" />
</Section>
</Sections>
</Interface>
`;
}

getCode(): string {
const idRef = { value: 0 };
let code = this.getBlockHeader(idRef);
code += new KopCodeHelper(this.codeBlock).getXml(idRef);
code += this.getBlockFooter(idRef);
return code;
}

getBlockHeader(idRef: { value: number }): string {
const header = `
<SW.Blocks.FC ID="${idRef.value++}">
<AttributeList>
${this.author !== undefined && this.author !== null ? '<HeaderAuthor>' + this.author + '</HeaderAuthor>' : ''}
<HeaderFamily>General</HeaderFamily>
<HeaderVersion>1.0</HeaderVersion>
<MemoryLayout>Optimized</MemoryLayout>
<Name>${this.name}</Name>
${this.number !== 0 ? '<Number>' + this.number + '</Number>' : ''}
<Namespace />
<ProgrammingLanguage>LAD</ProgrammingLanguage>
</AttributeList>
<ObjectList>`;
return header;
}

getBlockFooter(idRef: { value: number }): string {
const footer = `
<MultilingualText ID="${idRef.value++}" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>${this.title}</Text>
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text>${this.titleEnglish}</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="${idRef.value++}" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>${this.comment}</Text>
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text>${this.commentEnglish}</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.FC>`;
return footer;
}
}
11 changes: 11 additions & 0 deletions TiaCodegen-ts/src/Blocks/CodeBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseOperationOrSignal } from '../Commands/BaseOperationOrSignal';

export class CodeBlock extends BaseOperationOrSignal {
name: string;
safety: boolean = false;

constructor(name: string = '') {
super();
this.name = name;
}
}
37 changes: 37 additions & 0 deletions TiaCodegen-ts/src/Blocks/Network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IOperationOrSignal } from '../Interfaces/IOperationOrSignal';
import { BaseOperationOrSignal } from '../Commands/BaseOperationOrSignal';

export class Network extends BaseOperationOrSignal {
networkTitle: string | undefined;
description: string | undefined;
networkTitleEnglish: string | undefined;
descriptionEnglish: string | undefined;

constructor(
networkTitleOrOp?: string | IOperationOrSignal,
networkTitleEnglishOrOp?: string | IOperationOrSignal,
...operationOrSignals: IOperationOrSignal[]
) {
super();
if (typeof networkTitleOrOp === 'string') {
this.networkTitle = networkTitleOrOp;
this.networkTitleEnglish = typeof networkTitleEnglishOrOp === 'string'
? networkTitleEnglishOrOp
: undefined;
if (networkTitleEnglishOrOp !== undefined && typeof networkTitleEnglishOrOp !== 'string') {
this.children.push(networkTitleEnglishOrOp);
}
for (const op of operationOrSignals) {
this.children.push(op);
}
} else {
if (networkTitleOrOp !== undefined) this.children.push(networkTitleOrOp);
if (networkTitleEnglishOrOp !== undefined && typeof networkTitleEnglishOrOp !== 'string') {
this.children.push(networkTitleEnglishOrOp);
}
for (const op of operationOrSignals) {
this.children.push(op);
}
}
}
}
Loading