Skip to content

Commit

Permalink
Resolves issue #5 and issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Sep 19, 2019
1 parent 7eac202 commit d3d9140
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/data-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class DataWriter {
.ele('published', {}, publishedDate).up()
.ele('updated', {}, publishedDate).up().up()
.ele('entry')
.ele('id', {}, 'urn:uuid:' + DataWriter.getId()).up()
.ele('content')
.ele('ReadingType', xmlEspiNs)
.ele('accumulationBehaviour', {}, 4).up()
Expand All @@ -153,8 +154,9 @@ class DataWriter {
.ele('published', {}, publishedDate).up()
.ele('updated', {}, publishedDate).up().up()
.ele('entry')
.ele('id', {}, 'urn:uuid:' + DataWriter.getId()).up()
.ele('content')
.ele('IntervalBLock', xmlEspiNs)
.ele('IntervalBlock', xmlEspiNs)
.ele('interval')
.ele('duration', {}, this.intervalDuration).up()
.ele('start', {}, this.getStartTimestamp()).up()
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"devDependencies": {
"mocha": "^6.1.4",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"xml-parser": "^1.2"
},
"pre-commit": [
"standard"
Expand Down
29 changes: 28 additions & 1 deletion test/data-writer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var file = require('fs')
var xmlParser = require('xml-parser')
var assert = require('assert')
var DataWriter = require('../lib/index').DataWriter
var options = {
Expand Down Expand Up @@ -63,7 +65,6 @@ describe('data-writer', function() {
let expectedFeed = '<feed xmlns="http://www.w3.org/2005/Atom">'
let dataWriter = new DataWriter(options)
let result = dataWriter.output(true)
console.log(result)

assert.strictEqual(result.includes(expectedXmlHead), true)
assert.strictEqual(result.includes(expectedFeed), true)
Expand All @@ -72,3 +73,29 @@ describe('data-writer', function() {
})
})
})

describe('data-writer', function() {
describe('#output()', function() {
it('can validate Green Button XML Data tree', function(done) {
let xml = file.readFileSync(__dirname + '/./fixtures/test.xml', 'utf-8')
let result = xmlParser(xml)

assert.strictEqual(result.root.name, 'feed')
assert.strictEqual(result.root.attributes.xmlns, 'http://www.w3.org/2005/Atom')
assert.strictEqual(result.root.children.length, 8)

assert.strictEqual(result.root.children[0].name, 'entry')
assert.strictEqual(result.root.children[0].children[0].name, 'id')
assert.strictEqual(result.root.children[0].children[0].content, 'urn:uuid:CB6E2EA6-745B-431D-86F2-5A5E616C9661')
assert.strictEqual(result.root.children[0].children[1].name, 'title')
assert.strictEqual(result.root.children[0].children[2].name, 'content')
assert.strictEqual(result.root.children[0].children[2].children[0].name, 'UsagePoint')
assert.strictEqual(result.root.children[1].children[0].children[0].name, 'LocalTimeParameters')
assert.strictEqual(result.root.children[2].children[0].children[0].name, 'MeterReading')
assert.strictEqual(result.root.children[3].children[0].children[0].name, 'ReadingType')
assert.strictEqual(result.root.children[4].children[0].children[0].name, 'IntervalBlock')

done()
})
})
})
64 changes: 57 additions & 7 deletions test/fixtures/test.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>urn:uuid:23AC4BEC-B3CD-41DC-B39B-2F8BCB4768EC</id>
<title>GreenButton User XXXX Feed</title>
<updated>2012-10-24T00:00:00Z</updated>

<entry>
<id>urn:uuid:CB6E2EA6-745B-431D-86F2-5A5E616C9661</id>
<title/>
<link rel="self" href="RetailCustomer/9b6c7063/UsagePoint/01"/>
<link rel="up" href="RetailCustomer/9b6c7063/UsagePoint"/>
<link rel="related" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading"/>
<link rel="related" href="LocalTimeParameters/01"/>
<title>Single family</title>
<content>
<UsagePoint xmlns="http://naesb.org/espi">
<ServiceCategory>
Expand All @@ -13,11 +21,48 @@
<published>2019-07-22T16:38:13Z</published>
<updated>2019-07-22T16:38:13Z</updated>
</entry>

<entry>
<id>urn:uuid:FE317A0A-F7F5-4307-B158-28A34276E862</id>
<link rel="self" href="LocalTimeParameters/01"/>
<link rel="up" href="LocalTimeParameters"/>
<title>DST For Taiwan</title>
<content>
<LocalTimeParameters xmlns="http://naesb.org/espi">
<dstEndRule>B40E2000</dstEndRule>
<dstOffset>3600</dstOffset>
<dstStartRule>360E2000</dstStartRule>
<tzOffset>-18000</tzOffset>
</LocalTimeParameters>
</content>
<published>2019-07-22T16:38:13Z</published>
<updated>2019-07-22T16:38:13Z</updated>
</entry>

<entry>
<id>urn:uuid:9BCDAB06-6690-46A3-9253-A451AF4077D8</id>
<link rel="self" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading/01"/>
<link rel="up" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading"/>
<link rel="related" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading/01/IntervalBlock"/>
<link rel="related" href="ReadingType/07"/>
<title>Hourly Electricity Consumption</title>
<content>
<MeterReading xmlns="http://naesb.org/espi"/>
</content>
<published>2012-10-24T00:00:00Z</published>
<updated>2012-10-24T00:00:00Z</updated>
</entry>

<entry>
<id>urn:uuid:BEB04FF1-6294-4916-95AC-5597070C95D4</id>
<link rel="self" href="ReadingType/07"/>
<link rel="up" href="ReadingType"/>
<title>Energy Delivered (Wh)</title>
<content>
<ReadingType xmlns="http://naesb.org/espi">
<accumulationBehaviour>4</accumulationBehaviour>
<commodity>1</commodity>
<currency>840</currency>
<dataQualifier>12</dataQualifier>
<flowDirection>1</flowDirection>
<intervalLength>3600</intervalLength>
Expand All @@ -28,24 +73,29 @@
<uom>72</uom>
</ReadingType>
</content>
<published>2019-07-22T16:38:13Z</published>
<updated>2019-07-22T16:38:13Z</updated>
<published>2012-10-24T00:00:00Z</published>
<updated>2012-10-24T00:00:00Z</updated>
</entry>

<entry>
<id>urn:uuid:FE317A0A-F7F5-4307-B158-28A34276E862</id>
<link rel="self" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading/01/IntervalBlock/0173"/>
<link rel="up" href="RetailCustomer/9b6c7063/UsagePoint/01/MeterReading/01/IntervalBlock"/>
<title/>
<content>
<IntervalBLock xmlns="http://naesb.org/espi">
<IntervalBlock xmlns="http://naesb.org/espi">
<interval>
<duration>86400</duration>
<start>1563440400</start>
</interval>
<IntervalReading>
<timePeriod>
<duration>3600</duration>
<start>2019-07-18 17:00:00</start>
<start>1563440400</start>
</timePeriod>
<value>912</value>
</IntervalReading>
</IntervalBLock>
</IntervalBlock>
</content>
<published>2019-07-22T16:38:13Z</published>
<updated>2019-07-22T16:38:13Z</updated>
Expand Down

0 comments on commit d3d9140

Please sign in to comment.