2133f0b76b8e5d592ef517f6fd36fa47431c25e6
[mTask.git] / Shares / mTaskShare.icl
1 implementation module Shares.mTaskShare
2
3 import dynamic_string
4 import Utils.SDS
5 import Utils.Devices
6 import iTasks
7 import mTask
8 import Data.List
9 import Data.Error
10 import Data.Tuple
11 from Control.Monad import `b`
12 from Data.Func import $
13 from StdFunc import flip
14
15 derive class iTask MTaskShare
16
17 manageShares :: Task [MTaskDevice]
18 manageShares = whileUnchanged deviceStoreNP
19 $ \devs->case devs of
20 [] = viewInformation "No devices yet" [] []
21 _ = allTasks (map manageSharesOnDevice devs)
22
23 manageSharesOnDevice :: MTaskDevice -> Task MTaskDevice
24 manageSharesOnDevice dev = (case dev.deviceShares of
25 [] = viewInformation dev.deviceName [] "No shares yet"
26 shs = enterChoice dev.deviceName [ChooseFromGrid id] shs @ const ""
27 ) >>| treturn dev
28
29 updateShares :: MTaskDevice ([MTaskShare] -> [MTaskShare]) -> Task [MTaskShare]
30 updateShares dev tfun = upd (map upFun) (sdsFocus (Just (dev, -1)) deviceStore)
31 @ (\d->d.deviceShares) o fromJust o find ((==)dev)
32 where
33 upFun d = if (dev == d) ({d&deviceShares=tfun d.deviceShares}) d
34
35 //manageShares shares = withShared Nothing $ \cs->forever $
36 // (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical)
37 // @! ()
38
39 //updateShares :: [MTaskShare] -> Task BCValue
40 //updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs
41
42 //updateS :: MTaskShare -> Task BCValue
43 //updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $
44 // viewSharedInformation "Current value" [] (getSDSShare sh)
45 // ||- (
46 // updateSharedInformation "New value" [] (getSDSShare sh)
47 // >>= \nv->allTasks (map (withDevice treturn) sh.withDevice)
48 // >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs)
49 // >>| treturn nv
50 // )
51 // <<@ ArrangeHorizontal
52
53 //viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue]
54 //viewSharesGrid _ [] = viewInformation "No shares yet" [] []
55 //viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface)
56 // >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id]
57 // [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh]
58 // >>* [OnValue (withValue $ \s->Just (set (Just s) cs))]
59 // @! fromJust mshs
60
61 //viewShare :: MTaskShare -> Task BCValue
62 //viewShare m = viewSharedInformation "" [] (getSDSShare m)
63 // <<@ Title ("SDS: " +++ toString m.identifier)
64
65 makeShare :: String String Int BCValue -> MTaskShare
66 makeShare withTask human identifier value = {MTaskShare
67 |withTask=[withTask]
68 ,identifier=identifier
69 ,value=value
70 ,humanName=human
71 }
72
73 import GenPrint, StdMisc, StdDebug, TTY
74
75 gPrint{|BCState|} x st = gPrint{|*|} "BCState..." st
76
77 derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime
78 derive gPrint Parity, BaudRate, ByteSize
79
80 cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
81 cleanSharesTask taskid d = updateShares d id //TODO
82 //# shares = d.deviceShares
83 //| not (trace_tn $ printToString taskid) = undef
84 //| not (trace_tn $ printToString d.deviceTasks) = undef
85 //| not (trace_tn $ printToString $ getNames taskid d) = undef
86 //= upd (map $ up $ getNames taskid d) sdsStore
87 // where
88 // getNames :: Int MTaskDevice -> [String]
89 // getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
90 //
91 // up :: [String] MTaskShare -> MTaskShare
92 // up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
93
94 instance == MTaskShare where
95 (==) a b = a.identifier == b.identifier
96
97 getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
98 getRealShare dev share = sdsFocus ()
99 $ mapReadWriteError (deviceLens dev share)
100 $ sdsFocus (Just (dev, share.identifier))
101 $ deviceStore
102
103 //getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
104 //getRealShare dev share = sdsLens
105 // ("realShare" +++ toString share.identifier)
106 // (const $ Just (dev, share.identifier))
107 // (SDSRead $ const $ \rs->case find ((==)dev) rs of
108 // Nothing = Error $ exception "Device doesn't exist anymore"
109 // Just {deviceShares} = case find ((==)share) deviceShares of
110 // Nothing = Error $ exception "Share doesn't exist anymore"
111 // Just share = Ok share.MTaskShare.value
112 // )
113 // (SDSWrite $ const $ \rs w->partition ((==)dev) devs of
114 // ([], _) = Error $ exception "Device doesn't exist anymore"
115 // ([_,_:_], _) = Error $ exception "Multiple matching devices"
116 // ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
117 // ([], _) = Error $ exception "Share doesn't exist anymore"
118 // ([_,_:_], _) = Error $ exception "Multiple matching shares"
119 // ([s], shares)
120 // # s = {MTaskShare | s & value=val}
121 // # d = {MTaskDevice | d & deviceShares=[s:shares]}
122 // = Ok $ Just [d:devs])
123 // (SDSNotify $ const $ \rs w
124
125 deviceLens dev share = (mread, mwrite)
126 where
127 mread :: [MTaskDevice] -> MaybeError TaskException BCValue
128 mread devs = mb2error (exception "Device lost") (find ((==)dev) devs)
129 `b` \d->mb2error (exception "Share lost") (find ((==)share) d.deviceShares)
130 `b` \s->Ok s.MTaskShare.value
131
132 mwrite :: BCValue [MTaskDevice] -> MaybeError TaskException (Maybe [MTaskDevice])
133 mwrite val devs = case partition ((==)dev) devs of
134 ([], _) = Error $ exception "Device doesn't exist anymore"
135 ([_,_:_], _) = Error $ exception "Multiple matching devices"
136 ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
137 ([], _) = Error $ exception "Share doesn't exist anymore"
138 ([_,_:_], _) = Error $ exception "Multiple matching shares"
139 ([s], shares)
140 # s = {MTaskShare | s & value=val}
141 # d = {MTaskDevice | d & deviceShares=[s:shares]}
142 = Ok $ Just [d:devs]
143
144 updateShareFromPublish :: MTaskDevice Int BCValue -> Task BCValue
145 updateShareFromPublish dev ident val = set val
146 $ mapReadWriteError (deviceLens dev dummy)
147 $ deviceStoreNP
148 where
149 dummy = {MTaskShare|humanName="",value=BCValue 0,identifier=ident,withTask=[]}