Skip to content

Commit

Permalink
Merge pull request #2 from septk/feature/new-setup-modules
Browse files Browse the repository at this point in the history
Feature/new setup modules
  • Loading branch information
septk authored May 5, 2019
2 parents a6a77c8 + e7495a2 commit aebc425
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ compiled
.awcache
.rpt2_cache
docs
test/__snapshots__
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#SigfoxJS
# SigfoxJS

![Sigfox Logo img](https://temp.iotdk.dk/wp-content/uploads/2018/01/Sigfox_Logo_RGB_R-300x175.png )

## [WIP]

- Work in Progress
```javascript
const SigfoxApi = require('sigfox-js');

const apiConnection = new SigfoxApi({
username: 'username',
password: 'password'
});

const infoDevices = await connection.devices.getAllDevices();
```

#### To Do:

- Users [x]
- Coverages [ ]
- Devices [ ]
- Devices Types [ ]
- Groups [ ]
- Tiles [ ]
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"commit": "git-cz",
"semantic-release": "semantic-release",
"semantic-release-prepare": "ts-node tools/semantic-release-prepare",
"travis-deploy-once": "travis-deploy-once"
"travis-deploy-once": "travis-deploy-once",
"prepush": "npm run test:prod && npm run build",
"commitmsg": "commitlint -E HUSKY_GIT_PARAMS"
},
"lint-staged": {
"{src,test}/**/*.ts": [
Expand Down
153 changes: 153 additions & 0 deletions src/modules/devices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import axios, { AxiosInstance } from 'axios'

import { RootObject, ModemCertificate } from '../types/devices/getAllDevices'
import { API_CONFIG } from '../config/constants'

export class Devices {
rest: AxiosInstance
config: object

constructor(config: object) {
this.rest = axios.create({ baseURL: API_CONFIG.baseURL })
this.config = config
}

/**
* The endpoint to get Device Info.
* HTTP GET /devices/${deviceId}
* @returns {device}
*/
public getDeviceInfo(deviceId: string): Promise<RootObject> {
return this.rest
.get<RootObject>(API_CONFIG.baseURL + `/devices/${deviceId}`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* The endpoint to get info from all devices.
* HTTP GET /devices/?deep=false&sort=name&limit=100&offset=0
* To Do - query + params customizable
* @returns [Devices]
*/
public getAllDevices(): Promise<RootObject> {
return this.rest
.get<RootObject>(
API_CONFIG.baseURL + `/devices/?deep=false&sort=name&limit=100&offset=0`,
this.config
)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve a list of undelivered callbacks and errors for a given device, in reverse chronological order (most recent message first).
* HTTP GET /devices/${deviceId}/callbacks-not-delivered
* @returns [Callbacks - Errors]
*/
public getCallbacksNotDelivered(deviceId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/callbacks-not-delivered`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve the modem certificate associated with a device.
* HTTP GET /devices/${deviceId}/certificate/modem
* @returns {ModemCertificate}
*/
public getModemCert(deviceId: string): Promise<ModemCertificate> {
return this.rest
.get<ModemCertificate>(
API_CONFIG.baseURL + `/devices/${deviceId}/certificate/modem`,
this.config
)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve the product certificate associated with a device already registered.
* HTTP GET /devices/${deviceId}/certificate/product
* @returns {ProductCertificate}
*/
public getProductCert(deviceId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/certificate/product`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve a device's consumption for a given year.
* HTTP GET /devices/${deviceId}/consumption/${year}
* @returns {id, deviceConsumptions}
*/
public getDeviceConsYearly(deviceId: string, year: number) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/consumption/${year}`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve a device's consumption for a given month during a given year.
* HTTP GET /devices/${deviceId}/consumption/${year}/${month}
* @returns {id, deviceConsumptions}
*/
public getDeviceConsMonthly(deviceId: string, year: number, month: number) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/consumption/${year}/${month}`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve a list of messages for a given device according to request filters, with a 3-day history.
* HTTP GET /devices/${deviceId}/messages
* @returns {infoMessages}
*/
public getMessagesList(deviceId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/messages`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Return the number of messages for a given device, for the last day, last week and last month.
* HTTP GET /devices/${deviceId}/messages/metric
* @returns {lastDay, lastWeek, lastMonth}
*/
public getMessagesMetric(deviceId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/messages/metric`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve a list of location data of a device according to request filters.
* HTTP GET /devices/${deviceId}/locations
* @returns {data}
*/
public getLocations(deviceId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/${deviceId}/locations`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}

/**
* Retrieve the async job status for an unsubscribe devices action.
* HTTP GET /devices/bulk/unsubscribe/${jobId}
* @returns {jobDone, states}
*/
public unsubscribeActions(jobId: string) {
return this.rest
.get(API_CONFIG.baseURL + `/devices/bulk/unsubscribe/${jobId}`, this.config)
.then(response => response.data)
.catch(error => error.response.statusText)
}
}
19 changes: 3 additions & 16 deletions src/sigfox-js.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
import axios, { AxiosInstance } from 'axios'

import { ConfigParams } from './types/config-params'
import { Device } from './types/device'
import { API_CONFIG } from './config/constants'
import { Devices } from './modules/devices'

/**
* @class SigfoxApiWrapper
*
*/

export default class SigfoxApi {
public rest: AxiosInstance
public customParams: object
public devices: Devices

/**
* @constructor
* @param {ConfigParams} customParams ID Client Setup
*/
constructor(ConfigParams: ConfigParams) {
this.rest = axios.create({ baseURL: API_CONFIG.baseURL })
this.customParams = {
auth: ConfigParams,
API_CONFIG
}
}

/**
* The endpoint to get Device Info.
* @returns {device}
*/
public getDeviceInfo(deviceId: string) {
return this.rest
.get<Device>(API_CONFIG.baseURL + `/devices/${deviceId}`, this.customParams)
.then(response => response.data)
.catch(error => error.response.statusText)
this.devices = new Devices(this.customParams)
}
}
5 changes: 5 additions & 0 deletions src/types/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface Device {
activable: boolean
}

export interface DeviceSingle {
id: string
name: string
}

export interface Location {
lat: number
lng: number
Expand Down
57 changes: 57 additions & 0 deletions src/types/devices/getAllDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export interface Location {
lat: number
lng: number
}

export interface DeviceType {
id: string
}

export interface Group {
id: string
}

export interface Token {
state: number
detailMessage: string
end: any
}

export interface Contract {
id: string
}

export interface ModemCertificate {
id: string
}

export interface Datum {
id: string
name: string
lastCom: any
state: number
comState: number
location: Location
deviceType: DeviceType
group: Group
lqi: number
activationTime: any
token: Token
contract: Contract
creationTime: any
modemCertificate: ModemCertificate
prototype: boolean
automaticRenewal: boolean
automaticRenewalStatus: number
createdBy: string
lastEditionTime: any
lastEditedBy: string
activable: boolean
}

export interface Paging {}

export interface RootObject {
data: Datum[]
paging: Paging
}
23 changes: 23 additions & 0 deletions src/types/devices/modem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface Manufacturer {
id: string
}

export interface RadioConfiguration {
id: number
outputPower: number
balancedLinkBudget: boolean
}

export interface ModemCertificate {
id: string
manufacturer: Manufacturer
name: string
key: string
version: string
description: string
status: number
radioConfigurations: RadioConfiguration[]
inputSensitivity: number
modes: number[]
repeaterFunction: boolean
}
3 changes: 0 additions & 3 deletions test/__snapshots__/sigfox-js.test.ts.snap

This file was deleted.

11 changes: 11 additions & 0 deletions test/devices.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as nock from 'nock'

import { API_CONFIG } from '../src/config/constants'
import { Devices } from '../src/modules/devices'

describe('Devices Test', () => {
it('should be a class', () => {
const devices = new Devices({} as any)
expect(devices instanceof Devices).toBeTruthy()
})
})
24 changes: 0 additions & 24 deletions test/sigfox-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as nock from 'nock'

import SigfoxApi from '../src/sigfox-js'
import { ConfigParams } from '../src/types/config-params'
import { API_CONFIG } from '../src/config/constants'

/**
* SigfoxApi test
Expand All @@ -19,27 +18,4 @@ describe('SigfoxApi test', () => {
expect(new SigfoxApi({} as ConfigParams)).toBeInstanceOf(SigfoxApi)
})
})

describe('getDeviceInfo() call', () => {
it('should return device obj', async () => {
nock(API_CONFIG.baseURL)
.get(`/devices/9E1F98`)
.reply(200)
const sigfox = new SigfoxApi({} as ConfigParams)
const result = await sigfox.getDeviceInfo('9E1F98')
expect(result).toMatchSnapshot()
})

it('should return error', async () => {
const sigfox = new SigfoxApi({} as ConfigParams)
try {
nock(API_CONFIG.baseURL)
.get(`/devices/9E1F98`)
.reply(404)
await sigfox.getDeviceInfo('9E1F98')
} catch (error) {
expect(error).toMatchSnapshot()
}
})
})
})
Loading

0 comments on commit aebc425

Please sign in to comment.