Skip to content

Commit

Permalink
support 09 & 0a
Browse files Browse the repository at this point in the history
  • Loading branch information
LLLLimbo committed Jul 4, 2023
1 parent 57ff91b commit 4e36d3e
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 6 deletions.
5 changes: 2 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ This is the current list of supported messages, you can find more detailed infor
| 0x04 | 心跳包应答 | 运营平台->充电桩 | :white_check_mark: |
| 0x05 | 计费模型验证请求 | 充电桩->运营平台 | :white_check_mark: |
| 0x06 | 计费模型验证请求应答 | 运营平台->充电桩 | :white_check_mark: |
| 0x09 | 充电桩计费模型请求 | 充电桩->运营平台 | |
| 0x0A | 计费模型请求应答 | 运营平台->充电桩 | |
| 0x09 | 充电桩计费模型请求 | 充电桩->运营平台 | :white_check_mark: |
| 0x0A | 计费模型请求应答 | 运营平台->充电桩 | :white_check_mark: |
| 0x12 | 读取实时监测数据 | 运营平台->充电桩 | |
| 0x13 | 离线监测数据 | 充电桩->运营平台 | :white_check_mark: |
| 0x15 | 充电握手 | 充电桩->运营平台 | |
Expand Down Expand Up @@ -154,4 +154,3 @@ This is the current list of supported messages, you can find more detailed infor
| 0xA3 | 远程并充启机命令回复 | 运营平台->充电桩 | |
| 0xA4 | 运营平台远程控制并充启机 | 充电桩->运营平台 | |


9 changes: 9 additions & 0 deletions doc/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@



### Billing model request (09)

| Field | Type | Description |
| ------ | ------ | ----------- |
| header | Header | |
| id | string | device id |



### Offline data report (13)

| Field | Type | Description |
Expand Down
55 changes: 55 additions & 0 deletions doc/restapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,61 @@ Response body:



### Billing model response(0a)

Path: `/proxy/0a`

Request body:

| Field | Type | Description |
| ---------------- | ------ | ------------------------------------------------------------ |
| header | Header | |
| id | string | device id |
| billingModelCode | string | code of billing model |
| sharpUnitPrice | int | sharp unit price (X100000) |
| sharpServiceFee | int | sharp service fee (X100000) |
| peakUnitPrice | int | peak unit price (X100000) |
| peakServiceFee | int | peak service fee (X100000) |
| flatUnitPrice | int | flat unit price (X100000) |
| flatServiceFee | int | flat service fee (X100000) |
| valleyUnitPrice | int | valley unit price (X100000) |
| valleyServiceFee | int | valley service fee (X100000) |
| accrualRatio | int | see [pdf](云快充平台协议V1.6.pdf) for more information |
| rateList | []int | list of rate numbers per time period 0-sharp price 1-peak price 2-flat price 3-valley price |



Example request:

```json
{
"header":{
"encrypted": false,
"seq": 73
},
"billingModelCode": "01",
"sharpUnitPrice": 100000,
"sharpServiceFee": 100000,
"peakUnitPrice": 100000,
"peakServiceFee": 100000,
"flatUnitPrice": 100000,
"flatServiceFee": 100000,
"valleyUnitPrice": 100000,
"valleyServiceFee": 100000,
"rateList": [0, 0, 3, 2, 3, 0, 0, 0, 3, 0, 0, 1, 0, 3, 1, 1, 0, 3, 2, 3, 1, 2, 3, 1, 3, 2, 1, 0, 0, 0, 1, 0, 1, 2, 2, 0, 2, 0, 0, 0, 0, 1, 3, 2, 2, 2, 2, 2]
}
```





Response body:

| Field | Type | Description |
| ------- | ------ | ------------- |
| message | string | error message |



### Remote bootstrap(34)
Expand Down
14 changes: 14 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,17 @@ func SendSetBillingModelRequestMessage(req *SetBillingModelRequestMessage) error
}).Debug("[58] SetBillingModelRequest message sent")
return nil
}

