Skip to content

Commit

Permalink
Changed Everynet Plugin to monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
bgelatti committed Oct 17, 2024
1 parent 86246cb commit d538591
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 7,439 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

File renamed without changes.
File renamed without changes
18 changes: 18 additions & 0 deletions plugins/network-everynet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@tago-io/tcore-plugin-network-everynet",
"version": "0.7.0",
"private": false,
"main": "./src/index.ts",
"type": "module",
"tcore": {
"name": "Everynet LoRaWAN",
"short_description": "Adds support for Everynet LoRaWAN",
"full_description": "./README.md",
"icon": "./assets/icon.png",
"cluster": true,
"types": ["service", "encoder", "action-type"],
"permissions": ["device", "action", "device-data"]
},
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ interface IEverynetObject {
params: IParams;
}

export { IEverynetObject };
export type { IEverynetObject };
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { core, helpers } from "@tago-io/tcore-sdk";
import axios, { AxiosRequestConfig } from "axios";
import { Request, Response } from "express";
import sendResponse from "../lib/sendResponse";
import { IConfigParam } from "../types";
import { getDevice } from "./uplink";
import axios, { type AxiosRequestConfig } from "axios";
import type { Request, Response } from "express";
import sendResponse from "../lib/sendResponse.ts";
import type { IConfigParam } from "../types.ts";
import { getDevice } from "./uplink.ts";

