Exemplo n.º 1
0
 def set_command(self,command, args=''):
     '''
     transmit or execute commands from websocket or gpio interfaces
     '''
     if command == 'stop':
         if self.playing:
             self.midifactory.command('play 0')
             self.playing = False
         if self.recording:
             self.midifactory.command('record 0')
             self.recording = False
         if self.opened:
             pass
     elif command in ('play','record'):
         self.midifactory.command(command+' 1')
         if command == 'play':
             self.playing = True
         else:
             self.analyzed['cocktail'] = 0
             self.analyzed['result'] = ''
             if self.recording == False:
                 self.recording = True
                 self.notes = []
     elif command == 'cocktail':
         if self.analyzed['cocktail'] > 0:
             d = threads.deferToThread(self.serve, *(self.analyzed['cocktail'],))
             d.addCallback(self.wsfactory.sendmessage)
             self.wsfactory.sendmessage(u'Service en cours...\n')
         else:
             if self.recording:
                 self.midifactory.command('record 0')
                 self.recording = False
                 if self.analyzed['cocktail'] == 0 :
                     reactor.callLater(1, self.set_command, 'cocktail') #@UndefinedVariable
     elif command[:4] == 'test':
         cont = dbUtils.getPump(self.dbsession, command[6:])
         if len(cont) > 1:
             if cont[0] == 'gpio_in':
                 try:
                     try:
                         self.set_command(cont[4].split(",")[0],cont[4].split(",")[1])
                     except:
                         self.set_command(cont[4].split(",")[0])
                 except Exception, err:
                     print(err.message)
             elif command[5] == '-':
                 switchcontrol(cont,debug=self.debug)
                 self.serving = False
             else:
                 switchcontrol(cont,on=1,debug=self.debug)
                 self.serving = True
         else:
             log.msg("i2c error: no data found for pump n°:%s"
                     % command[6:])
Exemplo n.º 2
0
 def render_POST(self, request):
     param = False
     if self.debug:
         log.msg(request.content)
         log.msg("POST received Headers: " + str(request.getAllHeaders())+"\n")
         log.msg("POST received Args: " + str(request.args)+"\n")
         log.msg("POST received Content: " + str(request.content.getvalue())+"\n")
     try :
         '''
         Global exception handler to avoid letting bad connections open  
         '''
         try:
             '''
             Check if it is a url-encoded or json-encoded request 
             '''
             client = request.args['client'][0]
             action = request.args['action'][0]
             try :
                 command = request.args['command'][0]
             except : 
                 command = False
             try:
                 param = request.args['param'][0]
             except : 
                 if command:
                     if command == 'setrecipe':
                         param = request.args
             if self.debug:
                 log.msg("url-encoded request: %s" % action)
         except:
             dictreq = json.load(request.content)
             #log.msg(dictreq)
             client = dictreq['client']
             action = dictreq['action']
             command = dictreq['command']
             param = dictreq['params']
             if self.debug:
                 log.msg("json-encoded request: %s" % action)
             
         if client == 'web':
             if action in ('status',
                           'close',
                           'play',
                           'record',
                           'reload',
                           'cocktail',
                           'stop',
                           'config',
                           'pump',
                           'test'):
                 if action == 'config':
                     if param :
                         '''
                         Update Data
                         '''
                         if self.debug:
                             log.msg("request processed param = "+str(param))
                         self.updateDB(request,command, param)
                     else:
                         '''
                         Get Data
                         '''
                         if self.debug:
                             log.msg("request without parameter")
                         self.queryDB(request,command)
                 elif action == 'pump':
                     if command :
                         control = dbUtils.getPump(self.parent.dbsession, command[1:])
                         if self.debug:
                             log.msg("pump command: "+command)
                         try:
                             switchcontrol(control, int(not self.parent.serving), self.debug)
                             self.parent.serving = not self.parent.serving
                             #log.msg("serving = %s" % self.parent.serving)
                             request.write('0')
                         except:
                             log.msg("i2c system error")
                             request.write('1')
                     request.finish()
                             
                 elif action == 'cocktail':
                     d = threads.deferToThread(self.parent.serve, *(command,))
                     d.addCallbacks(self.resultOk, errback=self.resultFailed, 
                                        callbackArgs=(request,), errbackArgs=(request,))
             else :
                 request.write("bad command")
                 request.finish()
     except Exception, err:
         log.msg("Error: " + err.message)
         request.write("network error")
         request.finish()