Can you please hint me to debug this code?

2025.01.22 11:45 juanse003 Can you please hint me to debug this code?

Hi guys. I have a little ruby service that I use in my remotes raspberry pi's to turn on and off screens, reboot, daily reboot and not much more. Schedules are download from my server and it normally works fine from months. And then randomly it dies/freeze and it stops working.
Initially I make the mistake of not having time out in the connection. I fix this. Now, In reality is so simple that I don't now what might fail. Could you please have a look? (it's made so every day the system restarts so I don't need to care for long term memory issues) Thanks

require 'faraday' require 'faraday/net_http' require 'json' require 'date' require 'pry' Faraday.default_adapter = :net_http ############################################################################ $screen_access_code = `cat /ussrc/CODENAME`.split[0] #GET CODENAME FROM FILE ############################################################################ puts $screen_access_code $uri_for_data = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/get_data") # $uri_for_data = URI("http://localhost:3000/api/v1/nodes/#{$screen_access_code}/get_data") $counter = 0 $offset = 0 # time offsets in second for each node. To avoid all request at the same time $last_updated = nil # For debugging purposes $restart_before = 0 $restarted_at = 0 $node_data = nil $first_time_used = false $needrestart = false `xset +dpms` #Energy Star features on `xset dpms 0 0 0` # unlimited time with 0 `xset s noblank` `xset s off` `xrandr --display :0 --output HDMI-1 --mode 1920x1080` # Specially to avoid changes in 2K monitors def turn_on; `xrandr --display :0 --output HDMI-1 --mode 1920x1080`end; def turn_off; `xrandr --display :0 --output HDMI-1 --off`end; #turn off screen def restart_machine; `sudo reboot`end; def uptime_in_seconds; IO.read('/proc/uptime').split[0].to_i end; #Time since OS started def within_range?(time_now, time_frame) # Define a hash that maps each day of the week to a number weekdays = { "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6, "sunday" => 7 } # Set times vars current_day = weekdays[time_now.strftime("%A").downcase] current_hour = time_now.hour current_minute = time_now.min start_day = time_frame["weekday_from"] # + 1 to adapt from wday to cwday start_hour = DateTime.parse(time_frame["time_from"]).hour start_minute = DateTime.parse(time_frame["time_from"]).min end_day = time_frame["weekday_till"] # + 1 to adapt from wday to cwday end_hour = DateTime.parse(time_frame["time_till"]).hour end_minute = DateTime.parse(time_frame["time_till"]).min # puts "start_day: #{start_day}, hora: #{start_hour}, minuto: #{start_minute}" # puts "current_day: #{current_day}, hora: #{current_hour}, minuto: #{current_minute}" # puts "end_day: #{end_day}, hora: #{end_hour}, minuto: #{end_minute}" start_time_minutes = (start_day - 1) * 1440 + start_hour * 60 + start_minute end_time_minutes = (end_day - 1) * 1440 + end_hour * 60 + end_minute current_time_minutes = (current_day - 1) * 1440 + current_hour * 60 + current_minute # Handle multi-week spans if end_time_minutes < start_time_minutes end_time_minutes += 7 * 1440 end # Handle the case where current time is in the next week if current_time_minutes < start_time_minutes current_time_minutes += 7 * 1440 end current_time_minutes >= start_time_minutes && current_time_minutes <= end_time_minutes end def get_node_data begin first_time_timeoffset # Artictial delay to avoid all request at the same time and dpmsto work conn = Faraday.new($uri_for_data, request: { timeout: 10 }) response = conn.get STDOUT.puts("Entering request") if (response.status == 200) data = JSON.parse(response.body) if (data.is_a?(Hash) && data["access_code"] == $screen_access_code) $offset = data["offset"] $restart_before = data["restart_before"] $restarted_at = data["restarted_at"] $node_data = data $needrestart = data["needrestart"] STDOUT.puts(" Node data updated: #{$screen_access_code}") end end rescue Exception => e logger = Logger.new("screen_monitor_service_error.log") logger.error("Connection failure: #{e}") logger.close end end def screen_state(time_now) #decides wheter the monitor should be on or off return true if $node_data.nil? should_be_on = false $node_data["node_time_frames"].each do |timeframe| if within_range?(time_now, timeframe) should_be_on = true STDOUT.puts("Dentro de rango, ON") end end #STDOUT.puts("should_be_on: #{should_be_on}") return should_be_on end def first_time_timeoffset if $first_time_used == false sleep (10 + $offset) # 10 is a brief time so the whole system is initialized and dpms works correctly $first_time_used = true end end def should_act(time) # Returns whether the screen should be on/off; (true/false) based on Time.now screen_state_now = screen_state(time) monitor_is_x = `cat /sys/class/drm/card1-HDMI-A-1/enabled` monitor_state = true if monitor_is_x.include?("enabled") monitor_state = false if monitor_is_x.include?("disabled") STDOUT.puts("#{Time.now} monitor_state: #{monitor_state}") STDOUT.puts("#{Time.now}: Mon state: #{monitor_state} AND screen_state(time): #{screen_state(time)}") if screen_state_now #Should be on if monitor_state == false #So if it's off, then turn on turn_on STDOUT.puts("#{Time.now}: Monitor is turned ON") uri_for_on = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/turned_on/#{Time.now.to_i}") response = Faraday.new(uri_for_on, request: { timeout: 10 }).get end else #Should be off if monitor_state == true #So if it's on, then turn off turn_off STDOUT.puts("#{Time.now}: Monitor is turned OFF") uri_for_off = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/turned_off/#{Time.now.to_i}") response = Faraday.new(uri_for_off, request: { timeout: 10 }).get end end # daily restart if (uptime_in_seconds > 3600 && time.hour == 6) sleep (0+$offset) uri_for_restart = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Faraday.new(uri_for_restart, request: { timeout: 10 }).get 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 ($needrestart) uri_for_restart = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Faraday.new(uri_for_restart, request: { timeout: 10 }).get restart_machine end end def should_update(time) if ($counter == 0) get_node_data $counter = 5 # Minutes between petitions else $counter = $counter - 1 end end while true now = Time.now should_update(now) # update should_act(now) sleep 60 end require 'faraday' require 'faraday/net_http' require 'json' require 'date' require 'pry' Faraday.default_adapter = :net_http ############################################################################ $screen_access_code = `cat /ussrc/CODENAME`.split[0] #GET CODENAME FROM FILE ############################################################################ puts $screen_access_code $uri_for_data = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/get_data") # $uri_for_data = URI("http://localhost:3000/api/v1/nodes/#{$screen_access_code}/get_data") $counter = 0 $offset = 0 # time offsets in second for each node. To avoid all request at the same time $last_updated = nil # For debugging purposes $restart_before = 0 $restarted_at = 0 $node_data = nil $first_time_used = false $needrestart = false `xset +dpms` #Energy Star features on `xset dpms 0 0 0` # unlimited time with 0 `xset s noblank` `xset s off` `xrandr --display :0 --output HDMI-1 --mode 1920x1080` # Specially to avoid changes in 2K monitors def turn_on; `xrandr --display :0 --output HDMI-1 --mode 1920x1080`end; def turn_off; `xrandr --display :0 --output HDMI-1 --off`end; #turn off screen def restart_machine; `sudo reboot`end; def uptime_in_seconds; IO.read('/proc/uptime').split[0].to_i end; #Time since OS started def within_range?(time_now, time_frame) # Define a hash that maps each day of the week to a number weekdays = { "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6, "sunday" => 7 } # Set times vars current_day = weekdays[time_now.strftime("%A").downcase] current_hour = time_now.hour current_minute = time_now.min start_day = time_frame["weekday_from"] # + 1 to adapt from wday to cwday start_hour = DateTime.parse(time_frame["time_from"]).hour start_minute = DateTime.parse(time_frame["time_from"]).min end_day = time_frame["weekday_till"] # + 1 to adapt from wday to cwday end_hour = DateTime.parse(time_frame["time_till"]).hour end_minute = DateTime.parse(time_frame["time_till"]).min # puts "start_day: #{start_day}, hora: #{start_hour}, minuto: #{start_minute}" # puts "current_day: #{current_day}, hora: #{current_hour}, minuto: #{current_minute}" # puts "end_day: #{end_day}, hora: #{end_hour}, minuto: #{end_minute}" start_time_minutes = (start_day - 1) * 1440 + start_hour * 60 + start_minute end_time_minutes = (end_day - 1) * 1440 + end_hour * 60 + end_minute current_time_minutes = (current_day - 1) * 1440 + current_hour * 60 + current_minute # Handle multi-week spans if end_time_minutes < start_time_minutes end_time_minutes += 7 * 1440 end # Handle the case where current time is in the next week if current_time_minutes < start_time_minutes current_time_minutes += 7 * 1440 end current_time_minutes >= start_time_minutes && current_time_minutes <= end_time_minutes end def get_node_data begin first_time_timeoffset # Artictial delay to avoid all request at the same time and dpmsto work conn = Faraday.new($uri_for_data, request: { timeout: 10 }) response = conn.get STDOUT.puts("Entering request") if (response.status == 200) data = JSON.parse(response.body) if (data.is_a?(Hash) && data["access_code"] == $screen_access_code) $offset = data["offset"] $restart_before = data["restart_before"] $restarted_at = data["restarted_at"] $node_data = data $needrestart = data["needrestart"] STDOUT.puts(" Node data updated: #{$screen_access_code}") end end rescue Exception => e logger = Logger.new("screen_monitor_service_error.log") logger.error("Connection failure: #{e}") logger.close end end def screen_state(time_now) #decides wheter the monitor should be on or off return true if $node_data.nil? should_be_on = false $node_data["node_time_frames"].each do |timeframe| if within_range?(time_now, timeframe) should_be_on = true STDOUT.puts("Dentro de rango, ON") end end #STDOUT.puts("should_be_on: #{should_be_on}") return should_be_on end def first_time_timeoffset if $first_time_used == false sleep (10 + $offset) # 10 is a brief time so the whole system is initialized and dpms works correctly $first_time_used = true end end def should_act(time) # Returns whether the screen should be on/off; (true/false) based on Time.now screen_state_now = screen_state(time) monitor_is_x = `cat /sys/class/drm/card1-HDMI-A-1/enabled` monitor_state = true if monitor_is_x.include?("enabled") monitor_state = false if monitor_is_x.include?("disabled") STDOUT.puts("#{Time.now} monitor_state: #{monitor_state}") STDOUT.puts("#{Time.now}: Mon state: #{monitor_state} AND screen_state(time): #{screen_state(time)}") if screen_state_now #Should be on if monitor_state == false #So if it's off, then turn on turn_on STDOUT.puts("#{Time.now}: Monitor is turned ON") uri_for_on = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/turned_on/#{Time.now.to_i}") response = Faraday.new(uri_for_on, request: { timeout: 10 }).get end else #Should be off if monitor_state == true #So if it's on, then turn off turn_off STDOUT.puts("#{Time.now}: Monitor is turned OFF") uri_for_off = URI("https://www.xxx.es/api/v1/nodes/#{$screen_access_code}/turned_off/#{Time.now.to_i}") response = Faraday.new(uri_for_off, request: { timeout: 10 }).get end end # daily restart if (uptime_in_seconds > 3600 && time.hour == 6) sleep (0+$offset) uri_for_restart = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Faraday.new(uri_for_restart, request: { timeout: 10 }).get 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 ($needrestart) uri_for_restart = URI("https://www.xxxxx.es/api/v1/nodes/#{$screen_access_code}/restarted_at/#{Time.now.to_i}") response = Faraday.new(uri_for_restart, request: { timeout: 10 }).get restart_machine end end def should_update(time) if ($counter == 0) get_node_data $counter = 5 # Minutes between petitions else $counter = $counter - 1 end end while true now = Time.now should_update(now) # update should_act(now) sleep 60 end 
submitted by juanse003 to ruby [link] [comments]


