import iTasks
import Devices.mTaskSerial
import Devices.mTaskTCP
+import Utils.SDS
import Tasks.mTaskTask
import mTaskInterpret
import Generics.gCons
derive conses MTaskResource, TCPSettings
derive consName MTaskResource, TCPSettings
-channels :: MTaskDevice -> Shared Channels
+channels :: MTaskDevice -> Shared Channels
:: Channels :== ([MTaskMSGRecv], [MTaskMSGSend], Bool)
:: MTaskResource
:: MTaskDevice = {
deviceTask :: Maybe TaskId
+ ,deviceError :: Maybe String
,deviceChannels :: String
,deviceName :: String
,deviceTasks :: [MTaskTask]
class MTaskDuplex a where
synFun :: a (Shared Channels) -> Task ()
+startupDevices :: Task [MTaskDevice]
+connectDevice :: (MTaskDevice (Shared Channels) -> Task ()) MTaskDevice -> Task ()
manageDevices :: (MTaskDevice (Shared Channels) -> Task ()) [MTaskDevice] -> Task ()
sendToDevice :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task ()
import Devices.mTaskSerial
import Devices.mTaskTCP
import iTasks._Framework.Store
+import iTasks.UI.Definition, iTasks.UI.Editor, iTasks.UI.Editor.Builtin, iTasks.UI.Editor.Common, iTasks.UI.Layout.Default, iTasks.UI.Layout.Common
from Data.Func import $
channels :: MTaskDevice -> Shared Channels
channels d = memoryShare d.deviceChannels ([], [], False)
+startupDevices :: Task [MTaskDevice]
+startupDevices = upd (map reset) deviceStore
+ where reset d = {d & deviceTask=Nothing, deviceTasks=[], deviceError=Nothing}
+
makeDevice :: String MTaskResource -> Task MTaskDevice
makeDevice name res = get randomInt @ \rand->{MTaskDevice
|deviceChannels=name +++ toString rand
,deviceName=name
,deviceTasks=[]
,deviceTask=Nothing
+ ,deviceError=Nothing
,deviceData=res}
getSynFun :: MTaskResource -> ((Shared Channels) -> Task ())
Nothing = viewInformation "No type selected yet" [] ""
Just ty = enterInformation "Name" [] -&&- deviceSettings ty
>>= \(name, settings)->makeDevice name settings
- >>= \dev->appendTopLevelTask 'DM'.newMap True (tlt dev)
- >>= \tid->upd (\l->[{dev & deviceTask=Just tid}:l]) devices
+ >>= \dev->upd (\l->[dev:l]) devices
+ >>| connectDevice processFun dev
@! ""
where
- tlt dev = let ch = channels dev in processFun dev ch -||- getSynFun dev.deviceData ch
-
deviceSettings "SerialDevice" = getmTaskSerialDevice
deviceSettings "TCPDevice" = getmTaskTCPDevice
deviceTypes :: [MTaskResource]
deviceTypes = conses{|*|}
+connectDevice :: (MTaskDevice (Shared Channels) -> Task ()) MTaskDevice -> Task ()
+connectDevice pf d = let ch = channels d in appendTopLevelTask 'DM'.newMap True
+ (pf d ch -||- catchAll (getSynFun d.deviceData ch) errorHandle)
+ >>= \tid->withDevices d (\d->{d&deviceTask=Just tid,deviceError=Nothing}) @! ()
+ where
+ errorHandle e = withDevices d (\d->{d&deviceTask=Nothing,deviceError=Just e})
+
manageDevices :: (MTaskDevice (Shared Channels) -> Task ()) [MTaskDevice] -> Task ()
manageDevices processFun ds = anyTask [
addDevice deviceStore processFun <<@ Title "Add new device" @! ():
- [viewDevice d <<@ Title d.deviceName\\d<-ds]]
+ [viewDevice processFun d
+ <<@ Title d.deviceName\\d<-ds]]
<<@ ArrangeWithTabs @! ()
-viewDevice :: MTaskDevice -> Task ()
-viewDevice d = forever $ anyTask
+viewDevice :: (MTaskDevice (Shared Channels) -> Task ()) MTaskDevice -> Task ()
+viewDevice pf d = forever $ anyTask
[viewInformation "Device settings" [] d @! ()
,viewSharedInformation "Channels" [ViewAs dropEmpty] (channels d) @! ()
,forever $
>>* [OnAction (Action "Delete") $ ifValue (\t->t.ident <> -1) (deviceTaskDelete d)]
@! ()
] <<@ ArrangeHorizontal
- >>* [OnAction (Action "Delete Device") (always $ deleteDevice d)]
+ >>* [OnAction (Action "Delete Device") (always $ deleteDevice d):
+ if (isJust d.deviceTask) []
+ [OnAction (Action "Connect") (always $ connectDevice pf d)]]
where
dropEmpty (r,s,ss) = (filter ((=!=)MTEmpty) r,s,ss)
instance MTaskDuplex TCPSettings where
synFun :: TCPSettings (Shared Channels) -> Task ()
- synFun s channels = catchAll (
+ synFun s channels =
tcpconnect s.host s.port channels {ConnectionHandlers|
onConnect=onConnect,
whileConnected=whileConnected,
- onDisconnect=onDisconnect} @! ())
- (\v->traceValue v @! ())
+ onDisconnect=onDisconnect} @! ()
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)