Source

protocol-example / test.py

Full commit
import protocols, struct
from makejava import javaprotocol

protocols = protocols.load_protocols(".")

test_data = struct.pack("!bq", 0x021, 123456789)

result = protocols[4.0].parse(test_data)

assert result == {'timestamp': 123456789, 'type': 'ALIVE'}
assert (javaprotocol(protocols[4.0]) == ('Protocol40', '\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.DataOutputStream;\n\npublic class Protocol40 {\n    public static String VERSION = "4.0";\n    \npublic static final byte[] packAlive(long timestamp) {\n    try\n    {\n        final ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        final DataOutputStream daos = new DataOutputStream(baos);\n        daos.writeByte(0x21);\n        daos.writeLong(timestamp);\n        final byte[] data = baos.toByteArray();\n        baos.close();\n        daos.close();\n        return data;\n    } catch (Exception e) {\n        e.printStackTrace();\n    }    \n    return null;\n}\n\n\n\npublic static final byte[] packLog(long timestamp,String message,short signal,short voltage) {\n    try\n    {\n        final ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        final DataOutputStream daos = new DataOutputStream(baos);\n        daos.writeByte(0x23);\n        daos.writeLong(timestamp);\n        daos.writeString(message);\n        daos.writeShort(signal);\n        daos.writeShort(voltage);\n        final byte[] data = baos.toByteArray();\n        baos.close();\n        daos.close();\n        return data;\n    } catch (Exception e) {\n        e.printStackTrace();\n    }    \n    return null;\n}\n\n}\n')
)

print("OK")