-
Notifications
You must be signed in to change notification settings - Fork 1
/
votos.py
55 lines (45 loc) · 1.83 KB
/
votos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from autobahn import wamp
from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks
import json
class VotosBackend(ApplicationSession):
#Construtor usado para inicializacao
def __init__(self, config):
ApplicationSession.__init__(self, config)
self.init()
#Criacao da variavel que armazenara os votos de cada robo
def init(self):
self._votos = {
'bb8': 0,
'c3po': 0,
'r2d2': 0
}
@wamp.register(u'io.crossbar.demo.voto.get')
def getVotos(self):
#Retorna para o cliente uma json = {"robos": [{"c3po": 1}, {"r2d2": 0}, {"bb8": 1}]}
return json.dumps({'robos': [{key: value} for key, value in self._votos.items()]})
@wamp.register(u'io.crossbar.demo.voto.votar')
def submitVoto(self, robo):
#Recebe o identificador do robo e atualiza o valor na variavel
print(u'Robo {} recebeu mais um voto'.format(robo))
self._votos[robo] += 1
resultado = json.dumps({robo: self._votos[robo]})
self.publish(u'io.crossbar.demo.voto.onvoto', resultado)
@wamp.register(u'io.crossbar.demo.voto.reset')
def resetVotos(self):
#Atribui 0 a todos os elementos da votacao
self.init()
resultado = self.getVotos()
self.publish(u'io.crossbar.demo.voto.onreset', resultado)
@inlineCallbacks
def onJoin(self, details):
#Publica todos os procedimentos para utilizacao pelos clientes
res = yield self.register(self)
print("Votacao - Backend: {} procedimentos registrados!".format(len(res)))
if __name__ == '__main__':
runner = ApplicationRunner(
url = u'ws://{}:{}/ws'.format('localhost', 8080),
realm = 'embarcados')
runner.run(VotosBackend)