Last active
December 7, 2021 20:34
-
-
Save mrzmyr/3aa34e268c645751402bf00baaf663f3 to your computer and use it in GitHub Desktop.
SMA EV Charger - Get/Set Paramter API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const querystring = require('querystring'); | |
const axios = require('axios'); | |
const https = require('https'); | |
const USERNAME = ''; | |
const PASSWORD = ''; | |
const ROOT_URL = '' | |
class Client { | |
constructor(options) { | |
this.options = options; | |
this.client = axios.create({ | |
baseURL: ROOT_URL, | |
httpsAgent: new https.Agent({ | |
rejectUnauthorized: false | |
}) | |
}); | |
} | |
async auth() { | |
return this.client({ | |
url: `/token`, | |
method: 'POST', | |
headers: { | |
'content-type': 'application/x-www-form-urlencoded' | |
}, | |
data: querystring.stringify({ | |
...this.options, | |
grant_type: "password", | |
}), | |
}).then(resp => { | |
this.access_token = resp.data.access_token | |
console.debug('authenticated', resp.data.access_token); | |
return resp.data; | |
}) | |
} | |
async _req(options) { | |
console.debug('request', options); | |
return this.client({ | |
method: 'GET', | |
headers: { | |
Authorization: `Bearer ${this.access_token}` | |
}, | |
...options, | |
}).then(resp => { | |
return resp.data; | |
}) | |
} | |
async getMeasurments() { | |
return this._req({ | |
method: 'POST', | |
url: '/measurements/live', | |
data: [{"componentId":"IGULD:SELF"}] | |
}) | |
} | |
async getParameters() { | |
return this._req({ | |
method: 'POST', | |
url: '/parameters/search/', | |
data: { "queryItems":[{"componentId":"IGULD:SELF"}] } | |
}) | |
} | |
async getParameter(id) { | |
return this.getParameters().then(resp => { | |
return resp[0].values.find(param => param.channelId === id); | |
}); | |
} | |
async setParameter(id, value) { | |
return this._req({ | |
url: '/parameters/IGULD:SELF', | |
method: 'PUT', | |
data: { | |
"values": [{ | |
"timestamp": (new Date()).toISOString(), | |
"channelId": id, | |
"value": value | |
}] | |
}, | |
}).then(resp => { | |
return resp.data; | |
}); | |
} | |
} | |
(async () => { | |
try { | |
const client = new Client({ | |
username: USERNAME, | |
password: PASSWORD, | |
}); | |
await client.auth(); | |
// let measurements = await client.getMeasurments(); | |
// let parameters = await client.getParameters(); | |
// let single_parameter = await client.getParameter('Parameter.Inverter.WMax'); | |
// await client.setParameter('Parameter.Inverter.WMax', 11006); | |
} catch (error) { | |
console.log(error.response) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment