目录
一些主要配置项
(陆续补充中 。。。)
lora-app-server
# Valid values for sslmode are:
#
# * disable - No SSL
# * require - Always SSL (skip verification)
# * verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
# * verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
dsn="postgres://dbuser:123456@localhost/loraserver_as?sslmode=disable"
dsn 填写 postgres 信息, 用户名:密码@地址
如下是对外的 api 端口,这里必须用ssl,具体的生成参考
《loraserver 源码解析 (六) lora-app-server》
# Public ip:port of the application-server API.
#
# This is used by LoRa Server to connect to LoRa App Server. When running
# LoRa App Server on a different host than LoRa Server, make sure to set
# this to the host:ip on which LoRa Server can reach LoRa App Server.
# The port must be equal to the port configured by the 'bind' flag
# above.
public_host="localhost:8001"
# Settings for the "external api"
#
# This is the API and web-interface exposed to the end-user.
[application_server.external_api]
# ip:port to bind the (user facing) http server to (web-interface and REST / gRPC api)
bind="0.0.0.0:8080"
# http server TLS certificate
tls_cert="server.crt"
# http server TLS key
tls_key="server.key"
# JWT secret used for api authentication / authorization
# You could generate this by executing 'openssl rand -base64 32' for example
jwt_secret="DmQsCIf3FidmBjo6/IXD4YIwwXSzLS//AFKqChmgju4="
# when set, existing users can't be re-assigned (to avoid exposure of all users to an organization admin)"
disable_assign_existing_users=false
loraserver
dsn="postgres://dbuser:123456@localhost/loraserver_ns?sslmode=disable"
dsn 和 lora-app-server 类似, 注意这里 loraserver_ns 和 lora-app-server 的 loraserver_as 仅一字之差
# Time to wait for uplink de-duplication.
#
# This is the time that LoRa Server will wait for other gateways to receive
# the same uplink frame. Valid units are 'ms' or 's'.
# Please note that this value has influence on the uplink / downlink
# roundtrip time. Setting this value too high means LoRa Server will be
# unable to respond to the device within its receive-window.
deduplication_delay="200ms"
参考 《loraserver 源码解析 (五) loraserver》 的 startLoRaServer 相关描述,设备的报文广播后,可能有多个网关都收到了,它们都会给loraserver发送报文,于是loraserver就收到了多份报文,collectAndCallOnce()函数负责去除这些冗余的报文,并选择信号最强的网关进行回复。deduplication_delay配置去除冗余的等待时间。这个时间内冗余的报文会被干掉。
# Device session expiration.
#
# The TTL value defines the time after which a device-session expires
# after no activity. Valid units are 'ms', 's', 'm', 'h'. Note that these
# values can be combined, e.g. '24h30m15s'.
device_session_ttl="744h0m0s"
设备激活后的 device-session 的存活时间
# Get downlink data delay.
#
# This is the time that LoRa Server waits between forwarding data to the
# application-server and reading data from the queue. A higher value
# means that the application-server has more time to schedule a downlink
# queue item which can be processed within the same uplink / downlink
# transaction.
# Please note that this value has influence on the uplink / downlink
# roundtrip time. Setting this value too high means LoRa Server will be
# unable to respond to the device within its receive-window.
get_downlink_data_delay="100ms"
命令回复等待时间,第三方等待时间。 loraserver收到命令后,会等待这个时间,如果这个时间内第三方程序通过rpc或mqtt或http把要下发给设备的命令送过来了,那么loraserver就会把这个命令加密后发送给设备。否则如果是a类型设备就等到设备下一个请求报文时再捎带上回复给设备。 如果是b 或 c类型设备,则有专门的routine,不断读取这些命令,发给设备。
# LoRaWAN regional band configuration.
#
# Note that you might want to consult the LoRaWAN Regional Parameters
# specification for valid values that apply to your region.
# See: https://www.lora-alliance.org/lorawan-for-developers
[network_server.band]
# LoRaWAN band to use.
#
# Valid values are:
# * AS_923
# * AU_915_928
# * CN_470_510
# * CN_779_787
# * EU_433
# * EU_863_870
# * IN_865_867
# * KR_920_923
# * RU_864_870
# * US_902_928
name="CN_470_510"
用哪个频率信道,我们应该是CN带头的那两个中选一个吧
原理
loraserver 由 cobra 完成 配置
为了方便理解 loraserver 的配置
我这里利用 cobra 生成一个简单的配置程序
cobra init cfgdemo
假定我写了下面的配置文件。 toml格式的文件是cobra原生支持的
[GENERAL] #大小写忽略
log_level = 42
[redis]
url="redis://localhost:6379"
# 空格 被忽略 不参与解析
[network_server]
net_id="000000"
# 多层嵌套
[network_server.band]
name="EU_863_870"
[network_server.api]
bind="i am bind"
我只要依照上面配置文件的结构定义一个结构体,就可以借助 viper 解析出来
~/go/gopath/src/cfgdemo $ cat config/config.go
package config
type Config struct {
General struct {
LogLevel int `mapstructure:"log_level"`
}
Redis struct {
URL string `mapstructure:"url"`
}
NetworkServer struct {
NetID string `mapstructure:"net_id"`
Band struct {
Name string
}
Api struct {
Bind string
}
} `mapstructure:"network_server"`
}
var C Config
NetworkServer 用于解析 network_server
需要加上
`mapstructure:"network_server"`
func initConfig() {
if cfgFile != "" {
b, err := ioutil.ReadFile(cfgFile)
if err != nil {
log.WithError(err).WithField("config", cfgFile).Fatal("error loading config file")
}
viper.SetConfigType("toml")
if err := viper.ReadConfig(bytes.NewBuffer(b)); err != nil {
log.WithError(err).WithField("config", cfgFile).Fatal("error loading config file")
}
} else {
viper.SetConfigName("cfgdemo")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Warning("No configuration file found, using defaults.")
default:
log.WithError(err).Fatal("read configuration file error")
}
}
}
// Unmarshal 一下就可以把 cfgdemo.toml 的配置转换成config.C结构体变量 很方便啊~~~~
if err := viper.Unmarshal(&config.C); err != nil {
log.WithError(err).Fatal("unmarshal config error")
}
log.Print(config.C)
}
可以在 init函数里 指定 初值
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cfgdemo.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
viper.SetDefault("General.log_level", 2)
viper.SetDefault("network_server.net_id", "000000")
viper.SetDefault("network_server.band.name", "EU_863_870")
viper.SetDefault("network_server.api.bind", "0.0.0.0:8000")
}
~/go/gopath/src/cfgdemo $ go run main.go
INFO[0000] {{42} {redis://localhost:6379} {000000 {EU_863_870} {i am bind}}}
loraserver 的配置解析差不多就是这个样子,只是它的结构体复杂一些