2025.01.22 11:45 Here2Progress LISA - Would it be worth it in 5 years?

I’m 19 and yet to open a LISA. I find all aspects of a LISA to be beneficial, however I’m now questioning if the budget will be high enough by the time I decide to get a house.
Since the LISA was introduced, the market has gone up 30% and the budget has remained the same. I’ve seen some talks online about them reevaluating or terminating the LISA, but I don’t know how legit those sources are.
It will be another 4-5 years before I consider getting a house, but by then the market would have gone up even more.
My dilemma is if they don’t reevaluate the LISA, would it be worth it in 5 years?
submitted by Here2Progress to HousingUK [link] [comments]


2025.01.22 11:45 motion_less_ Drip Super Store

Drip Super Store https://preview.redd.it/p2c9bkk59jee1.png?width=710&format=png&auto=webp&s=afa13958d9b472d21e8865ea4b3a70ff2992db9e
THE DRIP IS IN THE SUPER STORE GUYS !
submitted by motion_less_ to LowSodiumHellDivers [link] [comments]


2025.01.22 11:45 vilomstren Avis Rental Promo Code for January 2025

Use the link for Avis Rental Promo Code for January 2025. The website features a wide selection of coupons, promo codes, and discount deals that are updated regularly for you to choose from and make your purchase more affordable.
submitted by vilomstren to MementoOffers [link] [comments]


