diff --git a/README.md b/README.md index 7bd7de8..30381d4 100644 --- a/README.md +++ b/README.md @@ -79,8 +79,8 @@ Or you can use the tab **files** to upload the file. (see image below)\ --> ### **WORK IN PROGRESS** - - (Scounger) bug fix: objects only if necessary +- (Scrounger) Calculate distance between ioBroker and tracker ### 0.1.2 (2023-02-24) diff --git a/build/lib/object_definition.js b/build/lib/object_definition.js index 7916748..4ab6f91 100644 --- a/build/lib/object_definition.js +++ b/build/lib/object_definition.js @@ -104,6 +104,42 @@ const stateAttrb = { read: true, write: false }, + prioritized_zone_entered_at: { + name: "Prioritized zone entered at", + desc: "Prioritized zone entered at", + type: "number", + role: "value", + def: null, + read: true, + write: false + }, + prioritized_zone_last_seen_at: { + name: "Prioritized zone last seen at", + desc: "Prioritized zone last seen at", + type: "number", + role: "value", + def: null, + read: true, + write: false + }, + prioritized_zone_type: { + name: "Prioritized zone type", + desc: "Prioritized zone type", + type: "string", + role: "value", + def: null, + read: true, + write: false + }, + prioritized_zone_id: { + name: "Prioritized zone id", + desc: "Prioritized zone id", + type: "string", + role: "value", + def: null, + read: true, + write: false + }, hw_status: { name: "Hardware Status", desc: "Hardware Status of the device", @@ -338,6 +374,16 @@ const stateAttrb = { read: true, write: false }, + distance: { + name: "Distance", + desc: "Distance to ioBroker", + type: "number", + role: "value.gps.distance", + def: 0, + unit: "m", + read: true, + write: false + }, speed: { name: "Speed", desc: "Speed in meters per second", diff --git a/build/main.js b/build/main.js index 1fa2c71..edf2204 100644 --- a/build/main.js +++ b/build/main.js @@ -177,6 +177,17 @@ class TractiveGPS extends utils.Adapter { val: value[1], ack: true }); + let sysConfig = await this.getForeignObjectAsync("system.config"); + if (sysConfig && sysConfig.common && sysConfig.common.longitude && sysConfig.common.latitude) { + let sysPoint = new import_geo_position.GeoPosition(sysConfig.common.latitude, sysConfig.common.longitude); + let petPoint = new import_geo_position.GeoPosition(value[0], value[1]); + await this.setStateAsync(`${device._id}.device_pos_report.distance`, { + val: Number(sysPoint.Distance(petPoint).toFixed(0)), + ack: true + }); + } else { + this.writeLog("No gps coordinates of system found!", "warn"); + } } else { if (typeof value === "object" && value !== null) { await this.setStateAsync(`${device._id}.device_pos_report.${key}`, { @@ -354,6 +365,11 @@ class TractiveGPS extends utils.Adapter { common: import_object_definition.stateAttrb["longitude"], native: {} }); + await this.extendObjectAsync(`${device._id}.device_pos_report.distance`, { + type: "state", + common: import_object_definition.stateAttrb["distance"], + native: {} + }); } else { await this.setObjectNotExistsAsync(`${device._id}.device_pos_report.${key}`, { type: "state", diff --git a/io-package.json b/io-package.json index 354c38e..ce34dcf 100644 --- a/io-package.json +++ b/io-package.json @@ -1,7 +1,7 @@ { "common": { "name": "tractive-gps", - "version": "0.1.2", + "version": "0.1.3", "news": { "0.1.2": { "en": "Dependencies updated\nUI updated", diff --git a/package-lock.json b/package-lock.json index 850b248..14f18b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "axios": "^1.3.4", "cron": "^2.2.0", "dayjs": "^1.11.7", + "geo-position.ts": "^1.4.1", "react-leaflet": "3.2.5", "source-map-support": "^0.5.21" }, @@ -4248,6 +4249,11 @@ "node": ">=12" } }, + "node_modules/geo-position.ts": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/geo-position.ts/-/geo-position.ts-1.4.1.tgz", + "integrity": "sha512-XGxxGbRBT6bVqbf9GpbQotuELf/OzXOQHmxPVtftB6fCLHlbuF+cYseekE/1UIHJ+OUcV9Jeoim+EjaVfJOFRw==" + }, "node_modules/get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -10814,6 +10820,11 @@ "json-bigint": "^1.0.0" } }, + "geo-position.ts": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/geo-position.ts/-/geo-position.ts-1.4.1.tgz", + "integrity": "sha512-XGxxGbRBT6bVqbf9GpbQotuELf/OzXOQHmxPVtftB6fCLHlbuF+cYseekE/1UIHJ+OUcV9Jeoim+EjaVfJOFRw==" + }, "get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", diff --git a/package.json b/package.json index 9040275..6eb80a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iobroker.tractive-gps", - "version": "0.1.2", + "version": "0.1.3", "description": "This adapter allows you to connect to the Tractive GPS service and retrieve the location of your pets.", "author": { "name": "xXBJXx", @@ -31,7 +31,8 @@ "cron": "^2.2.0", "dayjs": "^1.11.7", "react-leaflet": "3.2.5", - "source-map-support": "^0.5.21" + "source-map-support": "^0.5.21", + "geo-position.ts": "^1.4.1" }, "devDependencies": { "@alcalzone/release-script": "^3.5.9", diff --git a/src/lib/object_definition.ts b/src/lib/object_definition.ts index c956cd7..cd2d0fa 100644 --- a/src/lib/object_definition.ts +++ b/src/lib/object_definition.ts @@ -101,6 +101,42 @@ export const stateAttrb: StateAttr = { read: true, write: false, }, + prioritized_zone_entered_at: { + name: 'Prioritized zone entered at', + desc: 'Prioritized zone entered at', + type: 'number', + role: 'value', + def: null, + read: true, + write: false, + }, + prioritized_zone_last_seen_at: { + name: 'Prioritized zone last seen at', + desc: 'Prioritized zone last seen at', + type: 'number', + role: 'value', + def: null, + read: true, + write: false, + }, + prioritized_zone_type: { + name: 'Prioritized zone type', + desc: 'Prioritized zone type', + type: 'string', + role: 'value', + def: null, + read: true, + write: false, + }, + prioritized_zone_id: { + name: 'Prioritized zone id', + desc: 'Prioritized zone id', + type: 'string', + role: 'value', + def: null, + read: true, + write: false, + }, hw_status: { name: 'Hardware Status', desc: 'Hardware Status of the device', @@ -335,6 +371,16 @@ export const stateAttrb: StateAttr = { read: true, write: false, }, + distance: { + name: 'Distance', + desc: 'Distance to ioBroker', + type: 'number', + role: 'value.gps.distance', + def: 0, + unit: 'm', + read: true, + write: false, + }, speed: { name: 'Speed', desc: 'Speed in meters per second', diff --git a/src/main.ts b/src/main.ts index 9d88d2d..1747f60 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import * as utils from '@iobroker/adapter-core'; // import 'source-map-support/register.js'; import axios from 'axios'; import { CronJob } from 'cron'; +import { GeoPosition } from 'geo-position.ts'; import sourceMapSupport from 'source-map-support'; import { decrypt, encrypt } from './lib/Helper'; import { stateAttrb } from './lib/object_definition'; @@ -199,6 +200,21 @@ class TractiveGPS extends utils.Adapter { val: value[1], ack: true, }); + + let sysConfig = await this.getForeignObjectAsync('system.config'); + + if (sysConfig && sysConfig.common && sysConfig.common.longitude && sysConfig.common.latitude) { + let sysPoint = new GeoPosition(sysConfig.common.latitude, sysConfig.common.longitude); + let petPoint = new GeoPosition(value[0], value[1]); + + await this.setStateAsync(`${device._id}.device_pos_report.distance`, { + val: Number(sysPoint.Distance(petPoint).toFixed(0)), + ack: true, + }); + } else { + this.writeLog('No gps coordinates of system found!', "warn"); + } + } else { if (typeof value === 'object' && value !== null) { await this.setStateAsync(`${device._id}.device_pos_report.${key}`, { @@ -416,6 +432,11 @@ class TractiveGPS extends utils.Adapter { common: stateAttrb['longitude'], native: {}, }); + await this.extendObjectAsync(`${device._id}.device_pos_report.distance`, { + type: 'state', + common: stateAttrb['distance'], + native: {}, + }); } else { await this.setObjectNotExistsAsync(`${device._id}.device_pos_report.${key}`, { type: 'state', diff --git a/src/types/TractiveDevice.ts b/src/types/TractiveDevice.ts index 1ec97d9..22af022 100644 --- a/src/types/TractiveDevice.ts +++ b/src/types/TractiveDevice.ts @@ -41,6 +41,10 @@ export interface Tracker { charging_state: string; battery_state: string; power_saving_zone_id: string | null; + prioritized_zone_entered_at: number | null; + prioritized_zone_last_seen_at: number | null; + prioritized_zone_type: string, + prioritized_zone_id: string } export interface DeviceHwReport {