blink_tm 1.0.0 → 2.0.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: '080b57c4c4dac231dbb6353428aaaf7be86bd47e4da2379308c23d9ca5315aaa'
4
- data.tar.gz: e13ccff20189b8e1e944230b0c27ce87272677a9d50ee2c289098800f1860fe9
3
+ metadata.gz: 941ae55ced42e5243dd9a489783f1b02da24fcd9c29a9cf4e3f310ea26080fbc
4
+ data.tar.gz: bf0c03acd5b71bddd03e0e717b8c1f12acd824668eb6e2e56948db9f055a9a67
5
5
  SHA512:
6
- metadata.gz: 1ae79bb6028b57c8046e1cf68ccd62fcac30ae38a75579d6402855e9dce94d012877cd358bd0b4a99dc6ddd1376ba3548a3ac88aa59867ea54d9c7bdd7e5f16c
7
- data.tar.gz: 4cc14d89f98e59675fa2071005cda0b44a4c41f421c5ea0ba850193363b33c922c623187c939f9ed48bd152277732ceb7022e3ccedc88cc13ba710c2d52157f4
6
+ metadata.gz: 00543e80f694b6a711f55aeacd7ab7175675273b513af336b3e3307c73a07ea21f9960a41a81bd285cd6023ff35fa2a2979552f0a7725d1580328808989ac2e6
7
+ data.tar.gz: 03f51e941ee7531fd2dd29500383abe4d0d88af40b25208d2b2d5991fccae3a3b67954e2efa7aa6eacbb5ca1690230037220cde6d66ef3c5281385fc4ef1d8bb
data/exe/blink-tm CHANGED
@@ -1,19 +1,68 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'blink_tm'
3
3
 
4
- # Find Device running Blink Taskmanager
5
- dev = nil
6
- retry_count = 0
7
4
 
8
- while(!dev)
9
- dev = BlinkTM.find_device
5
+ if ARGV.any?(/^\-(\-version|v)$/)
6
+ puts "BlinkTM Version #{BlinkTM::BOLD}#{BlinkTM::VERSION}#{BlinkTM::RESET}"
7
+ exit
8
+ end
9
+
10
+ written_pid = false
10
11
 
11
- if dev
12
- puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device discovered successfully. Path: #{dev}#{BlinkTM::RESET}"
13
- else
14
- puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No device found. Retrying #{retry_count += 1}#{BlinkTM::RESET}"
15
- sleep 0.5
12
+ BlinkTM::log "BlinkTM Version #{BlinkTM::VERSION}"
13
+ if File.exist?(BlinkTM::LOCKFILE)
14
+ BlinkTM.log 'error', "Lock file #{BlinkTM::LOCKFILE} already exists! If it's not running you might manually delete this to make blinktm work."
15
+
16
+ pid = IO.read(BlinkTM::LOCKFILE) if File.readable?(BlinkTM::LOCKFILE)
17
+ process_name = File.readable?("/proc/#{pid}/cmdline") ? IO.read("/proc/#{pid}/cmdline") : 'unknown'
18
+ stat = File.readable?("/proc/#{pid}/stat") ? IO.read("/proc/#{pid}/stat") : nil
19
+ state = stat ? stat.gsub(/\(.+\)/, '').split[1] : 'unknown'
20
+ friendly_state = case state
21
+ when ?S then 'Sleeping'
22
+ when ?I then 'Idle'
23
+ when ?Z then 'Zombie'
24
+ when ?R then 'Running'
25
+ when ?T then 'Stopped'
26
+ else 'Unknown'
27
+ end
28
+
29
+ BlinkTM.log 'info', <<~EOF
30
+ Process Details:
31
+ \t- ID: #{pid}
32
+ \t- Command: #{File.basename(process_name.gsub(?\u0000, ?\s))}
33
+ \t- Status: #{friendly_state}
34
+ EOF
35
+
36
+ exit!
37
+ else
38
+ begin
39
+ IO.write(BlinkTM::LOCKFILE, $$)
40
+ written_pid = true
41
+ rescue Exception
42
+ puts "Can't write to #{BlinkTM::LOCKFILE}"
43
+ exit!
16
44
  end
