import iTasks
import mTask
import Data.List
+import Data.Error
+import Data.Tuple
+from Control.Monad import `b`
from Data.Func import $
from StdFunc import flip
-derive class iTask MTaskShareType, MTaskShare
-
-manageShares :: [MTaskShare] -> Task ()
-manageShares shares = withShared Nothing $ \cs->forever $
- (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical)
- @! ()
-
-updateShares :: [MTaskShare] -> Task BCValue
-updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs
-
-updateS :: MTaskShare -> Task BCValue
-updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $
- viewSharedInformation "Current value" [] (getSDSShare sh)
- ||- (
- updateSharedInformation "New value" [] (getSDSShare sh)
- >>= \nv->allTasks (map getDevice sh.withDevice)
- >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs)
- >>| treturn nv
- )
- <<@ ArrangeHorizontal
-
-viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue]
-viewSharesGrid _ [] = viewInformation "No shares yet" [] []
-viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface)
- >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id]
- [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh]
- >>* [OnValue (withValue $ \s->Just (set (Just s) cs))]
- @! fromJust mshs
-
-viewShare :: MTaskShare -> Task BCValue
-viewShare m = viewSharedInformation "" [] (getSDSShare m)
- <<@ Title ("SDS: " +++ toString m.identifier)
-
-getSDSShare :: MTaskShare -> Shared BCValue
-getSDSShare s=:{realShare=(MTaskWithShare id),value} = memoryShare id value
-
-makeShare :: String String Int BCValue -> Task MTaskShare
-makeShare withTask withDevice identifier value = treturn
- {MTaskShare
+derive class iTask MTaskShare
+
+manageShares :: Task [MTaskDevice]
+manageShares = whileUnchanged deviceStoreNP
+ $ \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) (sdsFocus (Just (dev, -1)) 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)
+// @! ()
+
+//updateShares :: [MTaskShare] -> Task BCValue
+//updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs
+
+//updateS :: MTaskShare -> Task BCValue
+//updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $
+// viewSharedInformation "Current value" [] (getSDSShare sh)
+// ||- (
+// updateSharedInformation "New value" [] (getSDSShare sh)
+// >>= \nv->allTasks (map (withDevice treturn) sh.withDevice)
+// >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs)
+// >>| treturn nv
+// )
+// <<@ ArrangeHorizontal
+
+//viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue]
+//viewSharesGrid _ [] = viewInformation "No shares yet" [] []
+//viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface)
+// >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id]
+// [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh]
+// >>* [OnValue (withValue $ \s->Just (set (Just s) cs))]
+// @! fromJust mshs
+
+//viewShare :: MTaskShare -> Task BCValue
+//viewShare m = viewSharedInformation "" [] (getSDSShare m)
+// <<@ Title ("SDS: " +++ toString m.identifier)
+
+makeShare :: String String Int BCValue -> MTaskShare
+makeShare withTask human identifier value = {MTaskShare
|withTask=[withTask]
- ,withDevice=[withDevice]
,identifier=identifier
,value=value
- ,realShare=MTaskWithShare $ "mTaskSDS-" +++ toString identifier
- } >>= \sh->set value (getSDSShare sh) >>| treturn sh
-
-updateShare :: Int BCValue -> Task ()
-updateShare ident val = get sdsStore
- >>= \sh->(case find (\s->s.identifier==ident) sh of
- Nothing = abort "Help, no share found with this ident"
- Just mts = set val (getSDSShare mts))
- >>| traceValue "Updated" @! ()
+ ,humanName=human
+ }
+
+import GenPrint, StdMisc, StdDebug, TTY
+
+gPrint{|BCState|} x st = gPrint{|*|} "BCState..." st
+
+derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime
+derive gPrint Parity, BaudRate, ByteSize
+
+cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
+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
+
+shareShare :: MTaskDevice MTaskShare -> Shared BCValue
+shareShare 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 = mb2error (exception "Device lost") (find ((==)dev) devs)
+ `b` \d->mb2error (exception "Share lost") (find ((==)share) d.deviceShares)
+ `b` \s->Ok s.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) = Ok $ Just [{MTaskDevice | d &
+ deviceShares=[{MTaskShare | s & value=val}:shares]}:devs]
+
+updateShareFromPublish :: MTaskDevice Int BCValue -> Task BCValue
+updateShareFromPublish dev ident val = set val
+ $ mapReadWriteError (deviceLens dev dummy)
+ $ deviceStoreNP
+where
+ dummy = {MTaskShare|humanName="",value=BCValue 0,identifier=ident,withTask=[]}