func SendBillingModelResponseMessage(req *BillingModelResponseMessage) error {
c, err := GetClient(req.Id)
if err != nil {
return err
}
resp := PackBillingModelResponseMessage(req)
_, _ = c.Write(resp)
log.WithFields(log.Fields{
"id": req.Id,
"request": BytesToHex(resp),
}).Debug("[0a] BillingModelResponse message sent")
return nil
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func enableHttpServer(opt *Options) {
r := gin.Default()
r.POST("/proxy/02", VerificationResponseRouter)
r.POST("/proxy/06", BillingModelVerificationResponseRouter)
r.POST("/proxy/0a", BillingModelResponseMessageRouter)
r.POST("/proxy/34", RemoteBootstrapRequestRouter)
r.POST("/proxy/36", RemoteShutdownRequestRouter)
r.POST("/proxy/40", TransactionRecordConfirmedRouter)
Expand Down Expand Up @@ -165,6 +166,9 @@ func drain(opt *Options, conn net.Conn) error {
case BillingModelVerification:
BillingModelVerificationRouter(opt, hex, header, conn)
break
case BillingModelRequest:
BillingModelRequestMessageRouter(opt, hex, header, conn)
break
case OfflineDataReport:
OfflineDataReportMessageRouter(opt, hex, header)
break
Expand Down
71 changes: 69 additions & 2 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
Verification = byte(0x01)
Heartbeat = byte(0x03)
BillingModelVerification = byte(0x05)
BillingModelRequest = byte(0x08)
BillingModelRequest = byte(0x09)
OfflineDataReport = byte(0x13)
ChargingHandshake = byte(0x15)
Configuration = byte(0x17)
Expand Down Expand Up @@ -180,6 +180,73 @@ func PackBillingModelVerificationMessage(hex []string, header *Header) *BillingM
return msg
}

type BillingModelRequestMessage struct {
Header *Header `json:"header"`
Id string `json:"Id"`
}

func PackBillingModelRequestMessage(hex []string, header *Header) *BillingModelRequestMessage {
//Id
id := ""
for _, v := range hex[6:13] {
id += v
}

msg := &BillingModelRequestMessage{
Header: header,
Id: id,
}
return msg
}

type BillingModelResponseMessage struct {
Header *Header `json:"header"`
Id string `json:"id"`
BillingModelCode string `json:"billingModelCode"`
SharpUnitPrice int `json:"sharpUnitPrice"`
SharpServiceFee int `json:"sharpServiceFee"`
PeakUnitPrice int `json:"peakUnitPrice"`
PeakServiceFee int `json:"peakServiceFee"`
FlatUnitPrice int `json:"flatUnitPrice"`
FlatServiceFee int `json:"flatServiceFee"`
ValleyUnitPrice int `json:"valleyUnitPrice"`
ValleyServiceFee int `json:"valleyServiceFee"`
AccrualRatio int `json:"accrualRatio"`
RateList []int `json:"rateList"`
}

func PackBillingModelResponseMessage(msg *BillingModelResponseMessage) []byte {
var resp bytes.Buffer
resp.Write([]byte{0x68, 0x5e})
seqStr := fmt.Sprintf("%x", GenerateSeq())
seq := ConvertIntSeqToReversedHexArr(seqStr)
resp.Write(HexToBytes(MakeHexStringFromHexArray(seq)))
if msg.Header.Encrypted {
resp.WriteByte(0x01)
} else {
resp.WriteByte(0x00)
}
resp.Write([]byte{0x0a})
resp.Write(HexToBytes(msg.Id))
resp.Write(HexToBytes(msg.BillingModelCode))
resp.Write(IntToBytes(msg.SharpUnitPrice))
resp.Write(IntToBytes(msg.SharpServiceFee))
resp.Write(IntToBytes(msg.PeakUnitPrice))
resp.Write(IntToBytes(msg.PeakServiceFee))
resp.Write(IntToBytes(msg.FlatUnitPrice))
resp.Write(IntToBytes(msg.FlatServiceFee))
resp.Write(IntToBytes(msg.ValleyUnitPrice))
resp.Write(IntToBytes(msg.ValleyServiceFee))
resp.Write([]byte(strconv.Itoa(msg.AccrualRatio)))

for _, v := range msg.RateList {
resp.Write(IntToBytes(v))
}

resp.Write(ModbusCRC(resp.Bytes()[2:]))
return resp.Bytes()
}

type BillingModelVerificationResponseMessage struct {
Header *Header `json:"header"`
Id string `json:"id"`
Expand Down Expand Up @@ -812,7 +879,7 @@ func PackSetBillingModelRequestMessage(msg *SetBillingModelRequestMessage) []byt
resp.Write([]byte(strconv.Itoa(msg.AccrualRatio)))

for _, v := range msg.RateList {
resp.Write([]byte(strconv.Itoa(v)))
resp.Write(IntToBytes(v))
}

resp.Write(ModbusCRC(resp.Bytes()[2:]))
Expand Down
28 changes: 27 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func BillingModelVerificationRouter(opt *Options, hex []string, header *Header,
log.WithFields(log.Fields{
"id": msg.Id,
"billing_model_code": msg.BillingModelCode,
}).Debug("[05] BillingModelRequest message")
}).Debug("[05] BillingModelVerification message")

//auto response
if opt.AutoBillingModelVerify {
Expand All @@ -113,6 +113,32 @@ func BillingModelVerificationRouter(opt *Options, hex []string, header *Header,
}
}

func BillingModelRequestMessageRouter(opt *Options, hex []string, header *Header, conn net.Conn) {
msg := PackBillingModelRequestMessage(hex, header)
log.WithFields(log.Fields{
"id": msg.Id,
}).Debug("[09] BillingModelRequest message")

//forward
if opt.MessageForwarder != nil {
//convert msg to json string bytes
b, _ := json.Marshal(msg)
_ = opt.MessageForwarder.Publish("09", b)
}
}

func BillingModelResponseMessageRouter(c *gin.Context) {
var req BillingModelResponseMessage
if c.ShouldBind(&req) == nil {
err := SendBillingModelResponseMessage(&req)
if err != nil {
c.JSON(500, gin.H{"message": err})
return
}
}
c.JSON(200, gin.H{"message": "done"})
}

func BillingModelVerificationResponseRouter(c *gin.Context) {
var req BillingModelVerificationResponseMessage
if c.ShouldBind(&req) == nil {
Expand Down

0 comments on commit 4e36d3e

Please sign in to comment.