2025.01.22 11:45 Thin_Albatross2720 Recycling LFs

Recycling LFs Legends often "copying" old characters (like 2 LF Namek SS Goku) Soo what think about adding LF tag Piccolo and Goku with Sacrifice Special Beam Canon
submitted by Thin_Albatross2720 to DragonballLegends [link] [comments]


2025.01.22 11:45 biggieCheesesie12 Which monster should I add next? #5

Which monster should I add next? #5 submitted by biggieCheesesie12 to MySingingMonsters [link] [comments]


2025.01.22 11:45 Thin-Pool-8025 When a character holds someone at gunpoint only to shoot themselves instead

When a character holds someone at gunpoint only to shoot themselves instead
  1. Player #119 - Squid Game
  2. Nacho Varga - Better Call Saul
submitted by Thin-Pool-8025 to TopCharacterTropes [link] [comments]


2025.01.22 11:45 GirasoleDE Welche Bücher wir jetzt lesen sollten

Welche Bücher wir jetzt lesen sollten submitted by GirasoleDE to DErwachsen [link] [comments]


2025.01.22 11:45 Practical_Ticket_680 What are the challenges do you face in your existing email app

Iam conducting this survey to understand email usage and challenges to create something useful.https://forms.gle/MghdR2MbQV9eJK187
submitted by Practical_Ticket_680 to indiebiz [link] [comments]


