Hi,
hier mal mein Script zum Abruf aller Daten vom Surepetcare-Server.
Ja, ich weiß, es gibt schon einen Adapter für die Daten von Surepetcare - da ist allerdings nicht alles enthalten.
Außerdem fehlt mir im Adapter z.B. die Funktion, die Katzenklappe für einzelne Katzen zu sperren. Das funktioniert hier mit ctrl('TAG_ID-DER-KATZE',MODE); (Freigang: Mode=2; Wohnungskatze: Mode=3).
//----------------------------------------------------------------------------------------------------
// VARIABLEN
//----------------------------------------------------------------------------------------------------
// TOKEN
var token = 'TOKEN HIER EINTRAGEN';
//----------------------------------------------------------------------------------------------------
// PFAD FÜR DATEN --- GGF ANPASSEN
var pfad_sp = 'javascript.0.surepetcare' //PFAD
var pfad_data = pfad_sp+'.data'; //PFAD FÜR ABGERUFENE DATEN
var pfad_ctrl = pfad_sp+'.ctrl'; //DP ZUR STEUERUNG
//----------------------------------------------------------------------------------------------------
// AKTUALISIERUNG --- GGF ANPASSEN
var aktualisierung_intervall = '*/5 * * * *'; //AKTUALSIERUNGSINTERVALL ALS ZEITPLAN
var pfad_iot_ifttt = 'iot.0.services.ifttt'; //PFAD ZU IFTTT
var pfad_iot_auto = 'iot.0.services.custom_automateit'; //PFAD ZU AUTOMATEIT
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
// DATENABFRAGE
//----------------------------------------------------------------------------------------------------
//DATENPUNKT ERSTELLEN ODER BEFÜLLEN
function createset(createkey, createval, createpfad){
if (createval === '' || createval === null){
createval = 0;
}
if ($('state[id='+createpfad+']').length > 0) {
setState(createpfad, createval);
}
else {
if (typeof createval === 'object') { var role = 'list'; }
else if (typeof createval === 'number') { var role = 'state'; }
else if (typeof createval === 'string') { var role = 'text'; }
else if (typeof createval === 'boolean') { var role = 'switch'; }
createState(createpfad, createval, {
name: createpfad,
role: role,
type: typeof createval,
desc: createkey,
read: true,
write: true,
def: createval
})
}
}
//JSON-LISTE ENTSCHACHTELN
function getdata(list, pfad_dp, t) {
t=t+1;
if (t < 10 && list !=''){
for (let [key, val] of Object.entries(list)) {
if (val != '' && val != null){
if(typeof val === 'object' && "id" in list[key] && "name" in list[key] && key != "parent") {
var id = list[key].id;
var name = list[key].name;
var pfad_key = pfad_dp+'.'+id+'-'+name;
}
else {
var pfad_key = pfad_dp+'.'+key;
}
if(typeof val != 'object'){
createset(key, val, pfad_key);
}
else if(typeof val === 'object'){
getdata(list[key], pfad_key, t);
}
}
}
}
}
//ABFRAGE STARTEN
function start() {
var json_list = [];
var request = require('request');
var urlstart = 'https://app.api.surehub.io/api/me/start';
request({url:urlstart, headers : {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Bearer ' + token
}
},
function (error, response, body) {
if (error === '' || error === null || error.length === 0 ) {
body = JSON.parse(body);
if("error" in body) {
log('Surepetcare --- ERROR Datenabfrage --- error: '+body.error.message);
}
else if ("data" in body) {
json_list = body.data;
getdata(json_list, pfad_data, 0);
log('Surepetcare --- Daten aktualisieren');
}
}
else {
log('Surepetcare --- ERROR Datenabfrage --- error: '+error);
}
}
);
}
//----------------------------------------------------------------------------------------------------
// DATEN AKTUALISIEREN --- ALLE x MINUTEN UND BEI NOTIFICATIONS ÜBER AUTOMATEIT UND IFTTT
//----------------------------------------------------------------------------------------------------
//ALLE x MINUTEN --- GGF VARIABLE aktualisierung_intervall ANPASSEN
(function () {if (aktualisierung) {clearSchedule(aktualisierung); aktualisierung = null;}})();
var aktualisierung = schedule(aktualisierung_intervall, function () {
start();
});
//PER AUTOMATEIT
on({id: pfad_iot_auto, change: "ne"}, function (obj) {
var value = obj.state.val;
if (value == 'surepetcare') {
start();
}
});
//PER IFTTT
on({id: pfad_iot_ifttt, change: "ne"}, function (obj) {
var value = obj.state.val;
if (value.indexOf('Surepetcare') + 1 > 0 ) {
start();
}
});
//BEI SCRIPTSTART
start();
//----------------------------------------------------------------------------------------------------
// STEUERUNG
//----------------------------------------------------------------------------------------------------
// DATEN SENDEN
function ctrlsend(postData, path, method) {
var https = require('https');
var options = {
hostname: 'app.api.surehub.io',
port: 443,
path: path,
method: method,
headers: {
"Host" : "app.api.surehub.io",
"Accept" : "application/json, text/plain, */*",
"Referer" : "https://surepetcare.io/",
"Content-Type" : "application/json;charset=utf-8",
"Origin" : "https://surepetcare.io",
"Authorization": "Bearer " + token
}};
var req = https.request(options, (res) => {
//log('headers: '+ JSON.stringify(res.headers));
//log('Surepetcare --- statusCode: '+ res.statusCode);
if (res.statusCode != 200){
log('Surepetcare --- ERROR Steuerung --- statusCode: '+ res.statusCode);
}
});
req.write(postData);
req.end();
}
// STEUERUNG
function ctrl(ctrlwho, ctrlmode) {
//PETS_ID AUS DP SAMMELN
var pets_id = '';
var pfad_pets_id = Array.prototype.slice.apply($(pfad_data+'.pets.*.id'));
for (var i_index in pfad_pets_id) {
i = pfad_pets_id[i_index];
if (pets_id.indexOf(getState(i).val) + 1 < 1) {
pets_id = [pets_id,getState(i).val,','].join('');
}
}
//PETS_TAG_ID AUS DP SAMMELN
var pets_tag_id = '';
var pfad_pets_tag_id = Array.prototype.slice.apply($(pfad_data+'.pets.*.tag_id'));
for (var i_index in pfad_pets_tag_id) {
i = pfad_pets_tag_id[i_index];
if (pets_tag_id.indexOf(getState(i).val) + 1 < 1) {
pets_tag_id = [pets_tag_id,getState(i).val,','].join('');
}
}
//DEVICES AUS DP SAMMELN
var hub_id = '';
var feed_id = '';
var flap_id = [];
var pfad_dev = Array.prototype.slice.apply($(pfad_data+'.devices.*.product_id'));
for (var i_index in pfad_dev) {
i = pfad_dev[i_index];
if (getState(i).val == 1) {
//PRODUCT_ID 1: HUB
var pfad_hub_id = (i.slice(0, i.length - 10))+'id';
if (hub_id.indexOf(getState(pfad_hub_id).val) + 1 < 1) {
hub_id = [hub_id,getState(pfad_hub_id).val,','].join('');
}
} else if (getState(i).val == 4) {
//PRODUCT_ID 4: FEED
var pfad_feed_id = (i.slice(0, i.length - 10))+'id';
if (feed_id.indexOf(getState(pfad_feed_id).val) + 1 < 1) {
feed_id = [feed_id,getState(pfad_feed_id).val,','].join('');
}
}else if (getState(i).val == 6) {
//PRODUCT_ID 6: FLAP
var pfad_flap_id = (i.slice(0, i.length - 10))+'id';
if (flap_id.indexOf(getState(pfad_flap_id).val) + 1 < 1) {
flap_id = [flap_id,getState(pfad_flap_id).val];
}
}
}
//STEUERUNG
if (flap_id.indexOf(ctrlwho) + 1 > 0) {
//STEUERUNG DER KATZENKLAPPE
//KLAPPE LOCKMODE 0=OFFEN; 1=NUR REIN; 2=NUR RAUS; 3=GESPERRT
var ctrlpath = '/api/device/'+ctrlwho+'/control';
var ctrlpostData = JSON.stringify( {'locking':ctrlmode} );
var sendmethod = 'PUT';
ctrlsend(ctrlpostData, ctrlpath, sendmethod);
}
if (hub_id.indexOf(ctrlwho) + 1 > 0){
//STEUERUNG DER LED AM HUB
//HUB DIMMER 0=OFF; 1=BRIGHT; 4=GEDIMMT
var ctrlpath = '/api/device/'+ctrlwho+'/control';
var ctrlpostData = JSON.stringify( {'led_mode':ctrlmode} );
var sendmethod = 'PUT';
ctrlsend(ctrlpostData, ctrlpath, sendmethod);
}
if (pets_id.indexOf(ctrlwho) + 1 > 0){
//MANUELLE STANDORTBESTIMMUNG EINES TIERS MITTELS ID
//PETPOSITION 1=IN; 2=OUT
var ctrldate = formatDate(new Date(), "JJJJ-MM-TT hh:mm:ss");
var ctrlpath = '/api/pet/'+ctrlwho+'/position';
var ctrlpostData = JSON.stringify( {'where':ctrlmode,'since': ctrldate} );
var sendmethod = 'POST';
ctrlsend(ctrlpostData, ctrlpath, sendmethod);
}
if (pets_tag_id.indexOf(ctrlwho) + 1 > 0) {
//FREIGANGSSPERRE EINES EINZELNEN TIERES MITTELS TAG_ID
//WOHNUNGSKATZE 2=FREIGANG 3=WOHNUNGSKATZE
for (var i_index in flap_id) {
i = flap_id[i_index];
if (i != ''){
var ctrlpath = '/api/device/'+i+'/tag/'+ctrlwho;
var ctrlpostData = JSON.stringify({'profile':ctrlmode});
var sendmethod = 'PUT';
ctrlsend(ctrlpostData, ctrlpath, sendmethod);
}
}
}
}
//----------------------------------------------------------------------------------------------------
// STEUERUNG MITTELS
// ctrl('xxxxxx',y); MIT xxxxxx=ID DES GERÄTS ODER TIERS UND y=ZUSTAND
// BEISPIELSWEISE ÜBER DATENPUNKT "CTRL":
//----------------------------------------------------------------------------------------------------
createState(pfad_ctrl, {}, {
name: "ctrl",
role: "list",
type: "array",
desc: "ctrl",
read: true,
write: true,
def: {}
});
on({id: pfad_ctrl, change: "ne"}, function (obj) {
if (obj.state.val != '' && obj.state.val != null) {
var ctrlid = obj.state.val[0];
var ctrlnr = obj.state.val[1];
ctrl(ctrlid,ctrlnr);
}
});
tbc