clean up share code and only show published tasks
[mTask.git] / Devices / mTaskDevice.icl
index 972aa7b..6a8c369 100644 (file)
@@ -1,5 +1,6 @@
 implementation module Devices.mTaskDevice
 
+from StdFunc import flip
 import Generics.gCons
 import mTaskInterpret
 import iTasks
@@ -15,10 +16,13 @@ import iTasks._Framework.Store
 
 from Data.Func import $
 
-derive class iTask MTaskDevice, MTaskResource, MTaskMSGRecv, MTaskMSGSend
+derive class iTask MTaskDevice, MTaskResource, MTaskMSGRecv, MTaskMSGSend, BCShare
 derive conses MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
 derive consName MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
 
+instance == MTaskDevice where
+       (==) a b = a.deviceChannels == b.deviceChannels
+
 channels :: MTaskDevice -> Shared Channels
 channels d = memoryShare d.deviceChannels ([], [], False)
 
@@ -60,7 +64,7 @@ manageDevices processFun ds = anyTask [
        <<@ ArrangeWithTabs @! ()
 
 viewDevice :: MTaskDevice -> Task ()
-viewDevice d = anyTask 
+viewDevice d = forever $ anyTask 
                [viewInformation "Device settings" [] d @! ()
                ,viewSharedInformation "Channels" [ViewAs dropEmpty] (channels d) @! ()
                ,forever $ 
@@ -68,39 +72,45 @@ viewDevice d = anyTask
                        >>* [OnAction (Action "Delete") $ ifValue (\t->t.ident <> -1) (deviceTaskDelete d)]
                        @! ()
                ] <<@ ArrangeHorizontal
+               >>* [OnAction (Action "Delete Device") (always $ deleteDevice d)]
        where
                dropEmpty (r,s,ss) = (filter ((=!=)MTEmpty) r,s,ss)
 
-sendToDevice :: (Map String (Main (ByteCode () Stmt))) String (MTaskDevice, Int) -> Task ()
-sendToDevice tmap mTask (device, timeout) =
-               get bcStateStore @ createBytecode
-       >>= \(msgs, st1)->set st1 bcStateStore @ toSDSRecords
-       >>= \sdss->upd ((++)sdss) sdsStore//MTaskShareaddToSDSShare
+deleteDevice :: MTaskDevice -> Task ()
+deleteDevice d = upd (\(r,s,ss)->(r,s,True)) (channels d)
+       >>| maybe (treturn ()) (flip removeTask topLevelTasks) d.deviceTask
+       >>| upd (filter ((==)d)) deviceStore
+       @! ()
+
+sendToDevice :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task ()
+sendToDevice wta mTask (device, timeout) =
+               get bcStateStore @ toMessages timeout o toRealByteCode (unMain mTask)
+       >>= \(msgs, st1)->set st1 bcStateStore @ toSDSRecords 
+       >>= \sdss->set sdss sdsStore//MTaskShareaddToSDSShare
        >>| makeShares sdss
        >>| sendMessage device msgs
-       >>| makeTask mTask -1
-       >>= \task->withDevices device (addTask timeout task)
+       >>| makeTask wta -1
+       >>= withDevices device o addTask
        @! ()
        where
-               createBytecode st = toMessages timeout $ toRealByteCode (unMain $ fromJust ('DM'.get mTask tmap)) st
                sharename i = device.deviceChannels +++ "-" +++ toString i
                toSDSRecords st = [{MTaskShare |
-                       initValue=toInt d1*265 + toInt d2,
-                       withTask=mTask,
-                       identifier=i,
-                       realShare="mTaskSDS-" +++ toString i}
-                               \\(i,[d1,d2])<-st.sdss]
+                       initValue=toInt (sdsval!!0)*265 + toInt (sdsval!!1),
+                       withTask=wta,
+                       identifier=sdsi,
+                       //We skip the only/local shares
+                       realShare="mTaskSDS-" +++ toString sdsi}
+                               \\{sdsi,sdspub,sdsval}<-st.sdss | sdspub]
                makeShares = foldr (\sh t->set sh.initValue (getSDSStore sh) >>| t) (treturn ())
 
-               addTask :: Int MTaskTask MTaskDevice -> MTaskDevice
-               addTask timeout task device = {device & deviceTasks=[task:device.deviceTasks]}
+               addTask :: MTaskTask MTaskDevice -> MTaskDevice
+               addTask task device = {device & deviceTasks=[task:device.deviceTasks]}
 
 sendMessage :: MTaskDevice [MTaskMSGSend] -> Task ()
 sendMessage dev msgs = upd (\(r,s,ss)->(r,msgs++s,ss)) (channels dev) @! ()
 
 withDevices :: MTaskDevice (MTaskDevice -> MTaskDevice) -> Task ()
-withDevices a trans = upd (map withDevice) deviceStore @! ()
-       where withDevice b = if (a.deviceChannels == b.deviceChannels) (trans b) b
+withDevices a trans = upd (map \b->if (b == a) (trans b) b) deviceStore @! ()
 
 deviceTaskAcked :: MTaskDevice Int -> Task ()
 deviceTaskAcked dev i