Skip to content

Commit

Permalink
corrigido: uso de apenas websocket e thread para a deteccao da webcam
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurAssuncao committed Nov 26, 2013
1 parent 00a5c14 commit e4ca82f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
10 changes: 5 additions & 5 deletions detector/WebSocketWebCam.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WebSocketWebCam(WebSocket):
processo = None

def handleMessage(self):
print 'Recebeu msg: ', self.data
with open('./file/estado_jogo_cliente.json', 'w') as arq:
arq.write(self.data)

Expand All @@ -21,18 +22,17 @@ def handleConnected(self):
detector_movimento.processo = self.processo
self.processo.start()
else:
print 'teste1'
print 'processo ja iniciado, usando este processo iniciado'
self.processo = detector_movimento.processo
self.processo.conexao = self
self.processo.gerenciador_estado_jogador.conexao = self
self.processo.gerenciador_estado_jogador._set_vivo(True)
else:
print 'teste'
self.processo.conexao = self

def handleClose(self):
print self.address, 'closed'
gerenciador_estado_jogador = detector_movimento.GerenciadorEstadoJogador(
)
gerenciador_estado_jogador._set_vivo(False)
self.processo.gerenciador_estado_jogador._set_vivo(False)
# self.processo.calibrado = False


Expand Down
42 changes: 29 additions & 13 deletions detector/detector_movimento.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from optparse import OptionParser
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
from multiprocessing import Process
import threading

processo = None

Expand Down Expand Up @@ -63,10 +64,13 @@ def atualizar_estado(self, movimento, calibrado):
str_json = json.dumps(estado_jogador)
if self.conexao is None:
# Recria o arquivo e insere o novo estado do jogador
print 'escreveu no arquivo: ', str_json
with open(self.ARQUIVO_ESTADO_JOGADOR, 'w') as arq:
arq.write(str_json)
else:
try:
print self.conexao.address
print 'Enviou: ', str_json
self.conexao.sendMessage(str_json)
except:
print 'Não foi possível enviar a mensagem ao cliente'
Expand All @@ -76,21 +80,29 @@ def _set_vivo(self, vivo):
Seta o estado vivo do jogador
:param vivo: se o jogador está vivo
'''
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR, 'r') as arq:
estado_jogo = json.loads(arq.read())
estado_jogo['jogador_vivo'] = vivo
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR, 'w') as arq:
arq.write(json.dumps(estado_jogo))
try:
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR, 'r') as arq:
estado_jogo = json.loads(arq.read())
estado_jogo['jogador_vivo'] = vivo
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR, 'w') as arq:
arq.write(json.dumps(estado_jogo))
except ValueError as e:
print e

def is_vivo(self):
'''
verifica se o jogador está vivo ou não
:returns: True se o jogador está vivo e False se não
'''
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR) as arq:
vivo_str = arq.read()
vivo = json.loads(vivo_str)
return vivo['jogador_vivo']
try:
with open(self.ARQUIVO_ESTADO_VIDA_JOGADOR) as arq:
vivo_str = arq.read()
print vivo_str
vivo = json.loads(vivo_str)
return vivo['jogador_vivo']
except ValueError as e:
print e
return False

def tela_atual(self):
'''
Expand All @@ -111,7 +123,7 @@ def finish(self):
self._set_vivo(True)


class DetectorMovimento(Process):
class DetectorMovimento(threading.Thread):

'''
Classe para detectar o movimento
Expand Down Expand Up @@ -142,7 +154,7 @@ def __init__(self, id_camera=0, agachar_desabilitado=False, conexao=None):
Construtor da Classe
:param id_camera: identificador da camera que será utilizada, o padrão é 0
'''
Process.__init__(self)
threading.Thread.__init__(self)
self.conexao = conexao
self.movimento = Movimentos.EM_PE
self.id_camera = id_camera
Expand Down Expand Up @@ -232,7 +244,7 @@ def iniciar(self):
while(self.camera.isOpened()):
contador = contador + 1
# a cada N loops ele verifica se o jogador ta vivo
if contador % 100 == 0:
if contador % 50 == 0:
if not self.gerenciador_estado_jogador.is_vivo():
print 'Jogador perdeu'
break
Expand Down Expand Up @@ -414,7 +426,11 @@ def reiniciar(self):
self.ys = []
self.desenhar_linhas = False
self.calibrado = False
self.gerenciador_estado_jogador = GerenciadorEstadoJogador()
self.gerenciador_estado_jogador.finish()
if self.conexao is None:
self.gerenciador_estado_jogador = GerenciadorEstadoJogador()
else:
self.gerenciador_estado_jogador = GerenciadorEstadoJogador(conexao=self.conexao)
self.iniciar()

def finalizar(self):
Expand Down

0 comments on commit e4ca82f

Please sign in to comment.