-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamlitz.py
65 lines (55 loc) · 1.83 KB
/
Streamlitz.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
import streamlit as st
import pymongo
import pandas as pd
import time
import paho.mqtt.client as mqtt
from streamlit_custom_notification_box import custom_notification_box as scnb
broker_address = "mqtt.eclipseprojects.io"
broker_port = 1883 # Default MQTT port
client1 = mqtt.Client()
client1.connect(broker_address, broker_port)
client1.subscribe("mytopic") #enter a topic which must be same in the publish code
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["IOT"] #your Database Name
collection = db["wii"] #your collection Name
def AlertBox(wht_msg, ip):
styles = {'material-icons':{'color': '#FF0000'},
'text-icon-link-close-container': {'box-shadow': '#3896de 0px 4px'},
'notification-text': {'':''},
'close-button':{'':''},
'link':{'':''}}
scnb(icon='info',
textDisplay=wht_msg,
externalLink='',
url='#',
styles=styles,
key=f"alert_box_{ip}")
def get_data():
data = list(collection.find({}, {'_id': 0}))
unique_data = {}
for entry in data:
ip = entry['IP:']
if ip not in unique_data:
unique_data[ip] = entry
return unique_data
def update_malicious(ip):
collection.update_one({"IP:": ip}, {'$set': {'Malicious:': "1"}})
AlertBox(f"IP {ip} marked as malicious.", ip)
def on_message(client, userdata, message):
msg = message.payload.decode("utf-8")
print(msg)
if msg == "1":
update_malicious('192.168.1.1')
def main():
st.title('Monitor')
table_placeholder = st.empty()
# Set the on_message callback
client1.on_message = on_message
client1.loop_start()
while True:
data = get_data()
df = pd.DataFrame(data).T
table_placeholder.table(df)
time.sleep(10)
if __name__ == '__main__':
main()