Skip to content

Commit

Permalink
Add loop for archive and pvpc data retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Aug 5, 2023
1 parent c83fad1 commit 30a8636
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 3 deletions.
1 change: 1 addition & 0 deletions .foreman
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
port: 3000
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
CMD ["./bin/foreman", "start"]
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem "turbo-rails"

## LIBRARIES
gem "bootsnap", require: false
gem "clockwork"
gem "foreman"
gem "http"
gem "huawei_solar"

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
clockwork (3.0.2)
activesupport
tzinfo
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
Expand All @@ -138,6 +141,7 @@ GEM
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
foreman (0.87.2)
globalid (1.1.0)
activesupport (>= 5.0)
http (5.1.1)
Expand Down Expand Up @@ -305,9 +309,11 @@ DEPENDENCIES
better_errors
bootsnap
capybara
clockwork
debug
dotenv-rails
error_highlight
foreman
http
huawei_solar
importmap-rails
Expand Down
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bin/puma -C config/puma.rb
clock: bin/clockwork config/clockwork.rb
4 changes: 2 additions & 2 deletions app/lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def loop_interval
ENV.fetch("SOLARIS_LOOP_INTERVAL", 30).to_i
end

def storage_interval
ENV.fetch("SOLARIS_STORAGE_INTERVAL", 300).to_i
def archive_interval
ENV.fetch("SOLARIS_ARCHIVE_INTERVAL", 300).to_i
end

def esios_api_key
Expand Down
46 changes: 46 additions & 0 deletions app/lib/solaris/esios.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Solaris
class ESIOS
def self.run
new.run
end

def run
perform_pvpc if condition_for_pvpc
end

private

def perform_pvpc
pvpc_import
pvpc_export
end

def condition_for_pvpc
PVPC.where(datetime: tomorrow_range).count != 24
end

def tomorrow
Date.tomorrow
end

def tomorrow_range
tomorrow.all_day
end

def pvpc_import
ESIOS::Import.for_date(tomorrow).each do |data|
pvpc = PVPC.find_or_initialize_by(datetime: data[:datetime])
pvpc.assign_attributes(import: data[:value])
pvpc.save
end
end

def pvpc_export
ESIOS::Export.for_date(tomorrow).each do |data|
pvpc = PVPC.find_or_initialize_by(datetime: data[:datetime])
pvpc.assign_attributes(export: data[:value])
pvpc.save
end
end
end
end
29 changes: 29 additions & 0 deletions app/lib/solaris/loop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Solaris
class Loop
def self.run
new.run
end

def run
perform_archive if condition_for_archive
end

private

def last_archive
Archive.order(created_at: :desc).first
end

def condition_for_archive
last_archive.nil? || last_archive.created_at < Config.archive_interval.seconds.ago
end

def perform_archive
loop do
break if Archive.create_from_inverter.persisted?
rescue Errno::ECONNRESET
Rails.logger.error "Connection reset, retrying..."
end
end
end
end
27 changes: 27 additions & 0 deletions bin/clockwork
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'clockwork' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("clockwork", "clockwork")
27 changes: 27 additions & 0 deletions bin/clockworkd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'clockworkd' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("clockwork", "clockworkd")
27 changes: 27 additions & 0 deletions bin/foreman
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'foreman' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("foreman", "foreman")
27 changes: 27 additions & 0 deletions bin/puma
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'puma' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("puma", "puma")
27 changes: 27 additions & 0 deletions bin/pumactl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'pumactl' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("puma", "pumactl")
15 changes: 15 additions & 0 deletions config/clockwork.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative "environment"

module Clockwork
configure do |config|
config[:tz] = ENV.fetch("SOLARIS_TIMEZONE", "Europe/Madrid").to_s
end

every(Config.loop_interval.seconds, "solaris.loop", thread: true) do
Solaris::Loop.run
end

every(1.day, "solaris.esios", at: ["20:30", "21:30", "22:30", "23:30"], thread: true) do
Solaris::ESIOS.run
end
end

0 comments on commit 30a8636

Please sign in to comment.