Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWowra committed May 25, 2022
2 parents ab771b4 + 8ce5f64 commit 6cb5faf
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 174 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# openSenseMap Client

Based on API version: `v9.8`
Based on API version: `v10.0.3`

[![NPM](https://nodei.co/npm/opensensemap-client.png)](https://npmjs.org/package/opensensemap-client)

Expand Down Expand Up @@ -134,6 +134,10 @@ start();
- [reset password with passwordResetToken](https://docs.opensensemap.org/#api-Users-password_reset)
- Function [passwordReset](https://killerjulian.github.io/opensensemap-client/modules.html#passwordReset)

## TypeDoc

Click [here](https://killerjulian.github.io/opensensemap-client/)

---

## Author
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"author": "KillerJulian <[email protected]>",
"license": "MIT",
"dependencies": {
"axios": "0.26.0",
"tslib": "2.3.1"
"axios": "0.27.2",
"tslib": "2.4.0"
},
"devDependencies": {
"@types/node": "12.20.46",
"@typescript-eslint/eslint-plugin": "5.12.0",
"@typescript-eslint/parser": "5.12.0",
"eslint": "8.9.0",
"typescript": "4.5.5",
"typedoc": "0.22.11"
"@types/node": "12.20.52",
"@typescript-eslint/eslint-plugin": "5.26.0",
"@typescript-eslint/parser": "5.26.0",
"eslint": "8.16.0",
"typescript": "4.7.2",
"typedoc": "0.22.15"
},
"scripts": {
"build": "yarn build:lib && yarn build:docs",
Expand Down
28 changes: 20 additions & 8 deletions src/api/boxes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import axios from 'axios';
import { Exposure, Location, Model, MQTT, RFC3339Date, Sensor, SensorTemplates, SensorUpdate, TTN } from './types';
import {
Exposure,
Location,
Model,
MQTT,
RFC3339Date,
Sensor,
SensorDeleted,
SensorEdited,
SensorNew,
SensorTemplates,
TTN
} from './types';

//
// https://docs.opensensemap.org/#api-Boxes
Expand All @@ -8,7 +20,7 @@ import { Exposure, Location, Model, MQTT, RFC3339Date, Sensor, SensorTemplates,
/**
* @see https://docs.opensensemap.org/#api-Boxes-getBox
*/
export async function getBox(senseBoxId: string): Promise<BoxData[]> {
export async function getBox(senseBoxId: string): Promise<BoxData> {
const r = await axios.get(`https://api.opensensemap.org/boxes/${senseBoxId}`, {
params: {
format: 'json'
Expand Down Expand Up @@ -97,7 +109,7 @@ export type PostNewBoxOptions = {
grouptag?: string;
model?: Model;
sensors?: Sensor[];
sensorTemplates?: SensorTemplates;
sensorTemplates?: SensorTemplates[];
mqtt?: MQTT;
ttn?: TTN;
useAuth?: boolean;
Expand Down Expand Up @@ -125,9 +137,9 @@ export async function updateBox(

export type UpdateBoxOptions = {
name?: string;
grouptag?: string;
grouptag?: string[];
location?: Location;
sensors?: SensorUpdate;
sensors?: (SensorEdited | SensorNew | SensorDeleted)[];
mqtt?: MQTT;
ttn?: TTN;
description?: string;
Expand Down Expand Up @@ -221,7 +233,7 @@ export interface BoxData {
exposure: Exposure;
model: string;
description?: string;
grouptag?: string;
grouptag?: string[];
weblink?: string;
image?: string;
currentLocation: BoxCurrentLocation;
Expand All @@ -242,11 +254,11 @@ export interface BoxData {
}

export interface BoxSensors {
_id: string;
title: string;
unit: string;
sensorType: string;
icon?: string;
_id: string;
lastMeasurement?: LastMeasurement | string;
}

Expand All @@ -256,7 +268,7 @@ export interface LastMeasurement {
}

export interface BoxCurrentLocation {
timestamp: RFC3339Date;
coordinates: Location;
type: string;
timestamp: RFC3339Date;
}
20 changes: 12 additions & 8 deletions src/api/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export async function getData(
export type GetDataOptions = {
'from-date'?: RFC3339Date | Date;
'to-date'?: RFC3339Date | Date;
download?: boolean;
outliers?: 'replace' | 'mark';
'outlier-window'?: number;
};
Expand All @@ -94,11 +93,15 @@ export async function deleteMeasurements(
options['to-date'] = options['to-date'].toISOString();
}

options?.timestamps?.forEach((element) => {
if (element && element instanceof Date) {
element = element.toISOString();
}
});
if (options && Array.isArray(options.timestamps)) {
options.timestamps = options.timestamps.map((element) => {
if (element instanceof Date) {
return element.toISOString();
}

return element;
});
}

const r = await axios.delete(`https://api.opensensemap.org/boxes/${senseBoxId}/${sensorId}/measurements`, {
headers: {
Expand Down Expand Up @@ -169,7 +172,6 @@ export type GetDataMultiOptions = {
'from-date'?: RFC3339Date | Date;
'to-date'?: RFC3339Date | Date;
columns?: string | AdvancedColumns[];
download?: boolean;
exposure?: string | Exposure[];
};

Expand Down Expand Up @@ -210,14 +212,16 @@ export async function postNewMeasurements(
data: PostNewMeasurementsData,
authorization?: string
): Promise<'Measurements saved in box'> {
data.forEach((element) => {
data = data.map((element) => {
if (typeof element.value === 'number') {
element.value = element.value.toString();
}

if (element.createdAt && element.createdAt instanceof Date) {
element.createdAt = element.createdAt.toISOString();
}

return element;
});

const r = await axios.post(`https://api.opensensemap.org/boxes/${senseBoxId}/data`, data, {
Expand Down
4 changes: 2 additions & 2 deletions src/api/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Columns, Exposure, Operation, RFC3339Date } from './types';
* @see https://docs.opensensemap.org/#api-Statistics-descriptive
*/
export async function descriptive(
boxId: string | undefined,
boxId: string[],
bbox: string | undefined,
phenomenon: string,
fromDate: RFC3339Date | Date,
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function descriptive(
const r = await axios.get('https://api.opensensemap.org/statistics/descriptive', {
params: Object.assign(
{
boxId,
boxId: boxId.join(),
bbox,
phenomenon,
'from-date': fromDate,
Expand Down
27 changes: 20 additions & 7 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type RFC3339Date = string;

export type Model =
| 'homeV2Lora'
| 'homeV2Ethernet'
| 'homeV2Wifi'
| 'homeEthernet'
| 'homeWifi'
| 'homeEthernetFeinstaub'
Expand All @@ -9,7 +12,8 @@ export type Model =
| 'luftdaten_sds011_dht11'
| 'luftdaten_sds011_dht22'
| 'luftdaten_sds011_bmp180'
| 'luftdaten_sds011_bme280';
| 'luftdaten_sds011_bme280'
| 'hackair_home_v2';

export type Exposure = 'indoor' | 'outdoor' | 'mobile' | 'unknown';

Expand Down Expand Up @@ -54,7 +58,8 @@ export type SensorTemplates =
| 'smt50'
| 'soundlevelmeter'
| 'windspeed'
| 'scd30';
| 'scd30'
| 'dps310';

export interface MQTT {
enabled: boolean;
Expand All @@ -69,7 +74,7 @@ export interface TTN {
dev_id: string;
app_id: string;
profile: 'lora-serialization' | 'sensebox/home' | 'json' | 'debug' | 'cayenne-lpp';
decodeOptions?: [];
decodeOptions?: unknown[];
port: number;
}

Expand All @@ -80,8 +85,16 @@ export interface Sensor {
icon?: string;
}

export interface SensorUpdate extends Sensor {
edited: string;
new: string;
deleted: string;
export interface SensorEdited extends Sensor {
_id: string;
edited: 'true' | string;
}

export interface SensorNew extends Sensor {
new: 'true' | string;
}

export interface SensorDeleted {
_id: string;
deleted: 'true' | string;
}
Loading

0 comments on commit 6cb5faf

Please sign in to comment.