-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)]}
-
-cleanSharesDevice :: String -> Task [MTaskShare]
-cleanSharesDevice did = upd (map (up did)) sdsStore
- where
- up :: String MTaskShare -> MTaskShare
- up i s = {MTaskShare | s & withDevice = [wt\\wt<-s.withDevice|wt <> i]}
+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)]}
+
+instance == MTaskShare where
+ (==) a b = a.identifier == b.identifier
+
+getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
+getRealShare dev share = sdsFocus ()
+ $ mapReadWriteError (deviceLens dev share)
+ $ sdsFocus (Just (dev, share.identifier))
+ $ deviceStore
+
+deviceLens dev share = (mread, mwrite)
+where
+ mread :: [MTaskDevice] -> MaybeError TaskException BCValue
+ mread devs = case find ((==)dev) devs of
+ Nothing = Error $ exception "Device doesn't exist anymore"
+ Just {deviceShares} = case find ((==)share) deviceShares of
+ Nothing = Error $ exception "Share doesn't exist anymore"
+ Just share = Ok share.MTaskShare.value
+
+ mwrite :: BCValue [MTaskDevice] -> MaybeError TaskException (Maybe [MTaskDevice])
+ mwrite val devs = case partition ((==)dev) devs of
+ ([], _) = Error $ exception "Device doesn't exist anymore"
+ ([_,_:_], _) = Error $ exception "Multiple matching devices"
+ ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
+ ([], _) = Error $ exception "Share doesn't exist anymore"
+ ([_,_:_], _) = Error $ exception "Multiple matching shares"
+ ([s], shares)
+ # s = {MTaskShare | s & value=val}
+ # d = {MTaskDevice | d & deviceShares=[s:shares]}
+ = Ok $ Just [d:devs]
+
+updateShareFromPublish :: MTaskDevice Int BCValue -> Task BCValue
+updateShareFromPublish dev ident val = set val
+ $ mapReadWriteError (deviceLens dev dummy)
+ $ deviceStoreNP
+where
+ dummy = {MTaskShare|value=BCValue 0,identifier=ident,withTask=[]}