Created
June 22, 2021 02:29
-
-
Save Ajak58a/d29023b9f19e95dec1039fb7475f4fc3 to your computer and use it in GitHub Desktop.
command_control.py
This file contains 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
import paho.mqtt.client as mqtt | |
import paho.mqtt.publish as publish | |
import random | |
import time | |
import os | |
from devices import * | |
welcome_msg = (""" | |
___ _ _ ___ _ | |
/ __|___ _ _| |_ _ _ ___| | / __|___ _ _| |_ ___ _ _ | |
| (__/ _ \ ' \ _| '_/ _ \ | | (__/ -_) ' \ _/ -_) '_| | |
\___\___/_||_\__|_| \___/_| \___\___|_||_\__\___|_| | |
______________________________________________________________V 1.0 | |
""") | |
log_msg = "" | |
def logs(log_data): | |
try: | |
output_file = open(database_file, "a") | |
output_file.write(log_data+'\n') | |
output_file.close() | |
except Exception as e: | |
print str(e) | |
#raw_input() | |
def read_log(): | |
with open(database_file) as f: | |
for line in f: | |
print line | |
f.close() | |
# The 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. | |
client.subscribe(topic_sub,0) | |
# The callback for when a PUBLISH message is received from the server. | |
def on_message(client, userdata, msg): | |
message = str(msg.payload) | |
def send_command(command,topic_pub): | |
publish.single(topic_pub, command, hostname=broker) | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect(broker, port, 60) | |
# Blocking call that processes network traffic, dispatches callbacks and | |
# handles reconnecting. | |
# Other loop*() functions are available that give a threaded interface and a | |
# manual interface. | |
client.loop_start() | |
#main Function stars here | |
while True: | |
os.system("cls") | |
print welcome_msg | |
print "" | |
print "Following devices are connected" | |
print devices_names | |
print "" | |
print "Previously sent commands" | |
read_log() | |
print '\r' + "Enter Devices ID", | |
i = input(":") | |
print "Enter Command to send", | |
input_cmd = raw_input(":") | |
#os.system("cls") | |
send_command(input_cmd,devices_names[i-1]) | |
log_msg = "Device name: "+devices_names[i-1]+" : Commnad sent is: "+input_cmd + '\n' | |
print log_msg | |
logs(str(log_msg)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment