Mr-Markus / ZigbeeNet

A .NET Standard library for working with ZigBee
Eclipse Public License 1.0
131 stars 47 forks source link

Use T4 template for generating zpi and zcl code #17

Closed Mr-Markus closed 5 years ago

Mr-Markus commented 5 years ago

Currently there are two Meta classes (ZpiMeta and ZclMeta) that parse JSON files and provide Methods to get instances for ZpiObject and ZclCommand.

ZpiMeta

public static ZpiObject GetCommand(SubSystem subSystem, byte cmdId)
{
      return ZpiObjects[subSystem].SingleOrDefault(cmd => cmd.CommandId == cmdId);
}

ZclMeta

public static ZclGlobalCommand GetGlobalCommand(byte cmdId)
{
      return GlobalCommands.Single(fc => fc.Id.Equals(cmdId));
}

public static ZclClusterCommand GetClusterCommand(string cluster, string cmd)
{
      return ClusterCommands.Single(fc => fc.Cluster.Equals(cluster) && fc.Name.Equals(cmd));
}

This was just a first solution to get an object with correct and typed Parameters as a ArgumentCollection.

As next step a code generation with T4 template could be a possible solution. Code in meta classes for parsing JSON file can be reused.

But i think that we still need the posibillity to dynamically create an instance of an object because we do not know the type if an message is incomming. At this point we have to parse the object and ist arguments into the right type.

eiaro commented 5 years ago

Please use same names in types as the API has. This makes it easier to follow.

Mr-Markus commented 5 years ago

I tought about the whole concept of commands and now i am at the point I would say that the current solution is not very well. Also T4 templates. Instead of using JSON definitions we should use own classes for each request and response. And these classes should be inherited from an base class (e.g. SerialPacket)

So I' thinking about removing ZpiObject, ZpiSREQ, ZpiMeta and the json files.

The sending and reading methods are already a good solution and should be further used

@nicolaiw What do you think?

Mr-Markus commented 5 years ago

I created a new branch rework with commit 8c31c819d970b26898c95f828d5183f4ec635eb7

I removed all ZpiObject classes and created Packet Directory in which classes with correct names are contained. This commit was the first step and just nine classes are implemented to start the Hardware and starts PermitJoin. On my machine the Example it is running:

[07:44:07 DBG] Opening interface... [07:44:08 DBG] Interface opened... [07:44:08 INF] Application started [07:44:08 DBG] Transmitted: SubSystem: SAPI, Type: SREQ, Length: 0, Cmd: 0 [07:44:10 DBG] Paket read: SubSystem: SAPI, Type: SRSP, Length: 0, Cmd: ZB_START_REQUEST_RSP [07:44:10 DBG] Paket read: SubSystem: ZDO, Type: AREQ, Length: 1, Cmd: ZDO_STATE_CHANGE_IND [07:44:10 INF] State changed: Started_as_ZigBee_Coordinator [07:44:10 DBG] Transmitted: SubSystem: ZDO, Type: SREQ, Length: 0, Cmd: 0 [07:44:10 DBG] Paket read: SubSystem: ZDO, Type: SRSP, Length: 1, Cmd: ZDO_MGMT_PERMIT_JOIN_REQ_SRSP [07:44:10 DBG] Paket read: SubSystem: ZDO, Type: AREQ, Length: 3, Cmd: ZDO_MGMT_PERMIT_JOIN_RSP

Code is not yet perfect, but i need to have some sleep. More will come soon

nicolaiw commented 5 years ago

As we implement it by hand I suggest we could close this Issue.