1

I want to port this Node.js script to control a Sky box over into Python, https://github.com/dalhundal/sky-remote/blob/master/sky-remote.js

I've gone through and done the best that I can do, code is below;

import time, math, socket, struct, time
from array import array
 #sky q port 5900
 class remote:
 commands={"power": 0, "select": 1, "backup": 2, "dismiss": 2, "channelup": 6, "channeldown": 7, "interactive": 8, "sidebar": 8, "help": 9, "services": 10, "search": 10, "tvguide": 11, "home": 11, "i": 14, "text": 15, "up": 16, "down": 17, "left": 18, "right": 19, "red": 32, "green": 33, "yellow": 34, "blue": 35, 0: 48, 1: 49, 2: 50, 3: 51, 4: 52, 5: 53, 6: 54, 7: 55, 8: 56, 9: 57, "play": 64, "pause": 65, "stop": 66, "record": 67, "fastforward": 69, "rewind": 71, "boxoffice": 240, "sky": 241}
 connectTimeout = 1000;
 def __init__(self, ip, port=49160):
 self.ip=ip
 self.port=port
 def showCommands(self):
 for command, value in self.commands.iteritems():
 print str(command)+ " : "+str(value) 
 def getCommand(self, code):
 try:
 return self.commands[code]
 except:
 print "Error: command '"+code+"' is not valid"
 return False
 def press (self, sequence):
 if isinstance(sequence, list):
 for item in sequence:
 toSend=self.getCommand(item)
 if toSend:
 self.sendCommand(toSend)
 time.sleep(0.5)
 else:
 toSend=self.getCommand(sequence)
 if toSend:
 self.sendCommand(toSend) 
 def sendCommand(self, code):
 commandBytes = array('l', [4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])
 try:
 client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 except socket.error, msg:
 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
 return
 try:
 client.connect((self.ip, self.port))
 except:
 print "Failed to connect to client"
 return
 l=12
 timeout=time.time()+self.connectTimeout
 while 1:
 data=client.recv(1024)
 data=data
 if len(data)<24:
 client.sendall(data[0:l])
 l=1
 else:
 client.sendall(buffer(commandBytes))
 commandBytes[1]=0
 client.sendall(buffer(commandBytes))
 client.close()
 break
 if time.time() > timeout:
 print "timeout error"
 break

I think the issue is how I form the buffers? I'm not entirely sure as this is the first time I've dealt with buffers. Having read through the Node.js documentation on new Buffer, it looks like it creates an array of Octets, whereas what I have is an array of ints, I may be wrong but an Octet is 8bits whilst an int is 4bits, I've tried changing the array to long and double, but this doesn't seem to resolve the issue

Matthijs Brouns
2,3391 gold badge28 silver badges38 bronze badges
asked Sep 25, 2017 at 8:30

1 Answer 1

2

Having had the time to have a quick read through on how Node.js handles buffers and buffers in general and it looks like what I thought was right.

Change;

commandBytes = array('l', [4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])

to;

commandBytes = bytearray([4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])

Also, then just pass the bytearray;

client.sendall(commandBytes)

instead of;

client.sendall(buffer(commandBytes))
answered Sep 30, 2017 at 16:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.