/* ExtraControls.js - ESP3D WebUI component file Copyright (c) 2021 Luc LEBOSSE. All rights reserved. This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with This code; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import { TargetedMouseEvent } from "preact" import type { FunctionalComponent } from "preact" import { useState } from "preact/hooks" import { T } from "../Translations" import { Sliders, Send } from "preact-feather" import { useUiContextFn } from "../../contexts" import { useTargetContext } from "../../targets" import { ButtonImg, Loading, Field, FullScreenButton, CloseButton, } from "../Controls" import { useTargetCommands } from "../../hooks" import { ContainerHelper } from "../Controls" /* * Local const * */ /*for fan, flowrate, speed /*each element has current, min, max /*like /*[ [{current:0, min:0, max:100}, {current:0, min:0, max:100}], [{current:100, min:1, max:300}, {current:100, min:1, max:300}], [{current:100, min:1, max:300}] ] */ type ControlValue = { current: any; min?: number; max?: number } const target_values: ControlValue[][] = [[], [], []] const isVisible = (pos: number): boolean => { const setting = ["showfanctrls", "showflowratectrls", "showspeedctrls"] return setting[pos] != undefined ? useUiContextFn.getValue(setting[pos]) : false } const presetList = (pos: number): any => { const setting = ["fanpresets", "flowratepresets", "speedpresets"] if (setting[pos] != undefined) { const list = useUiContextFn.getValue(setting[pos]) if (list) return list.split(";").map((item: string) => { return { display: `${item }%`, value: item } }) } return "" } const controlCommand = (pos: number, index: number, value: any): string => { const setting = ["fancmd", "flowratecmd", "speedcmd"] if (setting[pos] != undefined) { const cmd = useUiContextFn.getValue(setting[pos]) if (cmd) return cmd .replace("#", index) .replace("$", pos == 0 ? (parseInt(value) * 255) / 100 : value) } return "" } const controlMinMax = (pos: number): any => { const setting = ["fanpresets", "flowratepresets", "speedpresets"] if (setting[pos] != undefined) { const element = useUiContextFn.getElement(setting[pos]) if (element) return { min: element.min, max: element.max } } return "" } const ExtraControls: FunctionalComponent = () => { const { temperatures, fanSpeed, flowRate, feedRate, sensor } = useTargetContext() as any if ( !( useUiContextFn.getValue("showfanctrls") || useUiContextFn.getValue("showflowratectrls") || useUiContextFn.getValue("showfeedratectrls") || useUiContextFn.getValue("showsensorctrls") ) ) return null return (