/
distance.py
108 lines (93 loc) · 3.25 KB
/
distance.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'''
Copyright (©) 2019 - Randall Simpson
pi-iot
This class is used to gather distance sensor metrics from a raspberry pi.
'''
from sensor import Sensor
from metric import Metric
import datetime
import sys
import RPi.GPIO as GPIO
import time
import logging
logger = logging.getLogger('root')
class Distance(Sensor):
def __init__(self, source, metric_prefix, output, code, pin_trigger, pin_echo, format):
Sensor.__init__(self, source, metric_prefix, output)
self.pin_trigger = pin_trigger
self.pin_echo = pin_echo
self.format = format
self.tag_label = code
self.initPins()
def __del__(self):
#destructor to clean up Pins.
GPIO.cleanup()
def initPins(self):
#init pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pin_trigger, GPIO.OUT)
GPIO.setup(self.pin_echo, GPIO.IN)
GPIO.output(self.pin_trigger, GPIO.LOW)
#allow pins to initialize
time.sleep(2)
def reset(self):
#clean up first
try:
GPIO.cleanup()
finally:
self.initPins()
def get_info(self):
distance = None
try:
date = datetime.datetime.utcnow()
GPIO.output(self.pin_trigger, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.pin_trigger, GPIO.LOW)
while GPIO.input(self.pin_echo) == 0:
pulse_start_time = time.time()
while GPIO.input(self.pin_echo) == 1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
except Exception as e:
logger.error(f'Error in distance: {e}')
self.reset()
if distance is not None:
name = self.metric_prefix + 'distance'
if self.output == 'WF':
name = 'Distance'
if len(self.metric_prefix) > 0:
name = self.metric_prefix + '.' + name
if self.format == 'f':
distance = distance / 30.48
if self.format == 'i':
distance = distance / 30.48 * 12
self.metrics.append(Metric(name, distance, date))
def format_metrics(self):
rtnMetrics = []
while len(self.metrics) > 0:
metric = self.metrics.pop()
if self.output == 'WF':
tags = {}
tags['Pin-Trigger'] = self.pin_trigger
tags['Pin-Echo'] = self.pin_echo
tags['Sensor'] = self.tag_label
detail = {}
detail['value'] = metric.value
detail['tags'] = tags
m = {}
m[metric.name] = detail
rtnMetrics.append(m)
else:
m = {}
m['date'] = metric.date.isoformat() + 'Z'
m['metric'] = metric.name
m['value'] = metric.value
m['source'] = self.source
tags = {}
tags['pin-trigger'] = self.pin_trigger
tags['pin-echo'] = self.pin_echo
tags['sensor'] = self.tag_label
m['tags'] = tags
rtnMetrics.append(m)
return rtnMetrics