removed whileUnchanged for devices
[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 = viewInformation "" [] []//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 Int BCValue -> MTaskShare
64 makeShare withTask identifier value = {MTaskShare
65 |withTask=[withTask]
66 ,identifier=identifier
67 ,value=value
68 }
69
70 import GenPrint, StdMisc, StdDebug, TTY
71
72 gPrint{|BCState|} x st = gPrint{|*|} "BCState..." st
73
74 derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime
75 derive gPrint Parity, BaudRate, ByteSize
76
77 cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
78 cleanSharesTask taskid d = updateShares d id //TODO
79 //# shares = d.deviceShares
80 //| not (trace_tn $ printToString taskid) = undef
81 //| not (trace_tn $ printToString d.deviceTasks) = undef
82 //| not (trace_tn $ printToString $ getNames taskid d) = undef
83 //= upd (map $ up $ getNames taskid d) sdsStore
84 // where
85 // getNames :: Int MTaskDevice -> [String]
86 // getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
87 //
88 // up :: [String] MTaskShare -> MTaskShare
89 // up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
90
91 instance == MTaskShare where
92 (==) a b = a.identifier == b.identifier
93
94 getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
95 getRealShare dev share = sdsFocus ()
96 $ mapReadWriteError (deviceLens dev share)
97 $ sdsFocus (Just (dev, share.identifier))
98 $ deviceStore
99
100 //getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
101 //getRealShare dev share = sdsLens
102 // ("realShare" +++ toString share.identifier)
103 // (const $ Just (dev, share.identifier))
104 // (SDSRead $ const $ \rs->case find ((==)dev) rs of
105 // Nothing = Error $ exception "Device doesn't exist anymore"
106 // Just {deviceShares} = case find ((==)share) deviceShares of
107 // Nothing = Error $ exception "Share doesn't exist anymore"
108 // Just share = Ok share.MTaskShare.value
109 // )
110 // (SDSWrite $ const $ \rs w->partition ((==)dev) devs of
111 // ([], _) = Error $ exception "Device doesn't exist anymore"
112 // ([_,_:_], _) = Error $ exception "Multiple matching devices"
113 // ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
114 // ([], _) = Error $ exception "Share doesn't exist anymore"
115 // ([_,_:_], _) = Error $ exception "Multiple matching shares"
116 // ([s], shares)
117 // # s = {MTaskShare | s & value=val}
118 // # d = {MTaskDevice | d & deviceShares=[s:shares]}
119 // = Ok $ Just [d:devs])
120 // (SDSNotify $ const $ \rs w
121
122 deviceLens dev share = (mread, mwrite)
123 where
124 mread :: [MTaskDevice] -> MaybeError TaskException BCValue
125 mread devs = case find ((==)dev) devs of
126 Nothing = Error $ exception "Device doesn't exist anymore"
127 Just {deviceShares} = case find ((==)share) deviceShares of
128 Nothing = Error $ exception "Share doesn't exist anymore"
129 Just share = Ok share.MTaskShare.value
130
131 mwrite :: BCValue [MTaskDevice] -> MaybeError TaskException (Maybe [MTaskDevice])
132 mwrite val devs = case partition ((==)dev) devs of
133 ([], _) = Error $ exception "Device doesn't exist anymore"
134 ([_,_:_], _) = Error $ exception "Multiple matching devices"
135 ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
136 ([], _) = Error $ exception "Share doesn't exist anymore"
137 ([_,_:_], _) = Error $ exception "Multiple matching shares"
138 ([s], shares)
139 # s = {MTaskShare | s & value=val}
140 # d = {MTaskDevice | d & deviceShares=[s:shares]}
141 = Ok $ Just [d:devs]
142
143 updateShareFromPublish :: MTaskDevice Int BCValue -> Task BCValue
144 updateShareFromPublish dev ident val = set val
145 $ mapReadWriteError (deviceLens dev dummy)
146 $ deviceStoreNP
147 where
148 dummy = {MTaskShare|value=BCValue 0,identifier=ident,withTask=[]}