17
45
  end
18
46
 
19
- BlinkTM.start(dev)
47
+ retry_count = 0
48
+
49
+ # Find Device running Blink Taskmanager
50
+ dev = BlinkTM::find_device!
51
+ file = nil
52
+
53
+ begin
54
+ BlinkTM.start(dev) { |x| file = x }
55
+ rescue BlinkTM::SyncError
56
+ sleep 1
57
+ retry
58
+ rescue Errno::ENOENT, Errno::EIO, BlinkTM::NoDeviceError
59
+ file &.close
60
+ sleep 0.125
61
+ dev = BlinkTM::find_device!
62
+ retry
63
+ rescue Interrupt, SystemExit, SignalException
64
+ file &.close
65
+ File.delete(BlinkTM::LOCKFILE)
66
+ rescue Exception
67
+ File.delete(BlinkTM::LOCKFILE)
68
+ end
@@ -4,8 +4,6 @@
4
4
  module BlinkTM
5
5
  # Detect device
6
6
  def find_device
7
- dev = nil
8
-
9
7
  Dir.glob('/sys/bus/usb/devices/*').each { |x|
10
8
  v = File.join(x, 'idVendor')
11
9
  vendor = IO.read(v).strip if File.readable?(v)
@@ -14,39 +12,45 @@ module BlinkTM
14
12
  product = IO.read(p).strip if File.readable?(p)
15
13
 
16
14
  if vendor == '1a86' && product == '7523'
17
- puts "#{BOLD}#{GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: A potential device discovered: #{vendor}:#{product}#{RESET}"
15
+ log 'warn', "A potential device discovered: #{vendor}:#{product}"
18
16
 
19
17
  Dir.glob('/dev/ttyUSB[0-9]*').each { |x|
20
18
  if File.writable?(x)
21
- puts "#{BlinkTM::BOLD}#{BlinkTM::BLUE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Changing baudrate to 57600...#{BlinkTM::RESET}"
19
+ log 'success', "Changing baudrate to 57600..."
22
20
 
23
21
  if BlinkTM.set_baudrate(x, BlinkTM::BAUDRATE)
24
- puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Successfully Changed baudrate to 57600...#{BlinkTM::RESET}"
22
+ log 'success', 'Successfully Changed baudrate to 57600...'
25
23
  else
26
- puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Cannot change the baudrate#{BlinkTM::RESET}"
24
+ log 'error', 'Cannot change the baudrate'
27
25
  end
28
26
  else
29
- "#{BOLD}#{RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No permission granted to change Baudrate#{RESET}"
27
+ log 'error', 'No permission granted to change Baudrate'
30
28
  end
31
29
 
32
30
  if File.readable?(x)
33
31
  begin
34
- if File.open(x).readpartial(30).to_s.scrub.include?("BTM")
35
- puts "#{BOLD}#{ORANGE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Multiple Blink Task "\
36
- "Manager Hardware Found! "\
37
- "Selecting: #{vendor}:#{product}#{RESET}" if dev
38
-
39
- dev = x
40
- end
32
+ return x if File.open(x).read_nonblock(30).to_s.scrub.include?("BTM")
41
33
  rescue EOFError
42
- sleep 0.125
34
+ sleep 0.05
43
35
  retry
36
+ rescue Errno::ENOENT, Errno::EIO
44
37
  end
45
38
  end
46
39
  }
47
40
  end
48
41
  }
49
42
 
43
+ nil
44
+ end
45
+
46
+ @@retry_count = 0
47
+ def find_device!
48
+ while(!(dev = BlinkTM.find_device))
49
+ log 'error', "No device found. Retrying #{@@retry_count += 1}"
50
+ sleep 0.5
51
+ end
52
+
53
+ log 'success', "Device discovered successfully. Path: #{dev}#{BlinkTM::RESET}"
50
54
  dev
