make shares of devices
[mTask.git] / Shares / mTaskShare.icl
index 4cca134..9960f4e 100644 (file)
@@ -11,9 +11,24 @@ from StdFunc import flip
 
 derive class iTask MTaskShare
 
-manageShares :: [MTaskShare] -> Task MTaskShare
-manageShares x = enterChoice "" [ChooseFromGrid id] x
-       >>| manageShares x
+manageShares :: Task [MTaskDevice]
+manageShares = whileUnchanged deviceStore
+       $ \devs->case devs of
+               [] = viewInformation "No devices yet" [] []
+               _ = allTasks (map manageSharesOnDevice devs)
+
+manageSharesOnDevice :: MTaskDevice -> Task MTaskDevice
+manageSharesOnDevice dev = (case dev.deviceShares of
+               [] = viewInformation dev.deviceName [] "No shares yet"
+               shs = enterChoice dev.deviceName [ChooseFromGrid id] shs @ const ""
+       ) >>| treturn dev
+
+updateShares :: MTaskDevice ([MTaskShare] -> [MTaskShare]) -> Task [MTaskShare]
+updateShares dev tfun = upd (map upFun) deviceStore 
+               @ (\d->d.deviceShares) o fromJust o find ((==)dev)
+       where
+               upFun d = if (dev == d) ({d&deviceShares=tfun d.deviceShares}) d
+
 //manageShares shares = withShared Nothing $ \cs->forever $
 //     (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical) 
 //     @! ()
@@ -44,17 +59,15 @@ manageShares x = enterChoice "" [ChooseFromGrid id] x
 //viewShare m = viewSharedInformation "" [] (getSDSShare m)
 //     <<@ Title ("SDS: " +++ toString m.identifier)
 
-makeShare :: String String Int BCValue -> MTaskShare
-makeShare withTask withDevice identifier value = {MTaskShare
+makeShare :: String Int BCValue -> MTaskShare
+makeShare withTask identifier value = {MTaskShare
                |withTask=[withTask]
-               ,withDevice=[withDevice]
                ,identifier=identifier
                ,value=value
-//             ,realShare=MTaskWithShare $ "mTaskSDS-" +++ toString identifier
                }
 
-updateShare :: Int BCValue -> Task [MTaskShare]
-updateShare ident val = upd (map $ up ident val) sdsStore
+updateShare :: MTaskDevice Int BCValue -> Task [MTaskShare]
+updateShare dev ident val = updateShares dev $ map $ up ident val
        where
                up :: Int BCValue MTaskShare -> MTaskShare
                up i v s = if (s.identifier == i) {MTaskShare | s & value=val} s
@@ -64,20 +77,32 @@ derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId,
 derive gPrint Parity, BaudRate, ByteSize
 
 cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
-cleanSharesTask taskid d
-| not (trace_tn $ printToString taskid) = undef
-| not (trace_tn $ printToString d.deviceTasks) = undef
-| not (trace_tn $ printToString $ getNames taskid d) = undef
-= upd (map $ up $ getNames taskid d) sdsStore
-       where
-               getNames :: Int MTaskDevice -> [String]
-               getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
-               
-               up :: [String] MTaskShare -> MTaskShare
-               up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
+cleanSharesTask taskid d = updateShares d id //TODO
+//# shares = d.deviceShares
+//| not (trace_tn $ printToString taskid) = undef
+//| not (trace_tn $ printToString d.deviceTasks) = undef
+//| not (trace_tn $ printToString $ getNames taskid d) = undef
+//= upd (map $ up $ getNames taskid d) sdsStore
+//     where
+//             getNames :: Int MTaskDevice -> [String]
+//             getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
+//             
+//             up :: [String] MTaskShare -> MTaskShare
+//             up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
 
-cleanSharesDevice :: String -> Task [MTaskShare]
-cleanSharesDevice did = upd (map (up did)) sdsStore
+getRealShare :: MTaskDevice BCShare -> Shared BCValue
+getRealShare dev {sdsi} = SDSSource {SDSSource
+       | name = "mTaskShareMap-" +++ toString sdsi, read=rr, write=ww}
        where
-               up :: String MTaskShare -> MTaskShare
-               up i s = {MTaskShare | s & withDevice = [wt\\wt<-s.withDevice|wt <> i]}
+               rr name iworld = case read deviceStore iworld of
+                       (Error e, iworld) = (Error e, iworld)
+                       (Ok devices, iworld) = case find ((==)dev) devices of
+                               Nothing = (Error $ exception "Device doesn't exist anymore", iworld)
+                               Just {deviceShares} = case find (\s->s.identifier == sdsi) deviceShares of
+                                       Nothing = (Error $ exception "Share doesn't exist", iworld)
+                                       Just share = (Ok share.MTaskShare.value, iworld)
+                       
+               // Also send messages
+               ww name value iworld = undef//case modify (modFun name value) sdsStore of
+//                     (Error e, iworld) = (Error e, iworld)
+//                     (Ok shares, iworld) = (Ok $ const True, iworld)