Skip to content

Version 0.6.0 #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
060d05c
Import node-red lib + massive refactoring
pirropirro Jun 15, 2020
dfc2b1d
Enable API Connection
pirropirro Jun 16, 2020
783cbff
Set config file
pirropirro Jun 16, 2020
f8e664f
Refactor w/ HttpClient
pirropirro Jun 16, 2020
dc0a1a1
Add encode uri
pirropirro Jun 16, 2020
9490c9a
Replace body on request
pirropirro Jun 16, 2020
ee5c876
Remove useless stringify
pirropirro Jun 16, 2020
ed691ef
Fix minor bug
pirropirro Jun 16, 2020
13c2a49
Fix typings
pirropirro Jun 16, 2020
2a9b541
Fixed test
pirropirro Jun 17, 2020
e5325f7
Fix tests
pirropirro Jun 17, 2020
b3bebc3
Bump beta version
pirropirro Jun 17, 2020
3362cf7
Rename
pirropirro Jun 17, 2020
8344fe3
Bump version
pirropirro Jun 17, 2020
df79564
Add default
pirropirro Jun 17, 2020
79fe50c
Fix minor bugs
pirropirro Jun 18, 2020
2432767
Remove default export
pirropirro Jun 18, 2020
509b194
Lint
pirropirro Jun 18, 2020
2b54033
Add lint process before build
pirropirro Jun 18, 2020
96917e0
Bump version
pirropirro Jun 18, 2020
4821372
Set useCloudProtocolV2 as true by default
pirropirro Jun 18, 2020
71ce299
Bump version
pirropirro Jun 18, 2020
1bafd8d
Run tests using GitHub actions
Jun 18, 2020
ce875a4
Update readme and add example (#234)
Jun 18, 2020
2367ab6
Add lint before run test
pirropirro Jun 18, 2020
a29c362
Merge pull request #236 from arduino/fmirabito/gh_actions
Jun 18, 2020
f4abbd9
Fix 0 values
pirropirro Jun 19, 2020
aa483f1
Bump version
pirropirro Jun 19, 2020
7a242ff
Fix typo
pirropirro Jun 19, 2020
a90d279
Bump version
pirropirro Jun 19, 2020
8075ba1
Add wildcard on example deps
pirropirro Jun 19, 2020
0c5ab14
Fix mqtt version
pirropirro Aug 14, 2020
3e213fa
Bump version
pirropirro Aug 14, 2020
29c98c3
Remove beta version
pirropirro Aug 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename
  • Loading branch information
pirropirro committed Jun 17, 2020
commit 3362cf7cea710959d912c1229eefcebdbd586da4
6 changes: 3 additions & 3 deletions src/@arduino/cbor-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@


declare module '@arduino/cbor-js' {
export function encode(value: CBORValue[], numericKeys?: boolean): ArrayBuffer;
export function decode(data: ArrayBuffer, tagger?: (value: CBORValue) => CBORValue, simpleValue?: CBORValue): CBORValue[];
export function encode(value: SenML[], numericKeys?: boolean): ArrayBuffer;
export function decode(data: ArrayBuffer, tagger?: (value: SenML) => SenML, simpleValue?: SenML): SenML[];

export type CBORValue = {
export type SenML = {
bn?: string;
bt?: number;
bu?: string;
Expand Down
6 changes: 3 additions & 3 deletions src/client/ArduinoCloudClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { filter } from "rxjs/operators";
import { Subscription, Subject, Observable } from "rxjs";

import CBOR from "../cbor";
import SenML from "../senML";
import Utils from "../utils";
import { IConnectionBuilder } from '../builder/IConnectionBuilder';
import { IConnection, CloudMessage } from "../connection/IConnection";
Expand Down Expand Up @@ -161,8 +161,8 @@ export class ArduinoCloudClient implements IArduinoCloudClient {

public async sendProperty<T extends CloudMessageValue>(thingId: string, name: string, value: T, timestamp: number = new Date().getTime()): Promise<void> {
const topic = `/a/t/${thingId}/e/i`;
const values = CBOR.getSenML(name, value, timestamp, this.options.useCloudProtocolV2, null);
return this.sendMessage(topic, CBOR.encode(Utils.isArray(values) ? values : [values], true));
const values = SenML.parse(name, value, timestamp, this.options.useCloudProtocolV2, null);
return this.sendMessage(topic, SenML.CBOR.encode(Utils.isArray(values) ? values : [values], true));
}

public async onPropertyValue<T extends CloudMessageValue>(thingId: string, name: string, cb: OnMessageCallback<T>): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jws from 'jws';
import mqtt from 'mqtt';
import { Observable, Subject } from "rxjs";

import CBOR from '../cbor';
import SenML from '../senML';
import Utils from "../utils";
import { CloudMessageValue } from "../client/IArduinoCloudClient";
import { IConnection, CloudMessage, ConnectionOptions } from "./IConnection";
Expand Down Expand Up @@ -84,11 +84,11 @@ export class Connection implements IConnection {
let valueToSend: CloudMessageValue = {};

const messages: CloudMessage[] = [];
const properties = CBOR.decode(Utils.toArrayBuffer(msg));
const properties = SenML.CBOR.decode(Utils.toArrayBuffer(msg));

properties.forEach((p) => {
const value = CBOR.valueFrom(p);
[current, attribute] = CBOR.nameFrom(p).split(':');
const value = SenML.valueFrom(p);
[current, attribute] = SenML.nameFrom(p).split(':');
if (previous === '') previous = current;

if (previous !== current) {
Expand Down
5 changes: 3 additions & 2 deletions src/index.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

import CBOR from './cbor';
import SenML from './senML';
import fetch from "node-fetch";
import { HttpClientFactory } from './http/HttpClientFactory';
import { ArduinoCloudClient } from "./client/ArduinoCloudClient";
Expand All @@ -28,5 +28,6 @@ import { TokenConnectionBuilder } from "./builder/TokenConnectionBuilder";
const builders = [new TokenConnectionBuilder(), new APIConnectionBuilder(HttpClientFactory.Create(fetch))];
const DefaultClient = new ArduinoCloudClient(builders);

export { CBOR };
export default DefaultClient;
export { SenML, ArduinoCloudClient };
export { IArduinoCloudClient, CloudOptions, CloudMessageValue } from "./client/IArduinoCloudClient";
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

import "whatwg-fetch";
import CBOR from './cbor';
import SenML from './senML';
import { HttpClientFactory } from './http/HttpClientFactory';
import { ArduinoCloudClient } from "./client/ArduinoCloudClient";
import { APIConnectionBuilder } from "./builder/APIConnectionBuilder";
Expand All @@ -28,5 +28,6 @@ import { TokenConnectionBuilder } from "./builder/TokenConnectionBuilder";
const builders = [new TokenConnectionBuilder(), new APIConnectionBuilder(HttpClientFactory.Create(fetch))];
const DefaultClient = new ArduinoCloudClient(builders);

export { CBOR };
export default DefaultClient;
export { SenML, ArduinoCloudClient };
export { IArduinoCloudClient, CloudOptions, CloudMessageValue } from "./client/IArduinoCloudClient";
31 changes: 15 additions & 16 deletions src/cbor/index.ts → src/senML/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { encode, decode, CBORValue } from '@arduino/cbor-js';
import CBOR, { SenML } from '@arduino/cbor-js';

import Utils from "../utils";
import { CloudMessageValue } from "../client/IArduinoCloudClient";

function isPropertyValue(message: CBORValue | string[]): message is CBORValue {
return !!(message as CBORValue).n;
function isPropertyValue(message: SenML | string[]): message is SenML {
return !!(message as SenML).n;
}

function valueFrom(message: CBORValue | string[]): CloudMessageValue {
function valueFrom(message: SenML | string[]): CloudMessageValue {
return isPropertyValue(message)
? message.v || message.vs || message.vb
: message[2] || message[3] || message[4];
}

function nameFrom(property: CBORValue | string[]): string {
function nameFrom(property: SenML | string[]): string {
return isPropertyValue(property) ? property.n : property[0]
}

function toString(value: CBORValue[]): string {
const encoded = encode(value);
function toString(value: SenML[]): string {
const encoded = CBOR.encode(value);
return Utils.arrayBufferToBase64(encoded);
};

function toCloudProtocolV2(cborValue: CBORValue): CBORValue {
function toCloudProtocolV2(cborValue: SenML): SenML {
const cloudV2CBORValue = {};
let cborLabel = null;

Expand Down Expand Up @@ -52,8 +52,8 @@ function toCloudProtocolV2(cborValue: CBORValue): CBORValue {
return cloudV2CBORValue;
}

function parse(value: CloudMessageValue, name: string, timestamp: number, deviceId: string): CBORValue {
const parsed: CBORValue = {};
function format(value: CloudMessageValue, name: string, timestamp: number, deviceId: string): SenML {
const parsed: SenML = {};
if (timestamp !== -1) parsed.bt = timestamp || new Date().getTime()
parsed.n = name;

Expand All @@ -68,24 +68,23 @@ function parse(value: CloudMessageValue, name: string, timestamp: number, device
return parsed;
}

function getSenML(name: string, value: CloudMessageValue, timestamp: number, useCloudProtocolV2: boolean, deviceId: string): CBORValue | CBORValue[] {
function parse(name: string, value: CloudMessageValue, timestamp: number, useCloudProtocolV2: boolean, deviceId: string): SenML | SenML[] {
if (timestamp && !Number.isInteger(timestamp)) throw new Error('Timestamp must be Integer');
if (name === undefined || typeof name !== 'string') throw new Error('Name must be a valid string');

if (Utils.isObject(value)) return Object.keys(value)
.map((key, i) => parse(value[key], `${name}:${key}`, i === 0 ? timestamp : -1, i === 0 ? deviceId : undefined))
.map((key, i) => format(value[key], `${name}:${key}`, i === 0 ? timestamp : -1, i === 0 ? deviceId : undefined))
.map((cborValue) => useCloudProtocolV2 ? toCloudProtocolV2(cborValue) : cborValue);

let cborValue = parse(value, name, timestamp, deviceId);
let cborValue = format(value, name, timestamp, deviceId);
if (useCloudProtocolV2) cborValue = toCloudProtocolV2(cborValue);
return cborValue;
};

export default {
encode,
decode,
CBOR,
parse,
toString,
getSenML,
valueFrom,
nameFrom,
isPropertyValue,
Expand Down
8 changes: 4 additions & 4 deletions test/arduino-cloud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* a commercial license, send an email to license@arduino.cc.
*
*/
const ArduinoCloud = require('../dist/index.js').default;
const { CBOR } = require('../dist/index.js');
const ArduinoCloud = require('../dist/index.js');
const { SenML } = require('../dist/index.js');

const deviceId = '1f4ced70-53ad-4b29-b221-1b0abbdfc757';
const thingId = '2cea8542-d472-4464-859c-4ef4dfc7d1d3';
Expand Down Expand Up @@ -106,6 +106,6 @@ const sendPropertyAsDevice = (deviceId, thingId, name, value, timestamp = new Da
if (timestamp && !Number.isInteger(timestamp)) throw new Error('Timestamp must be Integer');
if (name === undefined || typeof name !== 'string') throw new Error('Name must be a valid string');

const senMlValue = CBOR.getSenML(name, value, timestamp, false, deviceId);
return ArduinoCloud.sendMessage(`/a/t/${thingId}/e/o`, CBOR.encode([senMlValue]));
const senMlValue = SenML.parse(name, value, timestamp, false, deviceId);
return ArduinoCloud.sendMessage(`/a/t/${thingId}/e/o`, SenML.CBOR.encode([senMlValue]));
};
Loading