Skip to content

Commit 3362cf7

Browse files
committed
Rename
1 parent b3bebc3 commit 3362cf7

File tree

10 files changed

+119
-117
lines changed

10 files changed

+119
-117
lines changed

src/@arduino/cbor-js.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626

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

31-
export type CBORValue = {
31+
export type SenML = {
3232
bn?: string;
3333
bt?: number;
3434
bu?: string;

src/client/ArduinoCloudClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { filter } from "rxjs/operators";
22
import { Subscription, Subject, Observable } from "rxjs";
33

4-
import CBOR from "../cbor";
4+
import SenML from "../senML";
55
import Utils from "../utils";
66
import { IConnectionBuilder } from '../builder/IConnectionBuilder';
77
import { IConnection, CloudMessage } from "../connection/IConnection";
@@ -161,8 +161,8 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
161161

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

168168
public async onPropertyValue<T extends CloudMessageValue>(thingId: string, name: string, cb: OnMessageCallback<T>): Promise<void> {

src/connection/Connection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import jws from 'jws';
22
import mqtt from 'mqtt';
33
import { Observable, Subject } from "rxjs";
44

5-
import CBOR from '../cbor';
5+
import SenML from '../senML';
66
import Utils from "../utils";
77
import { CloudMessageValue } from "../client/IArduinoCloudClient";
88
import { IConnection, CloudMessage, ConnectionOptions } from "./IConnection";
@@ -84,11 +84,11 @@ export class Connection implements IConnection {
8484
let valueToSend: CloudMessageValue = {};
8585

8686
const messages: CloudMessage[] = [];
87-
const properties = CBOR.decode(Utils.toArrayBuffer(msg));
87+
const properties = SenML.CBOR.decode(Utils.toArrayBuffer(msg));
8888

8989
properties.forEach((p) => {
90-
const value = CBOR.valueFrom(p);
91-
[current, attribute] = CBOR.nameFrom(p).split(':');
90+
const value = SenML.valueFrom(p);
91+
[current, attribute] = SenML.nameFrom(p).split(':');
9292
if (previous === '') previous = current;
9393

9494
if (previous !== current) {

src/index.lib.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
*/
2020

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

31-
export { CBOR };
3231
export default DefaultClient;
32+
export { SenML, ArduinoCloudClient };
33+
export { IArduinoCloudClient, CloudOptions, CloudMessageValue } from "./client/IArduinoCloudClient";

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
import "whatwg-fetch";
22-
import CBOR from './cbor';
22+
import SenML from './senML';
2323
import { HttpClientFactory } from './http/HttpClientFactory';
2424
import { ArduinoCloudClient } from "./client/ArduinoCloudClient";
2525
import { APIConnectionBuilder } from "./builder/APIConnectionBuilder";
@@ -28,5 +28,6 @@ import { TokenConnectionBuilder } from "./builder/TokenConnectionBuilder";
2828
const builders = [new TokenConnectionBuilder(), new APIConnectionBuilder(HttpClientFactory.Create(fetch))];
2929
const DefaultClient = new ArduinoCloudClient(builders);
3030

31-
export { CBOR };
3231
export default DefaultClient;
32+
export { SenML, ArduinoCloudClient };
33+
export { IArduinoCloudClient, CloudOptions, CloudMessageValue } from "./client/IArduinoCloudClient";

src/cbor/index.ts renamed to src/senML/index.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { encode, decode, CBORValue } from '@arduino/cbor-js';
1+
import CBOR, { SenML } from '@arduino/cbor-js';
22

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

6-
function isPropertyValue(message: CBORValue | string[]): message is CBORValue {
7-
return !!(message as CBORValue).n;
6+
function isPropertyValue(message: SenML | string[]): message is SenML {
7+
return !!(message as SenML).n;
88
}
99

10-
function valueFrom(message: CBORValue | string[]): CloudMessageValue {
10+
function valueFrom(message: SenML | string[]): CloudMessageValue {
1111
return isPropertyValue(message)
1212
? message.v || message.vs || message.vb
1313
: message[2] || message[3] || message[4];
1414
}
1515

16-
function nameFrom(property: CBORValue | string[]): string {
16+
function nameFrom(property: SenML | string[]): string {
1717
return isPropertyValue(property) ? property.n : property[0]
1818
}
1919

20-
function toString(value: CBORValue[]): string {
21-
const encoded = encode(value);
20+
function toString(value: SenML[]): string {
21+
const encoded = CBOR.encode(value);
2222
return Utils.arrayBufferToBase64(encoded);
2323
};
2424

25-
function toCloudProtocolV2(cborValue: CBORValue): CBORValue {
25+
function toCloudProtocolV2(cborValue: SenML): SenML {
2626
const cloudV2CBORValue = {};
2727
let cborLabel = null;
2828

@@ -52,8 +52,8 @@ function toCloudProtocolV2(cborValue: CBORValue): CBORValue {
5252
return cloudV2CBORValue;
5353
}
5454

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

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

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

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

79-
let cborValue = parse(value, name, timestamp, deviceId);
79+
let cborValue = format(value, name, timestamp, deviceId);
8080
if (useCloudProtocolV2) cborValue = toCloudProtocolV2(cborValue);
8181
return cborValue;
8282
};
8383

8484
export default {
85-
encode,
86-
decode,
85+
CBOR,
86+
parse,
8787
toString,
88-
getSenML,
8988
valueFrom,
9089
nameFrom,
9190
isPropertyValue,

test/arduino-cloud.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* a commercial license, send an email to license@arduino.cc.
1818
*
1919
*/
20-
const ArduinoCloud = require('../dist/index.js').default;
21-
const { CBOR } = require('../dist/index.js');
20+
const ArduinoCloud = require('../dist/index.js');
21+
const { SenML } = require('../dist/index.js');
2222

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

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

0 commit comments

Comments
 (0)