mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:23:07 +02:00
Merge branch 'main' of https://github.com/kuhyx/WUT_Computer_Science
This commit is contained in:
commit
d280a2f7ea
BIN
Programming/PSD/zin1/PDB_ZIN_1_KRZYSZTOF_RUDNICKI.pdf
Normal file
BIN
Programming/PSD/zin1/PDB_ZIN_1_KRZYSZTOF_RUDNICKI.pdf
Normal file
Binary file not shown.
28
Programming/PSD/zin1/konsument.py
Normal file
28
Programming/PSD/zin1/konsument.py
Normal file
@ -0,0 +1,28 @@
|
||||
import json
|
||||
from kafka import KafkaConsumer
|
||||
|
||||
# Thresholds
|
||||
TEMP_TOO_COLD = -10
|
||||
TEMP_TOO_HOT = 35
|
||||
|
||||
def process_temperature_reading(reading):
|
||||
temperature = reading['temperature']
|
||||
if temperature < TEMP_TOO_COLD:
|
||||
alert = f"WARNING: Temperature is too cold! ({temperature}°C)"
|
||||
elif temperature > TEMP_TOO_HOT:
|
||||
alert = f"WARNING: Temperature is too hot! ({temperature}°C)"
|
||||
else:
|
||||
alert = f"Temperature is normal. ({temperature}°C)"
|
||||
return alert
|
||||
|
||||
if __name__ == '__main__':
|
||||
consumer = KafkaConsumer(
|
||||
'temperature_readings',
|
||||
bootstrap_servers='localhost:9092',
|
||||
auto_offset_reset='earliest'
|
||||
)
|
||||
|
||||
for message in consumer:
|
||||
reading = json.loads(message.value)
|
||||
alert_message = process_temperature_reading(reading)
|
||||
print(alert_message)
|
||||
20
Programming/PSD/zin1/producent.py
Normal file
20
Programming/PSD/zin1/producent.py
Normal file
@ -0,0 +1,20 @@
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
from kafka import KafkaProducer
|
||||
from simulate_temperature_sensor import generate_temperature_reading
|
||||
|
||||
def serializer(message):
|
||||
return json.dumps(message).encode('utf-8')
|
||||
|
||||
producer = KafkaProducer(
|
||||
bootstrap_servers=['localhost:9092'],
|
||||
value_serializer=serializer
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
reading = generate_temperature_reading()
|
||||
print(f"Sending reading: {reading}")
|
||||
producer.send('temperature_readings', reading)
|
||||
time.sleep(random.randint(1, 5)) # Simulate readings sent at random intervals
|
||||
BIN
Programming/PSD/zin1/screen1.png
Normal file
BIN
Programming/PSD/zin1/screen1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
BIN
Programming/PSD/zin1/screen2.png
Normal file
BIN
Programming/PSD/zin1/screen2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 154 KiB |
BIN
Programming/PSD/zin1/screen3.png
Normal file
BIN
Programming/PSD/zin1/screen3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
BIN
Programming/PSD/zin1/screen4.png
Normal file
BIN
Programming/PSD/zin1/screen4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 138 KiB |
BIN
Programming/PSD/zin1/screen5.png
Normal file
BIN
Programming/PSD/zin1/screen5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 119 KiB |
12
Programming/PSD/zin1/simulate_temperature_sensor.py
Normal file
12
Programming/PSD/zin1/simulate_temperature_sensor.py
Normal file
@ -0,0 +1,12 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
def generate_temperature_reading():
|
||||
location_id = random.randint(1, 10) # Simulate 10 different locations
|
||||
temperature = random.uniform(-20, 40) # Temperature range from -20 to 40 degrees Celsius
|
||||
timestamp = time.time() # Current Unix timestamp
|
||||
return {
|
||||
'location_id': location_id,
|
||||
'temperature': round(temperature, 2),
|
||||
'timestamp': timestamp
|
||||
}
|
||||
76
Programming/PSD/zin1/zin1.tex
Normal file
76
Programming/PSD/zin1/zin1.tex
Normal file
@ -0,0 +1,76 @@
|
||||
\documentclass[a4paper, 12pt]{article}
|
||||
\usepackage{graphicx}
|
||||
\usepackage[utf8]{inputenc}
|
||||
|
||||
% Default fixed font does not support bold face
|
||||
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
|
||||
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12} % for normal
|
||||
|
||||
% Custom colors
|
||||
\usepackage{color}
|
||||
\definecolor{deepblue}{rgb}{0,0,0.5}
|
||||
\definecolor{deepred}{rgb}{0.6,0,0}
|
||||
\definecolor{deepgreen}{rgb}{0,0.5,0}
|
||||
|
||||
\usepackage{listings}
|
||||
|
||||
% Python style for highlighting
|
||||
\newcommand\pythonstyle{\lstset{
|
||||
language=Python,
|
||||
basicstyle=\ttm,
|
||||
morekeywords={self}, % Add keywords here
|
||||
keywordstyle=\ttb\color{deepblue},
|
||||
emph={MyClass,__init__}, % Custom highlighting
|
||||
emphstyle=\ttb\color{deepred}, % Custom highlighting style
|
||||
stringstyle=\color{deepgreen},
|
||||
frame=tb, % Any extra options here
|
||||
showstringspaces=false
|
||||
}}
|
||||
|
||||
|
||||
% Python environment
|
||||
\lstnewenvironment{python}[1][]
|
||||
{
|
||||
\pythonstyle
|
||||
\lstset{#1}
|
||||
}
|
||||
{}
|
||||
|
||||
% Python for external files
|
||||
\newcommand\pythonexternal[2][]{{
|
||||
\pythonstyle
|
||||
\lstinputlisting[#1]{#2}}}
|
||||
|
||||
% Python for inline
|
||||
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}
|
||||
|
||||
\title{Przetwarzanie strumieni danych i data science, \\ Zajęcia Zintegrowane 1}
|
||||
\author{Krzysztof Rudnicki, 307585}
|
||||
\begin{document}
|
||||
\maketitle
|
||||
\section{Przygotowanie maszyny wirtualnej}
|
||||
\paragraph{Przekopiowałem i uruchomiłem maszynę wirtualną z pliku .ova}
|
||||
\paragraph{Uruchomiłem serwis kafka korzystając z systemctl}
|
||||
\paragraph{Zweryfikowałem, że kafka działa (zarówno systemctl) jak i Hello World \\ \\ }
|
||||
\includegraphics[width=\textwidth]{screen1.png}
|
||||
\includegraphics[width=\textwidth]{screen3.png}
|
||||
\paragraph{Uruchomiłem kafdrop \\ \\}
|
||||
\includegraphics[width=\textwidth]{screen2.png}
|
||||
\paragraph{Przepisałem i uruchomiłem przykładowe skrypty python producenta i konsumenta \\ \\}
|
||||
\includegraphics[width=\textwidth]{screen4.png}
|
||||
|
||||
\section{Przerobienie skryptu Python}
|
||||
Postanowiłem przerobić pythonowy skrypt na taki który imituje sensor temperatury \\
|
||||
Chciałem stworzyć skrypt który z jednej strony jest prosty do napisania, a z drugiej imituje faktyczną praktyczną funkcjonalność którą można by użyć na przykład przy urządzeniach internetu rzeczy \\
|
||||
Generator danych:
|
||||
\pythonexternal{simulate_temperature_sensor.py}
|
||||
Producent:
|
||||
\pythonexternal{producent.py}
|
||||
Konsument:
|
||||
\pythonexternal{konsument.py}
|
||||
Przykładowe działanie: \\
|
||||
\begin{center}
|
||||
\includegraphics[width=0.8\paperwidth]{screen5.png}
|
||||
\end{center}
|
||||
|
||||
\end{document}
|
||||
Loading…
Reference in New Issue
Block a user