bf8f2a1c107d4eb2db0b1dc1e0d612b20077e393
[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 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=:{identifier} = sdsFocus (Just identifier) $ SDSSource {SDSSource
93 | name = "mTaskShareMap-" +++ toString identifier, read=rr, write=ww}
94 where
95 rr name iworld = case read deviceStore iworld of
96 (Error e, iworld) = (Error e, iworld)
97 (Ok devices, iworld) = case find ((==)dev) devices of
98 Nothing = (Error $ exception "Device doesn't exist anymore", iworld)
99 Just {deviceShares} = case find ((==)share) deviceShares of
100 Nothing = (Error $ exception "Share doesn't exist", iworld)
101 Just s = (Ok s.MTaskShare.value, iworld)
102
103 // Also send messages
104 ww name value iworld
105 | not (trace_tn ("Update to: " +++ printToString value)) = undef
106 = case modify (tuple () o modifyValue value) deviceStore iworld of
107 (Error e, iworld) = (Error e, iworld)
108 (Ok _, iworld) = case sendMessagesIW [MTUpd identifier value] dev iworld of
109 (Error e, iworld) = (Error e, iworld)
110 (Ok _, iworld) = (Ok $ maybe True ((==) identifier), iworld)
111
112 modifyValue :: BCValue [MTaskDevice] -> [MTaskDevice]
113 modifyValue v ds = filterMap ((==)dev) (updateShare identifier v) ds
114
115 filterMap :: (a -> Bool) (a -> a) [a] -> [a]
116 filterMap f t xs = [if (f x) (t x) x\\x<-xs]
117
118 getDeviceStoreForShare :: Int -> Shared [MTaskDevice]
119 getDeviceStoreForShare ident = sdsFocus (Just ident) $ sdsFocus () deviceStore
120
121 updateShareFromPublish :: MTaskDevice Int BCValue -> Task [MTaskDevice]
122 updateShareFromPublish dev ident val
123 = upd (filterMap ((==)dev) (updateShare ident val)) $ getDeviceStoreForShare ident
124
125 updateShare :: Int BCValue MTaskDevice -> MTaskDevice
126 updateShare ident val dev = {MTaskDevice | dev & deviceShares=filterMap
127 (\s->s.identifier==ident)
128 (\s->{MTaskShare | s & value=val})
129 dev.deviceShares}