Skip to content

Commit

Permalink
Add SNTP Time component
Browse files Browse the repository at this point in the history
  • Loading branch information
TonProtofy committed Oct 31, 2024
1 parent d6066e8 commit 1ff3f11
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
53 changes: 53 additions & 0 deletions packages/protodevice/src/device/SntpTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class SntpTime {
name;
type;
timeZone;

constructor(name, timeZone) {
this.name = name;
this.type = "time";
this.timeZone = timeZone
}

attach(pin, deviceComponents) {

const componentObjects = [
{
name: "time",
config: {
platform: "sntp",
id: this.name,
timezone: this.timeZone,
servers: [
"0.pool.ntp.org",
"1.pool.ntp.org",
"2.pool.ntp.org"
]

}
},

];

componentObjects.forEach((element) => {
if (!deviceComponents[element.name]) {
deviceComponents[element.name] = element.config;
} else {
if (!Array.isArray(deviceComponents[element.name])) {
deviceComponents[element.name] = [deviceComponents[element.name]];
}
deviceComponents[element.name] = [...deviceComponents[element.name], element.config];
}
});

return deviceComponents;
}

getSubsystem() {
return {}
}
}

export function sntpTime(name, timeZone) {
return new SntpTime(name, timeZone);
}
3 changes: 2 additions & 1 deletion packages/protodevice/src/device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ export * from './CANBus'
export * from './MKSServo42D'
export * from './Msa3xx'
export * from './ODrive'
export * from './INA226'
export * from './INA226'
export * from './SntpTime'
51 changes: 51 additions & 0 deletions packages/protodevice/src/nodes/SntpTime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { Node, Field, HandleOutput, NodeParams } from "protoflow";
import { getColor } from ".";

const SntpTime = ({ node = {}, nodeData = {}, children, color }: any) => {
const nameErrorMsg = 'Reserved name'
const [name, setName] = React.useState(nodeData['param-1'])
const tzErrorMsg = 'Invalid timezone. (Continent/City)'

const isValidTimeZone = (tz) => {
try {
Intl.DateTimeFormat(undefined, { timeZone: tz });
return true;
} catch (ex) {
return false; // Invalid time zone
}
};

const nodeParams: Field[] = [
{
label: 'Id', static: true, field: 'param-1', type: 'input', onBlur: () => { setName(nodeData['param-1']) },
error: nodeData['param-1']?.value?.replace(/['"]+/g, '') == 'sntp' ? nameErrorMsg : null
},
{
label: 'Timezone', static: true, field: 'param-2', type: 'input',
error: !isValidTimeZone(nodeData['param-2']?.value?.replace(/['"]+/g, ''))
? tzErrorMsg
: null
},


] as Field[]
return (
<Node node={node} isPreview={!node.id} title='SNTP Time' color={color} id={node.id} skipCustom={true} disableInput disableOutput>
<NodeParams id={node.id} params={nodeParams} />
</Node>
)
}

export default {
id: 'SntpTime',
type: 'CallExpression',
category: "Utils",
keywords: ["time", "sntp"],
check: (node, nodeData) => node.type == "CallExpression" && nodeData.to?.startsWith('sntpTime'),
getComponent: (node, nodeData, children) => <SntpTime color={getColor('SntpTime')} node={node} nodeData={nodeData} children={children} />,
getInitialData: () => { return { to: 'sntpTime',
"param-1": { value: "", kind: "StringLiteral" },
"param-2": { value: "Europe/Madrid", kind: "StringLiteral" },
} }
}
4 changes: 3 additions & 1 deletion packages/protodevice/src/nodes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Msa3xx from './Msa3xx';
import ODrive from './ODrive';
import INA226 from './INA226';
import LEDCOutput from './LEDCOutput';
import SntpTime from './SntpTime';


const deviceMasks = [
Expand Down Expand Up @@ -78,7 +79,8 @@ const deviceMasks = [
Msa3xx,
ODrive,
INA226,
LEDCOutput
LEDCOutput,
SntpTime,
]

const masksLength = deviceMasks.length
Expand Down

0 comments on commit 1ff3f11

Please sign in to comment.