@wal ,
falls meine Änderungen in Github nicht abgeschickt worden sind, reicht schon folgendes, markiert mit // NEU bzw. anders
async onEventChange(obj) {
const extractedJSON = JSON.parse(obj.val);
const beforecamera = extractedJSON.before.camera;
const before = extractedJSON.before; // NEU
const beforelabel = extractedJSON.before.label;
const afterid = extractedJSON.after.id;
const topscore = extractedJSON.after.top_score;
const eventtype = extractedJSON.type;
const id1 = beforecamera + '.event';
const id2 = beforecamera + '.objects.' + beforelabel;
const websnap = weburl + '/api/events/' + afterid + '/snapshot.jpg';
const webclip = weburl + '/api/events/' + afterid + '/clip.mp4';
const bsnap = before.has_snapshot; // NEU bzw. anders
const bclip = before.has_clip; // NEU bzw. anders
if ((bsnap == null) || (bclip == null)) {
this.log.info('restart MQTT Broker please !!!');
return;
}
this.log.debug(`Snap: ${bsnap}`);
this.log.debug(`Clip: ${bclip}`);
this.log.debug(`changed: ${obj.val}`);
try {
if (eventtype == 'new') {
//------------------------------
// Bewegung erkannt
//------------------------------
this.setState('event', { val: true, ack: true });
//------------------------------
// Kamera erkannt
//------------------------------
await this.setObjectNotExistsAsync(id1, {
type: 'state',
common: {
name: 'Camera detected',
type: 'boolean',
role: 'indicator',
read: true,
write: false,
def: false
},
native: {},
});
this.setState(id1, { val: true, ack: true });
//------------------------------
// Objekt erkannt
//------------------------------
await this.setObjectNotExistsAsync(id2 + '.event', {
type: 'state',
common: {
name: beforelabel + ' detected',
type: 'boolean',
role: 'indicator',
read: true,
write: false,
def: false
},
native: {},
});
this.setState(id2 + '.event', { val: true, ack: true });
} else if (eventtype == 'end') {
//------------------------------
// Event ID
//------------------------------
await this.setObjectNotExistsAsync(id2 + '.id', {
type: 'state',
common: {
name: 'Event ID',
type: 'string',
role: 'value',
read: true,
write: false,
def: 'none'
},
native: {},
});
this.setState(id2 + '.id', { val: afterid, ack: true });
//------------------------------
// Cam + Event ID
//------------------------------
await this.setObjectNotExistsAsync(id2 + '.camid', {
type: 'state',
common: {
name: 'Cam ID',
type: 'string',
role: 'value',
read: true,
write: false,
def: 'none'
},
native: {},
});
this.setState(id2 + '.camid', { val: beforecamera + '-' + afterid, ack: true });
this.setState('lastcamid', { val: beforecamera + '-' + afterid, ack: true });
this.setState('lastidurl', { val: websnap, ack: true });
//------------------------------
// WebURL
//------------------------------
const anz = this.config.webnum;
if (bsnap == true) { // NEU bzw. anders
for (let i = 0; i < anz; i++)
await this.setObjectNotExistsAsync(id2 + '.web.snap.snap_' + i.toString(), {
type: 'state',
common: {
name: 'Snapshot WebUrl ' + i.toString(),
type: 'string',
role: 'value',
read: true,
write: false,
def: ''
},
native: {},
});
for (let i = anz - 1; i > -1; i--) {
if (i == 0) {
this.setState(id2 + '.web.snap.snap_' + i.toString(), { val: websnap, ack: true });
} else {
const str = await this.getStateAsync(id2 + '.web.snap.snap_' + (i - 1).toString());
if (str != null)
this.setState(id2 + '.web.snap.snap_' + i.toString(), { val: str.val, ack: true });
}
}
}
if (bclip == true) { // NEU bzw. anders
for (let i = 0; i < anz; i++)
await this.setObjectNotExistsAsync(id2 + '.web.clip.clip_' + i.toString(), {
type: 'state',
common: {
name: 'Clip WebUrl ' + i.toString(),
type: 'string',
role: 'value',
read: true,
write: false,
def: ''
},
native: {},
});
for (let i = anz - 1; i > -1; i--) {
if (i == 0) {
this.setState(id2 + '.web.clip.clip_' + i.toString(), { val: webclip, ack: true });
} else {
const str = await this.getStateAsync(id2 + '.web.clip.clip_' + (i - 1).toString());
if (str != null)
this.setState(id2 + '.web.clip.clip_' + i.toString(), { val: str.val, ack: true });
}
}
}
//------------------------------
// Erkennungsrate
//------------------------------
await this.setObjectNotExistsAsync(id2 + '.score', {
type: 'state',
common: {
type: 'number',
read: true,
write: false,
name: 'detection rate',
role: 'value.score',
unit: '%',
def: 0
},
native: {},
});
this.setState(id2 + '.score', { val: Math.round(topscore * 100), ack: true });
//------------------------------
// Reset Event States
//------------------------------
this.setState('event', { val: false, ack: true });
this.setState(id1, { val: false, ack: true });
this.setState(id2 + '.event', { val: false, ack: true });
}
} catch (error) {
this.log.error(error);
}
}