This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Converting Python to Lua
#1
I found this python script that I´m anxious to get working with Lua. Not to familiar with http socket scripting though. 

Is it possible to get this to work?

Its for a Wifi coffee maker. Finally I can make coffee at the touch of a button  Big Grin

Code:
#!/usr/bin/env python
import sys
import socket
import json

#method names to validate
API_METHOD_BREW = "brew"
API_METHOD_RESET = "reset"

#IP address of the smarter coffee machine on your network
TCP_IP = 'XXX.XXX.XXX.XXX'
TCP_PORT = 2081
BUFFER_SIZE = 10

#default method to call
api_method = sys.argv[1]

if api_method == API_METHOD_BREW:
    message_to_send = "7"
elif api_method == API_METHOD_RESET:
    message_to_send = "\x10"

#make connection to machine and send message
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TCP_IP, TCP_PORT))
    s.send(message_to_send)
    data = s.recv(BUFFER_SIZE)
    s.close()
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();

#convert response from machine to unicode
return_code = unicode(data)

#set default values to ouput
success=0
message=""

#find out what the machine response means
if return_code =="\x03\x00~":
    success=1
    message="brewing"
elif return_code=="\x03\x01~":
    message="already brewing"
elif return_code=="\x03\x05~":
    message="no carafe"
elif return_code=="\x03\x06~":
    message="no water"
elif return_code=="\x03i~":
    success=1
    message="reset"
else:
    message="check machine"

#ouput JSON to whatever called this script
print json.dumps({'success': success,'message':message,'return_code':repr(data)[1:10]})

quit()
sys.exit()
Reply


Messages In This Thread
Converting Python to Lua - by FatMax - 29.08.2016, 22:43

Forum Jump: