Skip to content

Commit

Permalink
feat: add device config json
Browse files Browse the repository at this point in the history
  • Loading branch information
Mefjus committed Jul 30, 2021
1 parent 1614be2 commit 7ed1e40
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
61 changes: 61 additions & 0 deletions packages/device/src/actions/actionExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import rpio from 'rpio';

import {
Action,
ActionMode,
ActionValue,
OpenAction,
SleepAction,
WriteAction,
} from './actions.types';

const valueMapper = (value: ActionValue): number => {
switch (value) {
case 'HIGH':
return rpio.HIGH;
case 'LOW':
return rpio.LOW;
default:
throw Error(`Can't map action value.`);
}
};

const modeMapper = (mode: ActionMode): number => {
switch (mode) {
case 'OUTPUT':
return rpio.OUTPUT;
case 'INPUT':
return rpio.INPUT;
default:
throw Error(`Can't map action mode.`);
}
};

const sleepAction = ({ time }: SleepAction) => {
rpio.msleep(time);
};

const writeAction = ({ pin, value }: WriteAction) => {
rpio.write(pin, valueMapper(value));
};

const openAction = ({ pin, value, mode }: OpenAction) => {
rpio.open(pin, modeMapper(mode), valueMapper(value));
};

const execute = (actions: Array<Action>) => {
actions.forEach((action) => {
switch (action.type) {
case 'OPEN':
return openAction(action);
case 'SLEEP':
return sleepAction(action);
case 'WRITE':
return writeAction(action);
default:
throw Error(`Can't map action type.`);
}
});
};

export default execute;
27 changes: 27 additions & 0 deletions packages/device/src/actions/actions.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type ActionMode = 'OUTPUT' | 'INPUT';
export type ActionValue = 'LOW' | 'HIGH';

export type OpenAction = {
type: 'OPEN';
pin: number;
mode: ActionMode;
value: ActionValue;
};

export type WriteAction = {
type: 'WRITE';
pin: number;
value: ActionValue;
};

export type SleepAction = {
type: 'SLEEP';
time: number;
};

export type Action = OpenAction | WriteAction | SleepAction;

export type ActionConfig = {
onInit: Array<Action>;
onToggle: Array<Action>;
};
58 changes: 58 additions & 0 deletions packages/device/src/config/actions.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"onInit": [
{
"type": "OPEN",
"pin": 36,
"mode": "OUTPUT",
"value": "LOW"
},
{
"type": "OPEN",
"pin": 38,
"mode": "OUTPUT",
"value": "LOW"
},
{
"type": "OPEN",
"pin": 40,
"mode": "OUTPUT",
"value": "LOW"
}
],
"onToggle": [
{
"type": "WRITE",
"pin": 36,
"value": "HIGH"
},
{
"type": "WRITE",
"pin": 38,
"value": "LOW"
},
{
"type": "WRITE",
"pin": 40,
"value": "LOW"
},
{
"type": "SLEEP",
"time": 1000
},
{
"type": "WRITE",
"pin": 36,
"value": "LOW"
},
{
"type": "WRITE",
"pin": 38,
"value": "HIGH"
},
{
"type": "WRITE",
"pin": 40,
"value": "HIGH"
}
]
}
8 changes: 5 additions & 3 deletions packages/device/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as dotenv from 'dotenv';
import socketClient from 'socket.io-client';

import { onInit, onOpen } from './hooks';
import actionExecutor from './actions/actionExecutor';
import { ActionConfig } from './actions/actions.types';
import actionsConfig from './config/actions.config.json';

dotenv.config();
onInit();
actionExecutor((actionsConfig as ActionConfig).onInit);

const socket = socketClient(process.env.API_URL ?? '', {
query: {
Expand All @@ -24,7 +26,7 @@ socket.on('message', (eventType: WebSocketEvent) => {
console.log('New message with eventType:', eventType);
switch (eventType) {
case WebSocketEvent.TOGGLE_GATE: {
onOpen();
actionExecutor((actionsConfig as ActionConfig).onToggle);
break;
}
default: {
Expand Down
4 changes: 4 additions & 0 deletions packages/device/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.json' {
const value: never;
export default value;
}
2 changes: 1 addition & 1 deletion packages/device/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"module": "commonjs",
"isolatedModules": false
},
"include": ["src"],
"include": ["src", "**/*.json"],
"exclude": ["node_modules"]
}

0 comments on commit 7ed1e40

Please sign in to comment.