X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=miTask.icl;h=2a80f5cc8492b51a36f716add3ab037b69f4bf7f;hb=71726cdfddd9dfa11395f10ef3b5af28613fd5a0;hp=e5483ccad3eccec4adcf49f236dab682e845a0c7;hpb=ddfd196fa4999b117701121a2f4cb1fe378f902d;p=mTask.git diff --git a/miTask.icl b/miTask.icl index e5483cc..2a80f5c 100644 --- a/miTask.icl +++ b/miTask.icl @@ -1,142 +1,92 @@ module miTask import StdDebug, StdMisc - -from Text import class Text(concat,join,split), instance Text String +from StdFunc import flip import iTasks import mTask +import Devices.mTaskDevice +import Shares.mTaskShare +import Tasks.Examples +import Utils.SDS -derive class iTask MTaskMSGRecv, MTaskMSGSend - -Start :: *World -> *World -Start world = startEngine ( - enterInformation "Port Number?" [] - >>= \port->withShared ([], False, [], False) (mTaskTask port) - ) world -//Start world = startEngine mTaskTask world - -mTaskTask :: Int (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () -mTaskTask port ch = - syncNetworkChannel "localhost" port "\n" decode encode` ch ||- - sendMsg msgs ch ||- - ( - ( - consumeNetworkStream (processSDSs sdsShares messageShare) ch ||- - viewSharedInformation "channels" [ViewWith lens] ch ||- - viewSh sdsShares ch - ) >>* [OnAction ActionFinish (always shutDown)] - ) - where - encode` m - | not (trace_tn (toString (toJSON m))) = undef - = encode m - - messageShare :: Shared [String] - messageShare = sharedStore "mTaskMessagesRecv" [] - - processSDSs :: [(Int, Shared Int)] (Shared [String]) [MTaskMSGRecv] -> Task () - processSDSs _ _ [] = return () - processSDSs s y [x:xs] = updateSDSs s y x >>= \_->processSDSs s y xs - - updateSDSs :: [(Int, Shared Int)] (Shared [String]) MTaskMSGRecv -> Task () - updateSDSs _ m (MTMessage s) = upd (\l->[s:l]) m @! () - updateSDSs _ _ MTEmpty = return () - updateSDSs [(id, sh):xs] m n=:(MTPub i d) - | id == i = set ((toInt d.[0])*265 + toInt d.[1]) sh @! () - = updateSDSs xs m n +from Text import class Text(startsWith,concat,split,join), instance Text String - lens :: ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool) -> ([String], [String]) - lens (r,_,s,_) = (f r, map toString s) - where - f [] = [] - f [MTEmpty:xs] = f xs - f [x:xs] = [toString x:f xs] - - viewSh :: [(Int, Shared Int)] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () - viewSh [] ch = return () - viewSh [(i, sh):xs] ch - # sharename = "SDS-" +++ toString i - = ( - viewSharedInformation ("SDS-" +++ toString i) [] sh ||- - forever ( - enterInformation sharename [] - >>* [OnAction ActionOk - (ifValue (\j->j>=1 && j <= 3) - (\c->set c sh - >>= \_->sendMsg (toSDSUpdate i c) ch - @! () - ) - )] - ) - ) ||- viewSh xs ch - - sdsShares = makeShares st +import qualified Data.Map as DM - (msgs, st) = toMessages 500 (toRealByteCode (unMain bc)) +from Data.Func import $ +import Data.Tuple +import Data.List +import System.Directory - bc :: Main (ByteCode () Stmt) - bc = sds \x=1 In sds \pinnetje=1 In {main = - x =. x +. lit 1 :. - pub x :. - IF (pinnetje ==. lit 1) ( - analogWrite A0 (lit 1) :. - analogWrite A1 (lit 0) :. - analogWrite A2 (lit 0) - ) ( - IF (pinnetje ==. lit 2) ( - analogWrite A0 (lit 0) :. - analogWrite A1 (lit 1) :. - analogWrite A2 (lit 0) - ) ( - analogWrite A0 (lit 0):. - analogWrite A1 (lit 0):. - analogWrite A2 (lit 1) - ) - )} -// bc :: Main (ByteCode Int Stmt) -// bc = sds \x=1 In {main = -// If (x ==. lit 3) -// (x =. lit 1) -// (x =. x +. lit 1) :. pub x} +import iTasks._Framework.Store +import iTasks._Framework.Serialization -makeShares :: BCState -> [(Int, Shared Int)] -makeShares {sdss=[]} = [] -makeShares s=:{sdss=[(i,d):xs]} = - [(i, sharedStore ("mTaskSDS-" +++ toString i) 0):makeShares {s & sdss=xs}] +import TTY, iTasksTTY -sendMsg :: [MTaskMSGSend] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () -sendMsg m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! () +Start world = startEngine [ + publish "/manage" $ const $ mTaskManager + >>* [OnAction (Action "Shutdown") (always $ shutDown)], + publish "/" $ const demo + ] world -syncNetworkChannel :: String Int String (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n -syncNetworkChannel server port msgSeparator decodeFun encodeFun channel - = tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! () +demo = viewSharedInformation "Devices" [] deviceStoreNP + >>* [OnValue $ ifValue pred (cont o hd)] where - onConnect _ (received,receiveStopped,send,sendStopped) - = (Ok "",if (not (isEmpty send)) (Just (received,False,[],sendStopped)) Nothing, map encodeFun send,False) - whileConnected Nothing acc (received,receiveStopped,send,sendStopped) - = (Ok acc, Nothing, [], False) - whileConnected (Just newData) acc (received,receiveStopped,send,sendStopped) - # [acc:msgs] = reverse (split msgSeparator (concat [acc,newData])) - # write = if (not (isEmpty msgs && isEmpty send)) - (Just (received ++ map decodeFun (reverse msgs),receiveStopped,[],sendStopped)) - Nothing - = (Ok acc,write,map encodeFun send,False) - - onDisconnect l (received,receiveStopped,send,sendStopped) - = (Ok l,Just (received,True,send,sendStopped)) - -consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n -consumeNetworkStream processTask channel - = ((watch channel >>* [OnValue (ifValue ifProcess process)]) Task () + cont dev + # rs = getRealShare dev (hd dev.deviceShares) + = get rs >>= \oldvalue-> + forever (updateSharedInformation "Blinkyblink" [] (getRealShare dev (hd dev.deviceShares)) + >>* [OnAction ActionContinue (const $ Just $ treturn ())]) + -|| (wait "bigger than 10" (\x->x == oldvalue) rs + >>= viewInformation "Bigger:)" []) + + toInt :: BCValue -> Int + toInt (BCValue e) = dynToInt (dynamic e) + + dynToInt :: Dynamic -> Int + dynToInt (a :: Int) = a + dynToInt x = abort "Not an int" + +mTaskManager :: Task () +mTaskManager = (>>|) startupDevices $ + viewmTasks ||- + ((manageShares ||- whileUnchanged deviceStoreNP (manageDevices process)) + <<@ ArrangeSplit Vertical True) + <<@ ArrangeWithSideBar 0 LeftSide 260 True where - ifProcess (received,receiveStopped,_,_) - = receiveStopped || (not (isEmpty received)) + viewmTasks :: Task String + viewmTasks = enterChoiceWithShared "Available mTasks" [ChooseFromList id] mTaskTaskStore + >&^ \sh->whileUnchanged sh $ \mi->forever (case mi of + Nothing = viewInformation "No task selected" [] () + Just mTaskTask = get deviceStoreNP + >>= \devices->case devices of + [] = viewInformation "No devices yet" [] () + ds = fromJust ('DM'.get mTaskTask allmTasks) + >>= \bc->(enterChoice "Choose Device" [ChooseFromDropdown \t->t.deviceName] ds + -&&- enterInformation "Timeout" [] + ) >>* [OnAction (Action "Send") (withValue $ Just o sendTaskToDevice mTaskTask bc)] + @! () + ) - process (received,receiveStopped,_,_) - = upd empty channel - >>| if (isEmpty received) (return ()) (processTask received) - @! receiveStopped - - empty :: ([m],Bool,[n],Bool) -> ([m],Bool,[n],Bool) - empty (_,rs,s,ss) = ([],rs,s,ss) + process :: MTaskDevice (Shared Channels) -> Task () + process device ch = forever (watch ch >>* [OnValue ( + ifValue (not o isEmpty o fst3) + (\t->upd (appFst3 (const [])) ch >>| proc (fst3 t)))]) + where + proc :: [MTaskMSGRecv] -> Task () + proc [] = treturn () + proc [MTEmpty:ms] = proc ms + proc [m:ms] = traceValue (toString m) >>| (case m of +// MTSDSAck i = traceValue (toString m) @! () +// MTSDSDelAck i = traceValue (toString m) @! () + MTPub i val = updateShareFromPublish device i val @! () + MTTaskAck i mem = deviceTaskAcked device i mem @! () + MTTaskDelAck i = deviceTaskDeleteAcked device i @! () + MTDevSpec s = deviceAddSpec device s @! () + _ = treturn () + ) >>| proc ms