edd84d06d009c3e44bcc480ba507152ab2a795ca
[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 deviceStore
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) 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 if` i t e = if i t e
71
72 updateShareFromPublish :: MTaskDevice Int BCValue -> Task [MTaskShare]
73 updateShareFromPublish dev ident val = updateShares dev $ map $ up ident val
74 where
75 up :: Int BCValue MTaskShare -> MTaskShare
76 up i v s = if (s.identifier == i) {MTaskShare | s & value=val} s
77
78 import GenPrint, StdMisc, StdDebug, TTY
79 derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime
80 derive gPrint Parity, BaudRate, ByteSize
81
82 cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare]
83 cleanSharesTask taskid d = updateShares d id //TODO
84 //# shares = d.deviceShares
85 //| not (trace_tn $ printToString taskid) = undef
86 //| not (trace_tn $ printToString d.deviceTasks) = undef
87 //| not (trace_tn $ printToString $ getNames taskid d) = undef
88 //= upd (map $ up $ getNames taskid d) sdsStore
89 // where
90 // getNames :: Int MTaskDevice -> [String]
91 // getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i]
92 //
93 // up :: [String] MTaskShare -> MTaskShare
94 // up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
95
96 instance == MTaskShare where
97 (==) a b = a.identifier == b.identifier
98
99 getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
100 getRealShare dev share=:{identifier} = sdsFocus (Just identifier) $ SDSSource {SDSSource
101 | name = "mTaskShareMap-" +++ toString identifier, read=rr, write=ww}
102 where
103 rr name iworld = case read deviceStore iworld of
104 (Error e, iworld) = (Error e, iworld)
105 (Ok devices, iworld) = case find ((==)dev) devices of
106 Nothing = (Error $ exception "Device doesn't exist anymore", iworld)
107 Just {deviceShares} = case find ((==)share) deviceShares of
108 Nothing = (Error $ exception "Share doesn't exist", iworld)
109 Just share = (Ok share.MTaskShare.value, iworld)
110
111 // Also send messages
112 ww name value iworld
113 | not (trace_tn ("Update to: " +++ printToString value)) = undef
114 = case modify (tuple () o modifyValue value) deviceStore iworld of
115 (Error e, iworld) = (Error e, iworld)
116 (Ok _, iworld) = case sendMessagesIW [MTUpd identifier value] dev iworld of
117 (Error e, iworld) = (Error e, iworld)
118 (Ok _, iworld) = (Ok $ maybe True ((==) identifier), iworld)
119
120 modifyValue :: BCValue [MTaskDevice] -> [MTaskDevice]
121 modifyValue v ds = filterMap ((==)dev) deviceUpdate ds
122 where
123 deviceUpdate d = {MTaskDevice | d
124 & deviceShares=filterMap ((==)share) shareUpd d.deviceShares}
125 shareUpd s = {MTaskShare | s & value=v}
126
127 filterMap :: (a -> Bool) (a -> a) [a] -> [a]
128 filterMap f t xs = [if (f x) (t x) x\\x<-xs]