blink_tm 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 290de1234088715cb8d957462afdd3a8f54c1c876f2b49ae35de7546cb352da8
4
- data.tar.gz: e4a103649d1c326c86eb74931ca4a4796e150c186e9f6ef05926410d33ab31b2
3
+ metadata.gz: 0b0b02fc01c3e3de3cfd449cfb597d1668ace99bc1cbfff09d3851dd1697d6b8
4
+ data.tar.gz: eb28af7d601f618b53fd65fd288963cfa84018437532a8cf8aeb651e1871efe6
5
5
  SHA512:
6
- metadata.gz: 7641d9d08838db8707dd75a327a417090ef0c0ecdddf0f458f34c5caeffb1406dde99272db73ad30d5e3ef288b348534b5834e09365ead845b4db34efa598c5d
7
- data.tar.gz: 119773e1b395a46910267f6bee5f6c284a7bb3208953c40649c1d3981f31f6508f39b202d983a658ffedb7988b3dc4082be86c162df01b0063a0bf373352d4c9
6
+ metadata.gz: 64e11a3838a4c77219d888587407bf2b63e5210fec99e257519375b582e1b113e859d5785a20dff056a5a8757ab35dfbc483a5ee3063de56982f462e38f8d3cc
7
+ data.tar.gz: 63ffe2ecb618d3ed602ee46e19005103fba2bb9b6873a882d1820e39402f9136358914e4047d21fd8bf67f7e50d3a348922dd0809e60cdac52a35cb3e7fd16c6
data/exe/blink-tm CHANGED
@@ -7,6 +7,19 @@ if ARGV.any?(/^\-(\-version|v)$/)
7
7
  exit
8
8
  end
9
9
 
10
+ coords = ARGV.select { |x| x[/^\-(\-coords|c)=.+$/] }.last
11
+ daylight_checker_options = { latitude: nil, longitude: nil }
12
+
13
+ if coords && !coords.empty?
14
+ lat_long = coords.split('=').last
15
+ lat, long = lat_long.split(',')
16
+
17
+ abort('Please specify latitude and longitude splitted by a comma (,). For example -22.88,88.33') if !lat || !long
18
+
19
+ daylight_checker_options[:latitude] = lat
20
+ daylight_checker_options[:longitude] = long
21
+ end
22
+
10
23
  written_pid = false
11
24
 
12
25
  BlinkTM::log "BlinkTM Version #{BlinkTM::VERSION}"
@@ -47,11 +60,11 @@ end
47
60
  retry_count = 0
48
61
 
49
62
  # Find Device running Blink Taskmanager
50
- dev = BlinkTM::find_device!
51
63
  file = nil
52
64
 
53
65
  begin
54
- BlinkTM.start(dev) { |x| file = x }
66
+ dev = BlinkTM::find_device!
67
+ BlinkTM.start(dev, daylight_checker_options) { |x| file = x }
55
68
  rescue BlinkTM::SyncError
56
69
  sleep 1
57
70
  retry
@@ -62,9 +75,7 @@ rescue Errno::ENOENT, Errno::EIO, BlinkTM::NoDeviceError
62
75
  retry
63
76
  rescue Interrupt, SystemExit, SignalException
64
77
  file &.close
65
- File.delete(BlinkTM::LOCKFILE)
66
78
  rescue Exception
67
-
68
79
  mesg = <<~EOF
69
80
  :: Time: #{Time.now.strftime("%A %d %B %Y, %H:%M:%S:%5N %p")}:
70
81
  #{$!.full_message}
@@ -74,11 +74,15 @@ module BlinkTM
74
74
  "%06.2f".%(n).split('.').join
75
75
  end
76
76
 
77
- def start(device)
77
+ def start(device, daylight_checker_options)
78
78
  return false unless device
79
79
 
80
+ latitude, longitude = daylight_checker_options[:latitude]&.to_f, daylight_checker_options[:longitude]&.to_f
81
+ log 'success', "Set latitude to #{latitude}, longitude to #{longitude}" if !latitude.nil? && !longitude.nil?
82
+
80
83
  cpu_u = mem_u = swap_u = iostat = net_u = net_d = 0
81
84
  io_r = io_w = 0
85
+ built_in_led_state = 0
82
86
 
83
87
  Thread.new {
84
88
  while true
@@ -107,6 +111,7 @@ module BlinkTM
107
111
  }
108
112
 
109
113
  prev_crc32 = ''
114
+ prev_time = { min: -1, hour: -1 }
110
115
  raise NoDeviceError unless device
111
116
 
112
117
  in_sync = false
@@ -157,6 +162,22 @@ module BlinkTM
157
162
  _swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024)
