forked from info-beamer/36c3-cms
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inform users hourly about assets that need attention
- Loading branch information
Showing
3 changed files
with
108 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from json import dumps | ||
from logging import getLogger | ||
|
||
import paho.mqtt.client as mqtt | ||
|
||
from conf import CONFIG | ||
|
||
LOG = getLogger("MQTT") | ||
|
||
|
||
def send_message(*args, **kwargs): | ||
c = MQTT() | ||
r = c.send_message(*args, **kwargs) | ||
c.disconnect() | ||
return r | ||
|
||
|
||
class MQTT: | ||
def __init__(self): | ||
self.client = mqtt.Client() | ||
if CONFIG.get("MQTT_USERNAME") and CONFIG.get("MQTT_PASSWORD"): | ||
self.client.username_pw_set( | ||
CONFIG["MQTT_USERNAME"], CONFIG["MQTT_PASSWORD"] | ||
) | ||
self.client.connect(CONFIG["MQTT_SERVER"]) | ||
LOG.info("connected to mqtt server") | ||
|
||
def disconnect(self): | ||
self.client.disconnect() | ||
|
||
def send_message(self, message, level="INFO", component=None): | ||
comp = "infobeamer-cms" | ||
if component is not None: | ||
comp = f"{comp}/{component}" | ||
msg = dumps( | ||
{ | ||
"level": level, | ||
"component": comp, | ||
"msg": message, | ||
} | ||
) | ||
LOG.info(f"sending message: {msg}") | ||
r = self.client.publish( | ||
CONFIG["MQTT_TOPIC"], | ||
msg, | ||
) | ||
LOG.info(f"sent message: {r!r}") | ||
return r |