report_service.rb 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  2. #
  3. # Copyright (c) 2016, Electric Power Research Institute (EPRI)
  4. # All rights reserved.
  5. #
  6. # OpenADR ("this software") is licensed under BSD 3-Clause license.
  7. #
  8. # Redistribution and use in source and binary forms, with or without modification,
  9. # are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright notice, this
  12. # list of conditions and the following disclaimer.
  13. #
  14. # * Redistributions in binary form must reproduce the above copyright notice,
  15. # this list of conditions and the following disclaimer in the documentation
  16. # and/or other materials provided with the distribution.
  17. #
  18. # * Neither the name of EPRI nor the names of its contributors may
  19. # be used to endorse or promote products derived from this software without
  20. # specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. # OF SUCH DAMAGE.
  32. #
  33. # This EPRI software incorporates work covered by the following copyright and permission
  34. # notices. You may not use these works except in compliance with their respective
  35. # licenses, which are provided below.
  36. #
  37. # These works are provided by the copyright holders and contributors "as is" and any express or
  38. # implied warranties, including, but not limited to, the implied warranties of merchantability
  39. # and fitness for a particular purpose are disclaimed.
  40. #
  41. #########################################################################################
  42. # MIT Licensed Libraries
  43. #########################################################################################
  44. #
  45. # * actionmailer 3.2.12 (http://www.rubyonrails.org) - Email composition, delivery, and receiving framework (part of Rails).
  46. # * actionpack 3.2.12 (http://www.rubyonrails.org) - Web-flow and rendering framework putting the VC in MVC (part of Rails).
  47. # * activemodel 3.2.12 (http://www.rubyonrails.org) - A toolkit for building modeling frameworks (part of Rails).
  48. # * activerecord 3.2.12 (http://www.rubyonrails.org) - Object-relational mapper framework (part of Rails).
  49. # * activeresource 3.2.12 (http://www.rubyonrails.org) - REST modeling framework (part of Rails).
  50. # * activesupport 3.2.12 (http://www.rubyonrails.org) - A toolkit of support libraries and Ruby core extensions extracted from the Rails framework.
  51. # * arel 3.0.2 (http://github.com/rails/arel) - Arel is a SQL AST manager for Ruby
  52. # * bootstrap-sass 3.1.1.0 (https://github.com/twbs/bootstrap-sass) - Twitter's Bootstrap, converted to Sass and ready to drop into Rails or Compass
  53. # * builder 3.0.4 (http://onestepback.org) - Builders for MarkUp.
  54. # * bundler 1.12.5 (http://bundler.io) - The best way to manage your application's dependencies
  55. # * capybara 2.4.4 (http://github.com/jnicklas/capybara) - Capybara aims to simplify the process of integration testing Rack applications, such as Rails, Sinatra or Merb
  56. # * coffee-rails 3.2.2 () - Coffee Script adapter for the Rails asset pipeline.
  57. # * coffee-script-source 1.6.3 (http://jashkenas.github.com/coffee-script/) - The CoffeeScript Compiler
  58. # * docile 1.1.5 (https://ms-ati.github.io/docile/) - Docile keeps your Ruby DSLs tame and well-behaved
  59. # * edn 1.0.0 () - 'edn implements a reader for Extensible Data Notation by Rich Hickey.'
  60. # * erubis 2.7.0 (http://www.kuwata-lab.com/erubis/) - a fast and extensible eRuby implementation which supports multi-language
  61. # * execjs 1.4.0 (https://github.com/sstephenson/execjs) - Run JavaScript code from Ruby
  62. # * factory_girl 4.5.0 (https://github.com/thoughtbot/factory_girl) - factory_girl provides a framework and DSL for defining and using model instance factories.
  63. # * factory_girl_rails 4.5.0 (http://github.com/thoughtbot/factory_girl_rails) - factory_girl_rails provides integration between factory_girl and rails 3
  64. # * gem-licenses 0.1.2 (http://github.com/dblock/gem-licenses) - List all gem licenses.
  65. # * hike 1.2.3 (http://github.com/sstephenson/hike) - Find files in a set of paths
  66. # * i18n 0.6.5 (http://github.com/svenfuchs/i18n) - New wave Internationalization support for Ruby
  67. # * jdbc-postgresql 9.2.1000 (https://github.com/rosenfeld/jdbc-postgresql) - PostgresSQL jdbc driver for JRuby
  68. # * journey 1.0.4 (http://github.com/rails/journey) - Journey is a router
  69. # * jquery-rails 3.0.4 (http://rubygems.org/gems/jquery-rails) - Use jQuery with Rails 3
  70. # * json-schema 2.6.2 (http://github.com/ruby-json-schema/json-schema/tree/master) - Ruby JSON Schema Validator
  71. # * mail 2.4.4 (http://github.com/mikel/mail) - Mail provides a nice Ruby DSL for making, sending and reading emails.
  72. # * metaclass 0.0.4 (http://github.com/floehopper/metaclass) - Adds a metaclass method to all Ruby objects
  73. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  74. # * mocha 1.1.0 (http://gofreerange.com/mocha/docs) - Mocking and stubbing library
  75. # * multi_json 1.7.9 (http://github.com/intridea/multi_json) - A common interface to multiple JSON libraries.
  76. # * nokogiri 1.6.5 (http://nokogiri.org) - Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser
  77. # * polyglot 0.3.3 (http://github.com/cjheath/polyglot) - Augment 'require' to load non-Ruby file types
  78. # * rack-test 0.6.2 (http://github.com/brynary/rack-test) - Simple testing API built on Rack
  79. # * railties 3.2.12 (http://www.rubyonrails.org) - Tools for creating, working with, and running Rails applications.
  80. # * rake 10.1.0 (http://rake.rubyforge.org) - Ruby based make-like utility.
  81. # * rspec-core 2.14.3 (http://github.com/rspec/rspec-core) - rspec-core-2.14.3
  82. # * rspec-expectations 2.14.0 (http://github.com/rspec/rspec-expectations) - rspec-expectations-2.14.0
  83. # * rspec-mocks 2.14.1 (http://github.com/rspec/rspec-mocks) - rspec-mocks-2.14.1
  84. # * rspec-rails 2.14.0 (http://github.com/rspec/rspec-rails) - rspec-rails-2.14.0
  85. # * sass 3.2.9 (http://sass-lang.com/) - A powerful but elegant CSS compiler that makes CSS fun again.
  86. # * sass-rails 3.2.6 () - Sass adapter for the Rails asset pipeline.
  87. # * simplecov 0.9.0 (http://github.com/colszowka/simplecov) - Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging of coverage across test suites
  88. # * spork 1.0.0rc3 (http://github.com/sporkrb/spork) - spork
  89. # * therubyrhino 2.0.2 (http://github.com/cowboyd/therubyrhino) - Embed the Rhino JavaScript interpreter into JRuby
  90. # * thor 0.18.1 (http://whatisthor.com/) - A scripting framework that replaces rake, sake and rubigen
  91. # * tilt 1.4.1 (http://github.com/rtomayko/tilt/) - Generic interface to multiple Ruby template engines
  92. # * treetop 1.4.14 (https://github.com/cjheath/treetop) - A Ruby-based text parsing and interpretation DSL
  93. # * uglifier 2.1.2 (http://github.com/lautis/uglifier) - Ruby wrapper for UglifyJS JavaScript compressor
  94. # * xpath 2.0.0 (http://github.com/jnicklas/xpath) - Generate XPath expressions from Ruby
  95. # * blankslate 2.1.2.4 (http://github.com/masover/blankslate) - BlankSlate extracted from Builder.
  96. # * bourbon 3.1.8 (https://github.com/thoughtbot/bourbon) - Bourbon Sass Mixins using SCSS syntax.
  97. # * coffee-script 2.2.0 (http://github.com/josh/ruby-coffee-script) - Ruby CoffeeScript Compiler
  98. # * diff-lcs 1.2.4 (http://diff-lcs.rubyforge.org/) - Diff::LCS computes the difference between two Enumerable sequences using the McIlroy-Hunt longest common subsequence (LCS) algorithm
  99. # * jquery-ui-rails 4.0.3 (https://github.com/joliss/jquery-ui-rails) - jQuery UI packaged for the Rails asset pipeline
  100. # * parslet 1.4.0 (http://kschiess.github.com/parslet) - Parser construction library with great error reporting in Ruby.
  101. # * rack 1.4.5 (http://rack.github.com/) - a modular Ruby webserver interface
  102. # * rack-cache 1.2 (http://tomayko.com/src/rack-cache/) - HTTP Caching for Rack
  103. # * rack-ssl 1.3.3 (https://github.com/josh/rack-ssl) - Force SSL/TLS in your app.
  104. # * rails 3.2.12 (http://www.rubyonrails.org) - Full-stack web application framework.
  105. # * simplecov-html 0.8.0 (https://github.com/colszowka/simplecov-html) - Default HTML formatter for SimpleCov code coverage tool for ruby 1.9+
  106. # * tzinfo 0.3.37 (http://tzinfo.rubyforge.org/) - Daylight-savings aware timezone library
  107. # * warbler 1.4.0.beta1 (http://caldersphere.rubyforge.org/warbler) - Warbler chirpily constructs .war files of your Rails applications.
  108. #
  109. #########################################################################################
  110. # BSD Licensed Libraries
  111. #########################################################################################
  112. #
  113. # * activerecord-jdbc-adapter 1.2.9.1 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 2006-2012 Nick Sieger <nick@nicksieger.com>, Copyright (c) 2006-2008 Ola Bini <ola.bini@gmail.com>
  114. # * jdbc-postgres 9.2.1004 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 1997-2011, PostgreSQL Global Development Group
  115. # * d3js 3.5.16 (https://d3js.org/) Copyright (c) 2015 Mike Bostock
  116. #
  117. #########################################################################################
  118. # Ruby Licensed Libraries
  119. #########################################################################################
  120. #
  121. # * json 1.8.0 (http://json-jruby.rubyforge.org/) - JSON implementation for JRuby
  122. # * rubyzip 0.9.9 (http://github.com/aussiegeek/rubyzip) - rubyzip is a ruby module for reading and writing zip files
  123. # * httpclient 2.3.4.1 (http://github.com/nahi/httpclient) - gives something like the functionality of libwww-perl (LWP) in Ruby
  124. # * test-unit 2.5.5 (http://test-unit.rubyforge.org/) - test-unit - Improved version of Test::Unit bundled in Ruby 1.8.x.
  125. #
  126. #########################################################################################
  127. # Public domain - creative commons Licensed Libraries
  128. #########################################################################################
  129. #
  130. # * torquebox 3.1.2 (http://torquebox.org/) - TorqueBox Gem
  131. # * torquebox-cache 3.1.2 (http://torquebox.org/) - TorqueBox Cache Gem
  132. # * torquebox-configure 3.1.2 (http://torquebox.org/) - TorqueBox Configure Gem
  133. # * torquebox-core 3.1.2 (http://torquebox.org/) - TorqueBox Core Gem
  134. # * torquebox-messaging 3.1.2 (http://torquebox.org/) - TorqueBox Messaging Client
  135. # * torquebox-naming 3.1.2 (http://torquebox.org/) - TorqueBox Naming Client
  136. # * torquebox-rake-support 3.1.2 (http://torquebox.org/) - TorqueBox Rake Support
  137. # * torquebox-security 3.1.2 (http://torquebox.org/) - TorqueBox Security Gem
  138. # * torquebox-server 3.1.2 (http://torquebox.org/) - TorqueBox Server Gem
  139. # * torquebox-stomp 3.1.2 (http://torquebox.org/) - TorqueBox STOMP Support
  140. # * torquebox-transactions 3.1.2 (http://torquebox.org/) - TorqueBox Transactions Gem
  141. # * torquebox-web 3.1.2 (http://torquebox.org/) - TorqueBox Web Gem
  142. #
  143. #########################################################################################
  144. # Apache Licensed Libraries
  145. #########################################################################################
  146. #
  147. # * addressable 2.3.8 (https://github.com/sporkmonger/addressable) - URI Implementation
  148. # * bcrypt-ruby 3.0.1 (http://bcrypt-ruby.rubyforge.org) - OpenBSD's bcrypt() password hashing algorithm.
  149. # * database_cleaner 1.4.0 (http://github.com/bmabey/database_cleaner) - Strategies for cleaning databases. Can be used to ensure a clean state for testing.
  150. # * annotate 2.5.0 (http://github.com/ctran/annotate_models) - Annotates Rails Models, routes, fixtures, and others based on the database schema.
  151. # * nvd3 1.8.4 (http://nvd3.org/) Copeyright (c) 2014 Novus Partners - chart library based on d3js
  152. # * smack 3.3.1 (https://www.igniterealtime.org/projects/smack/) - XMPP library
  153. #
  154. #########################################################################################
  155. # LGPL
  156. #########################################################################################
  157. #
  158. # * jruby-1.7.4
  159. # * jruby-jars 1.7.4 (http://github.com/jruby/jruby/tree/master/gem/jruby-jars) - The core JRuby code and the JRuby stdlib as jar
  160. # ** JRuby is tri-licensed GPL, LGPL, and EPL.
  161. #
  162. #########################################################################################
  163. # MPL Licensed Libraries
  164. #########################################################################################
  165. #
  166. # * therubyrhino_jar 1.7.4 (http://github.com/cowboyd/therubyrhino) - Rhino's jars packed for therubyrhino
  167. #
  168. #########################################################################################
  169. # Artistic 2.0
  170. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  171. #
  172. #########################################################################################
  173. #
  174. #########################################################################################
  175. # GPL-2
  176. #########################################################################################
  177. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  178. #
  179. #########################################################################################
  180. # No License Given
  181. #########################################################################################
  182. #
  183. # * spork-testunit 0.0.8 (http://github.com/timcharper/spork-testunit) - spork-testunit
  184. # * sprockets 2.2.2 (http://getsprockets.org/) - Rack-based asset packaging system
  185. #
  186. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  187. require 'java'
  188. # require 'lib/oadr/oadr2b.jar'
  189. # $CLASSPATH << "lib/oadr/oadr2b.jar"
  190. require 'oadr_service.rb'
  191. java_import Java::epri.oadr2b.lib.OadrRegisterReport
  192. java_import Java::epri.oadr2b.lib.OadrRegisteredReport
  193. java_import Java::epri.oadr2b.lib.OadrReportRequest
  194. java_import Java::epri.oadr2b.lib.ReportSpecifier
  195. java_import Java::epri.oadr2b.lib.OadrCreateReport
  196. java_import Java::epri.oadr2b.lib.OadrReportRequest
  197. java_import Java::epri.oadr2b.lib.OadrCreatedReport
  198. java_import Java::epri.oadr2b.lib.OadrUpdateReport
  199. java_import Java::epri.oadr2b.lib.OadrReport
  200. #java_import Java::epri.oadr2b.lib.Interval
  201. java_import Java::epri.oadr2b.lib.OadrReportPayloadType
  202. java_import Java::epri.oadr2b.lib.SignalPayloadType
  203. java_import Java::epri.oadr2b.lib.OadrPayloadResourceStatusType
  204. java_import Java::epri.oadr2b.lib.OadrCancelReport
  205. java_import Java::epri.oadr2b.lib.OadrCanceledReport
  206. # java_import Java::epri.oadr2b.lib.OadrCreateOpt
  207. class ReportService < OadrService
  208. ########################################################
  209. def initialize
  210. super()
  211. register_function("epri.oadr2b.lib.OadrRegisterReport", :process_register_report)
  212. register_function("epri.oadr2b.lib.OadrRegisteredReport", :process_registered_report)
  213. register_function("epri.oadr2b.lib.OadrCreatedReport", :process_created_report)
  214. register_function("epri.oadr2b.lib.OadrCanceledReport", :process_canceled_report)
  215. register_function("epri.oadr2b.lib.OadrUpdateReport", :process_update_report)
  216. end
  217. ########################################################
  218. # @param [CreateReport] create_report
  219. def queue_create_report(create_report)
  220. oadr_create_report = OadrCreateReport.new
  221. oadr_create_report.request_id = create_report.request_id
  222. create_report.report_requests.each do |report_request|
  223. oadr_report_request = OadrReportRequest.new
  224. oadr_report_request.report_request_id = report_request.request_id
  225. oadr_report_request.report_specifier = Oadr2bFactory.create_report_specifier(report_request)
  226. oadr_create_report.oadr_report_requests.add(oadr_report_request)
  227. end
  228. payload = Oadr2bFactory.wrap_oadr_payload(oadr_create_report, 'set_oadr_create_report')
  229. Marshalling20b.instance.queue_message(create_report.ven, payload, 'EiReport', false)
  230. end
  231. ########################################################
  232. # @param [OadrUpdateReport] update_report
  233. def process_update_report(update_report)
  234. ven_id = update_report.ven_id
  235. request_id = update_report.request_id
  236. begin
  237. ven = validate_ven(ven_id, request_id)
  238. rescue OadrException => e
  239. return Oadr2bFactory.create_updated_report(ven_id, e.code, e.description, request_id)
  240. end
  241. begin
  242. # load the reports/intervals
  243. update_report.oadr_reports.each do |oadr_report|
  244. # oadr_report = OadrReport.new
  245. # conformance rule 304: validate report_request_id
  246. report_request = ReportRequest.find_by_request_id(oadr_report.report_request_id)
  247. if report_request.nil? or report_request.report.ven.id != ven.id
  248. return Oadr2bFactory.create_updated_report(ven_id, '452', 'Invalid ID', request_id)
  249. end
  250. report_instance = ReportInstance.new
  251. report_instance.report = ven.reports.find_by_report_specifier_id(oadr_report.report_specifier_id)
  252. report_instance.dtstart = ZuluTime.gregorian_calendar_to_ruby_datetime(oadr_report.dtstart.date_time)
  253. report_instance.duration = oadr_report.duration.duration if not oadr_report.duration.nil?
  254. report_instance.ei_report_id = oadr_report.ei_report_id
  255. report_instance.report_request_id = oadr_report.report_request_id
  256. report_instance.save
  257. save_intervals(report_instance, oadr_report)
  258. end
  259. rescue Exception => e
  260. OadrLogger.instance.log_caught_exception(e)
  261. return Oadr2bFactory.create_updated_report(ven_id, "500", "Internal Server Error", request_id)
  262. end
  263. Oadr2bFactory.create_updated_report(ven_id, '200', 'OK', request_id)
  264. end
  265. ########################################################
  266. # @param [OadrCreatedReport] created_report
  267. def process_created_report(created_report)
  268. ven_id = created_report.ven_id
  269. request_id = created_report.ei_response.request_id
  270. ven = validate_ven(ven_id, request_id)
  271. created_report = CreateReport.find_by_request_id(request_id)
  272. if not created_report.nil?
  273. created_report.active = true
  274. created_report.save
  275. end
  276. oadr_response = Oadr2bFactory.create_oadr_response(ven_id, '200', 'OK', request_id)
  277. oadr_response
  278. end
  279. ########################################################
  280. # @param [CreateReport] create_report
  281. def queue_cancel_report(create_report, report_to_follow)
  282. oadr_cancel_report = OadrCancelReport.new
  283. ven = create_report.ven
  284. oadr_cancel_report.request_id = create_report.request_id # SecureRandom.hex(10)
  285. create_report.report_requests.each do |report_request|
  286. # oadr_cancel_report.report_request_ids.add(create_report.request_id)
  287. oadr_cancel_report.report_request_ids.add(report_request.request_id)
  288. end
  289. oadr_cancel_report.report_to_follow = report_to_follow
  290. oadr_cancel_report.ven_id = ven.ven_id
  291. payload = Oadr2bFactory.wrap_oadr_payload(oadr_cancel_report, 'set_oadr_cancel_report')
  292. Marshalling20b.instance.queue_message(ven, payload, 'EiReport', false)
  293. end
  294. ########################################################
  295. # @param [OadrCanceledReport] canceled_report
  296. def process_canceled_report(canceled_report)
  297. ven_id = canceled_report.ven_id
  298. request_id = canceled_report.ei_response.request_id
  299. ven = validate_ven(ven_id, request_id)
  300. created_report = CreateReport.find_by_request_id(request_id)
  301. if not created_report.nil?
  302. created_report.active = false
  303. created_report.save
  304. end
  305. oadr_response = Oadr2bFactory.create_oadr_response(ven_id, '200', 'OK', request_id)
  306. oadr_response
  307. end
  308. ########################################################
  309. # @param [OadrRegisterReport] register_report
  310. def process_register_report(register_report)
  311. # for code completion: remember to remove
  312. #register_report = OadrRegisterReport.new
  313. ven_id = register_report.ven_id
  314. request_id = register_report.request_id
  315. begin
  316. ven = validate_ven(ven_id, request_id)
  317. rescue OadrException => e
  318. return Oadr2bFactory.created_registered_report(e.code, e.description, request_id)
  319. end
  320. # deactivate existing reports and intervals
  321. ven.reports.each do |report|
  322. report.is_active = false
  323. report.save
  324. report.report_interval_descriptions.each do |report_interval_description|
  325. report_interval_description.is_active = false
  326. report_interval_description.save
  327. end
  328. end
  329. begin
  330. # Destroy any existing `CreateReport` instances before entering the loop
  331. # in which new ones are created
  332. ven.create_reports.destroy_all
  333. register_report.oadr_reports.each do |oadr_report|
  334. report = ven.reports.find_by_report_specifier_id(oadr_report.report_specifier_id)
  335. report = ven.reports.new if report.nil?
  336. report.is_active = true
  337. report.duration = oadr_report.duration.duration
  338. report.report_request_id = oadr_report.report_request_id
  339. report.report_specifier_id = oadr_report.report_specifier_id
  340. report.report_name = ReportName.find_by_name(oadr_report.report_name)
  341. report.created_date_time = ZuluTime.gregorian_calendar_to_ruby_datetime(oadr_report.created_date_time)
  342. report.save
  343. save_interval_descriptions(report, oadr_report)
  344. generate_report_requests(report)
  345. end
  346. rescue Exception => e
  347. OadrLogger.instance.log_caught_exception(e)
  348. return Oadr2bFactory.created_registered_report("500", "Internal Server Error", request_id)
  349. end
  350. Oadr2bFactory.created_registered_report('200', 'OK', request_id)
  351. end
  352. ########################################################
  353. def process_registered_report(registered_report)
  354. ven_id = registered_report.ven_id
  355. request_id = registered_report.ei_response.request_id
  356. ven = validate_ven(ven_id, request_id)
  357. oadr_response = Oadr2bFactory.create_oadr_response(ven_id, "200", "OK", request_id)
  358. oadr_response
  359. end
  360. private
  361. ########################################################
  362. # @param [ReportInstance] report_instance
  363. # @param [OadrReport] oadr_report
  364. def save_intervals(report_instance, oadr_report)
  365. oadr_report.intervals.intervals.each do |oadr_interval|
  366. # for code completion... don't forget to remove
  367. # oadr_interval = Java::epri.oadr2b.lib.Interval.new
  368. oadr_interval.stream_payload_bases.each do |stream_payload|
  369. report_interval = ReportInterval.new
  370. report_interval.report_instance = report_instance
  371. report_interval.report = report_instance.report
  372. report_interval.dtstart = ZuluTime.gregorian_calendar_to_ruby_datetime(oadr_interval.dtstart.date_time) if not oadr_interval.dtstart.nil?
  373. # conformance rule 314: use dtstart from report if Interval does not contain a dtstart
  374. # if both dtstart values are null, what should the server do?
  375. report_interval.dtstart = ZuluTime.gregorian_calendar_to_ruby_datetime(oadr_report.dtstart.date_time) if report_interval.dtstart.nil?
  376. report_interval.duration = oadr_interval.duration.duration unless oadr_interval.duration.nil?
  377. report_interval.uid = oadr_interval.uid.text unless oadr_interval.uid.nil?
  378. stream_payload_value = stream_payload.get_value
  379. if stream_payload_value.java_class.to_s == 'epri.oadr2b.lib.OadrReportPayloadType'
  380. report_interval.rid = stream_payload_value.rid
  381. report_interval.report_interval_description = report_instance.report.report_interval_descriptions.find_by_rid(stream_payload_value.rid)
  382. report_interval.confidence = stream_payload_value.confidence
  383. report_interval.accuracy = stream_payload_value.accuracy
  384. report_interval.data_quality = DataQuality.find_by_name(stream_payload_value.oadr_data_quality)
  385. payload = stream_payload_value.payload_base.get_value
  386. if payload.java_class.to_s == 'epri.oadr2b.lib.PayloadFloatType'
  387. report_interval.value = payload.value
  388. elsif payload.java_class.to_s == 'epri.oadr2b.lib.OadrPayloadResourceStatusType'
  389. # TODO: Add 16 additional `oadrLoadControlStateType`s (GitHub issue 199)
  390. report_interval.online = payload.is_oadr_online
  391. report_interval.manual_override = payload.is_oadr_manual_override
  392. end
  393. elsif stream_payload_value.java_class == 'epri.oadr2b.lib.SignalPayloadType'
  394. end
  395. begin
  396. report_interval.save!
  397. rescue ActiveRecord::RecordInvalid => e
  398. if e.message =~ /dtstart has already been taken/i # uniqueness validation scoped to :report_interval_description_id
  399. # Remove (non-destructively) any nil attrs from the not-yet-persisted report_interval
  400. # (especially important for :id, :created_at, and :updated_at)
  401. fresh_interval_attrs = report_interval.attributes.keep_if { |key, value| !value.nil? }
  402. # Find the existing conflicting report interval record
  403. # Note the use of .first to ensure one record is returned, but there's a unique key on report_interval_description_id/dtstart
  404. # so there should be only one
  405. existing_interval = ReportInterval.where(
  406. report_interval_description_id: report_interval.report_interval_description_id, dtstart: report_interval.dtstart).first
  407. # Update existing_interval with the attributes of the
  408. # "fresh" report interval we originally tried to save
  409. existing_interval.assign_attributes(fresh_interval_attrs)
  410. # Save the updates to the existing report_interval record
  411. existing_interval.save
  412. else
  413. OadrLogger.instance.log_caught_exception(e)
  414. end
  415. rescue Exception => e
  416. OadrLogger.instance.log_caught_exception(e)
  417. end
  418. end
  419. end
  420. end
  421. ########################################################
  422. def save_interval_descriptions(report, oadr_report)
  423. oadr_report.oadr_report_descriptions.each do |description|
  424. report_interval_description = report.report_interval_descriptions.find_by_rid(description.rid)
  425. report_interval_description = report.report_interval_descriptions.new if report_interval_description.nil?
  426. report_interval_description.rid = description.rid
  427. report_interval_description.is_active = true
  428. # TODO: do we need this value?
  429. # report_interval_description.report_subject = description.report_subject
  430. report_interval_description.report_data_source = extract_data_source(description.report_data_source)
  431. report_interval_description.report_type = ReportType.find_by_name(description.report_type)
  432. report_interval_description.emix_item = description.item_base.value.class.name if not description.item_base.nil?#description.item_base.value.to_string if not description.item_base.nil?
  433. report_interval_description.reading_type = ReadingType.find_by_name(description.reading_type)
  434. report_interval_description.market_context = description.market_context
  435. set_interval_type(report_interval_description, description)
  436. if not description.oadr_sampling_rate.nil?
  437. report_interval_description.sampling_rate_min_period = description.oadr_sampling_rate.oadr_min_period
  438. report_interval_description.sampling_rate_max_period = description.oadr_sampling_rate.oadr_max_period
  439. report_interval_description.sampling_rate_on_change = description.oadr_sampling_rate.is_oadr_on_change
  440. end
  441. report_interval_description.save
  442. end
  443. end
  444. ########################################################
  445. def generate_report_requests(report)
  446. interval_descriptions = report.report_interval_descriptions.active
  447. # Only attempt matching for `ReportRequestConfiguration` instances
  448. # with a `:report_name_id` which matches that of `report`
  449. report_request_configurations = ReportRequestConfiguration.where(report_name_id: report.report_name_id)
  450. configurations_with_matches = []
  451. OadrLogger.instance.log_info("Evaluating Report #{ report.id } for auto ReportRequest generation.")
  452. report_request_configurations.each do |configuration|
  453. matching_interval_descriptions = interval_descriptions.where(
  454. sampling_rate_min_period: configuration.sampling_rate_min_period
  455. )
  456. OadrLogger.instance.log_info("--> Configuration '#{ configuration.name }' matched #{ matching_interval_descriptions.count }")
  457. unless matching_interval_descriptions.empty?
  458. config_and_matches_hash = { configuration: configuration, matches: matching_interval_descriptions }
  459. configurations_with_matches << config_and_matches_hash
  460. end
  461. end
  462. unless configurations_with_matches.empty?
  463. create_report = report.ven.create_reports.new
  464. create_report.name = "Auto Report #{ report.ven.ven_id }"
  465. create_report.request_id = CreateReport.generate_request_id
  466. create_report.save!
  467. OadrLogger.instance.log_info("--> CreateReport '#{ create_report.name }' created!")
  468. configurations_with_matches.each do |hash|
  469. report_request = create_report.report_requests.new()
  470. report_request.report = report
  471. report_request.dtstart = Time.now
  472. report_request.duration = 'PT0S'
  473. report_request.granularity = hash[:configuration].granularity
  474. report_request.report_back_duration = hash[:configuration].report_back_duration
  475. report_request.request_id = ReportRequest.generate_request_id
  476. report_request.save!
  477. OadrLogger.instance.log_info("-->--> ReportRequest '#{ report_request.request_id }' created!")
  478. hash[:matches].each do |interval_description|
  479. report_request_description = report_request.report_request_descriptions.new()
  480. report_request_description.report_request = report_request
  481. report_request_description.report_interval_description = interval_description
  482. report_request_description.save!
  483. OadrLogger.instance.log_info("-->-->--> ReportRequestDescription instance created!")
  484. end
  485. end
  486. # Make newly-generated ReportRequest instances available to the VEN
  487. queue_create_report(create_report)
  488. end
  489. end
  490. ########################################################
  491. def set_interval_type(report_interval_description, description)
  492. if not description.item_base.nil?
  493. case description.item_base.value.class.name
  494. when 'Java::EpriOadr2bLib::CurrencyType' then
  495. OadrLogger.instance.log_info(description.item_base.value.item_description.value)
  496. report_interval_description.item_description = description.item_base.value.item_description.value
  497. report_interval_description.item_units = description.item_base.value.item_units.value
  498. report_interval_description.si_scale_code = description.item_base.value.si_scale_code
  499. when 'Java::EpriOadr2bLib::EnergyRealType' then
  500. OadrLogger.instance.log_info(description.item_base.value.item_description)
  501. report_interval_description.item_description = description.item_base.value.item_description
  502. report_interval_description.item_units = description.item_base.value.item_units
  503. report_interval_description.si_scale_code = description.item_base.value.si_scale_code
  504. when 'Java::EpriOadr2bLib::PowerRealType' then
  505. OadrLogger.instance.log_info(description.item_base.value.item_description)
  506. report_interval_description.item_description = description.item_base.value.item_description
  507. report_interval_description.item_units = description.item_base.value.item_units
  508. report_interval_description.si_scale_code = description.item_base.value.si_scale_code
  509. report_interval_description.hertz = description.item_base.value.power_attributes.hertz
  510. report_interval_description.voltage = description.item_base.value.power_attributes.voltage
  511. report_interval_description.ac = description.item_base.value.power_attributes.is_ac
  512. when 'Java::EpriOadr2bLib::FrequencyType'
  513. OadrLogger.instance.log_info(description.item_base.value.item_description)
  514. report_interval_description.item_description = description.item_base.value.item_description
  515. report_interval_description.item_units = description.item_base.value.item_units
  516. report_interval_description.si_scale_code = description.item_base.value.si_scale_code
  517. when 'Java::EpriOadr2bLib::PulseCountType'
  518. OadrLogger.instance.log_info(description.item_base.value.item_description)
  519. report_interval_description.item_description = description.item_base.value.item_description
  520. report_interval_description.item_units = description.item_base.value.item_units
  521. report_interval_description.pulse_multiplier = description.item_base.value.pulse_factor
  522. when 'Java::EpriOadr2bLib::BaseUnitType'
  523. OadrLogger.instance.log_info(description.item_base.value.item_description)
  524. report_interval_description.item_description = description.item_base.value.item_description
  525. report_interval_description.item_units = description.item_base.value.item_units
  526. report_interval_description.si_scale_code = description.item_base.value.si_scale_code
  527. end
  528. end
  529. end
  530. ########################################################
  531. # @param [Java::epri.oadr2b.lib.EiTargetType] report_data_source
  532. def extract_data_source(report_data_source)
  533. return '' if report_data_source.nil? or report_data_source.resource_ids.nil?
  534. resource_ids = ''
  535. report_data_source.resource_ids.each do |resource_id|
  536. resource_ids += resource_id
  537. end
  538. resource_ids
  539. end
  540. end