| | |
| | | plc-recorder.exe |
| | | plc-recorder.exe* |
| | | log |
| | |
| | | ) |
| | | |
| | | func getDeviceList() ([]msg.PLCDevice, error) { |
| | | responseBody, err := util.HttpPost(config.Options.DeviceListWebApi, nil) |
| | | token, _ := util.CreateToken() |
| | | responseBody, err := util.HttpPost(config.Options.DeviceListWebApi, nil, token) |
| | | if err != nil { |
| | | logger.Warn("get device list from aps error:%s", err.Error()) |
| | | return nil, err |
| | |
| | | { |
| | | "nsq_server": "fai365.com:4150", |
| | | "parent_id": "guangsheng", |
| | | "jwt_key": "abc", |
| | | "nsq_server": "fai365.com:4150", |
| | | "pub_plc_data_topic": "aps.guangsheng.plc.livedata", |
| | | "write_plc_data_topic": "aps.guangsheng.plc.write", |
| | | "sub_device_topic": "aps.guangsheng.plc.device", |
| | |
| | | ) |
| | | |
| | | type Config struct { |
| | | ParentId string `json:"parent_id"` // 主账号id, 用于请求web接口 |
| | | JWTKey string `json:"jwt_key"` // 生成jwt的key |
| | | NsqServer string `json:"nsq_server"` // nsq TCP服务端地址 |
| | | PubPLCDataTopic string `json:"pub_plc_data_topic"` // 发布plc数据的topic |
| | | WritePLCDataTopic string `json:"write_plc_data_topic"` // 接收plc配置数据的topic |
| | |
| | | var Options Config |
| | | |
| | | func DefaultConfig() { |
| | | Options.ParentId = "guangsheng" |
| | | Options.JWTKey = "abcdefghijklmn" |
| | | Options.NsqServer = "fai365.com:4150" |
| | | Options.PubPLCDataTopic = "aps.factory.plc.livedata" |
| | | Options.WritePLCDataTopic = "aps.factory.plc.write" |
| | |
| | | require ( |
| | | github.com/apache/plc4x/plc4go v0.0.0-20230731062314-d0b9a9d01b04 |
| | | github.com/fatedier/beego v1.7.2 |
| | | github.com/golang-jwt/jwt/v4 v4.5.0 |
| | | github.com/nsqio/go-nsq v1.1.0 |
| | | github.com/rs/zerolog v1.30.0 |
| | | ) |
| | |
| | | github.com/fatedier/beego v1.7.2 h1:kVw3oKiXccInqG+Z/7l8zyRQXrsCQEfcUxgzfGK+R8g= |
| | | github.com/fatedier/beego v1.7.2/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk= |
| | | github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= |
| | | github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= |
| | | github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= |
| | | github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= |
| | | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= |
| | | github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= |
| | |
| | | |
| | | // aps 发布 |
| | | if config.Options.PostPLCDataWebApi != "" { |
| | | _, err := util.HttpPost(config.Options.PostPLCDataWebApi, b) |
| | | token, _ := util.CreateToken() |
| | | _, err := util.HttpPost(config.Options.PostPLCDataWebApi, b, token) |
| | | if err != nil { |
| | | logger.Warn(err.Error()) |
| | | } |
| | |
| | | "net/http" |
| | | ) |
| | | |
| | | func HttpPost(uri string, param []byte) ([]byte, error) { |
| | | func HttpPost(uri string, param []byte, jwtToken string) ([]byte, error) { |
| | | request, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(param)) |
| | | if err != nil { |
| | | return nil, err |
| | |
| | | |
| | | request.Header.Set("Content-Type", "application/json;charset=UTF-8") |
| | | |
| | | if jwtToken != "" { |
| | | request.Header.Set("Authorization", "Bearer "+jwtToken) |
| | | } |
| | | |
| | | response, err := http.DefaultClient.Do(request) |
| | | if err != nil { |
| | | return nil, err |
New file |
| | |
| | | package util |
| | | |
| | | import ( |
| | | "plc-recorder/config" |
| | | "time" |
| | | |
| | | "github.com/golang-jwt/jwt/v4" |
| | | ) |
| | | |
| | | type CustomClaims struct { |
| | | BaseClaims |
| | | jwt.StandardClaims |
| | | } |
| | | |
| | | type BaseClaims struct { |
| | | UserId string |
| | | Username string |
| | | ParentId string |
| | | } |
| | | |
| | | func CreateToken() (string, error) { |
| | | baseClaims := BaseClaims{ |
| | | UserId: "plc-recorder", |
| | | Username: "plc-recorder", |
| | | ParentId: config.Options.ParentId, |
| | | } |
| | | claims := CustomClaims{ |
| | | BaseClaims: baseClaims, |
| | | StandardClaims: jwt.StandardClaims{ |
| | | NotBefore: time.Now().Unix() - 1000, // 签名生效时间 |
| | | ExpiresAt: time.Now().Add(1 * time.Minute).Unix(), // 过期时间 |
| | | Issuer: "plc-recorder", // 签名的发行者 |
| | | }, |
| | | } |
| | | |
| | | token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| | | |
| | | signKey := []byte(config.Options.JWTKey) |
| | | |
| | | return token.SignedString(signKey) |
| | | } |