blink_tm 2.0.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: 941ae55ced42e5243dd9a489783f1b02da24fcd9c29a9cf4e3f310ea26080fbc
4
- data.tar.gz: bf0c03acd5b71bddd03e0e717b8c1f12acd824668eb6e2e56948db9f055a9a67
3
+ metadata.gz: 0b0b02fc01c3e3de3cfd449cfb597d1668ace99bc1cbfff09d3851dd1697d6b8
4
+ data.tar.gz: eb28af7d601f618b53fd65fd288963cfa84018437532a8cf8aeb651e1871efe6
5
5
  SHA512:
6
- metadata.gz: 00543e80f694b6a711f55aeacd7ab7175675273b513af336b3e3307c73a07ea21f9960a41a81bd285cd6023ff35fa2a2979552f0a7725d1580328808989ac2e6
7
- data.tar.gz: 03f51e941ee7531fd2dd29500383abe4d0d88af40b25208d2b2d5991fccae3a3b67954e2efa7aa6eacbb5ca1690230037220cde6d66ef3c5281385fc4ef1d8bb
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,7 +75,23 @@ 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
79
+ mesg = <<~EOF
80
+ :: Time: #{Time.now.strftime("%A %d %B %Y, %H:%M:%S:%5N %p")}:
81
+ #{$!.full_message}
82
+
83
+ Please notify these bugs to the issue tracker.
84
+ #{'-' * 70}
85
+ EOF
86
+
87
+ if File.writable?(BlinkTM::LOGFILE)
88
+ log = File.open(BlinkTM::LOGFILE, 'a')
89
+ log.write(mesg)
90
+ log.close
91
+ else
92
+ puts "Logs aren't writable\n\nHere's a detailed message of what has happened:"
93
+ puts mesg
94
+ end
95
+ ensure
67
96
  File.delete(BlinkTM::LOCKFILE)
68
97
  end
@@ -74,16 +74,20 @@ 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
85
89
  _cpu_u = LS::CPU.total_usage(POLLING).to_f
86
- cpu_u = _cpu_u.nan? ? 0 : _cpu_u.to_i
90
+ cpu_u = _cpu_u.nan? ? 255 : _cpu_u.to_i
87
91
  end
88
92
  }
89
93
 
@@ -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
@@ -150,10 +155,28 @@ module BlinkTM
150
155
  # disktotal(999990) diskused(999990)
151
156
 
152
157
  memstat = LS::Memory.stat
153
- mem_u = memstat[:used].to_i.*(1024).*(100).fdiv(memstat[:total].to_i * 1024).round
158
+ _mem_u = memstat[:used].to_i.*(1024).*(100).fdiv(memstat[:total].to_i * 1024)
159
+ mem_u = _mem_u.nan? ? 255 : _mem_u.round
154
160
 
155
161
  swapstat = LS::Swap.stat
156
- swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024).round
162
+ _swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024)
163
+ swap_u = _swap_u.nan? ? 255 : _swap_u.round
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
157
180
 
158
181
  # Output has to be exactly this long. If not, blink-taskmanager shows invalid result.
159
182
  # No string is split inside blink-task manager, it just depends on the string length.
@@ -163,13 +186,13 @@ module BlinkTM
163
186
  # ioWrite(9991) ioRead(9991)
164
187
 
165
188
  # Debugging string
166
- str = "#{"%03d" % cpu_u} #{"%03d" % mem_u} #{"%03d" % swap_u} "\
167
- "#{convert_bytes(net_u)} #{convert_bytes(net_d)} "\
168
- "#{convert_bytes(io_r)} #{convert_bytes(io_w)}"
189
+ # str = "#{"%03d" % cpu_u} #{"%03d" % mem_u} #{"%03d" % swap_u} "\
190
+ # "#{convert_bytes(net_u)} #{convert_bytes(net_d)} "\
191
+ # "#{convert_bytes(io_r)} #{convert_bytes(io_w)}"
169
192
 
170
193
  str = "!##{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
171
194
  "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
172
- "#{convert_bytes(io_r)}#{convert_bytes(io_w)}1~"
195
+ "#{convert_bytes(io_r)}#{convert_bytes(io_w)}#{built_in_led_state}1~"
173
196
 
174
197
  # Rescuing from suspend
175
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.0.0"
4
+ VERSION = "2.2.0"
5
5
  end
data/lib/blink_tm.rb CHANGED
@@ -35,6 +35,7 @@ module BlinkTM
35
35
  BOLD = "\e[1m"
36
36
  RESET = "\e[0m"
37
37
  LOCKFILE = '/tmp/blinktaskmanager.pid'
38
+ LOGFILE = '/tmp/blinktaskmanager.err.log'
38
39
 
39
40
  # Other constants
40
41
  ROOT_DEV = ::LinuxStat::Mounts.root
@@ -48,5 +49,6 @@ end
48
49
 
49
50
  require 'blink_tm/version'
50
51
  require 'fcntl'
52
+ require 'blink_tm/daylight_checker'
51
53
  require 'blink_tm/blink_tm'
52
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.0.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: 2021-12-24 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.2.29
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