-
Notifications
You must be signed in to change notification settings - Fork 105
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
5 changed files
with
161 additions
and
130 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
131 changes: 131 additions & 0 deletions
131
.../main/java/org/eclipse/vorto/codegen/bosch/templates/ProvisioningAPIRequestTemplate.xtend
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,131 @@ | ||
package org.eclipse.vorto.codegen.bosch.templates | ||
|
||
import org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType | ||
import org.eclipse.vorto.core.api.model.datatype.Entity | ||
import org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType | ||
import org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType | ||
import org.eclipse.vorto.core.api.model.datatype.PrimitiveType | ||
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel | ||
import org.eclipse.vorto.plugin.generator.InvocationContext | ||
import org.eclipse.vorto.plugin.generator.utils.IFileTemplate | ||
|
||
class ProvisioningAPIRequestTemplate implements IFileTemplate<InformationModel> { | ||
|
||
override getFileName(InformationModel context) { | ||
return "provisioningRequest.json"; | ||
} | ||
|
||
override getPath(InformationModel context) { | ||
return null; | ||
} | ||
|
||
override getContent(InformationModel model, InvocationContext context) { | ||
''' | ||
{ | ||
"id": "{{device-id}}", | ||
"hub": { | ||
"device": { | ||
"enabled": true | ||
}, | ||
"credentials": { | ||
"type": "hashed-password", | ||
"secrets": [ | ||
{ | ||
"password": "{{device-password}}" | ||
} | ||
] | ||
} | ||
}, | ||
"things": { | ||
"thing": { | ||
"attributes": { | ||
"thingName": "«model.displayname»", | ||
"definition": "«model.namespace»:«model.name»:«model.version»" | ||
}, | ||
"features": { | ||
«FOR fbProperty : model.properties SEPARATOR ","» | ||
"«fbProperty.name»" : { | ||
"definition": [ | ||
"«fbProperty.type.namespace»:«fbProperty.type.name»:«fbProperty.type.version»" | ||
], | ||
"properties": { | ||
«IF fbProperty.type.functionblock.status !== null && !fbProperty.type.functionblock.status.properties.isEmpty» | ||
"status": { | ||
«FOR statusProperty : fbProperty.type.functionblock.status.properties SEPARATOR ","» | ||
"«statusProperty.name»" : «IF statusProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(statusProperty.type as PrimitivePropertyType)»«ELSEIF statusProperty.type instanceof ObjectPropertyType»«getJsonObjectType(statusProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(statusProperty.type as DictionaryPropertyType)»«ENDIF» | ||
«ENDFOR» | ||
}«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty»,«ENDIF» | ||
«ENDIF» | ||
«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty» | ||
"configuration": { | ||
«FOR configProperty : fbProperty.type.functionblock.configuration.properties SEPARATOR ","» | ||
"«configProperty.name»" : «IF configProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(configProperty.type as PrimitivePropertyType)»«ELSEIF configProperty.type instanceof ObjectPropertyType»«getJsonObjectType(configProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(configProperty.type as DictionaryPropertyType)»«ENDIF» | ||
«ENDFOR» | ||
} | ||
«ENDIF» | ||
} | ||
} | ||
«ENDFOR» | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
} | ||
|
||
def getJsonDictionaryType(DictionaryPropertyType propertyType) { | ||
''' | ||
{ | ||
"key" : "value" | ||
} | ||
''' | ||
} | ||
|
||
def String getJsonObjectType(ObjectPropertyType propertyType) { | ||
if (propertyType.type instanceof org.eclipse.vorto.core.api.model.datatype.Enum) { | ||
var literals = (propertyType.type as org.eclipse.vorto.core.api.model.datatype.Enum).enums; | ||
if (literals.empty) { | ||
return "\"\"" | ||
} else { | ||
return "\"" + literals.get(0).name + "\""; | ||
} | ||
} else { | ||
return getEntityJson(propertyType.type as Entity).toString(); | ||
} | ||
} | ||
|
||
def getEntityJson(Entity entity) { | ||
''' | ||
{ | ||
«FOR property : entity.properties SEPARATOR ","» | ||
"«property.name»" : «IF property.type instanceof PrimitivePropertyType»«getJsonPrimitive(property.type as PrimitivePropertyType)»«ELSE»«getJsonObjectType(property.type as ObjectPropertyType)»«ENDIF» | ||
«ENDFOR» | ||
} | ||
''' | ||
} | ||
|
||
def getJsonPrimitive(PrimitivePropertyType propertyType) { | ||
if (propertyType.type === PrimitiveType.BASE64_BINARY) { | ||
return "\"\"" | ||
} else if (propertyType.type === PrimitiveType.BOOLEAN) { | ||
return false | ||
} else if (propertyType.type === PrimitiveType.BYTE) { | ||
return "\"\"" | ||
} else if (propertyType.type === PrimitiveType.DATETIME) { | ||
return "\"2019-04-01T18:25:43-00:00\"" | ||
} else if (propertyType.type === PrimitiveType.DOUBLE) { | ||
return 0.0 | ||
} else if (propertyType.type === PrimitiveType.FLOAT) { | ||
return 0.0 | ||
} else if (propertyType.type === PrimitiveType.INT) { | ||
return 0 | ||
} else if (propertyType.type === PrimitiveType.LONG) { | ||
return 0 | ||
} else if (propertyType.type === PrimitiveType.SHORT) { | ||
return 0 | ||
} else { | ||
return "\"\"" | ||
} | ||
} | ||
|
||
} |
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.