-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloggers.py
88 lines (66 loc) · 2.32 KB
/
loggers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from urllib.request import urlopen
class HealthChecks:
"""Ping a HealthChecks.io URL for each event"""
def __init__(self, url, test_mode=False):
"""
Setup a logger with the specified URL
:param url: the URL to log to on each event
"""
self.url = url
self.test_mode = test_mode
def log(self, event):
"""
Log the event to the specified URL via GET method
:param event: the event
"""
print("Logging to %s:" % self.url)
print("Event: %s" % event)
if not self.test_mode:
urlopen(self.url)
class ThingSpeak:
"""ThingSpeak channel used to log pump on/off events"""
def __init__(self, api_key, test_mode=False):
"""
Setup a ThingSpeak channel with the specified API key
Args:
api_key - write API key to update the channel
test_mode - enable/disable logging to the remote service
"""
self.api_key = api_key
self.test_mode = test_mode
def log(self, event):
"""
Log the event to the ThingSpeak channel (timestamp is inferred by ThingSpeak)
:arg
event - a list of fields to log. Can be empty.
:return url used to log to ThingSpeak (includes API key and list of fields)
"""
url = "https://api.thingspeak.com/update?api_key=" + self.api_key
for i, field in enumerate(event):
url += "&field%d=%s" % (i + 1, field)
if not self.test_mode:
urlopen(url)
return url
class ConsoleLogger:
def log(self, event):
print("Event: %s" % event)
class AlarmClock:
"""Logger that allows alarms to be attached for specific events"""
def __init__(self):
"""
Initialise an AlarmClock
"""
self.events = []
self.alarms = []
def add_alarm(self, trigger, alarm):
"""
Add an an alarm and trigger to the alarm clock.
:param trigger: a function that takes a list of events, and returns True or False
:param alarm: a function invoked if the trigger is met
"""
self.alarms.append((trigger, alarm))
def log(self, event):
self.events.append(event)
for trigger, alarm in self.alarms:
if trigger(self.events):
alarm()