Commit 960be52eaf717900580a587deea4cef943208c06
1 parent
535edd78a8
Exists in
main
tested slackbot, ready to deploy
Showing 2 changed files with 19 additions and 17 deletions Side-by-side Diff
ssa/slackbot/slackbot.py
View file @
960be52
... | ... | @@ -7,7 +7,7 @@ |
7 | 7 | import configparser |
8 | 8 | import influxdb |
9 | 9 | import time |
10 | -import datetime | |
10 | +import dateutil.parser | |
11 | 11 | import requests |
12 | 12 | |
13 | 13 | PUMP1_FLOW_QUERY = """ |
... | ... | @@ -22,7 +22,7 @@ |
22 | 22 | def __init__(self): |
23 | 23 | self.config = configparser.ConfigParser() |
24 | 24 | self.config.read("slackbot_config.ini") |
25 | - self.sleep_time_sec = int(self.config.get("update_period_minutes", "30")) * 60 | |
25 | + self.sleep_time_sec = int(self.config["general"]["update_period_minutes"]) * 60 | |
26 | 26 | |
27 | 27 | def run(self): |
28 | 28 | while True: |
29 | 29 | |
30 | 30 | |
31 | 31 | |
32 | 32 | |
33 | 33 | |
34 | 34 | |
35 | 35 | |
... | ... | @@ -31,36 +31,37 @@ |
31 | 31 | |
32 | 32 | def post_update(self): |
33 | 33 | # poll latest data |
34 | - with influxdb.InfluxDBClient( | |
34 | + idb_client = influxdb.InfluxDBClient( | |
35 | 35 | self.config["influxdb"].get("host", "localhost"), |
36 | 36 | self.config["influxdb"].get("port", "8086"), |
37 | 37 | self.config["influxdb"].get("username", None), |
38 | 38 | self.config["influxdb"].get("password", None), |
39 | 39 | self.config["influxdb"].get("dbname", "paddle"), |
40 | - ) as idb_client: | |
41 | - result1 = idb_client.query(PUMP1_FLOW_QUERY) | |
42 | - (pump1, time1) = self.parse_pump_query_result(result1, "Pump1") | |
43 | - result2 = idb_client.query(PUMP2_FLOW_QUERY) | |
44 | - (pump2, time2) = self.parse_pump_query_result(result2, "Pump2") | |
40 | + ) | |
41 | + result1 = idb_client.query(PUMP1_FLOW_QUERY) | |
42 | + (pump1, time1) = self.parse_pump_query_result(result1, "Pump1") | |
43 | + result2 = idb_client.query(PUMP2_FLOW_QUERY) | |
44 | + (pump2, time2) = self.parse_pump_query_result(result2, "Pump2") | |
45 | + idb_client.close() | |
45 | 46 | |
46 | 47 | # post to slack channel |
47 | - dtime1 = datetime.datetime.fromisoformat(time1) | |
48 | - dtime2 = datetime.datetime.fromisoformat(time2) | |
49 | - 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")})" | |
48 | + # parse iso times and convert to local timezone | |
49 | + dtime1 = dateutil.parser.isoparse(time1).astimezone() | |
50 | + dtime2 = dateutil.parser.isoparse(time2).astimezone() | |
51 | + 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')})" | |
50 | 52 | |
51 | 53 | json_message = { |
52 | - "channel": self.config["slack"].get("channel_id"), | |
54 | + "channel": self.config["slack"]["channel_id"], | |
53 | 55 | "text": message, |
54 | 56 | } |
55 | - | |
57 | + | |
56 | 58 | headers = { |
57 | - "Authorization": f"Bearer {self.config["slack"].get("token")}", | |
58 | - "Content-type": "application/json" | |
59 | + "Authorization": f"Bearer {self.config['slack']['token']}", | |
60 | + "Content-type": "application/json", | |
59 | 61 | } |
60 | - post_url = self.config["slack"].get("post_url") | |
62 | + post_url = self.config["slack"]["post_url"] | |
61 | 63 | |
62 | 64 | requests.post(url=post_url, headers=headers, json=json_message) |
63 | - | |
64 | 65 | |
65 | 66 | def parse_pump_query_result(self, result, name): |
66 | 67 | data = list(result.get_points(name))[0] |