interface IDownlinkParams {
device: string;
Expand All @@ -21,16 +21,28 @@ interface IDownlinkParams {
* @param deviceID - device ID
* @returns {void}
*/
async function setDeviceDownlinkParams(body: IDownlinkParams, deviceID: string) {
async function setDeviceDownlinkParams(
body: IDownlinkParams,
deviceID: string,
) {
const deviceParams = await core.getDeviceParamList(deviceID);

const defaultParamSettings = (key) => {
const index = deviceParams.push({ key, value: "", id: helpers.generateResourceID() });
const index = deviceParams.push({
key,
value: "",
id: helpers.generateResourceID(),
});
return deviceParams[index - 1];
};
const downlinkParam = deviceParams.find((x) => x.key === "downlink") || defaultParamSettings("downlink");
const fportParam = deviceParams.find((x) => x.key === "port") || defaultParamSettings("port");
const confirmedParam = deviceParams.find((x) => x.key === "confirmed") || defaultParamSettings("confirmed");
const downlinkParam =
deviceParams.find((x) => x.key === "downlink") ||
defaultParamSettings("downlink");
const fportParam =
deviceParams.find((x) => x.key === "port") || defaultParamSettings("port");
const confirmedParam =
deviceParams.find((x) => x.key === "confirmed") ||
defaultParamSettings("confirmed");

downlinkParam.value = String(body.payload);
fportParam.value = String(body.port);
Expand All @@ -47,22 +59,37 @@ async function setDeviceDownlinkParams(body: IDownlinkParams, deviceID: string)
* @param res - request response
* @returns {void}
*/
async function downlinkService(config: IConfigParam, req: Request, res: Response) {
const authorization = req.headers["Authorization"] || req.headers["authorization"];
async function downlinkService(
config: IConfigParam,
req: Request,
res: Response,
) {
const authorization = req.headers.Authorization || req.headers.authorization;
if (!authorization || authorization !== config.authorization_code) {
console.error(`[Network Server] Request refused, authentication is invalid: ${authorization}`);
return sendResponse(res, { body: "Invalid authorization header", status: 401 });
console.error(
`[Network Server] Request refused, authentication is invalid: ${authorization}`,
);
return sendResponse(res, {
body: "Invalid authorization header",
status: 401,
});
}

const body = <IDownlinkParams>req.body;
if (!body.device) {
return sendResponse(res, { body: "Missing device paramater with device eui as value", status: 400 });
return sendResponse(res, {
body: "Missing device paramater with device eui as value",
status: 400,
});
}
if (!body.port) {
return sendResponse(res, { body: "Missing port paramater", status: 400 });
}
if (!body.payload) {
return sendResponse(res, { body: "Missing payload paramater with hexadecimal value", status: 400 });
return sendResponse(res, {
body: "Missing payload paramater with hexadecimal value",
status: 400,
});
}

const device = await getDevice(body.device);
Expand Down Expand Up @@ -98,11 +125,15 @@ async function downlinkService(config: IConfigParam, req: Request, res: Response
.catch((error) => {
// console.error(`Downlink error: ${params.device} - ${params.authorization}: ${JSON.stringify(error.message)}`);
return sendResponse(res, {
body: { everynet_error: error.response.data, url, message: "downlink_claim error" },
body: {
everynet_error: error.response.data,
url,
message: "downlink_claim error",
},
status: error.response.status,
});
});
}

export default downlinkService;
export { IDownlinkParams };
export type { IDownlinkParams };
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { core } from "@tago-io/tcore-sdk";
import { IDeviceData } from "@tago-io/tcore-sdk/build/Types";
import { IConfigParam } from "../types";
import downlinkService, { IDownlinkParams } from "./downlink";
import type { IDeviceData } from "@tago-io/tcore-sdk/Types";
import type { IConfigParam } from "../types.ts";
import downlinkService, { type IDownlinkParams } from "./downlink.ts";

class ResMockup {
_status = 200;
json(body: {}) {
json(body: any) {
if (this._status >= 400) {
console.error(body);
}
Expand All @@ -32,18 +32,29 @@ async function downlinkAction(
pluginConfig: IConfigParam,
actionID: string,
actionSettings: IDownlinkParams,
dataList: IDeviceData
dataList: IDeviceData,
) {
const action = await core.getActionInfo(actionID);

if (Array.isArray(dataList) && dataList[0].variable) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const variables = action.trigger.conditions.map((condition: any) => condition.variable);
const variables = action.trigger.conditions.map(
(condition: any) => condition.variable,
);
const data = dataList.find((item) => variables.includes(item.variable));

actionSettings.payload = actionSettings.payload.replace(/\$VALUE\$/g, data.value);
actionSettings.payload = actionSettings.payload.replace(/\$VARIABLE\$/g, data.variable);
actionSettings.payload = actionSettings.payload.replace(/\$SERIE\$/g, data.serie);
actionSettings.payload = actionSettings.payload.replace(
/\$VALUE\$/g,
data.value,
);
actionSettings.payload = actionSettings.payload.replace(
/\$VARIABLE\$/g,
data.variable,
);
actionSettings.payload = actionSettings.payload.replace(
/\$SERIE\$/g,
data.serie,
);

if (actionSettings.device.toLowerCase() === "$device_id$") {
const deviceInfo = await core.getDeviceInfo(data.origin);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { core } from "@tago-io/tcore-sdk";
import { Request, Response } from "express";
import sendResponse from "../lib/sendResponse";
import { IConfigParam } from "../types";
import { getDevice } from "./uplink";
import type { Request, Response } from "express";
import sendResponse from "../lib/sendResponse.ts";
import type { IConfigParam } from "../types.ts";
import { getDevice } from "./uplink.ts";

interface IClassAConfig {
downlink_key: string;
Expand Down Expand Up @@ -35,18 +35,33 @@ interface IEverynetParams {
* @param res - request response
* @returns {void}
*/
async function everynetDownlinkRequest(config: IConfigParam, req: Request, res: Response) {
async function everynetDownlinkRequest(
config: IConfigParam,
req: Request,
res: Response,
) {
const body = <IEverynetParams>req.body;

const authorization = req.headers["Authorization"] || req.headers["authorization"] || body?.meta?.application;
const authorization =
req.headers.Authorization ||
req.headers.authorization ||
body?.meta?.application;
if (!authorization || authorization !== config.authorization_code) {
console.error(`[Network Server] Request refused, authentication is invalid: ${authorization}`);
return sendResponse(res, { body: "Invalid authorization header", status: 401 });
console.error(
`[Network Server] Request refused, authentication is invalid: ${authorization}`,
);
return sendResponse(res, {
body: "Invalid authorization header",
status: 401,
});
}

const serieNumber = body.device || body.meta?.device;
if (!serieNumber) {
return sendResponse(res, { body: { message: `Device not found ${serieNumber}` }, status: 204 });
return sendResponse(res, {
body: { message: `Device not found ${serieNumber}` },
status: 204,
});
}

const device = await getDevice(serieNumber);
Expand All @@ -60,7 +75,8 @@ async function everynetDownlinkRequest(config: IConfigParam, req: Request, res:
return sendResponse(res, { body: {}, status: 204 });
}

const convertConfirmed = (value?: string | boolean) => value && (value === "true" || typeof value === "boolean");
const convertConfirmed = (value?: string | boolean) =>
value && (value === "true" || typeof value === "boolean");
const response = {
meta: {
network: body.meta?.network,
Expand All @@ -73,7 +89,7 @@ async function everynetDownlinkRequest(config: IConfigParam, req: Request, res:
},
type: "downlink_response",
params: {
confirmed: convertConfirmed(confirmedParam?.value) ? true : false,
confirmed: !!convertConfirmed(confirmedParam?.value),
payload: String(downlinkParam?.value),
port: fportParam ? Number(fportParam?.value) : 1,
},
Expand All @@ -82,9 +98,11 @@ async function everynetDownlinkRequest(config: IConfigParam, req: Request, res:
downlinkParam.sent = true;
await core.setDeviceParams(device.id, params);

console.info(`Downlink Request delivered with success: ${serieNumber} - ${authorization}`);
console.info(
`Downlink Request delivered with success: ${serieNumber} - ${authorization}`,
);
return sendResponse(res, { body: response, status: 200 });
}

export default everynetDownlinkRequest;
export { IClassAConfig };
export type { IClassAConfig };
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import inspectFormat from "../lib/inspectFormat";
import toTagoFormat, { IDeviceDataLatLng, IToTagoObject } from "../lib/toTagoFormat";
import { IEverynetObject } from "./IEverynetObject";
import inspectFormat from "../lib/inspectFormat.ts";
import toTagoFormat, {
type IDeviceDataLatLng,
type IToTagoObject,
} from "../lib/toTagoFormat.ts";
import type { IEverynetObject } from "./IEverynetObject.ts";

/**
* In the solutions params is where usually latitude and longitude for your antenna signal comes from.
Expand All @@ -9,11 +12,17 @@ import { IEverynetObject } from "./IEverynetObject";
* @param {string | number} serie - serie for the variables
* @returns {IDeviceDataLatLng[]} data to be stored
*/
function transformSolutionParam(solutions: IEverynetObject["params"]["solutions"], serie: string) {
function transformSolutionParam(
solutions: IEverynetObject["params"]["solutions"],
serie: string,
) {
let toTago: IDeviceDataLatLng[] = [];
for (const s of solutions) {
let fixedJSON: IToTagoObject = {};
fixedJSON.gw_location = { value: `${s.lat}, ${s.lng}`, location: { lat: s.lat, lng: s.lng } };
fixedJSON.gw_location = {
value: `${s.lat}, ${s.lng}`,
location: { lat: s.lat, lng: s.lng },
};
// delete s.lat;
// delete s.lng;

Expand Down Expand Up @@ -45,27 +54,32 @@ export default async function parser(payload: any) {

// Get a unique serie for the incoming data.
if (payload.params.solutions) {
toTago = toTago.concat(transformSolutionParam(payload.params.solutions, serie));
delete payload.params.solutions;
toTago = toTago.concat(
transformSolutionParam(payload.params.solutions, serie),
);
payload.params.solutions = undefined;
}

if (payload.meta) {
toTago = toTago.concat(toTagoFormat(payload.meta, serie));
delete payload.meta;
payload.meta = undefined;
}

if (payload.params.radio) {
toTago = toTago.concat(inspectFormat(payload.params.radio, serie));
delete payload.params.radio;
payload.params.radio = undefined;
}

if (payload.params) {
if (payload.params.payload) {
payload.params.payload = Buffer.from(payload.params.payload, "base64").toString("hex");
payload.params.payload = Buffer.from(
payload.params.payload,
"base64",
).toString("hex");
}

toTago = toTago.concat(inspectFormat(payload.params, serie));
delete payload.params;
payload.params = undefined;
}

return toTago;
Expand Down
Loading

0 comments on commit d538591

Please sign in to comment.