2025.01.22 11:45 B1WITHYURI1558 After watching a lot of SVTFOE and Bleach, I noticed how similar Star’s butterfly form and one of Aizen’s hogyoku forms are. Is it inspired by it or is it just a coincidence?

After watching a lot of SVTFOE and Bleach, I noticed how similar Star’s butterfly form and one of Aizen’s hogyoku forms are. Is it inspired by it or is it just a coincidence? submitted by B1WITHYURI1558 to StarVStheForcesofEvil [link] [comments]


2025.01.22 11:45 Tek-Isopod Network Rail Signallar Interview Help

I have an interview to be a signallar for Network Rail and I was hoping I could get some answers to specific questions I have.
I understand the general role of a signallar but was wondering if anyone could help me with some specifics.
What is the main area where a signallar would work and what sort of system would be used in modern signalling?
If possible, what sort of activities will take place during the interview, is there anything I should be prepared for or research specifically?
What are some areas of signalling which are crucial to the role?
What is the role of a signallar when there are delays or cancellation?
Finally, what should a signallar do when two trains want to go on the same track?
submitted by Tek-Isopod to uktrains [link] [comments]


2025.01.22 11:45 Splex777 Title

Pdf bhejdo aaj ke paper ki bhai kisi pe ho to
submitted by Splex777 to JEE [link] [comments]


2025.01.22 11:45 tobuyornottobuy11 Re🏡 Birkin 40 HSS - Bleu Saphir - Brushed gold hardware - Brand new - EU/WW 🌎

Re🏡 Birkin 40 HSS - Bleu Saphir - Brushed gold hardware - Brand new - EU/WW 🌎 submitted by tobuyornottobuy11 to RepladiesDesigner [link] [comments]


2025.01.22 11:45 Beratungsmarketing Mapping 1,800 Israeli settler attacks in the West Bank since October 2023 | Israel-Palestine conflict News | Al Jazeera

Mapping 1,800 Israeli settler attacks in the West Bank since October 2023 | Israel-Palestine conflict News | Al Jazeera submitted by Beratungsmarketing to World_Now [link] [comments]


2025.01.22 11:45 Nitaschi RPGMaker games not working.

