diff --git a/Programming/PSD/zin1/PDB_ZIN_1_KRZYSZTOF_RUDNICKI.pdf b/Programming/PSD/zin1/PDB_ZIN_1_KRZYSZTOF_RUDNICKI.pdf new file mode 100644 index 00000000..960d8415 Binary files /dev/null and b/Programming/PSD/zin1/PDB_ZIN_1_KRZYSZTOF_RUDNICKI.pdf differ diff --git a/Programming/PSD/zin1/konsument.py b/Programming/PSD/zin1/konsument.py new file mode 100644 index 00000000..89e14898 --- /dev/null +++ b/Programming/PSD/zin1/konsument.py @@ -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) diff --git a/Programming/PSD/zin1/producent.py b/Programming/PSD/zin1/producent.py new file mode 100644 index 00000000..089cd336 --- /dev/null +++ b/Programming/PSD/zin1/producent.py @@ -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 diff --git a/Programming/PSD/zin1/screen1.png b/Programming/PSD/zin1/screen1.png new file mode 100644 index 00000000..9c006bc1 Binary files /dev/null and b/Programming/PSD/zin1/screen1.png differ diff --git a/Programming/PSD/zin1/screen2.png b/Programming/PSD/zin1/screen2.png new file mode 100644 index 00000000..6916803e Binary files /dev/null and b/Programming/PSD/zin1/screen2.png differ diff --git a/Programming/PSD/zin1/screen3.png b/Programming/PSD/zin1/screen3.png new file mode 100644 index 00000000..b7385f4e Binary files /dev/null and b/Programming/PSD/zin1/screen3.png differ diff --git a/Programming/PSD/zin1/screen4.png b/Programming/PSD/zin1/screen4.png new file mode 100644 index 00000000..5060f378 Binary files /dev/null and b/Programming/PSD/zin1/screen4.png differ diff --git a/Programming/PSD/zin1/screen5.png b/Programming/PSD/zin1/screen5.png new file mode 100644 index 00000000..0f97a1e6 Binary files /dev/null and b/Programming/PSD/zin1/screen5.png differ diff --git a/Programming/PSD/zin1/simulate_temperature_sensor.py b/Programming/PSD/zin1/simulate_temperature_sensor.py new file mode 100644 index 00000000..5f730d0b --- /dev/null +++ b/Programming/PSD/zin1/simulate_temperature_sensor.py @@ -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 + } diff --git a/Programming/PSD/zin1/zin1.tex b/Programming/PSD/zin1/zin1.tex new file mode 100644 index 00000000..8a5125d7 --- /dev/null +++ b/Programming/PSD/zin1/zin1.tex @@ -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} \ No newline at end of file