Created
June 13, 2018 19:16
-
-
Save metahertz/ca38fdc8ff5bc586b1efcffb7b978cde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
import time | |
import sys | |
from threading import Thread | |
import paho.mqtt.client as mqtt | |
import json | |
# The PAHO-MQTT library callback for when a PUBLISH message is received from the server. | |
def on_message(client, userdata, msg): | |
#We could do things here when messages arrive on watchdogControlQueue (LC_MGMT/mgmt-$hackerDesk) | |
#Such as allowing proctors to force the reset of a given station. | |
# mosquitto_pub -h 192.168.128.2 -t "LC_MGMT/mgmt-1" -m "Hello hello" | |
# Will get to here (for hacker desk 1) so we can action. | |
#print(msg.topic+" "+str(msg.payload)) | |
print(str(msg.payload)) | |
msg_json = json.loads(msg.payload) | |
print(msg_json) | |
if str(msg_json['station']) == ourHackerDeskNumber: | |
print("Message for Our station.") | |
color = int(msg_json['status']) | |
print(color) | |
on_RadioChange(color) | |
# The PAHO-MQTT library callback for when the client receives a CONNACK response from the server. | |
def on_connect(client, userdata, flags, rc): | |
print("Connected with result code "+str(rc)) | |
# Subscribing in on_connect() means that if we lose the connection and | |
# reconnect then subscriptions will be renewed. | |
#mqttClient.subscribe(watchdogControlQueue) | |
print("We connected, subscribing to queue") | |
mqttClient.subscribe("CLIENT_UPDATES/in", qos=0) | |
#def process_mqtt(): | |
#while True: | |
# # MQTT Loop, needs calling regularly to process data. | |
# mqttClient.loop() | |
# time.sleep(1) | |
#def run_gui(): | |
# guiThread = Thread(target=TrafficLights()) | |
# guiThread.start() | |
# #ourGui = TrafficLights() | |
ourHackerDeskNumber = sys.argv[1] | |
#ourGui = TrafficLights() | |
#run_gui() | |
global mqttClient | |
mqttClient = mqtt.Client() | |
mqttClient.on_connect = on_connect | |
mqttClient.on_message = on_message | |
mqttClient.connect("192.168.129.2", 1883, 60) | |
mqttClient.subscribe("CLIENT_STATUS/in", qos=0) | |
#mqttThread = Thread(target=process_mqtt) | |
#mqttThread.start() | |
#def process_gui(): | |
window = Tk() | |
window.title("Hacking Challenge Status Monitor - CLUS 2018") | |
frame = Frame(window) | |
frame.pack() | |
color = StringVar() | |
#radio_red = Radiobutton(frame, text="Red", bg="red", variable=self.color, value="R", command=self.on_RadioChange) | |
#radio_red.grid(row=10, column=1) | |
canvas = Canvas(window, width=450, height=130, bg="white") | |
canvas.pack() | |
oval_wifi = canvas.create_oval(10, 10, 110, 110, fill="red") | |
label_wifi = canvas.create_text((60, 120), text="WiFi") | |
oval_router = canvas.create_oval(120, 10, 220, 110, fill="red") | |
label_router = canvas.create_text((170, 120), text="Router") | |
oval_camera = canvas.create_oval(230, 10, 330, 110, fill="red") | |
label_camera = canvas.create_text((280, 120), text="Camera") | |
oval_safe = canvas.create_oval(340, 10, 440, 110, fill="red") | |
label_safe = canvas.create_text((390, 120), text="Safe") | |
#color.set('0') | |
#self.canvas.itemconfig(self.oval_red, fill="red") | |
#window.update_idletasks() | |
#window.update() | |
#self.window.mainloop() | |
window.update() | |
def on_RadioChange(color): | |
print("In radiochange") | |
color = str(color) | |
if color == '0': | |
canvas.itemconfig(oval_wifi, fill="red") | |
canvas.itemconfig(oval_router, fill="red") | |
canvas.itemconfig(oval_camera, fill="red") | |
canvas.itemconfig(oval_safe, fill="red") | |
elif color == '1': | |
canvas.itemconfig(oval_wifi, fill="green") | |
canvas.itemconfig(oval_router, fill="red") | |
canvas.itemconfig(oval_camera, fill="red") | |
canvas.itemconfig(oval_safe, fill="red") | |
elif color == '2': | |
canvas.itemconfig(oval_wifi, fill="green") | |
canvas.itemconfig(oval_router, fill="green") | |
canvas.itemconfig(oval_camera, fill="red") | |
canvas.itemconfig(oval_safe, fill="red") | |
elif color == '3': | |
print("Making GUI Change for status 3") | |
canvas.itemconfig(oval_wifi, fill="green") | |
canvas.itemconfig(oval_router, fill="green") | |
canvas.itemconfig(oval_camera, fill="green") | |
canvas.itemconfig(oval_safe, fill="red") | |
elif color == '4': | |
canvas.itemconfig(oval_wifi, fill="green") | |
canvas.itemconfig(oval_router, fill="green") | |
canvas.itemconfig(oval_camera, fill="green") | |
canvas.itemconfig(oval_safe, fill="green") | |
window.update() | |
#self.window.update_idletasks() | |
#self.window.update() | |
while True: | |
# MQTT Loop, needs calling regularly to process data. | |
mqttClient.loop() | |
time.sleep(1) | |
#guiThread = Thread(target=process_gui) | |
#guiThread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment