-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6066e8
commit 1ff3f11
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
} } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters