Updated to get bulb status

Updated to get bulb status and clear up a pesky golint shadow error
This commit is contained in:
Dan 2016-12-04 20:51:47 +11:00
parent 377ea34792
commit 48c34427c0
7 changed files with 107 additions and 3 deletions

View File

@ -48,3 +48,29 @@ capabilities
30008 Sleep
30009 Level Move (https://gist.github.com/hardillb/ec88e86597d65584b1ba)
3000A ?
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetDeviceStatusResponse xmlns:u="urn:Belkin:service:bridge:1">
<DeviceStatusList>
<?xml version="1.0" encoding="utf-8"?>
<DeviceStatusList>
<DeviceStatus>
<IsGroupAction>NO</IsGroupAction>
<DeviceID available="YES">94103EF6BF42867F</DeviceID>
<CapabilityID>10006,10008,30008,30009,3000A</CapabilityID>
<CapabilityValue>1,200:0,0:1480683978,,</CapabilityValue>
<LastEventTimeStamp>0</LastEventTimeStamp>
</DeviceStatus>
<DeviceStatus>
<IsGroupAction>NO</IsGroupAction>
<DeviceID available="YES">94103EF6BF42CFA6</DeviceID>
<CapabilityID>10006,10008,30008,30009,3000A</CapabilityID>
<CapabilityValue>1,255:0,0:1480683978,,</CapabilityValue>
<LastEventTimeStamp>0</LastEventTimeStamp>
</DeviceStatus>
</DeviceStatusList>
</DeviceStatusList>
</u:GetDeviceStatusResponse>
</s:Body>
</s:Envelope>

View File

@ -275,7 +275,6 @@ func (d *Device) GetBridgeEndDevices(uuid string) *EndDevices {
d.printf("Unmarshal Error: %s\n", err)
}
fmt.Println(resp)
return &resp
}
@ -324,3 +323,49 @@ func (d *Device) Bulb(id, cmd, value string, group bool) error {
}
return nil
}
//BulbStatusList ...
type BulbStatusList struct {
DeviceStatus []DeviceStatus `xml:"Body>GetDeviceStatusResponse>DeviceStatusList>DeviceStatusList>DeviceStatus"`
}
//DeviceStatus ...
type DeviceStatus struct {
DeviceID string `xml:"DeviceID"`
CapabilityValue string `xml:"CapabilityValue"`
}
//GetBulbStatus return map of [DeviceID]status values
func (d *Device) GetBulbStatus(ids string) (map[string]string, error) {
result := make(map[string]string)
message := newGetBulbStatus(ids)
response, err := post(d.Host, "bridge", "GetDeviceStatus", message)
if err != nil {
return nil, fmt.Errorf("unable to fetch Bulb status => %s\n", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GetBulbStatus returned status code => %d\n", response.StatusCode)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("unable to read data => %s\n", err)
}
data = []byte(html.UnescapeString(string(data)))
statusInfo := BulbStatusList{}
err = xml.Unmarshal(data, &statusInfo)
if err != nil {
return nil, fmt.Errorf("Unmarshal Error: %s\n", err)
}
for k := range statusInfo.DeviceStatus {
result[statusInfo.DeviceStatus[k].DeviceID] = statusInfo.DeviceStatus[k].CapabilityValue
}
return result, nil
}

View File

@ -81,5 +81,11 @@ func newSetBulbStatus(id, capability, value string, group bool) string {
</DeviceStatusList>
</u:SetDeviceStatus>`+
messageFooter, g, id, capability, value)
}
func newGetBulbStatus(id string) string {
return fmt.Sprintf(messageHeader+`<u:GetDeviceStatus xmlns:u="urn:Belkin:service:bridge:1">
<DeviceIDs>%s</DeviceIDs>
</u:GetDeviceStatus>`+messageFooter, id)
}

View File

@ -118,7 +118,8 @@ func (d *Device) ManageSubscription(listenerAddress string, timeout int, subscri
d.UnSubscribe(id)
// Setup a new subscription, if this fails, next attempt will be when timer triggers again
newID, err := d.Subscribe(listenerAddress, timeout)
var newID string
newID, err = d.Subscribe(listenerAddress, timeout)
if err != 200 {
log.Println("Error with subscription attempt: ", err)
} else {

View File

@ -1,5 +1,5 @@
// Package wemo ...
// Copyright 2014 Matt Ho
/* Copyright 2014 Matt Ho
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -12,6 +12,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
package wemo
import (

View File

@ -32,6 +32,7 @@ func main() {
offCommand,
toggleCommand,
bulbCommand,
bulbStatusCommand,
}
app.Run(os.Args)
}

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"github.com/codegangsta/cli"
@ -90,4 +91,27 @@ func bulbAction(c *cli.Context) {
if err != nil {
log.Println(err)
}
}
var bulbStatusCommand = cli.Command{
Name: "bulbStatus",
Usage: "Status of a bulb!",
Description: "bulb --host 192.168.1.25:49153 --id 94103EF6BF42867F",
Flags: []cli.Flag{
cli.StringFlag{Name: "host", Value: "192.168.1.25:49153", Usage: "device host and ip e.g. 10.0.1.2:49128"}, //, ""},
cli.StringFlag{Name: "id", Value: "", Usage: "device id"},
},
Action: bulbStatusAction,
}
func bulbStatusAction(c *cli.Context) {
host := c.String("host")
id := c.String("id")
device := &wemo.Device{
Host: host,
}
fmt.Println(device.GetBulbStatus(id))
}