51
55
  end
52
56
 
@@ -103,114 +107,105 @@ module BlinkTM
103
107
  }
104
108
 
105
109
  prev_crc32 = ''
110
+ raise NoDeviceError unless device
106
111
 
107
- begin
108
- raise NoDeviceError unless device
109
-
110
- in_sync = false
112
+ in_sync = false
111
113
 
112
- fd = IO.sysopen(
113
- device,
114
- Fcntl::O_RDWR | Fcntl::O_NOCTTY | Fcntl::O_NONBLOCK | Fcntl::O_TRUNC
115
- )
114
+ fd = IO.sysopen(
115
+ device,
116
+ Fcntl::O_RDWR | Fcntl::O_NOCTTY | Fcntl::O_NONBLOCK | Fcntl::O_TRUNC
117
+ )
116
118
 
117
- file = IO.open(fd)
119
+ file = IO.open(fd)
120
+ yield file
118
121
 
119
- until in_sync
120
- # Clear out any extra zombie bits
121
- file.syswrite(?~.freeze)
122
- # Start the device
123
- file.syswrite(?#.freeze)
124
- file.flush
122
+ until in_sync
123
+ # Clear out any extra zombie bits
124
+ file.syswrite(?~.freeze)
125
+ # Start the device
126
+ file.syswrite(?#.freeze)
127
+ file.flush
125
128
 
126
- sleep 0.125
129
+ sleep 0.125
127
130
 
128
- begin
129
- if file.readpartial(8000).include?(?~)
130
- in_sync = true
131
- break
132
- end
133
- rescue EOFError
134
- sleep 0.05
135
- retry
131
+ begin
132
+ if file.read_nonblock(8000).include?(?~)
133
+ in_sync = true
134
+ break
136
135
  end
136
+ rescue EOFError
137
+ sleep 0.05
138
+ retry
137
139
  end
140
+ end
138
141
 
139
- sync_error_count = 0
142
+ sync_error_count = 0
140
143
 
141
- puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device ready!#{BlinkTM::RESET}"
142
- file.read
144
+ log 'success', 'Device ready!'
145
+ file.read
143
146
 
144
- while true
145
- # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
146
- # netUpload(999991) netDownload(999990)
147
- # disktotal(999990) diskused(999990)
148
-
149
- memstat = LS::Memory.stat
150
- mem_u = memstat[:used].to_i.*(1024).*(100).fdiv(memstat[:total].to_i * 1024).round
151
-
152
- swapstat = LS::Swap.stat
153
- swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024).round
154
-
155
- # Output has to be exactly this long. If not, blink-taskmanager shows invalid result.
156
- # No string is split inside blink-task manager, it just depends on the string length.
157
- #
158
- # cpu(100) memUsed(100) swapUsed(100)
159
- # netDownload(9991) netUpload(9991)
160
- # ioWrite(9991) ioRead(9991)
161
-
162
- # Debugging string
163
- # str = "#{"%03d" % cpu_u} #{"%03d" % mem_u} #{"%03d" % swap_u} "\
164
- # "#{convert_bytes(net_u)} #{convert_bytes(net_d)} "\
165
- # "#{convert_bytes(io_r)} #{convert_bytes(io_w)}"
166
-
167
- str = "!##{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
168
- "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
169
- "#{convert_bytes(io_r)}#{convert_bytes(io_w)}1~"
170
-
171
- # Rescuing from suspend
172
- file.syswrite(str)
173
- file.flush
174
- crc32 = file.read.scrub![/\d+/]
175
-
176
- unless crc32 == prev_crc32 || prev_crc32.empty?
177
- raise SyncError if sync_error_count > 1
178
- sync_error_count += 1
179
- else
180
- sync_error_count = 0 unless sync_error_count == 0
181
- end
147
+ while true
148
+ # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
149
+ # netUpload(999991) netDownload(999990)
150
+ # disktotal(999990) diskused(999990)
182
151
 
183
- prev_crc32 = BlinkTM.crc32(str[2..-2])
184
- sleep REFRESH
185
- end
186
- rescue Interrupt, SystemExit, SignalException
187
- file &.close
188
- exit 0
189
- rescue Errno::ENOENT, BlinkTM::NoDeviceError
190
- file &.close
191
- device = find_device
192
-
193
- unless device
194
- puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Error establishing connection. Don't worry if this is a valid device. Retrying...#{BlinkTM::RESET}"
195
- sleep 0.1
152
+ memstat = LS::Memory.stat
153
+ mem_u = memstat[:used].to_i.*(1024).*(100).fdiv(memstat[:total].to_i * 1024).round
154
+
155
+ swapstat = LS::Swap.stat
156
+ swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024).round
157
+
158
+ # Output has to be exactly this long. If not, blink-taskmanager shows invalid result.
159
+ # No string is split inside blink-task manager, it just depends on the string length.
160
+ #
161
+ # cpu(100) memUsed(100) swapUsed(100)
162
+ # netDownload(9991) netUpload(9991)
163
+ # ioWrite(9991) ioRead(9991)
164
+
165
+ # 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)}"
169
+
170
+ str = "!##{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
171
+ "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
172
+ "#{convert_bytes(io_r)}#{convert_bytes(io_w)}1~"
173
+
174
+ # Rescuing from suspend
175
+ file.syswrite(str)
176
+ file.flush
177
+ crc32 = file.read.scrub![/\d+/]
178
+
179
+ unless crc32 == prev_crc32 || prev_crc32.empty?
180
+ raise SyncError if sync_error_count > 1
181
+ sync_error_count += 1
182
+ else
183
+ sync_error_count = 0 unless sync_error_count == 0
196
184
  end
