virtualsnack.py 14.9 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 13
# for emulator code
import sys
import string

Mark Tearle's avatar
Mark Tearle committed
14 15 16
class ContainedMultiSelect(npyscreen.BoxTitle):
    _contained_widget = npyscreen.TitleMultiSelect

Mark Tearle's avatar
Mark Tearle committed
17 18 19 20 21 22 23 24 25 26 27 28 29
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


Mark Tearle's avatar
Mark Tearle committed
30
class VirtualSnack(npyscreen.Form):
31

Mark Tearle's avatar
Mark Tearle committed
32
    def while_waiting(self):
Mark Tearle's avatar
Mark Tearle committed
33
        self.date_widget.value = datetime.now().ctime()
34 35
	self.sentfield.value = self.parentApp.sent
	self.receivedfield.value = self.parentApp.received
Mark Tearle's avatar
Mark Tearle committed
36 37
        self.display()

38

Mark Tearle's avatar
Mark Tearle committed
39 40 41 42
    def create(self, *args, **keywords):
        super(VirtualSnack, self).create(*args, **keywords)


Mark Tearle's avatar
Mark Tearle committed
43 44 45 46 47 48 49 50 51 52 53 54
        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
55

Mark Tearle's avatar
Mark Tearle committed
56
	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
57

Mark Tearle's avatar
Mark Tearle committed
58
	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)
59

Mark Tearle's avatar
Mark Tearle committed
60 61 62 63
	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
64

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

Mark Tearle's avatar
Mark Tearle committed
67 68
        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
69

Mark Tearle's avatar
Mark Tearle committed
70 71 72 73 74
        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"
Mark Tearle's avatar
Mark Tearle committed
75
	self.add_handlers({"^Q": self.exit_application})
76 77 78
        
	self.sentfield = self.add(npyscreen.TitleText, name = "Sent:", value="", editable=False )
        self.receivedfield = self.add(npyscreen.TitleText, name = "Received:", value="", editable=False )
79 80 81 82

    def exit_application(self,name):
        self.parentApp.setNextForm(None)
        self.editing = False
Mark Tearle's avatar
Mark Tearle committed
83 84 85


class VirtualSnackApp(npyscreen.NPSAppManaged):
86
    keypress_timeout_default = 1
Mark Tearle's avatar
Mark Tearle committed
87 88 89

    def onStart(self):
	self.addForm("MAIN", VirtualSnack, name="Virtual Snack")
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	
	# 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)
107

108 109 110 111 112 113 114 115 116 117 118 119
    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
Mark Tearle's avatar
Mark Tearle committed
120 121 122

		self.do_send("# Virtual Snack\n")
		self.do_prompt()
123 124 125 126 127 128 129 130 131 132
                 
                #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:
Mark Tearle's avatar
Mark Tearle committed
133 134 135 136
			self.handle_command(data)
			#response = 'OK ... ' + data
                        #sock.send(response)
			#self.sent = response
137 138 139 140 141 142 143 144 145 146 147 148 149
			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()
Mark Tearle's avatar
Mark Tearle committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    
    # Snack Emulator comms below
    
    def do_send(self, data):
        # Get the list sockets which are ready to be written through select
        read_sockets,write_sockets,error_sockets = select.select([],self.CONNECTION_LIST,[],0.01)
 
        for sock in write_sockets:
		sock.send(data)

	self.sent = data

    # Snack 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^WSnack 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):
	# FIXME
        # display = self.wTree.get_widget("label1")
        # display.set_text("%-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 handle_command(self, command):
        command = string.upper(command)
        print command
        if string.find(command, "HELP",0) == 0:
            self.do_help()
        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()
261 262 263 264

if __name__ == "__main__":
    App = VirtualSnackApp()
    App.run()
Mark Tearle's avatar
Mark Tearle committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473


# code below from vvend.py