158
163
  swap_u = _swap_u.nan? ? 255 : _swap_u.round
159
164
 
165
+ time_minute = Time.now.min
166
+ time_hour = Time.now.hour
167
+
168
+ if (time_minute != prev_time[:min] || time_hour != prev_time[:hour]) && !latitude.nil? && !longitude.nil?
169
+ dark_outside = DaylightChecker.new(
170
+ latitude,
171
+ longitude,
172
+ Time.now
173
+ ).is_it_dark?
174
+
175
+ built_in_led_state = dark_outside ? 1 : 0
176
+
177
+ prev_time[:min] = time_minute
178
+ prev_time[:hour] = time_hour
179
+ end
180
+
160
181
  # Output has to be exactly this long. If not, blink-taskmanager shows invalid result.
161
182
  # No string is split inside blink-task manager, it just depends on the string length.
162
183
  #
@@ -171,7 +192,7 @@ module BlinkTM
171
192
 
172
193
  str = "!##{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
173
194
  "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
174
- "#{convert_bytes(io_r)}#{convert_bytes(io_w)}1~"
195
+ "#{convert_bytes(io_r)}#{convert_bytes(io_w)}#{built_in_led_state}1~"
175
196
 
176
197
  # Rescuing from suspend
177
198
  file.syswrite(str)
