Merge branch 'master' of gitlab.science:mlubbers/mTask
[mTask.git] / Tasks / mTaskTask.icl
index e828257..d586f4f 100644 (file)
@@ -3,6 +3,8 @@ implementation module Tasks.mTaskTask
 import mTask
 import iTasks
 import Devices.mTaskDevice
+import Data.List
+from Data.Func import $
 
 import iTasks._Framework.Serialization
 
@@ -14,23 +16,27 @@ makeTask name ident = get currentDateTime
 
 import StdDebug
 import StdMisc
-sendTaskToDevice :: String (Main (ByteCode a Stmt)) (MTaskDevice, MTaskInterval) -> Task [MTaskDevice]
+sendTaskToDevice :: String (Main (ByteCode a Stmt)) (MTaskDevice, MTaskInterval) -> Task MTaskTask
 sendTaskToDevice wta mTask (device, timeout)
-| not (trace_tn "compiling task") = undef
 # (msgs, newState=:{sdss}) = toMessages timeout mTask device.deviceState
-| not (trace_tn "Done compiling task") = undef
 # shares = [makeShare wta "" sdsi sdsval\\{sdsi,sdsval}<-sdss, (MTSds sdsi` _)<-msgs | sdsi == sdsi`]
 = updateShares device ((++) shares)
        >>| sendMessages msgs device
        >>| makeTask wta -1
-       >>= withDevices device o addTaskUpState newState
+       >>= \t->upd (addTaskUpState newState t) (deviceShare device) 
+       >>| wait "Waiting for task to be acked" (taskAcked t) (deviceShare device)
+       >>| treturn t
        where
                addTaskUpState :: BCState MTaskTask MTaskDevice -> MTaskDevice
                addTaskUpState st task device = { MTaskDevice | device &
                        deviceState=st, deviceTasks=[task:device.deviceTasks]}
+               taskAcked t d = maybe True (\t->t.ident <> -1) $ find (eq t) d.deviceTasks
+               eq t1 t2 = t1.dateAdded == t2.dateAdded &&
+                       t1.MTaskTask.name == t2.MTaskTask.name
 
-//liftmTask :: String (Main (ByteCode a Stmt)) (MTaskDevice, MTaskInterval) -> Task a
-//liftmTask wta mTask (device, timeout)
-//     = sendTaskToDevice wta mTask (device, timeout)
-//     >>| wait "waiting for task to return" $ sdsFocus
-//     >>| treturn 
+liftmTask :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task ()
+liftmTask wta mTask c=:(dev, _)= sendTaskToDevice wta mTask c
+       >>= \t->wait "Waiting for mTask to return" (taskRemoved t) (deviceShare dev)
+       >>| viewInformation "Done!" [] ()
+where
+       taskRemoved t d = isNothing $ find (\t1->t1.ident==t.ident) d.deviceTasks