# FIXME 
# class appgui:
#    def __init__(self):
#        """
#        In this init we are going to display the main
#        serverinfo window
#        """
#        gladefile="vvend.glade"
#        windowname="vvend"
#        self.wTree=gtk.glade.XML (gladefile,windowname)
#        # we only have two callbacks to register, but
#        # you could register any number, or use a
#        # special class that automatically
#        # registers all callbacks. If you wanted to pass
#        # an argument, you would use a tuple like this:
#        # dic = { "on button1_clicked" :
#        #         (self.button1_clicked, arg1,arg2) , ...
#
#        dic = {
#            "on_button1_clicked" : self.keypad_clicked,
#            "on_button2_clicked" : self.keypad_clicked,
#            "on_button3_clicked" : self.keypad_clicked,
#            "on_button4_clicked" : self.keypad_clicked,
#            "on_button5_clicked" : self.keypad_clicked,
#            "on_button6_clicked" : self.keypad_clicked,
#            "on_button7_clicked" : self.keypad_clicked,
#            "on_button8_clicked" : self.keypad_clicked,
#            "on_button9_clicked" : self.keypad_clicked,
#            "on_button10_clicked" : self.keypad_clicked,
#            "on_button11_clicked" : self.keypad_clicked,
#            "on_button11_clicked" : self.keypad_clicked,
#            "on_door_toggled" : self.door_changed,
#            "on_vvend_destroy_event" : self.quit,
#            "on_vvend_delete_event" : self.quit }
#        self.wTree.signal_autoconnect (dic)
#        display = self.wTree.get_widget("label1")
#        label_font = pango.FontDescription('monospace 28')
#        display.modify_font(label_font)
#
#        label_style = display.get_style().copy()
#        fg_color = display.get_colormap().alloc_color('lightgreen')
#        label_style.fg[gtk.STATE_NORMAL] = fg_color
#        display.set_style(label_style)
#
#        w = self.wTree.get_widget("eventbox1")
#        wstyle = w.get_style().copy()
#        bg_color = w.get_colormap().alloc_color('black')
#        wstyle.bg[gtk.STATE_NORMAL] = bg_color
#        w.set_style(wstyle)
#
#        display.set_text("*5N4CK0RZ*")
#
#        # vending machine password set here
#        self.vendpw = "AAAAAAAAAAAAAAAA"
#
#        self.messageid = None
#        self.switches = Switches()
#
#        #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#        #s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
##
##    listenhost=""
##    listenport=5150
##    s.bind((listenhost, listenport))
##    # only one connection
##    s.listen(1)
##    s.setblocking(1)
##    #GDK->gtk.gdk in GTK V 2.0
##    id=gtk.input_add(s, gtk.gdk.INPUT_READ, self.handleNewConnection)
#
#        self.server()
#
#
#        return
#
#        def __del__(self):
#            try:
#                self.sock.close()
#                self.sock.shutdown()
#            except:
#                pass
#
#####CALLBACKS

# FIXME
#    def keypad_clicked(self,widget):
#        key = widget.get_label()
#        if key == 'RESET':
#            key = '11'
#        else:
#            key = '0'+key
#        self.do_send('2'+key+' keypress\n')

# FIXME
#    def door_changed(self, widget):
#        self.switches.set_door_open(not widget.get_active())
#        if widget.get_active():
#            self.do_send('401 door closed\n')
#        else:
#            self.do_send('400 door open\n')

# FIXME
#    def handleNewConnection(self,source,condition):
#        #source is a socket in GTK v 1 and a fd in version 2
#        conn, addr = source.accept()
#        sys.stdout.write(conn.recv(1))
#        conn.send("bing\n")
#        return gtk.TRUE

# from http://www.pythonbrasil.com.br/moin.cgi/MonitorandoSocketsComPygtk


# FIXME
#    def send(self, data=None, widget=None):
#        text = self.entry.get_text()
#        self.do_send(text)
#        self.entry.set_text('')

        #


# FIXME
#    def server(self):
#
#        # inicializa o servidor
#        port = 5150
#
#        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#        self.sock.bind(('localhost', port))
#        self.sock.listen(0)
#        print "listening on ", port
#
#        #
#        #
#
#        self.server_tag = gtk.input_add(self.sock, gtk.gdk.INPUT_READ, self.accept)
#
#        # mantemos uma lista dos clientes conectados
#
#        self.clients = {}

# FIXME
#    def accept(self, source, condition):
#
#        #
#        # esperando para ser aceito
#
#        conn, addr = source.accept()
#        self.insert("%s:%s conectado\n" % addr)
#
#
#        self.do_prompt()
#        self.clients[addr] = (conn, gtk.input_add(conn, gtk.gdk.INPUT_READ, self.write))

# FIXME
#    def write(self, source, condition):

#
#        data = source.recv(1024)
#        if data.strip() == 'bye' or not len(data):
#
#            # se o cliente enviar um ''bye'', desconecte-o :)
#
#            source.close()
#            for addr, (conn, tag) in self.clients.iteritems():
#                if source is conn:
#                    gtk.input_remove(tag)
#                    self.insert('%s:%s desconectado\n' % addr)
#                    del self.clients[addr]
#
#                    self.server_tag = gtk.input_add(self.sock, gtk.gdk.INPUT_READ, self.accept)
#                    break
#        elif data.strip() == 'quit':
#            self.quit()
#        else:
#            for (addr, port), (conn, tag) in self.clients.iteritems():
#                if source is conn:
#                    self.insert('%s:%s >>> %s\n'%(addr, port, data.strip()))
#                    self.handle_command(data.strip())
#                    break
#
#        return gtk.TRUE

# FIXME
#    def insert(self, data):
#        statusbar = self.wTree.get_widget("statusbar1")
#        if self.messageid:
#            statusbar.remove(1, self.messageid)
#        self.messageid=statusbar.push(1,data)
#
#    def quit(self, *args):
#        sys.stdout.write("quiting...\n")
#        gtk.input_remove(self.server_tag)
#        for addr, (conn, tag) in self.clients.iteritems():
#            gtk.input_remove(tag)
#            conn.close()
#        self.sock.close()
#        self.sock.shutdown()
#
#        gtk.mainquit()
#        sys.stdout.write("quit!\n")