Hallo,
der Link stimmte nicht. Hab es mal korrigiert.
Mein Ziel ist es mittels Apache Kafka
https://kafka.apache.org/ Nachrichten an meinen Server zu schicken (z.B. das ein Motor eingeschaltet wurden). Es gibt für Kafka zwei Python Clients:
https://github.com/confluentinc/confluent-kafka-python (enthält C-Code) und
https://github.com/dpkp/kafka-python (scheint keinen C-Code zu beinhalten).
Hab gerade versucht es zum laufen zu bekommen. Habe also mit pip das Modul in den Ordner gepackt so das ich folgende Struktur erhalte:
icon.png
icon.svg
kafka/
kafka_python-1.3.5.dist-info/
manifest
tut3.py
Mein Code sieht dann wie folgt aus:
Code: Alles auswählen
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
import sys
import ftrobopy # Import the ftrobopy module
from TxtStyle import *
from kafka import KafkaProducer
import time
class FtcGuiApplication(TxtApplication):
def __init__(self, args):
TxtApplication.__init__(self, args)
# create the empty main window
w = TxtWindow("Tut_3e")
producer = KafkaProducer(bootstrap_servers='192.168.1.100:9092')
txt_ip = os.environ.get('TXT_IP') # try to read TXT_IP environment variable
if txt_ip == None: txt_ip = "localhost" # use localhost otherwise
try:
self.txt = ftrobopy.ftrobopy(txt_ip, 65000) # try to connect to IO server
except:
self.txt = None
vbox = QVBoxLayout()
if not self.txt:
# display error of TXT could no be connected
# error messages is centered and may span
# over several lines
err_msg = QLabel("Error connecting IO server") # create the error message label
err_msg.setWordWrap(True) # allow it to wrap over several lines
err_msg.setAlignment(Qt.AlignCenter) # center it horizontally
vbox.addWidget(err_msg) # attach it to the main output area
else:
# initialization went fine. So the main gui
# is being drawn
button = QPushButton("Toggle O1") # create a button labeled "Toggle O1"
button.clicked.connect(self.on_button_clicked) # connect button to event handler
vbox.addWidget(button) # attach it to the main output area
# configure all TXT outputs to normal mode
M = [ self.txt.C_OUTPUT, self.txt.C_OUTPUT, self.txt.C_OUTPUT, self.txt.C_OUTPUT ]
I = [ (self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ),
(self.txt.C_SWITCH, self.txt.C_DIGITAL ) ]
self.txt.setConfig(M, I)
self.txt.updateConfig()
# initially switch light on
self.light_on = True # remember that the light is on
self.txt.setPwm(0,512) # set PWm to 512 (full on)
self.timer = QTimer(self) # create a timer
self.timer.timeout.connect(self.on_timer) # connect timer to on_timer slot
self.timer.start(100); # fire timer every 100ms (10 hz)
self.button_state = False # assume initually the button is not pressed
w.centralWidget.setLayout(vbox)
w.show()
self.exec_()
def report(self):
self.producer.send('txt1', b"Bin_da")
def toggle_light(self):
self.report()
self.light_on = not self.light_on # change state
if self.light_on: # set output accordingly
self.txt.setPwm(0,512) # PWN=512 means full on
else:
self.txt.setPwm(0,0) # PWM=0 means off
# an event handler for our button (called a "slot" in qt)
# it will be called whenever the user clicks the button
def on_button_clicked(self):
self.toggle_light()
# an event handler for the timer (also a qt slot)
def on_timer(self):
self.report()
# check if input state differs from saved state
if self.button_state != self.txt.getCurrentInput(0):
# change saved state to reflect input state
self.button_state = not self.button_state
# toggle lamp state if button has been pressed
if self.button_state:
self.toggle_light()
if __name__ == "__main__":
FtcGuiApplication(sys.argv)
Ergebnis ist: Es passiert einfach gar nichts. Das Webinterface sagt das es läuft. Passieren tut nichts. Komme ich eigentlich irgendwie an die Ausgabe des Interpreters?
Danke schonmal für eure Hilfe!