Commit 1594fa6e3677d248923940107657b49e8cb34558

Authored by Greg Sandstrom
1 parent 8be919fec4
Exists in main

add ctr temperature, units to all slackbot messages

Showing 1 changed file with 11 additions and 4 deletions Inline Diff

ssa/slackbot/slackbot.py View file @ 1594fa6
""" 1 1 """
slackbot.py 2 2 slackbot.py
3 3
Use slack app integration to post messages via the slack api 4 4 Use slack app integration to post messages via the slack api
""" 5 5 """
6 6
import configparser 7 7 import configparser
import influxdb 8 8 import influxdb
import time 9 9 import time
import dateutil.parser 10 10 import dateutil.parser
import requests 11 11 import requests
12 12
PUMP1_FLOW_QUERY = """ 13 13 PUMP1_FLOW_QUERY = """
SELECT last(value) as value From Pump1 14 14 SELECT last(value) as value From Pump1
""" 15 15 """
PUMP2_FLOW_QUERY = """ 16 16 PUMP2_FLOW_QUERY = """
SELECT last(value) as value From Pump2 17 17 SELECT last(value) as value From Pump2
""" 18 18 """
19 19
20 TEMPERATURE_QUERY = """
21 SELECT last(value) as value from ChannelWaterCTR
22 """
20 23
24
class SlackBot: 21 25 class SlackBot:
def __init__(self): 22 26 def __init__(self):
self.config = configparser.ConfigParser() 23 27 self.config = configparser.ConfigParser()
self.config.read("slackbot_config.ini") 24 28 self.config.read("slackbot_config.ini")
self.sleep_time_sec = int(self.config["general"]["update_period_minutes"]) * 60 25 29 self.sleep_time_sec = int(self.config["general"]["update_period_minutes"]) * 60
26 30
def run(self): 27 31 def run(self):
while True: 28 32 while True:
self.post_update() 29 33 self.post_update()
time.sleep(self.sleep_time_sec) 30 34 time.sleep(self.sleep_time_sec)
31 35
def post_update(self): 32 36 def post_update(self):
# poll latest data 33 37 # poll latest data
idb_client = influxdb.InfluxDBClient( 34 38 idb_client = influxdb.InfluxDBClient(
self.config["influxdb"].get("host", "localhost"), 35 39 self.config["influxdb"].get("host", "localhost"),
self.config["influxdb"].get("port", "8086"), 36 40 self.config["influxdb"].get("port", "8086"),
self.config["influxdb"].get("username", None), 37 41 self.config["influxdb"].get("username", None),
self.config["influxdb"].get("password", None), 38 42 self.config["influxdb"].get("password", None),
self.config["influxdb"].get("dbname", "paddle"), 39 43 self.config["influxdb"].get("dbname", "paddle"),
) 40 44 )
result1 = idb_client.query(PUMP1_FLOW_QUERY) 41 45 result1 = idb_client.query(PUMP1_FLOW_QUERY)
(pump1, time1) = self.parse_pump_query_result(result1, "Pump1") 42 46 (pump1, time1) = self.parse_query_result(result1, "Pump1")
result2 = idb_client.query(PUMP2_FLOW_QUERY) 43 47 result2 = idb_client.query(PUMP2_FLOW_QUERY)
(pump2, time2) = self.parse_pump_query_result(result2, "Pump2") 44 48 (pump2, time2) = self.parse_query_result(result2, "Pump2")
49 result3 = idb_client.query(TEMPERATURE_QUERY)
50 (ctr_temp, time3) = self.parse_query_result(result3, "ChannelWaterCTR")
idb_client.close() 45 51 idb_client.close()
46 52
# post to slack channel 47 53 # post to slack channel
# parse iso times and convert to local timezone 48 54 # parse iso times and convert to local timezone
dtime1 = dateutil.parser.isoparse(time1).astimezone() 49 55 dtime1 = dateutil.parser.isoparse(time1).astimezone()
dtime2 = dateutil.parser.isoparse(time2).astimezone() 50 56 dtime2 = dateutil.parser.isoparse(time2).astimezone()
message = f"Pump1: {pump1:4.1f} ({dtime1.strftime('%d %b %Y, %I:%M%p')})\nPump2: {pump2:4.1f} ({dtime2.strftime('%d %b %Y, %I:%M%p')})" 51 57 dtime3 = dateutil.parser.isoparse(time3).astimezone()
58 message = f"Pump1: {pump1:4.1f}GPM ({dtime1.strftime('%d %b %Y, %I:%M%p')})\nPump2: {pump2:4.1f}GPM ({dtime2.strftime('%d %b %Y, %I:%M%p')})\nCTR Temperature: {ctr_temp:4.1f}C ({dtime3.strftime('%d %b %Y, %I:%M%p')})"
52 59
json_message = { 53 60 json_message = {
"channel": self.config["slack"]["channel_id"], 54 61 "channel": self.config["slack"]["channel_id"],
"text": message, 55 62 "text": message,
} 56 63 }
57 64
headers = { 58 65 headers = {
"Authorization": f"Bearer {self.config['slack']['token']}", 59 66 "Authorization": f"Bearer {self.config['slack']['token']}",
"Content-type": "application/json", 60 67 "Content-type": "application/json",
} 61 68 }
post_url = self.config["slack"]["post_url"] 62 69 post_url = self.config["slack"]["post_url"]
63 70
requests.post(url=post_url, headers=headers, json=json_message) 64 71 requests.post(url=post_url, headers=headers, json=json_message)
65 72