status-go/api/connection.go

73 lines
1.6 KiB
Go
Raw Normal View History

package api
import (
"fmt"
)
2018-06-27 08:11:45 +00:00
// connectionState represents device connection state and type,
// as reported by mobile framework.
//
2018-02-16 02:53:18 +00:00
// Zero value represents default assumption about network (online and unknown type).
2018-06-27 08:11:45 +00:00
type connectionState struct {
Offline bool `json:"offline"`
2018-06-27 08:11:45 +00:00
Type connectionType `json:"type"`
Expensive bool `json:"expensive"`
}
2018-06-27 08:11:45 +00:00
// connectionType represents description of available
// connection types as reported by React Native (see
// https://facebook.github.io/react-native/docs/netinfo.html)
// We're interested mainly in 'wifi' and 'cellular', but
// other types are also may be used.
2018-06-27 08:11:45 +00:00
type connectionType byte
const (
offline = "offline"
wifi = "wifi"
cellular = "cellular"
unknown = "unknown"
2018-06-27 08:11:45 +00:00
none = "none"
)
2018-06-27 08:11:45 +00:00
// newConnectionType creates new connectionType from string.
func newConnectionType(s string) connectionType {
switch s {
case cellular:
2018-06-27 08:11:45 +00:00
return connectionCellular
case wifi:
2018-06-27 08:11:45 +00:00
return connectionWifi
}
2018-06-27 08:11:45 +00:00
return connectionUnknown
}
2018-02-09 20:55:05 +00:00
// ConnectionType constants
const (
2018-06-27 08:11:45 +00:00
connectionUnknown connectionType = iota
connectionCellular // cellular, LTE, 4G, 3G, EDGE, etc.
connectionWifi // WIFI or iOS simulator
)
2018-06-27 08:11:45 +00:00
// string formats ConnectionState for logs. Implements Stringer.
func (c connectionState) String() string {
if c.Offline {
return offline
}
var typ string
switch c.Type {
2018-06-27 08:11:45 +00:00
case connectionWifi:
typ = wifi
2018-06-27 08:11:45 +00:00
case connectionCellular:
typ = cellular
default:
typ = unknown
}
if c.Expensive {
return fmt.Sprintf("%s (expensive)", typ)
}
return typ
}