Skip to content

Commit

Permalink
grouped declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kaperys committed Aug 9, 2018
1 parent fa5f379 commit e0611da
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 76 deletions.
22 changes: 12 additions & 10 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import (
// MessageType is the type sent to and returned by the TV in the `type` field.
type MessageType string

// ErrorMessageType is returned by the TV when an error has occurred.
const ErrorMessageType MessageType = "error"
const (
// ErrorMessageType is returned by the TV when an error has occurred.
ErrorMessageType MessageType = "error"

// RegisterMessageType is sent to the TV in a registration request.
const RegisterMessageType MessageType = "register"
// RegisterMessageType is sent to the TV in a registration request.
RegisterMessageType MessageType = "register"

// RegisteredMessageType is returned by the TV in response to a registration request.
const RegisteredMessageType MessageType = "registered"
// RegisteredMessageType is returned by the TV in response to a registration request.
RegisteredMessageType MessageType = "registered"

// RequestMessageType is sent to the TV when issuing Commands.
const RequestMessageType MessageType = "request"
// RequestMessageType is sent to the TV when issuing Commands.
RequestMessageType MessageType = "request"

// ResponseMessageType is returned by the TV in response to a request.
const ResponseMessageType MessageType = "response"
// ResponseMessageType is returned by the TV in response to a request.
ResponseMessageType MessageType = "response"
)

// Message represents the JSON message format used in request and responses to
// and from the TV.
Expand Down
102 changes: 52 additions & 50 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,81 @@ import "github.com/mitchellh/mapstructure"
// Command is the type used by tv.Command to interact with the TV.
type Command string

// APIServiceListCommand lists the API services available on the TV.
const APIServiceListCommand Command = "ssap://api/getServiceList"
const (
// APIServiceListCommand lists the API services available on the TV.
APIServiceListCommand Command = "ssap://api/getServiceList"

// ApplicationManagerForegroundAppCommand returns information about the forgeground app.
const ApplicationManagerForegroundAppCommand Command = "ssap://com.webos.applicationManager/getForegroundAppInfo"
// ApplicationManagerForegroundAppCommand returns information about the forgeground app.
ApplicationManagerForegroundAppCommand Command = "ssap://com.webos.applicationManager/getForegroundAppInfo"

// AudioGetVolumeCommand returns information about the TV's configured audio output volume.
const AudioGetVolumeCommand Command = "ssap://audio/getVolume"
// AudioGetVolumeCommand returns information about the TV's configured audio output volume.
AudioGetVolumeCommand Command = "ssap://audio/getVolume"

// AudioSetVolumeCommand sets the TV's configured audio output volume.
const AudioSetVolumeCommand Command = "ssap://audio/setVolume"
// AudioSetVolumeCommand sets the TV's configured audio output volume.
AudioSetVolumeCommand Command = "ssap://audio/setVolume"

// AudioVolumeDownCommand decrements the TV's configured audio output volume.
const AudioVolumeDownCommand Command = "ssap://audio/volumeDown"
// AudioVolumeDownCommand decrements the TV's configured audio output volume.
AudioVolumeDownCommand Command = "ssap://audio/volumeDown"

// AudioVolumeStatusCommand returns information about the TV's configured audio output volume.
// Same as AudioGetVolumeCommand.
const AudioVolumeStatusCommand Command = "ssap://audio/getVolume"
// AudioVolumeStatusCommand returns information about the TV's configured audio output volume.
// Same as AudioGetVolumeCommand.
AudioVolumeStatusCommand Command = "ssap://audio/getVolume"

// AudioVolumeUpCommand increments the TV's configured audio output volume.
const AudioVolumeUpCommand Command = "ssap://audio/volumeUp"
// AudioVolumeUpCommand increments the TV's configured audio output volume.
AudioVolumeUpCommand Command = "ssap://audio/volumeUp"

// AudioVolumeSetMuteCommand sets/toggles muting the TV's configured audio output.
const AudioVolumeSetMuteCommand Command = "ssap://audio/setMute"
// AudioVolumeSetMuteCommand sets/toggles muting the TV's configured audio output.
AudioVolumeSetMuteCommand Command = "ssap://audio/setMute"

// MediaControlFastForwardCommand fast forwards the current media.
const MediaControlFastForwardCommand Command = "ssap://media.controls/fastForward"
// MediaControlFastForwardCommand fast forwards the current media.
MediaControlFastForwardCommand Command = "ssap://media.controls/fastForward"

// MediaControlPauseCommand pauses the current media.
const MediaControlPauseCommand Command = "ssap://media.controls/pause"
// MediaControlPauseCommand pauses the current media.
MediaControlPauseCommand Command = "ssap://media.controls/pause"

// MediaControlPlayCommand plays or resumes the current media.
const MediaControlPlayCommand Command = "ssap://media.controls/play"
// MediaControlPlayCommand plays or resumes the current media.
MediaControlPlayCommand Command = "ssap://media.controls/play"

// MediaControlRewindCommand rewinds the current media.
const MediaControlRewindCommand Command = "ssap://media.controls/rewind"
// MediaControlRewindCommand rewinds the current media.
MediaControlRewindCommand Command = "ssap://media.controls/rewind"

// MediaControlStopCommand stops the current media.
const MediaControlStopCommand Command = "ssap://media.controls/stop"
// MediaControlStopCommand stops the current media.
MediaControlStopCommand Command = "ssap://media.controls/stop"

// SystemLauncherCloseCommand closes a given application.
const SystemLauncherCloseCommand Command = "ssap://system.launcher/close"
// SystemLauncherCloseCommand closes a given application.
SystemLauncherCloseCommand Command = "ssap://system.launcher/close"

// SystemLauncherGetAppStateCommand returns information about the given application state.
const SystemLauncherGetAppStateCommand Command = "ssap://system.launcher/getAppState"
// SystemLauncherGetAppStateCommand returns information about the given application state.
SystemLauncherGetAppStateCommand Command = "ssap://system.launcher/getAppState"

// SystemLauncherLaunchCommand launches the given application.
const SystemLauncherLaunchCommand Command = "ssap://system.launcher/launch"
// SystemLauncherLaunchCommand launches the given application.
SystemLauncherLaunchCommand Command = "ssap://system.launcher/launch"

// SystemLauncherOpenCommand opens a previously launched application.
const SystemLauncherOpenCommand Command = "ssap://system.launcher/open"
// SystemLauncherOpenCommand opens a previously launched application.
SystemLauncherOpenCommand Command = "ssap://system.launcher/open"

// SystemNotificationsCreateToastCommand creates a "toast" notification.
const SystemNotificationsCreateToastCommand Command = "ssap://system.notifications/createToast"
// SystemNotificationsCreateToastCommand creates a "toast" notification.
SystemNotificationsCreateToastCommand Command = "ssap://system.notifications/createToast"

// SystemTurnOffCommand turns the TV off.
const SystemTurnOffCommand Command = "ssap://system/turnOff"
// SystemTurnOffCommand turns the TV off.
SystemTurnOffCommand Command = "ssap://system/turnOff"

// TVChannelDownCommand changes the channel down.
const TVChannelDownCommand Command = "ssap://tv/channelDown"
// TVChannelDownCommand changes the channel down.
TVChannelDownCommand Command = "ssap://tv/channelDown"

// TVChannelListCommand returns information about the available channels.
const TVChannelListCommand Command = "ssap://tv/getChannelList"
// TVChannelListCommand returns information about the available channels.
TVChannelListCommand Command = "ssap://tv/getChannelList"

// TVChannelUpCommand changes the channel up.
const TVChannelUpCommand Command = "ssap://tv/channelUp"
// TVChannelUpCommand changes the channel up.
TVChannelUpCommand Command = "ssap://tv/channelUp"

// TVCurrentChannelCommand returns information about the current channel.
const TVCurrentChannelCommand Command = "ssap://tv/getCurrentChannel"
// TVCurrentChannelCommand returns information about the current channel.
TVCurrentChannelCommand Command = "ssap://tv/getCurrentChannel"

// TVCurrentChannelProgramCommand returns information about the current program playing on
// the current channel.
const TVCurrentChannelProgramCommand Command = "ssap://tv/getChannelProgramInfo"
// TVCurrentChannelProgramCommand returns information about the current program playing on
// the current channel.
TVCurrentChannelProgramCommand Command = "ssap://tv/getChannelProgramInfo"
)

// ServiceList returns information about the available services.
func (tv *TV) ServiceList() (*ServiceList, error) {
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 23 additions & 2 deletions tv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package webos
import (
"encoding/json"
"fmt"
"math/rand"
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/pkg/errors"
)

var (
// Protocol is the protocol used to connect to the TV.
Protocol = "wss"

// Port is the port used to connect to the TV.
Port = 3001
)

// TV represents the TV. It contains the websocket connection, necessary channels
// used for communication and methods used for interaction with the TV.
type TV struct {
Expand All @@ -21,8 +30,9 @@ type TV struct {
}

// NewTV dials the socket and returns a pointer to a new TV.
func NewTV(dialer *websocket.Dialer, addr string) (*TV, error) {
ws, resp, err := dialer.Dial(fmt.Sprintf("wss://%s:3001", addr), nil)
func NewTV(dialer *websocket.Dialer, ip string) (*TV, error) {
addr := fmt.Sprintf("%s://%s:%d", Protocol, ip, Port)
ws, resp, err := dialer.Dial(addr, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -199,3 +209,14 @@ func (tv *TV) teardownResponseChannel(id string) {
delete(tv.res, id)
}
}

// requestID returns a random 8 character string. Requests and Responses sent to and from
// the TV are linked by this ID.
func requestID() string {
rs := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, 8)
for i := range b {
b[i] = rs[rand.Intn(len(rs))]
}
return string(b)
}
14 changes: 0 additions & 14 deletions util.go

This file was deleted.

0 comments on commit e0611da

Please sign in to comment.