81ebb8b69dcd281767682b6b03b958cec511e6a7
[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 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 derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime
72 derive gPrint Parity, BaudRate, ByteSize
73
74 cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
75 cleanSharesTask taskid d = updateShares d id //TODO
76 //# shares = d.deviceShares
77 //| not (trace_tn $ printToString taskid) = undef
78 //| not (trace_tn $ printToString d.deviceTasks) = undef
79 //| not (trace_tn $ printToString $ getNames taskid d) = undef
80 //= upd (map $ up $ getNames taskid d) sdsStore
81 // where
82 // getNames :: Int MTaskDevice -> [String]
83 // getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
84 //
85 // up :: [String] MTaskShare -> MTaskShare
86 // up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
87
88 instance == MTaskShare where
89 (==) a b = a.identifier == b.identifier
90
91 getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
92 getRealShare dev share = sdsFocus ()
93 $ mapReadWriteError (deviceLens dev share)
94 $ sdsFocus (Just (dev, share.identifier))
95 $ deviceStore
96
97 deviceLens dev share = (mread, mwrite)
98 where
99 mread :: [MTaskDevice] -> MaybeError TaskException BCValue
100 mread devs = case find ((==)dev) devs of
101 Nothing = Error $ exception "Device doesn't exist anymore"
102 Just {deviceShares} = case find ((==)share) deviceShares of
103 Nothing = Error $ exception "Share doesn't exist anymore"
104 Just share = Ok share.MTaskShare.value
105
106 mwrite :: BCValue [MTaskDevice] -> MaybeError TaskException (Maybe [MTaskDevice])
107 mwrite val devs = case partition ((==)dev) devs of
108 ([], _) = Error $ exception "Device doesn't exist anymore"
109 ([_,_:_], _) = Error $ exception "Multiple matching devices"
110 ([d=:{deviceShares}], devs) = case partition ((==)share) deviceShares of
111 ([], _) = Error $ exception "Share doesn't exist anymore"
112 ([_,_:_], _) = Error $ exception "Multiple matching shares"
113 ([s], shares)
114 # s = {MTaskShare | s & value=val}
115 # d = {MTaskDevice | d & deviceShares=[s:shares]}
116 = Ok $ Just [d:devs]
117
118 updateShareFromPublish :: MTaskDevice Int BCValue -> Task BCValue
119 updateShareFromPublish dev ident val = set val
120 $ mapReadWriteError (deviceLens dev dummy)
121 $ deviceStoreNP
122 where
123 dummy = {MTaskShare|value=BCValue 0,identifier=ident,withTask=[]}