forked from yorkhackspace/SpacehackClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
135 lines (118 loc) · 4.81 KB
/
game.py
File metadata and controls
135 lines (118 loc) · 4.81 KB
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
126
127
128
129
130
131
132
133
# SpaceHack! Game client main module
#York Hackspace January 2014
#This runs on a Beaglebone Black
import sys
import mosquitto
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.ADC as ADC
from Adafruit_CharLCD import Adafruit_CharLCD
from NokiaLCD import NokiaLCD
import Keypad_BBB
from collections import OrderedDict
import commands
import json
import time
import os
#import game libraries
from gamelibs import config_manager
from gamelibs import lcd_manager
from controls import control_manager
#Vars
roundconfig = {}
keypad = None
hasregistered = False
timeoutstarted = 0.0
resetBlocks = False
#Who am I? Get my ip address
ipaddress = commands.getoutput("/sbin/ifconfig").split("\n")[1].split()[1][5:]
#configuration. Load the config and get various dictionaries and arrays back
configFileName = 'game-' + ipaddress +'.config'
config, controlids, controldefs, sortedlist = config_manager.loadConfig(configFileName)
#initialise all of the LCDs and return a list of LCD objects
myLcdManager = lcd_manager.LcdManager(sortedlist, config)
#initialise all controls
control_manager.initialiseControls(config, sortedlist, myLcdManager)
#MQTT client
client = mosquitto.Mosquitto("Game-" + ipaddress) #client ID
print config['local']
server = config['local']['server']
#MQTT message arrived
def on_message(mosq, obj, msg):
"""Process incoming MQTT message"""
print(msg.topic + " - " + str(msg.payload))
nodes = msg.topic.split('/')
global timeoutstarted
global timeoutdisplayblocks
global myLcdManager
if nodes[0]=='clients':
if nodes[2]=='configure':
if str(msg.payload) == 'reboot':
os.system('reboot')
else:
myLcdManager = lcd_manager.LcdManager(sortedlist, config)
processRoundConfig(str(msg.payload))
timeoutstarted = 0.0
timeoutdisplayblocks = 0
elif nodes[2] == 'instructions':
myLcdManager.display(str(msg.payload), 20, "0")
#start timer?
if 'timeout' in roundconfig and roundconfig['timeout'] > 0.0:
resetBlocks = True
timeoutstarted = time.time()
elif nodes[2] == 'timeout':
roundconfig['timeout'] = float(str(msg.payload))
elif nodes[2] in controlids:
ctrlid = nodes[2]
if nodes[3] == 'enabled':
if str(msg.payload) == "0":
roundconfig['controls'][ctrlid]['enabled'] = False
#switch it off
myLcdManager.display(" ", config['local']['controls'][ctrlid]['display']['width'], ctrlid)
else:
roundconfig['controls'][ctrlid]['enabled'] = True
#switch it on
myLcdManager.display(roundconfig['controls'][ctrlid]['name'], config['local']['controls'][ctrlid]['display']['width'], ctrlid)
elif nodes[3] == 'name':
if str(msg.payload) == '':
myLcdManager.clear(ctrlid)
else:
myLcdManager.display(str(msg.payload), config['local']['controls'][ctrlid]['display']['width'], ctrlid, False)
elif nodes[0] == 'server':
if nodes[1] == 'ready':
mess = str(msg.payload)
if mess == 'started':
myLcdManager = lcd_manager.LcdManager(sortedlist, config)
client.publish("server/register", json.dumps(config['interface']))
elif mess == 'ready':
global hasregistered
if not hasregistered:
hasregistered = True
client.publish("server/register", json.dumps(config['interface']))
elif mess == 'poweroff':
os.system('poweroff')
#Process an incoming config for a round
def processRoundConfig(roundconfigstring):
"""Process an incoming config for a round"""
control_manager.initialiseControls(config, sortedlist, myLcdManager)
x = json.loads(roundconfigstring)
for key in x.keys():
roundconfig[key] = x[key]
control_manager.processRoundConfig(config, roundconfig, controlids)
#Setup MQTT
client.on_message = on_message
client.connect(server)
subsbase = "clients/" + ipaddress + "/"
client.subscribe(subsbase + "configure")
client.subscribe(subsbase + "instructions")
client.subscribe(subsbase + "timeout")
client.subscribe("server/ready")
for controlid in [x['id'] for x in config['interface']['controls']]:
client.subscribe(subsbase + str(controlid) + '/name')
client.subscribe(subsbase + str(controlid) + '/enabled')
#Main loop
while(client.loop(0) == 0):
control_manager.pollControls(config, roundconfig, controlids, client, ipaddress)
myLcdManager.displayTimer(timeoutstarted, resetBlocks, roundconfig.get('timeout', 0))
if resetBlocks:
resetBlocks = False