8c17f09423768eebe75f71d318dd96818a4f1379
[mTask.git] / miTask.icl
1 module miTask
2
3 import StdDebug, StdMisc
4 from StdFunc import flip
5
6 import iTasks
7 import mTask
8 import Devices.mTaskDevice
9 import Shares.mTaskShare
10 import Tasks.Examples
11 import Utils.SDS
12
13 from Text import class Text(startsWith,concat,split,join), instance Text String
14
15 import qualified Data.Map as DM
16
17 from Data.Func import $
18 import Data.Tuple
19 import Data.List
20 import System.Directory
21
22 import iTasks._Framework.Store
23 import iTasks._Framework.Serialization
24
25 import TTY, iTasksTTY
26
27 Start world = startEngine [
28 publish "/manage" $ const $ mTaskManager
29 >>* [OnAction (Action "Shutdown") (always $ shutDown 0)],
30 publish "/" $ const demo
31 ] world
32
33 demo :: Task ()
34 demo = viewSharedInformation "Devices" [] deviceStoreNP
35 >>* [OnValue $ ifValue pred (cont o hd)]
36 where
37 pred [] = False
38 pred [x:_] = not $ isEmpty x.deviceShares
39
40 cont :: MTaskDevice -> Task ()
41 cont dev
42 # rs = getRealShare dev (hd dev.deviceShares)
43 = get rs >>= \oldvalue->
44 forever (updateSharedInformation "Blinkyblink" [] (getRealShare dev (hd dev.deviceShares))
45 >>* [OnAction ActionContinue (const $ Just $ treturn ())])
46 -|| (wait "bigger than 10" (\x->x == oldvalue) rs
47 >>= viewInformation "Bigger:)" [])
48
49 toInt :: BCValue -> Int
50 toInt (BCValue e) = dynToInt (dynamic e)
51
52 dynToInt :: Dynamic -> Int
53 dynToInt (a :: Int) = a
54 dynToInt x = abort "Not an int"
55
56 mTaskManager :: Task ()
57 mTaskManager = (>>|) startupDevices $
58 forever viewmTasks ||-
59 ((manageShares ||- forever (manageDevices process)) <<@ ArrangeSplit Vertical True)
60 <<@ ArrangeWithSideBar 0 LeftSide 260 True
61 where
62 viewmTasks :: Task [MTaskDevice]
63 viewmTasks = enterChoiceWithShared "Available mTasks" [ChooseFromList id] mTaskTaskStore
64 >>= \task->get deviceStoreNP
65 >>* [OnValue $ (ifValue isEmpty) $ \_->
66 viewInformation "No devices yet" [] [] >>= treturn
67 ,OnValue $ (ifValue $ not o isEmpty) $ \d->
68 fromJust ('DM'.get task allmTasks)
69 >>= \bc->(enterChoice "Choose Device" [ChooseFromDropdown \t->t.deviceName] d
70 -&&- enterInformation "Timeout" []
71 ) >>* [OnAction (Action "Send") (withValue $ Just o sendTaskToDevice task bc)]
72 ]
73
74 process :: MTaskDevice (Shared Channels) -> Task ()
75 process device ch = forever
76 $ traceValue "Waiting for channel change"
77 >>| wait "process" (not o isEmpty o fst3) ch
78 >>= \(r,s,ss)->upd (appFst3 (const [])) ch
79 >>| proc r
80 where
81 proc :: [MTaskMSGRecv] -> Task ()
82 proc [] = treturn ()
83 proc [MTEmpty:ms] = proc ms
84 proc [m:ms] = traceValue (toString m) >>| (case m of
85 // MTSDSAck i = traceValue (toString m) @! ()
86 // MTSDSDelAck i = traceValue (toString m) @! ()
87 MTPub i val = updateShareFromPublish device i val @! ()
88 MTTaskAck i mem = deviceTaskAcked device i mem @! ()
89 MTTaskDelAck i = deviceTaskDeleteAcked device i @! ()
90 MTDevSpec s = deviceAddSpec device s @! ()
91 _ = treturn ()
92 ) >>| proc ms