源码地址
loraserver lorawan netserver,和 gateway通信 与app server交互
lorawan 它用于lorawan通信数据的序列化
安装golang
下载源码
go get https://github.com/brocaar/loraserver
go get https://github.com/brocaar/lorawan
由于我的 $GOPATH 是 ~/go/gopath
所以它们最终下载到了 ~/go/gopath/src/github.com/loraserver 和 ~/go/gopath/src/github.com/lorawan
安装依赖库
loraserver lorawan用到了大量的第三方库
loraserver 目录下的 Makefile的 requirements 罗列了所有需要安装的第三方库(好像还是不全哦)
requirements:
@go get -u github.com/kisielk/errcheck
@go get -u github.com/golang/lint/golint
@go get -u github.com/kardianos/govendor
@go get -u github.com/smartystreets/goconvey
@go get -u golang.org/x/tools/cmd/stringer
@go get -u github.com/golang/protobuf/proto
@go get -u github.com/golang/protobuf/protoc-gen-go
@go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
@go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
@go get -u github.com/elazarl/go-bindata-assetfs/...
@go get -u github.com/jteeuwen/go-bindata/...
@go get -u github.com/golang/dep/cmd/dep
@go get -u github.com/goreleaser/goreleaser
@dep ensure -v
如果你可以爬墙,也许可以直接 make requirements 来安装,说不定就全部装上了。
我不能爬墙,所以 golang.org google.golang.org 等网站是无法通过go get 安装的,会提示类似下面的错误
"golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused
幸运的是 golang.org google.golang.org 依赖库 在 github 上都有
都在 https://github.com/golang 这里面
我们可以通过 git clone gitbub的代码来曲线安装,具体的操作方法请参考
go redis 客户端 这个 redigo 已经被移到 gomodule 仓库, 为了编译 loraserver,我们得下载 https://github.com/garyburd/redigo 的redigo。
redis 安装
下载 https://redis.io/download redis-4.0.10.tar
tar -xvf redis-4.0.10.tar.gz
make
make test
sudo make install
生成migrations
运行 go generate cmd/loraserver/main.go
会借助 go-bindata 生成 internal/migrations/migrations_gen.go
安装完所有的依赖后 dep ensure -v
在我电脑下显示如下,被墙了,会有2个失败的,忽略即可The following issues were found in Gopkg.toml:
✗ unable to deduce repository and source type for "google.golang.org/grpc": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://google.golang.org/grpc?go-get=1": Get http://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused
✗ unable to deduce repository and source type for "golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused
ProjectRoot name validation failed
装loraserver后,我多了这些第三方库
~/go/gopath/src/github.com $ ls
alecthomas fatih grpc-ecosystem lib rubenv
apex fsnotify hashicorp magiconair satori
aws garyburd imdario masterminds sirupsen
blakesmith ghodss jacobsa mattn smartystreets
brocaar golang jmoiron mitchellh spf13
caarlos0 gomodule jteeuwen nsf
campoy google jtolds pelletier
eclipse goreleaser kardianos pkg
elazarl grpc kisielk rogpeppe
~/go/gopath/src $ ls
github.com google.golang.org
golang.org gopkg.in
~/go/gopath/src/golang.org/x $ ls
crypto lint net sys text tools
~/go/gopath/src/google.golang.org $ ls
genproto grpc
~/go/gopath/src/gopkg.in $ ls
gorp.v1 yaml.v2
多了这么多程序
~/go/gopath/bin/linux_386 $ ls
cobra gocode protoc-gen-go
dep goconvey protoc-gen-grpc-gateway
errcheck godef protoc-gen-swagger
go-bindata golint stringer
go-bindata-assetfs govendor
尝试编译后会报个语法错误
loraserver 有4个地方用 uuid的时候 没有处理error返回值
dp.DeviceProfile.DeviceProfileID = uuid.NewV4().String()
github.com/satori/go.uuid 目录的 generator.go 改一下
/* raw
func NewV4() (UUID, error) {
return global.NewV4()
}
*/
func NewV4() (UUID) { // modify by wjs, fix loraserver build error
uuid,_ := global.NewV4()
return uuid
}
这样改不好,正确的做法我想应该是改 loraserver 源码,判断 error返回值。暂时这么改通过编译吧:)
make build (在loraserver目录)
会在 build 目录下生成 loraserver 程序。~/go/gopath/src/github.com/brocaar/loraserver $ make build
于是生成可执行文件
~/go/gopath/src/github.com/brocaar/loraserver/build $ ls
loraserver
loraserver 源码解析 (二) 运行起来