-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor component attachment logic and add SD Offline Logger component
- Loading branch information
1 parent
1ff3f11
commit 7a3adbd
Showing
5 changed files
with
151 additions
and
10 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
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,79 @@ | ||
import { device } from "./Device"; | ||
|
||
class SdOfflineLogger { | ||
name; | ||
type; | ||
timeId; | ||
jsonFileName; | ||
intervalSeconds; | ||
publishDataWhenOnline; | ||
publishDataTopic; | ||
sensorsArray; | ||
|
||
constructor(name, timeId, jsonFileName, intervalSeconds, publishDataWhenOnline, publishDataTopic) { | ||
this.name = name; | ||
this.type = "sd_card_component"; | ||
this.timeId = timeId; | ||
this.jsonFileName = jsonFileName; | ||
this.intervalSeconds = intervalSeconds; | ||
this.publishDataWhenOnline = publishDataWhenOnline; | ||
this.publishDataTopic = publishDataTopic; | ||
this.sensorsArray = []; | ||
} | ||
|
||
attach(pin, deviceComponents) { | ||
deviceComponents?.sensor?.forEach((sensor) => { | ||
if (sensor.name){ | ||
this.sensorsArray.push(sensor.name); | ||
} else if (sensor.id){ | ||
this.sensorsArray.push(sensor.id); | ||
} | ||
}); | ||
|
||
const componentObjects = [ | ||
{ | ||
name: "external_components", | ||
config: { | ||
//@ts-ignore | ||
source: "github://Protofy-xyz/esphome-components", | ||
refresh: "10s", | ||
components: ["sd_card_component"] | ||
} | ||
}, | ||
{ | ||
name: "sd_card_component", | ||
config: { | ||
id: this.name, | ||
cs_pin: pin, | ||
time_id: this.timeId, | ||
json_file_name: this.jsonFileName, | ||
interval_seconds: this.intervalSeconds, | ||
publish_data_when_online: this.publishDataWhenOnline, | ||
publish_data_topic: deviceComponents.mqtt?.topic_prefix+this.publishDataTopic, | ||
sensors: this.sensorsArray, | ||
}, | ||
}, | ||
]; | ||
|
||
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 sdOfflineLogger(name, timeId, jsonFileName, intervalSeconds, publishDataWhenOnline, publishDataTopic) { | ||
return new SdOfflineLogger(name, timeId, jsonFileName, intervalSeconds, publishDataWhenOnline, publishDataTopic); | ||
} |
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,46 @@ | ||
import React from "react"; | ||
import { Node, Field, HandleOutput, NodeParams } from "protoflow"; | ||
import { getColor } from "."; | ||
|
||
const SdOfflineLogger = ({ node = {}, nodeData = {}, children, color }: any) => { | ||
const nameErrorMsg = 'Reserved name' | ||
const [name, setName] = React.useState(nodeData['param-1']) | ||
const intervalErrorMsg = 'Add units h/m/s/ms' | ||
const nodeParams: Field[] = [ | ||
{ | ||
label: 'Name', static: true, field: 'param-1', type: 'input', onBlur: () => { setName(nodeData['param-1']) }, | ||
error: nodeData['param-1']?.value?.replace(/['"]+/g, '') == 'sd_card_component' ? nameErrorMsg : null | ||
}, | ||
{ label: 'Time ID', static: true, field: 'param-2', type: 'input', }, | ||
{ label: 'JSON File Name', static: true, field: 'param-3', type: 'input' }, | ||
{ | ||
label: 'Interval Seconds', static: true, field: 'param-4', type: 'input', | ||
error: !['h', 'm', 's', 'ms'].includes(nodeData['param-4']?.value?.replace(/['"0-9]+/g, '')) ? intervalErrorMsg : null | ||
}, | ||
{ label: 'Publish Data When Online', static: true, field: 'param-5', type: 'boolean' }, | ||
{ label: 'Publish Data Topic', static: true, field: 'param-6', type: 'input' }, | ||
|
||
] as Field[] | ||
return ( | ||
<Node node={node} isPreview={!node.id} title='SD offline logger' color={color} id={node.id} skipCustom={true} disableInput disableOutput> | ||
<NodeParams id={node.id} params={nodeParams} /> | ||
</Node> | ||
) | ||
} | ||
|
||
export default { | ||
id: 'SdOfflineLogger', | ||
type: 'CallExpression', | ||
category: "Utils", | ||
keywords: ["sd", "log", "offline"], | ||
check: (node, nodeData) => node.type == "CallExpression" && nodeData.to?.startsWith('sdOfflineLogger'), | ||
getComponent: (node, nodeData, children) => <SdOfflineLogger color={getColor('SdOfflineLogger')} node={node} nodeData={nodeData} children={children} />, | ||
getInitialData: () => { return { to: 'sdOfflineLogger', | ||
"param-1": { value: "", kind: "StringLiteral" }, | ||
"param-2": { value: "", kind: "StringLiteral" }, | ||
"param-3": { value: "/offline_data.json", kind: "StringLiteral" }, | ||
"param-4": { value: "30s", kind: "StringLiteral" }, | ||
"param-5": { value: true, kind: "FalseKeyword" }, | ||
"param-6": { value: "/ofline_data", 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