-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnightscout.1m.rb
executable file
·103 lines (88 loc) · 2.35 KB
/
nightscout.1m.rb
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
#!/usr/bin/env ruby
# <xbar.title>Nightscout display</xnar.title>
# <xbar.version>v2.1.7</xbar.version>
# <xbar.desc>Display your glucose levels from the Nightscout site</xbar.desc>
# <xbar.dependencies>ruby</xbar.dependencies>
# <xbar.author>Luismi Ramirez</xbar.author>
# <xbar.author.github>luismiramirez</xbar.author.github>
require 'json'
require 'net/https'
require 'uri'
# Edit these values with yours
SITE = 'https://your-nightscout-site.example/'
TOKEN = 'your-token'
UNIT = 'mg/dl'
LIMITS = {
'mg/dl' => {
very_high: 260,
high: 180,
low: 70,
very_low: 55
},
'mmol/L' => {
very_high: 14.4,
high: 10,
low: 3.9,
very_low: 3.1
}
}
NIGHTSCOUT_URI = URI(
"#{SITE}/api/v3/entries/history?token=#{TOKEN}&limit=2"
).freeze
TEN_MINUTES_AGO = Time.now.utc - 600
def request_data
req = Net::HTTP::Get.new(NIGHTSCOUT_URI.request_uri)
req['Last-Modified'] = TEN_MINUTES_AGO
http_client.request(req)
rescue
puts 'Error connecting to Nightscout'
exit 0
end
def http_client
http = Net::HTTP.new(NIGHTSCOUT_URI.host, NIGHTSCOUT_URI.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http
end
def parse_values(parsed_response)
{
previous: parsed_response.first['sgv'],
current: parsed_response.last['sgv'],
direction: direction_map.fetch(parsed_response.last['direction'], '')
}
end
def calculate_delta(previous, current)
delta = current - previous
return delta if delta.zero?
operator = delta.positive? ? '+' : '-'
"#{operator} #{delta.abs}"
end
def direction_map
{
'FortyFiveUp' => '↗',
'FortyFiveDown' => '↘',
'SingleUp' => '↑',
'SingleDown' => '↓',
'Flat' => '→',
'DoubleUp' => '⇈',
'DoubleDown' => '⇊',
'NotComputable' => '-'
}
end
def determine_icon(current_value)
limits_by_unit = LIMITS[UNIT]
if current_value >= limits_by_unit[:very_high] || current_value <= limits_by_unit[:very_low]
'🚨'
elsif current_value >= limits_by_unit[:high] || current_value <= limits_by_unit[:low]
'⚠️'
else
'👍'
end
end
response = request_data
values = parse_values(JSON[response.body]['result'])
delta = calculate_delta(values[:previous], values[:current])
icon = determine_icon(values[:current])
puts [icon, values[:current], values[:direction], delta, UNIT].join(' ')
puts '---'
puts "Your Nightscout site | href=#{SITE}"