Skip to content
Snippets Groups Projects
Commit b830b5e6 authored by Mark Tearle's avatar Mark Tearle
Browse files

Remove socket handling code and snack emulation

parent 46716634
Branches
Tags
No related merge requests found
...@@ -2,13 +2,6 @@ ...@@ -2,13 +2,6 @@
import npyscreen import npyscreen
from datetime import datetime from datetime import datetime
# Incorporates code
# from http://www.binarytides.com/python-socket-server-code-example/
# Socket server in python using select function
import socket
import select
import errno
# Incorporates code from # Incorporates code from
#Pymodbus Asynchronous Server Example #Pymodbus Asynchronous Server Example
...@@ -51,19 +44,6 @@ class CokeButtonPress(npyscreen.MiniButtonPress): ...@@ -51,19 +44,6 @@ class CokeButtonPress(npyscreen.MiniButtonPress):
if self.when_pressed_callback: if self.when_pressed_callback:
self.when_pressed_callback(widget=self) self.when_pressed_callback(widget=self)
class Switches:
def __init__(self):
self.misc_input = 0xff
self.switch_input = 0x3f
def door_open(self):
return self.switch_input & 0x20
def set_door_open(self, open = True):
if open:
self.switch_input |= 0x20
else:
self.switch_input &= ~0x20
class VirtualCoke(npyscreen.Form): class VirtualCoke(npyscreen.Form):
def while_waiting(self): def while_waiting(self):
...@@ -172,102 +152,27 @@ class VirtualCokeApp(npyscreen.NPSAppManaged): ...@@ -172,102 +152,27 @@ class VirtualCokeApp(npyscreen.NPSAppManaged):
keypress_timeout_default = 1 keypress_timeout_default = 1
def onStart(self): def onStart(self):
# initialise virtual vending machine # initialise virtual coke machine
# vending machine password set here
self.vendpw = "AAAAAAAAAAAAAAAA"
self.switches = Switches()
# self.textdisplay = "*5N4CK0RZ*"
self.F = self.addForm("MAIN", VirtualCoke, name="Virtual Coke") self.F = self.addForm("MAIN", VirtualCoke, name="Virtual Coke")
# socket code # socket code was here
self.CONNECTION_LIST = [] # list of socket clients
self.RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 5150
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("0.0.0.0", PORT))
self.server_socket.listen(10)
# Add server socket to the list of readable connections
self.CONNECTION_LIST.append(self.server_socket)
self.sent="" self.sent=""
self.received="Chat server started on port " + str(PORT) self.received="in onStart"
def while_waiting(self): def while_waiting(self):
# Get the list sockets which are ready to be read through select pass
read_sockets,write_sockets,error_sockets = select.select(self.CONNECTION_LIST,[],[],0.01) # socket code was here
for sock in read_sockets:
#New connection
if sock == self.server_socket:
# Handle the case in which there is a new connection recieved through self.server_socket
sockfd, addr = self.server_socket.accept()
self.CONNECTION_LIST.append(sockfd)
self.received = "Client (%s, %s) connected" % addr
self.do_send("000 Virtual Coke is alive \n")
self.do_prompt()
#Some incoming message from a client
else:
# Data recieved from client, process it
try:
#In Windows, sometimes when a TCP program closes abruptly,
# a "Connection reset by peer" exception will be thrown
data = sock.recv(self.RECV_BUFFER)
# echo back the client message
if data:
self.handle_command(data)
#response = 'OK ... ' + data
#sock.send(response)
#self.sent = response
self.received = data
# client disconnected, so remove from socket list
except:
#print "Client (%s, %s) is offline" % addr
sock.close()
#self.CONNECTION_LIST.remove(sock)
continue
def onCleanExit(self): def onCleanExit(self):
self.server_socket.close() pass
# socket code was here
# Coke Emulator comms below # Coke Emulator comms below
def do_send(self, data): def do_send(self, data):
# Get the list sockets which are ready to be written through select # socket code was here
read_sockets,write_sockets,error_sockets = select.select([],self.CONNECTION_LIST,[],0.01)
for sock in write_sockets:
try:
sock.send(data)
except socket.error, e:
if isinstance(e.args, tuple):
self.sent = "errno is %d" % e[0]
if e[0] == errno.EPIPE:
# remote peer disconnected
self.sent = "Detected remote disconnect"
else:
# determine and handle different error
pass
else:
self.sent = "socket error ", e
self.CONNECTION_LIST.remove(sock)
sock.close()
return
except IOError, e:
# Hmmm, Can IOError actually be raised by the socket module?
self.sent = "Got IOError: ", e
return
self.sent = data self.sent = data
...@@ -294,105 +199,6 @@ class VirtualCokeApp(npyscreen.NPSAppManaged): ...@@ -294,105 +199,6 @@ class VirtualCokeApp(npyscreen.NPSAppManaged):
# Coke Emulator code below # Coke Emulator code below
def do_prompt(self):
self.do_send("# ")
def do_help(self):
help = """
Valid commands are:
ABOUT ROM information
B[S][nn] beep [synchronously] for a duration nn (optional)
C[S][nn] silence [synchronously] for a duration nn (optional)
Dxxxxxxxxxx show a message on the display
ECHO {ON|OFF} turn echo on or off
GETROM download the ROM source code using xmodem
H[...] this help screen
*JUMPxxxx jumps to a subroutine at location xxxx
*PEEKxxxx returns the value of the byte at location xxxx
*POKExxxxyy sets the value of location xxxx to yy
PING pongs
S[...] query all internal switch states
+Vnn vend an item
+VALL vend all items
*Wxxxxxxxxxxxx set a new password for authenticated vends. xxx=16 chars
password will be converted to uppercase
Very few functions are available when the machine is in standalone
mode (DIP SW 1 is set)
+ denotes that this item requires authentication if DIP SW 2 is set
* denotes that DIP SW 3 must be set to use these
Commands starting with # are ignored (comments)
"""
self.do_send(help)
def do_about(self):
about = """
The Virtual Vending^WCoke Machine Company
Mark Tearle, October 2014
"""
self.do_send(about)
def do_vend_all(self):
for i in range(11,99):
self.do_send("101 Vending "+str(i)+"\n")
self.do_send("153 Home sensors failing\n")
self.do_send("102 Vend all motors complete\n")
def do_vend(self,command):
fail = None
if fail:
self.do_send("153 Home sensors failing\n")
else:
# FIXME
# self.insert("Vending "+command)
self.do_send("100 Vend successful\n")
def do_display(self,string):
# self.textdisplay = "%-10.10s" % (string)
self.do_send('300 Written\n')
def do_beep(self,command):
sys.stdout.write("\a")
self.do_send('500 Beeped\n')
def do_silence(self,command):
pass
def do_switches(self):
self.do_send("600 3F 3F\n")
def do_pong(self):
self.do_send("000 PONG!\n")
def do_echo(self):
self.do_send("000 Not implemented\n")
def handle_command(self, command):
command = string.upper(command)
if string.find(command, "HELP",0) == 0:
self.do_help()
elif string.find(command, "ECHO",0) == 0:
self.do_echo()
elif string.find(command, "ABOUT",0) == 0:
self.do_about()
elif string.find(command, "PING",0) == 0:
self.do_pong()
elif string.find(command, "VALL",0) == 0:
self.do_vend_all()
elif string.find(command, "V",0) == 0:
self.do_vend(command)
elif string.find(command, "B",0) == 0:
self.do_beep(command)
elif string.find(command, "C",0) == 0:
self.do_silence(command)
elif string.find(command, "S",0) == 0:
self.do_switches()
elif string.find(command, "D",0) == 0:
self.do_display(command[1:])
self.do_prompt()
if __name__ == "__main__": if __name__ == "__main__":
App = VirtualCokeApp() App = VirtualCokeApp()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment