-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_charging_pile.go
137 lines (112 loc) · 4.89 KB
/
protocol_charging_pile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package charging_pile_das
import (
"github.com/giskook/charging_pile_das/conn"
"github.com/giskook/charging_pile_das/pkg"
"github.com/giskook/charging_pile_das/protocol"
"github.com/giskook/gotcp"
"log"
"sync"
)
type Charging_Pile_Protocol struct {
}
func (this *Charging_Pile_Protocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
smconn := c.GetExtraData().(*conn.Conn)
var once sync.Once
once.Do(smconn.UpdateReadflag)
buffer := smconn.GetBuffer()
tcp_conn := c.GetRawConn()
for {
if smconn.ReadMore {
data := make([]byte, 2048)
readLengh, err := tcp_conn.Read(data)
log.Printf("<IN> %x %x\n", smconn, data[0:readLengh])
if err != nil {
return nil, err
}
if readLengh == 0 {
return nil, gotcp.ErrConnClosing
}
buffer.Write(data[0:readLengh])
}
cmdid, pkglen := protocol.CheckProtocol(buffer)
log.Printf("protocol id %d\n", cmdid)
if smconn.Status == conn.ConnUnauth {
if cmdid != protocol.PROTOCOL_REQ_LOGIN &&
cmdid != protocol.PROTOCOL_HALF_PACK &&
cmdid != protocol.PROTOCOL_ILLEGAL {
cmdid = protocol.PROTOCOL_SWALLOW
}
}
pkgbyte := make([]byte, pkglen)
buffer.Read(pkgbyte)
switch cmdid {
case protocol.PROTOCOL_REQ_LOGIN:
p := protocol.ParseLogin(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_LOGIN, p), nil
case protocol.PROTOCOL_REQ_HEART:
p := protocol.ParseHeart(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_HEART, p), nil
case protocol.PROTOCOL_REQ_SETTING:
p := protocol.ParseSetting(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_SETTING, p), nil
case protocol.PROTOCOL_REQ_PRICE:
p := protocol.ParsePrice(pkgbyte, smconn.Charging_Pile.Station_ID)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_PRICE, p), nil
case protocol.PROTOCOL_REQ_TIME:
p := protocol.ParseTime(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_TIME, p), nil
case protocol.PROTOCOL_REP_STOP_CHARGING:
p := protocol.ParseStopCharging(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_STOP_CHARGING, p), nil
case protocol.PROTOCOL_REP_NSQ_NOTIFY_SET_PRICE:
p := protocol.ParseNotifySetPrice(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_NSQ_NOTIFY_SET_PRICE, p), nil
case protocol.PROTOCOL_REP_CHARGING_STARTED:
p := protocol.ParseChargingStarted(pkgbyte, smconn.Charging_Pile.Station_ID, smconn.Charging_Pile.DB_ID)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_CHARGING_STARTED, p), nil
case protocol.PROTOCOL_REP_CHARGING_DATA_UPLOAD:
log.Println(smconn.Charging_Pile.TransactionID)
p := protocol.ParseChargingUpload(pkgbyte, smconn.Charging_Pile.Station_ID, smconn.Charging_Pile.DB_ID, smconn.Charging_Pile.TransactionID, smconn.Charging_Pile.StartTime, smconn.Charging_Pile.StartMeterReading)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_CHARGING_DATA_UPLOAD, p), nil
case protocol.PROTOCOL_REQ_THREE_PHASE_MODE:
p := protocol.ParseThreePhaseMode(pkgbyte, smconn.Charging_Pile.PhaseMode, smconn.Charging_Pile.AuthMode, smconn.Charging_Pile.LockMode)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REQ_THREE_PHASE_MODE, p), nil
case protocol.PROTOCOL_REP_PIN:
p := protocol.ParseRepPin(pkgbyte, smconn.Charging_Pile.Station_ID, smconn.Charging_Pile.DB_ID)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_PIN, p), nil
case protocol.PROTOCOL_REP_GUN_STATUS:
p := protocol.ParseRepGunStatus(pkgbyte)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_GUN_STATUS, p), nil
case protocol.PROTOCOL_REP_CHARGING:
p := protocol.ParseRepCharging(pkgbyte, smconn.Charging_Pile.TransactionID)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_CHARGING, p), nil
case protocol.PROTOCOL_REP_CHARGING_STOPPED:
p := protocol.ParseChargingStopped(pkgbyte, smconn.Charging_Pile.Station_ID, smconn.Charging_Pile.DB_ID, smconn.Charging_Pile.StartTime, smconn.Charging_Pile.StartMeterReading)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_CHARGING_STOPPED, p), nil
case protocol.PROTOCOL_REP_UPLOAD_OFFLINE_PACAKGE:
p := protocol.ParseUploadOfflineData(pkgbyte, smconn.Charging_Pile.Station_ID, smconn.Charging_Pile.DB_ID)
smconn.ReadMore = false
return pkg.New_Charging_Pile_Packet(protocol.PROTOCOL_REP_UPLOAD_OFFLINE_PACAKGE, p), nil
case protocol.PROTOCOL_ILLEGAL:
smconn.ReadMore = true
case protocol.PROTOCOL_HALF_PACK:
smconn.ReadMore = true
case protocol.PROTOCOL_SWALLOW:
smconn.ReadMore = false
}
}
}