import React from 'react'; import { useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { FiCheck, FiMenu, FiSave } from 'react-icons/fi'; import { Card } from '@app/components/generic/Card'; import { EnumSelect } from '@app/components/generic/form/EnumSelect'; import { Input } from '@app/components/generic/form/Input'; import { IconButton } from '@app/components/generic/IconButton'; import { Toggle } from '@app/components/generic/Toggle'; import { connection, setConnection } from '@app/core/connection'; import { useAppDispatch, useAppSelector } from '@app/hooks/redux'; import { Button } from '@components/generic/Button'; import { PrimaryTemplate } from '@components/templates/PrimaryTemplate'; import { IBLEConnection, IHTTPConnection, ISerialConnection, } from '@meshtastic/meshtasticjs'; import type { BLEConnectionParameters, HTTPConnectionParameters, SerialConnectionParameters, } from '@meshtastic/meshtasticjs/dist/types'; export interface ConnectionProps { navOpen: boolean; setNavOpen: React.Dispatch>; } enum connType { HTTP, BLE, SERIAL, } export const Connection = ({ navOpen, setNavOpen, }: ConnectionProps): JSX.Element => { const dispatch = useAppDispatch(); const [selectedConnType, setSelectedConnType] = React.useState(connType.HTTP); const [bleDevices, setBleDevices] = React.useState([]); const [serialDevices, setSerialDevices] = React.useState([]); const [httpIpSource, setHttpIpSource] = React.useState<'local' | 'remote'>( 'local', ); const { t } = useTranslation(); const hostOverrideEnabled = useAppSelector( (state) => state.meshtastic.hostOverrideEnabled, ); const hostOverride = useAppSelector((state) => state.meshtastic.hostOverride); const { register, handleSubmit, formState } = useForm<{ method: connType; }>({ defaultValues: { method: connType.HTTP, }, }); const connect = async ( connectionType: connType, params: | HTTPConnectionParameters | SerialConnectionParameters | BLEConnectionParameters, ): Promise => { connection.complete(); connection.disconnect(); if (connectionType === connType.BLE) { setConnection(new IBLEConnection()); } else if (connectionType === connType.HTTP) { setConnection(new IHTTPConnection()); } else { setConnection(new ISerialConnection()); } // @ts-ignore tmp await connection.connect(params); }; const updateBleDeviceList = async (): Promise => { const devices = await ble.getDevices(); setBleDevices(devices); }; const updateSerialDeviceList = async (): Promise => { const devices = await serial.getPorts(); console.log(devices); setSerialDevices(devices); }; React.useEffect(() => { if (selectedConnType === connType.BLE) { void updateBleDeviceList(); } if (selectedConnType === connType.SERIAL) { void updateSerialDeviceList(); } }, [selectedConnType]); const onSubmit = handleSubmit((data) => { // void connection.setOwner(data); }); const connectionURL: string = hostOverrideEnabled ? hostOverride : import.meta.env.NODE_ENV === 'production' ? window.location.hostname : (import.meta.env.SNOWPACK_PUBLIC_DEVICE_IP as string) ?? 'http://meshtastic.local'; const ble = new IBLEConnection(); const serial = new ISerialConnection(); return ( } onClick={(): void => { setNavOpen(!navOpen); }} /> } footer={ } >
{ setSelectedConnType(parseInt(e.target.value)); }} /> {selectedConnType === connType.HTTP && ( <> { setHttpIpSource(e.target.value as 'local' | 'remote'); }} /> {httpIpSource === 'local' ? ( ) : ( )} )} {selectedConnType === connType.BLE && (
Previously connected devices
{bleDevices.map((device) => (
=> { console.log('clicked'); await connect(connType.BLE, { device: device, }); }} className="flex justify-between p-2 bg-gray-700 rounded-md" key={device.id} >
{device.name}
=> { console.log('clicked'); await connect(connType.BLE, { device: device, }); }} icon={} />
))}
)} {selectedConnType === connType.SERIAL && (
Previously connected devices
{serialDevices.map((device) => (
{device.getInfo().usbProductId} {device.getInfo().usbVendorId}
=> { await connect(connType.SERIAL, { // @ts-ignore tmp device: device, }); }} icon={} />
))}
)}
); };