Friday, July 8, 2022

Can you help me reviewing this snippet of code?


<=== EDIT JUST THIS ##################################################################### $counter = 0 $time_on = 8 #default value $time_off = 23 #default value $last_updated = nil $restart_before = 0 $restarted_at = 0 $uri_for_data = URI("https://www.xxxx.xx/api/v1/nodes/#{$screen_access_code}/get_data") def turn_on; `xset dpms force on`end; #turn on screen (idempotent method) def turn_off; `xset dpms force off`end; #turn off screen (idempotent method) def restart_machine; `sudo reboot`end; def uptime_in_seconds; IO.read('/proc/uptime').split[0].to_i end; #Time since OS started def get_node_data begin retries ||= 0 response = Net::HTTP.get($uri_for_data) data = JSON.parse(response) if (data.is_a?(Hash) && data["access_code"] == $screen_access_code) $time_on = data["time_on"].to_i $time_off = data["time_off"].to_i #Integers already $restart_before = data["restart_before"] $restarted_at = data["restarted_at"] $last_updated = Time.now puts "Schedules updated to ON: #{$time_on}, OFF: #{$time_off}, at: #{Time.now}" else raise "Invalid object" end rescue Exception => e puts e.message end end # It only takes data every 15 minutes. It is managed calling from main loop and sleeping 60 seconds def should_update(time) if ($counter == 0) get_node_data $counter = 15 else $counter = $counter - 1 end end def should_act(time) if (time.hour == $time_on && time.min == 0) turn_on puts "turned on at #{time.hour}:#{time.min}" end if (time.hour == $time_off && time.min == 0) turn_off puts "turned off at #{time.hour}:#{time.min}" end # daily restart if (uptime_in_seconds > 3600 && time.hour == $time_on) uri_for_restart = URI("https://www.xxxx.xx/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Net::HTTP.get(uri_for_restart) restart_machine end # if uptime is less than 30 min, won't do nothing to avoid several restarts; restart before is meant to be about Time.now + ~{15..25} min if (uptime_in_seconds > 1800 && time.to_i < $restart_before) uri_for_restart = URI("https://www.redvi.eu/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Net::HTTP.get(uri_for_restart) restart_machine end end while true now = Time.now should_update(now) # update should_act(now) puts "Checking at: #{now}" sleep 60 end monitor.service[Unit] Description=Monitor Power Scheduler Wants=graphical.target After=graphical.target StandardOutput=/usr/src/monitor.log StandardError=/usr/src/monitor.log [Service] Environment=DISPLAY=:0.0 Environment=XAUTHORITY=/home/pi/.Xauthority Type=simple ExecStart=/home/pi/.rbenv/shims/ruby /usr/src/monitor.rb Restart=on-abort User=root Group=sudo [Install] WantedBy=graphical.target Thanks" title="Can you help me reviewing this snippet of code?">full image - Repost: Can you help me reviewing this snippet of code? (from Reddit.com, Can you help me reviewing this snippet of code?)
Hi guys, I don't have any coworkers or mentors. I am a humble solopreneur with my little project.It is a backend (in Rails) that is used to manage the operation of a smart TV's network. Each node is a Rasberry Pi, and I use a small code in ruby to check the "keep alive" and to make remote restarts. The little script is invoked as service with SystemD. Could you please have a look and tell me your feedback/suggestions? I have been learning on the way and I would like my stuff supervised by an adult :)monitor.rbrequire 'net/http' require 'json' require 'date' ##################################################################### $screen_access_code = "xxxxxx" # <=== EDIT JUST THIS ##################################################################### $counter = 0 $time_on = 8 #default value $time_off = 23 #default value $last_updated = nil $restart_before = 0 $restarted_at = 0 $uri_for_data = URI("https://www.xxxx.xx/api/v1/nodes/#{$screen_access_code}/get_data") def turn_on; `xset dpms force on`end; #turn on screen (idempotent method) def turn_off; `xset dpms force off`end; #turn off screen (idempotent method) def restart_machine; `sudo reboot`end; def uptime_in_seconds; IO.read('/proc/uptime').split[0].to_i end; #Time since OS started def get_node_data begin retries ||= 0 response = Net::HTTP.get($uri_for_data) data = JSON.parse(response) if (data.is_a?(Hash) && data["access_code"] == $screen_access_code) $time_on = data["time_on"].to_i $time_off = data["time_off"].to_i #Integers already $restart_before = data["restart_before"] $restarted_at = data["restarted_at"] $last_updated = Time.now puts "Schedules updated to ON: #{$time_on}, OFF: #{$time_off}, at: #{Time.now}" else raise "Invalid object" end rescue Exception => e puts e.message end end # It only takes data every 15 minutes. It is managed calling from main loop and sleeping 60 seconds def should_update(time) if ($counter == 0) get_node_data $counter = 15 else $counter = $counter - 1 end end def should_act(time) if (time.hour == $time_on && time.min == 0) turn_on puts "turned on at #{time.hour}:#{time.min}" end if (time.hour == $time_off && time.min == 0) turn_off puts "turned off at #{time.hour}:#{time.min}" end # daily restart if (uptime_in_seconds > 3600 && time.hour == $time_on) uri_for_restart = URI("https://www.xxxx.xx/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Net::HTTP.get(uri_for_restart) restart_machine end # if uptime is less than 30 min, won't do nothing to avoid several restarts; restart before is meant to be about Time.now + ~{15..25} min if (uptime_in_seconds > 1800 && time.to_i < $restart_before) uri_for_restart = URI("https://www.redvi.eu/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Net::HTTP.get(uri_for_restart) restart_machine end end while true now = Time.now should_update(now) # update should_act(now) puts "Checking at: #{now}" sleep 60 end monitor.service[Unit] Description=Monitor Power Scheduler Wants=graphical.target After=graphical.target StandardOutput=/usr/src/monitor.log StandardError=/usr/src/monitor.log [Service] Environment=DISPLAY=:0.0 Environment=XAUTHORITY=/home/pi/.Xauthority Type=simple ExecStart=/home/pi/.rbenv/shims/ruby /usr/src/monitor.rb Restart=on-abort User=root Group=sudo [Install] WantedBy=graphical.target Thanks


Mining:
Bitcoin, Cryptotab browser - Pi Network cloud PHONE MINING
Fone, cloud PHONE MINING cod. dhvd1dkx - Mintme, PC PHONE MINING


Exchanges:
Coinbase.com - Stex.com - Probit.com


Donations:
Done crypto



Comments System

Disqus Shortname

Disqus Shortname

designcart
Powered by Blogger.