feat: added psd zin1 work

This commit is contained in:
Krzysztof R. 2024-04-01 17:43:10 +02:00
parent 0dbc778150
commit 97eb034c7f
10 changed files with 136 additions and 0 deletions

Binary file not shown.

View 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)

View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

View 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
}

View 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}