What's Package¶
The Port package is a collection of reusable libraries. Users can develop packages by inheriting the PortObject
, then link it to a Message, allowing for straightforward usage through Message calls.
Download¶
NAME | Language | Package Manager | OS | STABLE |
---|---|---|---|---|
Port.SDK | C# | Nuget | Windows x64 | Yes |
Package Annotation¶
name | targets | type | arguments | description |
---|---|---|---|---|
Port | class | - |
Class Type |
Declaring a port attribute in a class designates that class as one managed by the port system. Once declared, the class can be registered as part of a package. |
Vaild | method property |
bool |
invalid comment |
Maps the property to a pre-declared Message Property. |
Message | property | string|double |
- |
Messages are declared, and the values defined as properties can be controlled through package calls. |
Logger | property | ILogger |
- |
Specifies that the Logger field is to be injected with a logging system or service. |
Property | property | IProperty |
Message name |
Maps the property to a pre-declared Message Property. |
Valid¶
[Valid("Invlid for connection")]
public bool Valid()
{
return true;
}
Port¶
This annotation indicates that the Heater class is managed within the Port Package.
[Port(typeof(Heater))]
public class Heater
...
Logger¶
This annotation specifies that the Logger field is to be injected with a logging system or service.
[Logger]
public ILogger Logger { get; set; }
...
Logger.Write(string.Join(",", v));
...
Property¶
This annotation maps the property to a declared Message Property.
[Property]
public IProperty Property { get; set; }
...
if (this.Property.TryToGetValue("Unit", out string v1)){
...
}
...
Message¶
Properties declared with Message Annotation are defined as API Messages and made available to the end-user. They apply only to properties with get and set accessors, and these getters and setters can be accessed and modified via a REST API.
private static Random r = new Random(100);
[Message(PortDataType.Num, PropertyFormat.Json, "Unit")]
public double Temp
{
get
{
try
{
if (this.Property != null)
{
if (this.Property.TryToGetValue("Unit", out string v1) && (v1 == "F"))
{
return (r.NextDouble() * 9 / 5) + 32;
}
else if (this.Property.TryToGetValue("Unit", out string v2) && (v2 == "C"))
{
return (r.NextDouble());
}
else
{
return double.NaN;
}
}
return double.NaN;
}
catch (Exception e)
{
if (Logger != null)
Logger.Write(e.Message);
}
return double.NaN;
}
}
EnumCode¶
EnumCode Annotation are declared, the values of the enum can be accessed via an API. The API allows for the retrieval of information about the enumeration values, enabling external systems or users to interact with and obtain details about the enum through the API interface.
Comment¶
When Comment Annotation are declared, the comments associated with the property can be exposed through the API. This allows users or external systems to access descriptive information or documentation about the property via the API, providing context and clarity on the property's purpose or usage.
How to create a packages¶
Let's develop a package. In the Port Application, all operations are grouped at the package level and function at the message level. Every operation is defined within messages, allowing users to increase code reusability through messages.
Create a class library(.NET)¶
bulb package¶
[Port(typeof(Bulb))]
public class Bulb
{
[Logger]
public ILogger Logger { get; set; }
private SerialPortStream serialPort = new SerialPortStream();
[Valid("")]
public bool Valid()
{
return true;
}
private string comport;
[Message(PortDataType.Text)]
public string Comport
{
set
{
try
{
if (this.serialPort.PortName != value)
{
this.serialPort = new SerialPortStream();
this.serialPort.PortName = value.ToString();
this.serialPort.BaudRate = 9600;
this.serialPort.DataBits = 8;
this.serialPort.StopBits = StopBits.One;
this.serialPort.Parity = Parity.Even;
}
}
catch (System.Exception ex)
{
Logger.Write("[ERROR]" + ex.Message);
}
}
get
{
return comport;
}
}
private string offon = string.Empty;
[Message(PortDataType.Enum), Property(PropertyFormat.Json)]
public string OffOn
{
set
{
var prop = MessageProperty.Get();
try
{
if (prop != null)
{
//prop.Arguments[0]
//Logger.Write("[Arguments]" + prop.Arguments[0]);
this.offon = value;
}
}
catch (Exception ex)
{
Logger.Write("[ERROR]" + ex.Message);
}
}
get
{
return this.offon;
}
}
}
heater package¶
[Port(typeof(Heater))]
public class Heater
{
[Message(PortDataType.Text)]
public string Power
{
set;
get;
}
[Valid("Invlid for connection")]
public bool Valid()
{
return true;
}
private static Random r = new Random(100);
[Message(PortDataType.Num), Property(PropertyFormat.Json)]
public double Temp
{
get
{
var prop = MessageProperty.Get();
if (prop != null)
{
if (prop.TryToGetValue("Arguments", out object value) && value.ToString() == "F")
{
return ((r.NextDouble() * 9 / 5) + 32);
}
else if (prop.TryToGetValue("Arguments", out object v2) && v2.ToString() == "C")
{
return (r.NextDouble());
}
}
return (1);
}
}
}
Warning
When creating a library in a .NET environment, declaring an excessively large array may result in a PrivateImplementationDetails error. It is recommended to use a List instead.
How to publish a library (.NET)¶
This manual provides step-by-step instructions on how to publish a .NET Core project using Visual Studio Code.
1. Prerequisites
- Install .NET SDK: Make sure you have the latest version of the .NET SDK installed. Download here.
- Install C# Extension: Install the C# extension in Visual Studio Code from the Marketplace.
- Verify the Build: Ensure your project builds and runs correctly:
dotnet build dotnet run
2. Publish Command
Basic Publish Command
Run the following command to publish your project:
dotnet publish -c Release -o ./publish
-c Release
: Builds the project in Release mode.-o ./publish
: Specifies the output folder for the published files.
Publishing for Specific Runtime
To target a specific runtime (e.g., Windows, Linux, macOS), use the following command:
dotnet publish -c Release -r win-x64 --self-contained false
-r win-x64
: Targets Windows 64-bit. Examples for other platforms:linux-x64
: Linux 64-bitosx-x64
: macOS 64-bit--self-contained false
: Requires .NET runtime to be installed on the target environment.
3. Automating Publish with Tasks in VS Code
Set up a task in VS Code to automate the publish process:
- Open the
.vscode/tasks.json
file. If it doesn’t exist, create it. - Add the following configuration:
{
"version": "2.0.0",
"tasks": [
{
"label": "Publish .NET Core",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"-c",
"Release",
"-o",
"./publish"
],
"problemMatcher": "$msCompile"
}
]
}
Run the task in VS Code:
- Open the Command Palette (
Ctrl+Shift+P
). - Select
Tasks: Run Task
. - Choose
Publish .NET Core
.
4. Deployment
The published files will be available in the ./publish
folder. Copy these files to the desired deployment location:
- Local Deployment: Copy files to a server or hosting environment.
- Docker Deployment: Use a
Dockerfile
to containerize your application:FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app COPY ./publish . ENTRYPOINT ["dotnet", "YourApp.dll"]
5. Logging and Debugging
To log publish output to a file, use:
dotnet publish > publish_log.txt
Check the logs for any errors and resolve them before deployment.
6. Additional Resources
7. With VS2022¶
Packing packages¶
-
cd [Publish target location]
-
port pack [dllname] [pkg-name]
-
check logs