implementation module miTaskDevices import mTask import iTasks :: Channels :== ([MTaskMSGRecv], [MTaskMSGSend], Bool) :: TCPDevice = {hostname :: String, port :: Int} :: SerialDevice = {settings :: TTYSettings} :: DeviceType = SerialDevice | TCPDevice derive class iTask TCPDevice, SerialDevice, DeviceType getmTaskDevice :: Task a | mTaskDevice a getmTaskDevice = enterInformation "Device type" [] >&^ \st->whileUnchanged st $ \dt->case dt of Nothing = viewInformation "No type selected yet" [] Nothing Just SerialDevice = getSerialDevice @ pure Just TCPDevice = getTDevice @ pure >>* [OnValue (ifValue isJust fromJust)] where getSD :: Task SerialDevice getSD = entermTaskDevice getTD :: Task TCPDevice getTD = entermTaskDevice instance mTaskDevice TCPDevice where entermTaskDevice = enterInformation "" [] viewmTaskDevice = viewInformation "" [] syncTask d ch = catchAll ( tcpconnect d.host d.port ch {ConnectionHandlers| onConnect=onConnect, whileConnected=whileConnected, onDisconnect=onDisconnect} @! ()) (\v->traceValue v @! ()) where onConnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool) onConnect _ (msgs,send,sendStopped) = (Ok "", Just (msgs,[],sendStopped), map encode send, False) whileConnected :: (Maybe String) String ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool) whileConnected Nothing acc (msgs,send,sendStopped) = (Ok acc, Nothing, [], False) whileConnected (Just newData) acc (msgs,send,sendStopped) = (Ok acc, Just (msgs ++ [decode newData],[],False), map encode send, False) onDisconnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool)) onDisconnect l (msgs,send,sendStopped) = (Ok l, Nothing) instance mTaskDevice SerialDevice where entermTaskDevice = enterTTYSettings >>= \s->{SerialDevice|settings=s} viewmTaskDevice = viewInformation "" [] syncTask d ch = syncSerialSettings d.settings encode decode ch