@@ -0,0 +1,104 @@
1
+ require 'date'
2
+
3
+ class DaylightChecker
4
+ attr_reader :latitude, :longitude, :time, :date, :offset_in_hours
5
+ # Each day earth moves across sun about 360 / 365.24 = 0.9856
6
+ EARTH_ORBITAL_DEGREE_PER_DAY = 0.9856
7
+
8
+ # Is a constant that adjusts the mean anomaly based on the time of year.
9
+ # It's derived from a more complex formula involving the Earth's orbital eccentricity
10
+ # and the position of perihelion (the point in Earth's orbit that's closest to the Sun).
11
+ MEAN_ANOMALY_ADJUSTMENT = 3.289
12
+
13
+ AXIAL_TILT = 0.39782
14
+ TIME_CORRECTION_FACTOR = 0.06571
15
+ DEGREES_TO_RADIANS = Math::PI / 180
16
+ RADIANS_TO_DEGREES = 180 / Math::PI
17
+
18
+ # Math.cos(23.44 (angle in degrees) * (Math::PI / 180))
19
+ COSINE_OBLIQUITY_ECLIPTIC = 0.91747
20
+
21
+ def initialize(latitude, longitude, time)
22
+ @latitude = latitude
23
+ @longitude = longitude
24
+ @time = time
25
+ @date = time.to_date
26
+ @offset_in_hours = time.utc_offset / 3600.0
27
+ end
28
+
29
+ def is_it_dark?
30
+ sunrise_time, sunset_time = sun_times(date, latitude, longitude, offset_in_hours)
31
+ current_time = time.utc.hour + offset_in_hours
32
+
33
+ current_time < sunrise_time or current_time > sunset_time
34
+ end
35
+
36
+ private
37
+
38
+ def sun_times(date, latitude, longitude, offset_in_hours)
39
+ # Calculation of the sun rise/set is based on the papers:
40
+ # http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
41
+ # http://aa.quae.nl/en/reken/zonpositie.html
42
+
43
+ # Convert the longitude to hour value and calculate an approximate time
44
+ lng_hour = longitude / 15.0
45
+
46
+ t_rise = date.yday + ((6 - lng_hour) / 24)
47
+ t_set = date.yday + ((18 - lng_hour) / 24)
48
+
49
+ # Calculate the Sun's mean anomaly
50
+ m_rise = (EARTH_ORBITAL_DEGREE_PER_DAY * t_rise) - MEAN_ANOMALY_ADJUSTMENT
51
+ m_set = (EARTH_ORBITAL_DEGREE_PER_DAY * t_set) - MEAN_ANOMALY_ADJUSTMENT
52
+
53
+ # Calculate the Sun's true longitude, and adjust angle to be between 0 and 360
54
+ l_rise = (m_rise + (1.916 * Math.sin(m_rise * DEGREES_TO_RADIANS)) + (0.020 * Math.sin(2 * m_rise * DEGREES_TO_RADIANS)) + 282.634) % 360
55
+ l_set = (m_set + (1.916 * Math.sin(m_set * DEGREES_TO_RADIANS)) + (0.020 * Math.sin(2 * m_set * DEGREES_TO_RADIANS)) + 282.634) % 360
56
+
57
+ # Calculate the Sun's right ascension, and adjust angle to be between 0 and 360
58
+ ra_rise = RADIANS_TO_DEGREES * Math.atan(COSINE_OBLIQUITY_ECLIPTIC * Math.tan(l_rise * DEGREES_TO_RADIANS))
59
+ ra_rise = (ra_rise + 360) % 360
60
+ ra_set = RADIANS_TO_DEGREES * Math.atan(COSINE_OBLIQUITY_ECLIPTIC * Math.tan(l_set * DEGREES_TO_RADIANS))
61
+ ra_set = (ra_set + 360) % 360
62
+
63
+ # Right ascension value needs to be in the same quadrant as L
64
+ l_quadrant_rise = (l_rise / 90).floor * 90
65
+ ra_quadrant_rise = (ra_rise / 90).floor * 90
66
+ ra_rise = ra_rise + (l_quadrant_rise - ra_quadrant_rise)
67
+
68
+ l_quadrant_set = (l_set / 90).floor * 90
69
+ ra_quadrant_set = (ra_set / 90).floor * 90
70
+ ra_set = ra_set + (l_quadrant_set - ra_quadrant_set)
71
+
72
+ # Right ascension value needs to be converted into hours
73
+ ra_rise = ra_rise / 15
74
+ ra_set = ra_set / 15
75
+
76
+ # Calculate the Sun's declination
77
+ sin_dec_rise = AXIAL_TILT * Math.sin(l_rise * DEGREES_TO_RADIANS)
78
+ cos_dec_rise = Math.cos(Math.asin(sin_dec_rise))
79
+
80
+ sin_dec_set = AXIAL_TILT * Math.sin(l_set * DEGREES_TO_RADIANS)
81
+ cos_dec_set = Math.cos(Math.asin(sin_dec_set))
82
+
83
+ # Calculate the Sun's local hour angle
84
+ cos_h_rise = (Math.sin(-0.83 * DEGREES_TO_RADIANS) - (sin_dec_rise * Math.sin(latitude * DEGREES_TO_RADIANS))) / (cos_dec_rise * Math.cos(latitude * DEGREES_TO_RADIANS))
85
+ h_rise = (360 - RADIANS_TO_DEGREES * Math.acos(cos_h_rise)) / 15
86
+
87
+ cos_h_set = (Math.sin(-0.83 * DEGREES_TO_RADIANS) - (sin_dec_set * Math.sin(latitude * DEGREES_TO_RADIANS))) / (cos_dec_set * Math.cos(latitude * DEGREES_TO_RADIANS))
88
+ h_set = RADIANS_TO_DEGREES * Math.acos(cos_h_set) / 15
89
+
90
+ # Calculate local mean time of rising/setting
91
+ t_rise = h_rise + ra_rise - (TIME_CORRECTION_FACTOR * t_rise) - 6.622
92
+ t_set = h_set + ra_set - (TIME_CORRECTION_FACTOR * t_set) - 6.622
93
+
94
+ # Adjust back to UTC, and keep the value between 0 and 24
95
+ ut_rise = (t_rise - lng_hour) % 24
96
+ ut_set = (t_set - lng_hour) % 24
97
+
98
+ # Convert UT value to local time zone of latitude/longitude
99
+ local_t_rise = (ut_rise + offset_in_hours) % 24
100
+ local_t_set = (ut_set + offset_in_hours) % 24
101
+
102
+ return local_t_rise, local_t_set
103
+ end
104
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlinkTM
4
- VERSION = "2.1.0"
4
+ VERSION = "2.2.0"
5
5
  end
data/lib/blink_tm.rb CHANGED
@@ -49,5 +49,6 @@ end
49
49
 
50
50
  require 'blink_tm/version'
51
51
  require 'fcntl'
52
+ require 'blink_tm/daylight_checker'
52
53
  require 'blink_tm/blink_tm'
53
54
  require 'blink_tm/crc32'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blink_tm
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-16 00:00:00.000000000 Z
11
+ date: 2023-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: linux_stat
@@ -41,6 +41,7 @@ files:
41
41
  - ext/crc32/extconf.rb
42
42
  - lib/blink_tm.rb
43
43
  - lib/blink_tm/blink_tm.rb
44
+ - lib/blink_tm/daylight_checker.rb
44
45
  - lib/blink_tm/version.rb
45
46
  homepage: https://github.com/souravgoswami/blink-tm
46
47
  licenses:
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  - !ruby/object:Gem::Version
62
63
  version: '0'
63
64
  requirements: []
64
- rubygems_version: 3.3.8
65
+ rubygems_version: 3.3.25
65
66
  signing_key:
66
67
  specification_version: 4
67
68
  summary: A controller for Arduino OLED System Monitor, Blink Task Manager