Need example usage of GetState (websocket)

I’ve had success in using socket.io to manipulate things like volume and track, but I am stumbling on GetState. Below is a snip of what I am trying:

this.socket.emit('GetState', '', function(data) {
				var obj = JSON.parse(data);
				if (obj.status == 'play')
				{
					this.socket.emit('pause');
				} else
				{
					this.socket.emit('play');
				}
			});

Neither play nor pause is changed in Volumio.

I also tried as this.socket.emit(‘GetState’, function(data) but same result.

First of all, it’s getState, not GetState. Maybe this can get you started:

var io = require('socket.io-client'); var socket = io.connect('http://localhost:3000'); socket.emit('getState', '') socket.on('pushState', function(data) { console.log(data.status); } );

1 Like

Would you mind explaining what each line does? I’m extraordinarily new to APIs and am also looking for any learning resources you might be able to recommend.
I assume that any line preceded by ‘var’ sets a variable. emit (from the api documentation page) seems to be how one communicates requests to volumio, the only real line I dont follow I suppose is the last one.

Please see here:
volumio.github.io/docs/API/WebSocket_APIs.html

Basically, emit is a message you send.
On, means that when you receive a particular message, you will do something.

socket.emit('getState', '')

Hey Volumio, tell me the state!

socket.on('pushState', function(data) { console.log(data.status); } );
The state has changed (or Volumio replied to the aforementioned emit) and you will print to console the status parameter of state (play, pause etc). If you want to print the artist, you will do

console.log(data.artist);

Awesome! Thank you for your help and prompt responses!

I am trying to do a similar thing in python for rotary encoders.

When I press the button of one I want it to pause/play depending on the current state.

From this page (gist.github.com/ivesdebruycker/ … c1d28e9b9e) I got some idea of what is going on and borrowed this snippet.

[code]def on_push_state(*args):
# print(‘state’, args)
global status
status = args[0][‘status’].encode(‘ascii’, ‘ignore’)
print status

socketIO.on(‘pushState’, on_push_state)

get initial state

socketIO.emit(‘getState’, ‘’, on_push_state)[/code]

my problem appears to be the status is not being updated.

I have kludged together bits form all over the place and it is looking a little messy, but I will attach what I have right now, as embarrassing as it will be.

In full:

#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# This class uses a standard rotary encoder with push switch
#
import subprocess
import sys
import time
from rotary_class import RotaryEncoder
from socketIO_client import SocketIO, LoggingNamespace

# Define the IO elements
socketIO = SocketIO('localhost', 3000)
status = 'unknown'


# Switch definitions
LEFT_A = 17
LEFT_B = 27
LEFT_BUTTON = 10
RIGHT_A = 23
RIGHT_B = 24 
RIGHT_BUTTON = 25

# This is the event callback routine to handle left knob events
def left_knob_event(event):
	handle_event(event,"Left")	
	return

# This is the event callback routine to handle right knob events
def right_knob_event(event):
	handle_event(event,"Right")	
	return
	
def on_push_state(*args):
		# print('state', args)
		global status
		status = args[0]['status'].encode('ascii', 'ignore')
		print status

socketIO.on('pushState', on_push_state)

# get initial state
socketIO.emit('getState', '', on_push_state)


# This is the event callback routine to handle events
def handle_event(event, name):
	if event == RotaryEncoder.CLOCKWISE:
		if name == "Right":
			socketIO.emit('volume', '+');
		else :
			socketIO.emit('seek',5);
		print name, "Clockwise event =", RotaryEncoder.CLOCKWISE
	elif event == RotaryEncoder.ANTICLOCKWISE:
		if name == "Right":
			socketIO.emit('volume', '-');
		else:
			socketIO.emit('seek',-5);
		print name, "Anticlockwise event =", RotaryEncoder.BUTTONDOWN
	elif event == RotaryEncoder.BUTTONDOWN:
		if name == "Right":
			print status
			if status == 'play':
				socketIO.emit('pause');
			else:
				socketIO.emit('play');
		else:
			socketIO.emit('next');
		print name, "Button down event =", RotaryEncoder.BUTTONDOWN
	#elif event == RotaryEncoder.BUTTONUP:
	#	print name, "Button up event =", RotaryEncoder.BUTTONUP
	return

# Define the left and right knobs
leftknob = RotaryEncoder(LEFT_A,LEFT_B,LEFT_BUTTON,left_knob_event)
rightknob = RotaryEncoder(RIGHT_A,RIGHT_B,RIGHT_BUTTON,right_knob_event)

# Wait for events
while True:
	time.sleep(0.5)

# End of program

Additional:

running this from cmdline returns a result.

curl volumio.local:3000/api/v1/getState

Whereas in py the getstate appears to be returning “None” when I tried this

print socketIO.emit(‘getState’)
None

I am wondering if I shouldn’t be using socketIO.emit, but something else.

Another Update:

This time I tried adding this [code]
socketIO.on(‘pushState’, on_push_state)

get initial state

socketIO.emit(‘getState’, ‘’, on_push_state)

try:
socketIO.wait(0.1)
except KeyboardInterrupt:
pass
[/code]

Result in:
print status
pause

I can wait 0.1 seconds.

More testing to follow…