-
Notifications
You must be signed in to change notification settings - Fork 18
/
controller.py
125 lines (92 loc) · 3.15 KB
/
controller.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.graphics import *
from lib.connectionToServer import Client
import lib.config
from mode1 import ScreenSaver
from mode2 import Learning
from mode3 import Discovering
from mode4 import Credits
class Controller(Widget):
def __init__(self, **kwargs):
super(Controller, self).__init__(**kwargs)
self.clientId = kwargs.pop('cid', 1)
self.modes = [ScreenSaver(controller=self, modeId=1),
Learning(controller=self, modeId=2),
Discovering(controller=self, modeId=3),
Credits(controller=self, modeId=4)]
self.currentModeId = -1
self.startConnection()
self.serverIsReady = False
# messages from server arrive here
#
def onNewMessageFromServer(self, message):
print "new message from server:", message
if message == "reset_all":
#TODO cleanup serverIsReady hack
self.serverIsReady = False
self.stopCurrentMode()
self.reset_all_modes()
self.currentModeId = -1
elif message == "change_mode/1": # mode 1 messages
self.serverIsReady = True
self.stopCurrentMode()
self.currentModeId = 1
self.updateCurrentMode()
self.add_widget(self.currentMode)
# only start if we are numero uno
if self.clientId == "1":
self.currentMode.start()
elif message == "scan_start":
if self.serverIsReady:
self.currentMode.start()
elif message == "scan_fadein":
if self.serverIsReady:
self.currentMode.fadein()
elif message == "change_mode/2": # mode 2 messages
self.stopCurrentMode()
self.currentModeId = 2
self.updateCurrentMode()
self.add_widget(self.currentMode)
self.currentMode.start()
elif message == "change_mode/3": # mode 3 messages
self.stopCurrentMode()
self.currentModeId = 3
self.updateCurrentMode()
self.add_widget(self.currentMode)
self.currentMode.start()
elif message == "change_mode/4":
self.stopCurrentMode()
self.currentModeId = 4
self.updateCurrentMode()
self.add_widget(self.currentMode)
self.currentMode.start()
else:
print "message is not recognized:", message
def sendMessage(self, message):
print "Sending message ", message
self.connection.sendMessage(message)
def stopCurrentMode(self):
if self.currentModeId == -1:
return
self.currentMode.stop()
self.remove_widget(self.currentMode)
def reset_all_modes(self):
for mode in self.modes:
mode.reset()
def updateCurrentMode(self):
# TODO: check over/under flows
self.currentMode = self.modes[self.currentModeId - 1]
# connection stuff
#
def connectionListen(self, dt):
self.connection.listen()
def cleanupOnExit(self):
self.connection.close()
def startConnection(self):
self.connection = Client(host=lib.config.host, port=lib.config.port, callback=self.onNewMessageFromServer)
self.connection.connect()
# send who we are
self.connection.sendMessage(self.clientId)
# start listening
Clock.schedule_interval(self.connectionListen, 0)