197
185
 
198
- retry
199
- rescue BlinkTM::SyncError
200
- file &.close
201
- sleep 0.005
202
- retry
203
- rescue BlinkTM::DeviceNotReady
204
- file &.close
205
- sleep 0.1
206
- retry
207
- rescue Exception
208
- puts $!.full_message
209
- file &.close
186
+ prev_crc32 = BlinkTM.crc32(str[2..-2])
187
+ sleep REFRESH
188
+ end
189
+
190
+ unless device
191
+ puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Error establishing connection. Don't worry if this is a valid device. Retrying...#{BlinkTM::RESET}"
210
192
  sleep 0.1
211
- retry
212
193
  end
213
194
  end
214
195
 
196
+ def log(type, message = nil)
197
+ message, type = type, nil if type && !message
198
+
199
+ colour = case type
200
+ when 0, 'fatal', 'error' then BlinkTM::RED
201
+ when 1, 'warn' then BlinkTM::ORANGE
202
+ when 2, 'info' then BlinkTM::BLUE
203
+ when 3, 'success', 'ok' then BlinkTM::GREEN
204
+ else ''
205
+ end
206
+
207
+ puts "#{BlinkTM::BOLD}#{colour}:: #{Time.now.strftime('%H:%M:%S.%2N')}: #{message}#{BlinkTM::RESET}"
208
+ end
209
+
215
210
  extend(self)
216
211
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlinkTM
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/blink_tm.rb CHANGED
@@ -34,6 +34,7 @@ module BlinkTM
34
34
  ORANGE = "\e[38;2;245;155;20m"
35
35
  BOLD = "\e[1m"
36
36
  RESET = "\e[0m"
37
+ LOCKFILE = '/tmp/blinktaskmanager.pid'
37
38
 
38
39
  # Other constants
39
40
  ROOT_DEV = ::LinuxStat::Mounts.root
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: 1.0.0
4
+ version: 2.0.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-07-15 00:00:00.000000000 Z
11
+ date: 2021-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: linux_stat
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.2.21
64
+ rubygems_version: 3.2.29
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A controller for Arduino OLED System Monitor, Blink Task Manager