-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamaha-node.js
77 lines (66 loc) · 2.12 KB
/
yamaha-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// yamaha-js.js libary by derz
// implementation of the Yamaha Android audio controller Streo whatever XML interface
const fetch = require("node-fetch")
const xml = require("xml2js")
const fs = require("fs").promises
const { Value, Cache, Range, Toggle, Type, TypeList, Input, InputList, Location, Feature, FeatureList, Resource, FileResource, StringResource, RequestCont, Request, } = require("./yamaha-utils.js")
// re exporting some stuff:
module.exports = {Resource, FileResource, StringResource }
class Definition {
constructor( data ) {
this.update( data )
}
update( data ) {
this.endpoint = data.endpoint
this.features = new FeatureList( data.features )
this.requests = new RequestCont( data.requests )
}
}
class Host {
constructor( host ) {
this.host = host
this.d = new Definition(require("./definition.json"))
this.endpoint = `http://${this.host}${this.d.endpoint}`
this.features = this.d.features
this.requests = this.d.requests
}
getFeature( feature ) {
return this.getRequest( this.requests.byName(feature.locationGet.sReqId) )
.then( res => feature.locationGet.location.apply( res ) )
.then( val => feature.parse(val))
}
setFeature( feature, value ) {
// invalidate cache of request type:
this.requests.byName(feature.locationGet.sReqId).cache.clear()
return this.setRequest( feature.setRequest, value.raw ? value.raw : value ).then(
res => res.YAMAHA_AV.$.RC
)
}
async getRequest( request ) {
if( request.cacheValid ) // if still has validcache
return request.getCache()
let res = await fetch( this.endpoint, {
method: "post",
body: request.data,
headers: {
"Content-Type": "application/xml",
}
})
let text = await res.text()
let parsed = await xml.parseStringPromise( text )
request.updateCache( parsed );
return parsed
}
async setRequest( request, data ) {
const res = await fetch( this.endpoint, {
method: "post",
body: request.data.replace("${data}",data),
headers: {
"Content-Type": "application/xml",
}
}).then(res => res.text())
const parsed = xml.parseStringPromise(res)
return parsed
}
}
module.exports.Host = Host