oadr2b_factory.rb 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. # $CLASSPATH << "lib/oadr/oadr2b.jar"
  189. require 'marshalling20b.rb'
  190. require 'convert_event.rb'
  191. java_import Java::epri.oadr2b.lib.OadrCreatedPartyRegistration
  192. java_import Java::epri.oadr2b.lib.OadrQueryRegistration
  193. java_import Java::epri.oadr2b.lib.OadrCanceledPartyRegistration
  194. java_import Java::epri.oadr2b.lib.OadrCancelPartyRegistration
  195. java_import Java::epri.oadr2b.lib.OadrRequestReregistration
  196. java_import Java::epri.oadr2b.lib.OadrCreatedOpt
  197. java_import Java::epri.oadr2b.lib.OadrCanceledOpt
  198. java_import Java::epri.oadr2b.lib.OadrDistributeEvent
  199. java_import Java::epri.oadr2b.lib.OadrDistributeEvent::OadrEvent
  200. java_import Java::epri.oadr2b.lib.EiResponse
  201. java_import Java::epri.oadr2b.lib.EventDescriptor::EiMarketContext
  202. java_import Java::epri.oadr2b.lib.EiActivePeriod
  203. # java_import Java::epri.oadr2b.lib.CurrentValue
  204. java_import Java::epri.oadr2b.lib.PayloadFloatType
  205. java_import Java::epri.oadr2b.lib.ObjectFactory
  206. java_import Java::epri.oadr2b.lib.xcal.Properties
  207. java_import Java::epri.oadr2b.lib.xcal.Properties::Tolerance
  208. java_import Java::epri.oadr2b.lib.xcal.Properties::Tolerance::Tolerate
  209. java_import Java::epri.oadr2b.lib.xcal.Dtstart
  210. java_import Java::epri.oadr2b.lib.xcal.DurationPropType
  211. java_import Java::epri.oadr2b.lib.xcal.Uid
  212. java_import Java::epri.oadr2b.lib.OadrResponse
  213. java_import Java::epri.oadr2b.lib.xcal.DurationPropType
  214. java_import Java::java.lang.Boolean
  215. java_import Java::epri.oadr2b.lib.OadrProfiles
  216. java_import Java::epri.oadr2b.lib.OadrTransports
  217. java_import Java::epri.oadr2b.lib.OadrTransportType
  218. java_import Java::epri.oadr2b.lib.OadrPayload
  219. java_import Java::epri.oadr2b.lib.OadrSignedObject
  220. java_import Java::epri.oadr2b.lib.OadrRegisterReport
  221. java_import Java::epri.oadr2b.lib.ReportSpecifier
  222. java_import Java::epri.oadr2b.lib.xcal.Interval
  223. java_import Java::epri.oadr2b.lib.SpecifierPayload
  224. java_import Java::epri.oadr2b.lib.OadrUpdatedReport
  225. class Oadr2bFactory
  226. ########################################################
  227. def self.create_interval_payload_float(val)
  228. of = ObjectFactory.new
  229. pft = PayloadFloatType.new
  230. pft.value = val
  231. jaxb_payload_float = of.create_payload_float(pft)
  232. signal_payload_type = of.create_signal_payload_type
  233. signal_payload_type.payload_base = jaxb_payload_float
  234. jaxb_signal_payload_type = of.create_signal_payload(signal_payload_type)
  235. jaxb_signal_payload_type
  236. end
  237. ########################################################
  238. # @param [EmixUnit] emix_unit
  239. def self.create_item_base(emix_unit)
  240. ConvertEmixUnit.instance.convert_emix_unit(emix_unit)
  241. end
  242. ########################################################
  243. def self.create_dtstart(dt)
  244. dtstart = Dtstart.new
  245. dtstart.date_time = ZuluTime.get_gregorian_calendar_ruby_dt(dt)
  246. dtstart
  247. end
  248. ########################################################
  249. def self.create_report_interval(dtstart, duration_string)
  250. # return an empty interval if dtstart or duration is nil
  251. if dtstart.nil?
  252. return nil
  253. end
  254. report_interval = Java::epri.oadr2b.lib.xcal.Interval.new
  255. report_interval.properties = Properties.new
  256. report_interval.properties.dtstart = Dtstart.new
  257. report_interval.properties.dtstart.date_time = ZuluTime.get_gregorian_calendar_ruby_dt(dtstart)
  258. # `DurationPropType.new.public_send('duration=', DURATION_STRING)` necessary
  259. # to convert string (e.g. 'PT05M') to duration type expected by ReportSpecifier
  260. report_interval.properties.duration = self.create_duration_from_string(duration_string)
  261. report_interval
  262. end
  263. ########################################################
  264. # @param [ReportRequest] report_request
  265. def self.create_report_specifier(report_request)
  266. report_specifier = ReportSpecifier.new
  267. # `DurationPropType.new.public_send('duration=', DURATION_STRING)` necessary
  268. # to convert string (e.g. 'PT05M') to duration type expected by ReportSpecifier
  269. report_specifier.granularity = self.create_duration_from_string(report_request.granularity)
  270. report_specifier.report_back_duration = self.create_duration_from_string(report_request.report_back_duration)
  271. report_specifier.report_interval = self.create_report_interval(report_request.dtstart, report_request.duration)
  272. if (report_request.is_metadata?)
  273. # conformance rule 306
  274. report_specifier.report_specifier_id = "METADATA"
  275. return report_specifier
  276. end
  277. report_specifier.report_specifier_id = report_request.report.report_specifier_id
  278. not_applicable = ReadingType.find_by_name('x-notApplicable')
  279. # if no rids have been selected, request all rids for the selected report
  280. descriptons = (report_request.report_request_descriptions.count == 0 ? report_request.report.report_interval_descriptions : report_request.report_interval_descriptions)
  281. # TODO: add the intervals?
  282. # report_request.report.report_interval_descriptions.each do |description|
  283. descriptons.each do |description|
  284. specifier_payload = SpecifierPayload.new
  285. specifier_payload.rid = description.rid
  286. # specifier_payload.reading_type = description.reading_type.name
  287. specifier_payload.reading_type = not_applicable.name
  288. report_specifier.specifier_payloads.add(specifier_payload)
  289. end
  290. report_specifier
  291. end
  292. ########################################################
  293. def self.create_current_value(val)
  294. # use full name to avoid namespace collision with ActiveRecord CurrentValue
  295. current_value = Java::epri::oadr2b::lib::CurrentValue.new
  296. current_value.payload_float = PayloadFloatType.new
  297. current_value.payload_float.set_value(val)
  298. current_value
  299. end
  300. ########################################################
  301. def self.create_market_context(market_context_uri)
  302. market_context = EiMarketContext.new
  303. market_context.market_context = market_context_uri
  304. market_context
  305. end
  306. ########################################################
  307. def self.create_active_period(dtstart, duration, tolerance, notification, rampup, recovery)
  308. active_period = EiActivePeriod.new
  309. active_period.properties = Properties.new
  310. active_period.properties.dtstart = Dtstart.new
  311. active_period.properties.dtstart.date_time = ZuluTime.get_gregorian_calendar_ruby_dt(dtstart)
  312. active_period.properties.duration = self.create_duration_minutes(duration)
  313. active_period.properties.tolerance = Tolerance.new
  314. active_period.properties.tolerance.tolerate = Tolerate.new
  315. active_period.properties.tolerance.tolerate.startafter = "PT" + tolerance.to_s + "M"
  316. active_period.properties.xei_notification = self.create_duration_minutes(notification)
  317. active_period.properties.xei_ramp_up = self.create_duration_minutes(rampup)
  318. active_period.properties.xei_recovery = self.create_duration_minutes(recovery)
  319. active_period
  320. end
  321. ########################################################
  322. def self.create_response(response_code, response_description, request_id)
  323. response = EiResponse.new
  324. response.response_code = response_code
  325. response.response_description = response_description
  326. response.request_id = request_id
  327. response
  328. end
  329. ########################################################
  330. def self.create_updated_report(ven_id, response_code, response_description, request_id)
  331. updated_report = OadrUpdatedReport.new
  332. updated_report.ven_id = ven_id
  333. updated_report.ei_response = self.create_response(response_code, response_description, request_id)
  334. self.wrap_oadr_payload(updated_report, 'set_oadr_updated_report')
  335. end
  336. ########################################################
  337. def self.create_duration_from_string(duration_string)
  338. # Converts formatted duration string (e.g. 'PT06H30M00S') to proper java duration object
  339. duration_obj = DurationPropType.new
  340. duration_obj.duration = duration_string
  341. return duration_obj
  342. end
  343. ########################################################
  344. def self.create_duration_seconds(secs)
  345. # Converts duration-in-seconds integer to proper java duration object
  346. duration = DurationPropType.new
  347. duration.duration = 'PT' + secs.to_s + "S"
  348. duration
  349. end
  350. ########################################################
  351. def self.create_duration_minutes(mins)
  352. # Converts duration-in-minutes integer to proper java duration object
  353. duration = DurationPropType.new
  354. duration.duration = "PT" + mins.to_s + "M"
  355. duration
  356. end
  357. ########################################################
  358. def self.create_uid(val)
  359. uid = Uid.new
  360. uid.text = val.to_s
  361. uid
  362. end
  363. ########################################################
  364. def self.create_oadr_profiles
  365. oadr_profiles = OadrProfiles.new
  366. oadr_profiles.get_oadr_profiles.add(self.create_oadr_profile('2.0a'))
  367. oadr_profiles.get_oadr_profiles.add(self.create_oadr_profile('2.0b'))
  368. oadr_profiles
  369. end
  370. ########################################################
  371. def self.create_created_party_registration(response_code, response_description, request_id, ven_id, vtn_id, poll_interval, registration_id)
  372. created_party_registration = OadrCreatedPartyRegistration.new
  373. created_party_registration.schema_version = '2.0b'
  374. created_party_registration.registration_id = registration_id
  375. created_party_registration.ei_response = self.create_response(response_code, response_description, request_id)
  376. created_party_registration.oadr_requested_oadr_poll_freq = self.create_duration_seconds(poll_interval)
  377. created_party_registration.ven_id = ven_id
  378. created_party_registration.vtn_id = vtn_id
  379. created_party_registration.oadr_profiles = Oadr2bFactory.create_oadr_profiles
  380. # self.wrap_oadr_payload(oadr_message, oadr_message_type)
  381. # created_party_registration
  382. self.wrap_oadr_payload(created_party_registration, 'set_oadr_created_party_registration')
  383. end
  384. ########################################################
  385. def self.create_canceled_registration(response_code, response_description, ven_id, request_id, registration_id)
  386. canceled_party_registration = OadrCanceledPartyRegistration.new
  387. canceled_party_registration.schema_version = '2.0b'
  388. canceled_party_registration.ei_response = self.create_response(response_code, response_description, request_id)
  389. canceled_party_registration.registration_id = registration_id
  390. canceled_party_registration.ven_id = ven_id
  391. self.wrap_oadr_payload(canceled_party_registration, 'set_oadr_canceled_party_registration')
  392. end
  393. ########################################################
  394. def self.create_created_opt(response_code, response_description, request_id, opt_id)
  395. created_opt = OadrCreatedOpt.new
  396. created_opt.schema_version = '2.0b'
  397. created_opt.ei_response = self.create_response(response_code, response_description, request_id)
  398. created_opt.opt_id = opt_id
  399. self.wrap_oadr_payload(created_opt, 'set_oadr_created_opt')
  400. end
  401. ########################################################
  402. def self.create_canceled_opt(response_code, response_description, request_id, opt_id)
  403. canceled_opt = OadrCanceledOpt.new
  404. canceled_opt.schema_version = '2.0b'
  405. canceled_opt.ei_response = self.create_response(response_code, response_description, request_id)
  406. canceled_opt.opt_id = opt_id
  407. self.wrap_oadr_payload(canceled_opt, 'set_oadr_canceled_opt')
  408. end
  409. ########################################################
  410. def self.create_cancel_registration(ven_id, registration_id)
  411. cancel_registration = OadrCancelPartyRegistration.new
  412. cancel_registration.schema_version = '2.0b'
  413. cancel_registration.ven_id = ven_id
  414. cancel_registration.registration_id = registration_id
  415. cancel_registration.request_id = SecureRandom.hex(10)
  416. self.wrap_oadr_payload(cancel_registration, 'set_oadr_cancel_party_registration')
  417. end
  418. ########################################################
  419. def self.create_distribute_event(response_code, response_description, request_id, vtn_id, ven)
  420. distribute_event = OadrDistributeEvent.new
  421. distribute_event.schema_version = '2.0b'
  422. distribute_event.ei_response = EiResponse.new
  423. distribute_event.ei_response.response_code = response_code
  424. distribute_event.ei_response.response_description = response_description
  425. distribute_event.ei_response.request_id = request_id
  426. distribute_event.request_id = SecureRandom.hex(10)
  427. distribute_event.vtn_id = vtn_id
  428. # return distribute_event if events.nil?
  429. if not ven.nil? and not ven.sorted_events.nil?
  430. ven.sorted_events.each do |event|
  431. # only include published events in the payload
  432. if event.published?
  433. oadr_event = ConvertEvent.event_to_oadr_event(event, ven)
  434. distribute_event.oadr_events.add(oadr_event)
  435. end
  436. end
  437. end
  438. self.wrap_oadr_payload(distribute_event, 'set_oadr_distribute_event')
  439. end
  440. ########################################################
  441. def self.create_register_report(ven_id)
  442. register_report = OadrRegisterReport.new
  443. register_report.request_id = SecureRandom.hex(10)
  444. register_report.ven_id = ven_id
  445. self.wrap_oadr_payload(register_report, 'set_oadr_register_report')
  446. end
  447. ########################################################
  448. def self.created_registered_report(response_code, response_description, request_id)
  449. registered_report = OadrRegisteredReport.new
  450. registered_report.ei_response = self.create_response(response_code, response_description, request_id)
  451. self.wrap_oadr_payload(registered_report, 'set_oadr_registered_report')
  452. end
  453. ########################################################
  454. private
  455. # set the oadr schema version and wrap the object
  456. # with a payload and signed object
  457. # ALL oadr messages should call this function after creating the message
  458. def self.wrap_oadr_payload(oadr_message, oadr_message_type)
  459. oadr_message.schema_version = '2.0b'
  460. oadr_payload = OadrPayload.new
  461. oadr_payload.oadr_signed_object = OadrSignedObject.new
  462. oadr_payload.oadr_signed_object.public_send(oadr_message_type, oadr_message)
  463. oadr_payload
  464. end
  465. ########################################################
  466. def self.create_oadr_profile(profile_name)
  467. oadr_profile = OadrProfiles::OadrProfile.new
  468. oadr_profile.oadr_profile_name = profile_name
  469. oadr_profile.oadr_transports = self.create_oadr_transports
  470. oadr_profile
  471. end
  472. ########################################################
  473. def self.create_oadr_transports
  474. oadr_transports = OadrTransports.new
  475. oadr_transport = OadrTransports::OadrTransport.new
  476. oadr_transport.oadr_transport_name = OadrTransportType::SIMPLE_HTTP
  477. oadr_transports.get_oadr_transports.add(oadr_transport)
  478. oadr_transport = OadrTransports::OadrTransport.new
  479. oadr_transport.oadr_transport_name = OadrTransportType::XMPP
  480. oadr_transports.get_oadr_transports.add(oadr_transport)
  481. oadr_transports
  482. end
  483. ########################################################
  484. def self.create_oadr_reregistration(ven_id)
  485. request_registration = OadrRequestReregistration.new
  486. request_registration.schema_version = '2.0b'
  487. request_registration.ven_id = ven_id
  488. self.wrap_oadr_payload(request_registration, 'set_oadr_request_reregistration')
  489. end
  490. ########################################################
  491. def self.create_oadr_response(ven_id, response_code, response_description, request_id)
  492. oadr_response = OadrResponse.new
  493. oadr_response.schema_version = '2.0b'
  494. oadr_response.ei_response = self.create_response(response_code, response_description, request_id)
  495. oadr_response.ven_id = ven_id if not ven_id.nil?
  496. self.wrap_oadr_payload(oadr_response, 'set_oadr_response')
  497. end
  498. end