virtualsnack.py 4.91 KB
Newer Older
1 2
#!/usr/bin/env python
import npyscreen
Mark Tearle's avatar
Mark Tearle committed
3
from datetime import datetime
4

5 6 7 8 9
# Incorporates code
# from http://www.binarytides.com/python-socket-server-code-example/
# Socket server in python using select function
import socket, select

Mark Tearle's avatar
Mark Tearle committed
10 11 12
class ContainedMultiSelect(npyscreen.BoxTitle):
    _contained_widget = npyscreen.TitleMultiSelect

Mark Tearle's avatar
Mark Tearle committed
13
class VirtualSnack(npyscreen.Form):
14

Mark Tearle's avatar
Mark Tearle committed
15
    def while_waiting(self):
Mark Tearle's avatar
Mark Tearle committed
16
        self.date_widget.value = datetime.now().ctime()
17 18
	self.sentfield.value = self.parentApp.sent
	self.receivedfield.value = self.parentApp.received
Mark Tearle's avatar
Mark Tearle committed
19 20
        self.display()

21

Mark Tearle's avatar
Mark Tearle committed
22 23 24 25
    def create(self, *args, **keywords):
        super(VirtualSnack, self).create(*args, **keywords)


Mark Tearle's avatar
Mark Tearle committed
26 27 28 29 30 31 32 33 34 35 36 37
        self.textdisplay = self.add(npyscreen.FixedText, value="*5N4CK0RZ*", editable=False, relx=9)
        self.textdisplay.important = True
	
	self.kpbuttons = []
	kpx = 1
	kpy = 1
	for keypad in range(0,10):
		kpx = ((keypad % 4) * 6 ) + 3
		kpy = int(keypad / 4) + 4
		self.kpbuttons.append(self.add(npyscreen.MiniButton,name="%d"%keypad, relx = kpx, rely = kpy))
		
	self.reset= self.add(npyscreen.MiniButton,name="RESET",  relx = kpx + 7, rely = kpy)
Mark Tearle's avatar
Mark Tearle committed
38

Mark Tearle's avatar
Mark Tearle committed
39
	self.dip = self.add(npyscreen.MultiSelect, name = "Door", max_width=15, relx = 4, rely = 10, max_height=4, value = [], values = ["DOOR"], scroll_exit=True)
Mark Tearle's avatar
Mark Tearle committed
40

Mark Tearle's avatar
Mark Tearle committed
41
	self.dip = self.add(npyscreen.MultiSelect, name = "DIP Switch", max_width = 45, rely =3, relx = 30, max_height=10, value = [], values = ["DIP1", "DIP2", "DIP3","DIP4","DIP5","DIP6","DIP7","DIP8"], scroll_exit=True)
42

Mark Tearle's avatar
Mark Tearle committed
43 44 45 46
	self.nickel= self.add(npyscreen.MiniButton,name="0.05", rely= 3, relx=50)
	self.dime= self.add(npyscreen.MiniButton,name="0.10", relx=50)
	self.quarter= self.add(npyscreen.MiniButton,name="0.25", relx=50)
	self.dollar= self.add(npyscreen.MiniButton,name="1.00", relx=50)
Mark Tearle's avatar
Mark Tearle committed
47

Mark Tearle's avatar
Mark Tearle committed
48
	self.mode= self.add(npyscreen.MiniButton,name="MODE", relx=50)
Mark Tearle's avatar
Mark Tearle committed
49

Mark Tearle's avatar
Mark Tearle committed
50 51
        self.wStatus1 = self.add(npyscreen.FixedText, value="Last Command", editable=False, relx=2, rely=12)
        self.wStatus1.important = True
Mark Tearle's avatar
Mark Tearle committed
52

Mark Tearle's avatar
Mark Tearle committed
53 54 55 56 57 58
        self.wStatus2 = self.add(npyscreen.FixedText, value="", editable=False)

	
	self.date_widget = self.add(npyscreen.FixedText, value=datetime.now().ctime(), editable=False)
        self.date_widget.value = "Hello"
	self.add_handlers({"^T": self.exit_application})
59 60 61
        
	self.sentfield = self.add(npyscreen.TitleText, name = "Sent:", value="", editable=False )
        self.receivedfield = self.add(npyscreen.TitleText, name = "Received:", value="", editable=False )
62 63 64 65

    def exit_application(self,name):
        self.parentApp.setNextForm(None)
        self.editing = False
Mark Tearle's avatar
Mark Tearle committed
66 67 68


class VirtualSnackApp(npyscreen.NPSAppManaged):
69
    keypress_timeout_default = 1
Mark Tearle's avatar
Mark Tearle committed
70 71 72

    def onStart(self):
	self.addForm("MAIN", VirtualSnack, name="Virtual Snack")
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	
	# socket code
    	self.CONNECTION_LIST = []    # list of socket clients
    	self.RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
    	PORT = 5000

        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.received="Chat server started on port " + str(PORT)
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    def while_waiting(self):
        # Get the list sockets which are ready to be read through select
        read_sockets,write_sockets,error_sockets = select.select(self.CONNECTION_LIST,[],[],0.01)
 
        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
                 
                #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:
			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):
        self.server_socket.close()
129 130 131 132

if __name__ == "__main__":
    App = VirtualSnackApp()
    App.run()