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