-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package device | ||
|
||
import ( | ||
"crypto/rsa" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/dehydr8/kasa-go/model" | ||
"github.com/dehydr8/kasa-go/protocol" | ||
) | ||
|
||
type Device struct { | ||
config *model.DeviceConfig | ||
transport protocol.Protocol | ||
} | ||
|
||
type DeviceInfoResult struct { | ||
DeviceId string `json:"device_id"` | ||
DeviceOn bool `json:"device_on"` | ||
Model string `json:"model"` | ||
Type string `json:"type"` | ||
Alias string `json:"nickname"` | ||
Rssi int `json:"rssi"` | ||
OnTime int `json:"on_time"` | ||
SoftwareVersion string `json:"sw_ver"` | ||
HardwareVersion string `json:"hw_ver"` | ||
MAC string `json:"mac"` | ||
Overheated bool `json:"overheated"` | ||
PowerProtectionStatus string `json:"power_protection_status"` | ||
OvercurrentStatus string `json:"overcurrent_status"` | ||
SignalLevel int `json:"signal_level"` | ||
SSID string `json:"ssid"` | ||
} | ||
|
||
type EnergyUsageResult struct { | ||
CurrentPower int `json:"current_power"` | ||
MonthEnergy int `json:"month_energy"` | ||
MonthRuntime int `json:"month_runtime"` | ||
TodayEnergy int `json:"today_energy"` | ||
TodayRuntime int `json:"today_runtime"` | ||
} | ||
|
||
type EnergyUsageResponse struct { | ||
protocol.AesProtoBaseResponse | ||
Result EnergyUsageResult `json:"result"` | ||
} | ||
|
||
type DeviceInfoResponse struct { | ||
protocol.AesProtoBaseResponse | ||
Result DeviceInfoResult `json:"result"` | ||
} | ||
|
||
func NewDevice(key *rsa.PrivateKey, config *model.DeviceConfig) (*Device, error) { | ||
transport, err := protocol.NewAesTransport(key, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Device{ | ||
config: config, | ||
transport: transport, | ||
}, nil | ||
} | ||
|
||
func (d *Device) Address() string { | ||
return d.config.Address | ||
} | ||
|
||
func (d *Device) GetEnergyUsage() (*EnergyUsageResult, error) { | ||
var response EnergyUsageResponse | ||
req := map[string]interface{}{ | ||
"method": "get_energy_usage", | ||
} | ||
|
||
err := d.transport.Send(&req, &response) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if response.ErrorCode != 0 { | ||
return nil, fmt.Errorf("error code: %d", response.ErrorCode) | ||
} | ||
|
||
return &response.Result, nil | ||
} | ||
|
||
func (d *Device) GetDeviceInfo() (*DeviceInfoResult, error) { | ||
var response DeviceInfoResponse | ||
req := map[string]interface{}{ | ||
"method": "get_device_info", | ||
} | ||
|
||
err := d.transport.Send(&req, &response) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if response.ErrorCode != 0 { | ||
return nil, fmt.Errorf("error code: %d", response.ErrorCode) | ||
} | ||
|
||
// try decoding nickname | ||
nickname, err := base64.StdEncoding.DecodeString(response.Result.Alias) | ||
|
||
if err == nil { | ||
response.Result.Alias = string(nickname) | ||
} | ||
|
||
// try decoding ssid | ||
ssid, err := base64.StdEncoding.DecodeString(response.Result.SSID) | ||
|
||
if err == nil { | ||
response.Result.SSID = string(ssid) | ||
} | ||
|
||
return &response.Result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
var ( | ||
GLOBAL_LOGGER log.Logger | ||
) | ||
|
||
func SetupLogging(lvl string) { | ||
GLOBAL_LOGGER = log.NewLogfmtLogger(os.Stderr) | ||
GLOBAL_LOGGER = level.NewFilter(GLOBAL_LOGGER, level.Allow(level.ParseDefault(lvl, level.InfoValue()))) | ||
GLOBAL_LOGGER = log.With(GLOBAL_LOGGER, "ts", log.TimestampFormat( | ||
func() time.Time { return time.Now() }, | ||
time.DateTime, | ||
), "caller", log.Caller(4)) | ||
} | ||
|
||
func Info(keyvals ...interface{}) error { | ||
return level.Info(GLOBAL_LOGGER).Log(keyvals...) | ||
} | ||
|
||
func Debug(keyvals ...interface{}) error { | ||
return level.Debug(GLOBAL_LOGGER).Log(keyvals...) | ||
} | ||
|
||
func Warn(keyvals ...interface{}) error { | ||
return level.Warn(GLOBAL_LOGGER).Log(keyvals...) | ||
} | ||
|
||
func Error(keyvals ...interface{}) error { | ||
return level.Error(GLOBAL_LOGGER).Log(keyvals...) | ||
} |
Oops, something went wrong.