/*
MachineSettings.tsx - ESP3D WebUI Target file
Copyright (c) 2020 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 { Fragment } from "preact"
import { useEffect, useState } from "preact/hooks"
import { T } from "../../../components/Translations"
import { processor } from "./processor"
import { useTargetCommands } from "../../../hooks"
import { useToastsContext, useUiContext, useUiContextFn } from "../../../contexts"
import { Target } from "./index"
import {
formatFileSizeToString,
} from "../../../components/Helpers"
import {
Field,
Loading,
ButtonImg,
CenterLeft,
} from "../../../components/Controls"
import { RefreshCcw, XCircle, Send, Flag } from "preact-feather"
import { CMD } from "./CMD-source"
type MachineSettingElement = {
type?: string
value?: string
initial?: string
cmd?: string
hasmodified?: boolean
haserror?: boolean
}
const machineSettings: { cache: any[] } = { cache: [] }
const MachineSettings = () => {
const [isLoading, setIsLoading] = useState(false)
const [collected, setCollected] = useState("0 B")
const { sendSerialCmd } = useTargetCommands()
const { uisettings } = useUiContext()
const { toasts } = useToastsContext()
const processCallBack = (_data: string, total: number) => {
setCollected(formatFileSizeToString(total))
}
const processFeedback = (feedback: any) => {
if (feedback.status) {
if (feedback.status == "error") {
console.log("got error")
toasts.addToast({
content: feedback.content
? `${T("S22")}:${T(feedback.content)}`
: T("S4"),
type: "error",
})
} else if (feedback.command == "eeprom") {
machineSettings.cache = CMD.command("formatEeprom", feedback.content)
}
}
setIsLoading(false)
}
const onCancel = (_e?: any) => {
useUiContextFn.haptic()
toasts.addToast({
content: T("S175"),
type: "error",
})
processor.stopCatchResponse()
machineSettings.cache = []
setIsLoading(false)
}
const onRefresh = (e?: any) => {
if (e) useUiContextFn.haptic()
//get command
const response = CMD.command("eeprom")
//send query
if (
processor.startCatchResponse(
"CMD",
"eeprom",
processFeedback,
null,
processCallBack
)
) {
setCollected("0 B")
setIsLoading(true)
sendSerialCmd(response.cmd, () => {})
}
}
const sendCommand = (element: MachineSettingElement, setvalidation: (v: any) => void) => {
sendSerialCmd(`${element.cmd}=${(element.value || '').trim()}`, () => {
element.initial = element.value
setvalidation(generateValidation(element))
})
//TODO: Should answer be checked ?
}
const generateValidation = (fieldData: MachineSettingElement) => {
const validation: { message: any; valid: boolean; modified: boolean } = {
message: