Packages
Pure Elixir implementation of the China Southern Power Grid (CSG) CEP Edge Gateway Plug-and-Play Protocol (TS2.2) with :telemetry instrumentation.
Current section
Files
Jump to
Current section
Files
README.md
# ExCEP
Pure Elixir implementation of the **China Southern Power Grid (CSG) CEP** Edge Gateway Plug-and-Play Protocol (**TS2.2** — 第2部分第2篇:边缘网关即插即用接口及协议), with `Jason` JSON encoding, pure-Elixir **SM2/SM3** signing, `deflate`/zlib compression, and `:telemetry ~> 1.4` instrumentation.
## Installation
Add `ex_cep` to your `mix.exs` dependencies:
```elixir
def deps do
[
{:ex_cep, "~> 0.1.1"}
]
end
```
## Architecture & Modules
| Layer / Feature | Modules |
|---|---|
| Top-level Facade & Supervisor | `ExCEP`, `ExCEP.Supervisor` |
| Stack Configuration & Interop | `config/config.exs`, `ExCEP.Config`, `ExCEP.Compat` |
| Telemetry | `ExCEP.Telemetry` (`:telemetry ~> 1.4`) |
| Common Types & Error Codes | `ExCEP.Common.{ErrorCode, AlarmEvent, Timestamp, Types}` |
| National Crypto (SM2/SM3/SM4) | `ExCEP.Crypto.{Signer, NoopSigner, SM2, SM3, SM4}` |
| Device Model Parser & Catalog (TS2.1) | `ExCEP.Model`, `ExCEP.Model.{Point, Service, Parser, IcdParser, Consistency, Catalog}` (5 domains, 26 devices, 168 LNs, 1,395 DOs) |
| APDU & ASDU Codecs | `ExCEP.Codec.{Frame, AuthAsdu, DataAsdu}` |
| Service Codecs | `ExCEP.Codec.Services.{Common, Management, Business}` |
| TCP / TLS / Cipher Transport | `ExCEP.Transport.{ConnectionParams, Socket, Cipher, NoopCipher, FrameChannel}` |
| Edge Gateway Client | `ExCEP.Gateway.{Client, DualChannel, ReconnectWorker, Handler, DefaultHandler, FileTransfer, SubscriptionManager, ReportScheduler}` |
| Edge Cluster Server | `ExCEP.Cluster.{Server, Session, Handler, DefaultHandler, ControlClient, ManagementClient}` |
| Examples & Fuzzing | `examples/{dual_channel_demo,file_transfer_demo,cluster_server,edge_gateway}.exs`, `fuzz/fuzz_decoders.exs` |
## Quick Start
```elixir
{cluster_pub, cluster_priv} = ExCEP.Crypto.SM2.generate_keypair()
{gw_pub, gw_priv} = ExCEP.Crypto.SM2.generate_keypair()
{:ok, server} =
ExCEP.start_server(
channel: :business,
port: 9902,
signer: {ExCEP.Crypto.SM2, private_key: cluster_priv, peer_public_key: gw_pub}
)
{:ok, gw} =
ExCEP.connect("127.0.0.1", 9902,
channel: :business,
gateway_id: "GW-001",
signer: {ExCEP.Crypto.SM2, private_key: gw_priv, peer_public_key: cluster_pub}
)
{:ok, models} = ExCEP.get_model(server, "GW-001")
{:ok, _resp} = ExCEP.sbo_control(server, "GW-001", "DEV-1", "Breaker1", :yk, "1")
:ok = ExCEP.disconnect(gw)
:ok = ExCEP.stop_server(server)
```
## Telemetry
Attach the default logger with `ExCEP.Telemetry.attach_default_logger/1` or subscribe to `ExCEP.Telemetry.events/0`.
Pass an optional flat scalar `telemetry_context: %{...}` map to `ExCEP.Gateway.Client`, `ExCEP.Gateway.ReconnectWorker`, `ExCEP.Gateway.DualChannel`, or `ExCEP.Cluster.Server` (propagated to `ExCEP.Cluster.Session`) to attach correlation fields (e.g. `channel_id`, `config_revision`) to all emitted events and spans without overwriting native library keys (`result`, `status`, `channel`, etc.).
## Running Tests & Demo
```bash
mix deps.get
mix compile --warnings-as-errors
mix test
mix run fuzz/fuzz_decoders.exs 500
mix run examples/dual_channel_demo.exs
mix run examples/file_transfer_demo.exs
```