Hello i downloaded a couple games that are made in RPGMaker and none of them seem to work. I open them they load for like 3secs and then close do any of u know the problem?
submitted by Nitaschi to RPGMaker [link] [comments]


2025.01.22 11:45 Aromatic_Feeling_957 Help

Help Need some help picking out new icon, im in between Ribery TS or WW Carlos Alberto. They both fit my team, got baby ribery as well but doesnt make the team anymore. Pls some advice
submitted by Aromatic_Feeling_957 to fut [link] [comments]


2025.01.22 11:45 Pale-Cow8884 Dynasty Warriors: Origins Crashing Issues...

I’ve been encountering random crashes during battles, and it’s honestly driving me crazy. What’s frustrating is that my PC runs other high-spec games perfectly fine, even those with more demanding requirements. But for some reason, this game keeps crashing no matter what I try.
I’ve updated my drivers, verified the game files, lowered the graphics settings, and even reinstalled the game, but the problem persists. What’s even more annoying is that this game isn’t cheap – I paid about $70 for it, and I feel like this level of instability is unacceptable for a game at this price. I'm also worried that Koei might ignore this issue or fail to recognize it entirely...
submitted by Pale-Cow8884 to dynastywarriors [link] [comments]


2025.01.22 11:45 sancho_sk New logo design for Elon, you're welcome!

New logo design for Elon, you're welcome! submitted by sancho_sk to pics [link] [comments]


2025.01.22 11:45 erer1243 Currently, it's January 22, 2025 at 06:45AM

Currently, it's January 22, 2025 at 06:45AM
submitted by erer1243 to every15min [link] [comments]


2025.01.22 11:45 Key_Locksmith_2294 Remplacement bondé douche italienne

Remplacement bondé douche italienne Bonjour,
J'ai une vieille douche italienne, qui renvoit des odeurs d'égoûts. En démontant la bonde d'évacuation celle-ci semble très mal posée, l'ensemble n'étant pas fixé au sol et l'ensemble sort directement.
J'aimerai la remplacer pour résoudre le problème d'odeur, mais je ne vois pas trop ce que je peux mettre à la place. Auriez-vous des conseils?
Merci!
submitted by Key_Locksmith_2294 to Plomberie [link] [comments]


2025.01.22 11:45 SinkinTitanic1912 The Wand Of Asal

The Wand Of Asal In my dream from the pandemic, in a minecart (Minecraft style) there were Dumbledore and McGonagall while Azula in a black dress was dominating the world (Dumbledore and McGonagall were underground).
Fast forward, (still underground) they are in front of a shop and a disabled man on a wheelchair (whom I've never seen before in my life) comes out of the store to join them. Dumbledore is like, "we've been waiting for you" and gives the man what he names the "Wand of Asal" (Ağasal'ın asası in my OG dream (I don't know who Ağasal is)) which is the most powerful wand in the world allegedly and looks like a flattened Elder Wand more or less.
submitted by SinkinTitanic1912 to thomastheplankengine [link] [comments]


2025.01.22 11:44 eighteencarps Can I be angled aroace and still experience romantic attraction?

I consider myself arospec and asexual. The closest arospec label I would probably pick for myself is grayromantic. I experience romantic attraction rarely, and it’s often intertangled with alterous attraction. Sometimes I feel like I shouldn’t call myself aro, though.
Any thoughts?
submitted by eighteencarps to AngledAroAce [link] [comments]


2025.01.22 11:44 Kzitold94 Any tips fo finish my journal?

Any tips fo finish my journal? submitted by Kzitold94 to webfishing [link] [comments]


2025.01.22 11:44 Neil_Kuckmann Dear Daddy Nexon, Please make the game harder

I would like it if the game was worth playing again instead of this breathe through my nostrils with a straw kind of handicap.
I'm not sure who all the players are that need these kinds of handicaps but this game used to require all your mental facets intact to play.
submitted by Neil_Kuckmann to TheFirstDescendant [link] [comments]


2025.01.22 11:44 FuturMadao 24 to 32. Same room, same light, same angle. Not the same camera tho, still need to improve.

24 to 32. Same room, same light, same angle. Not the same camera tho, still need to improve. submitted by FuturMadao to uglyduckling [link] [comments]


https://yandex.ru/