From 4bb252b0423c73d134a541db44b8b08eab507a39 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 17 May 2017 11:29:34 +0200 Subject: [PATCH] make everything more robust --- Devices/mTaskDevice.icl | 6 +-- Shares/mTaskShare.dcl | 17 +++--- Shares/mTaskShare.icl | 116 +++++++++++++++++++++++----------------- Tasks/mTaskTask.icl | 2 +- miTask.icl | 4 +- todo.txt | 1 - 6 files changed, 85 insertions(+), 61 deletions(-) diff --git a/Devices/mTaskDevice.icl b/Devices/mTaskDevice.icl index c8bee61..95c549e 100644 --- a/Devices/mTaskDevice.icl +++ b/Devices/mTaskDevice.icl @@ -102,10 +102,9 @@ deleteDevice :: MTaskDevice -> Task () deleteDevice d = upd (\(r,s,ss)->(r,s,True)) (channels d) >>| maybe (treturn ()) (flip removeTask topLevelTasks) d.deviceTask >>| upd (filter ((<>)d)) deviceStore -// >>| upd (removeShares d) sdsStore + >>| cleanSharesDevice d.deviceName @! () - sendMessages :: [MTaskMSGSend] -> (MTaskDevice -> Task Channels) sendMessages msgs = upd (\(r,s,ss)->(r,msgs++s,ss)) o channels @@ -127,7 +126,8 @@ deviceTaskDelete :: MTaskDevice MTaskTask -> Task () deviceTaskDelete dev task = sendMessages [MTTaskDel task.ident] dev @! () deviceTaskDeleteAcked :: MTaskDevice Int -> Task () -deviceTaskDeleteAcked d i = withDevices d $ deleteTask +deviceTaskDeleteAcked d i = cleanSharesTask i d + >>| withDevices d deleteTask where deleteTask d = {d & deviceTasks=[s\\s<-d.deviceTasks | i <> s.ident]} deviceAddSpec :: MTaskDevice MTaskDeviceSpec -> Task () diff --git a/Shares/mTaskShare.dcl b/Shares/mTaskShare.dcl index 0e73a82..8508cb3 100644 --- a/Shares/mTaskShare.dcl +++ b/Shares/mTaskShare.dcl @@ -5,19 +5,24 @@ import iTasks._Framework.Serialization import mTask import Devices.mTaskDevice -derive class iTask MTaskShareType, MTaskShare +derive class iTask MTaskShare -:: MTaskShareType = MTaskWithShare String | MTaskLens String :: MTaskShare = {withTask :: [String] ,withDevice :: [String] ,identifier :: Int - ,realShare :: MTaskShareType ,value :: BCValue } +//Constructor +makeShare :: String String Int BCValue -> MTaskShare -manageShares :: [MTaskShare] -> Task () +//General viewing task +manageShares :: [MTaskShare] -> Task MTaskShare -makeShare :: String String Int BCValue -> Task MTaskShare +//Clean out shares when a task has been removed +cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare] +//Clean out shares when a device has been removed +cleanSharesDevice :: String -> Task [MTaskShare] -updateShare :: Int BCValue -> Task () +updateShare :: Int BCValue -> Task [MTaskShare] +//updateShare :: Int BCValue -> Task () diff --git a/Shares/mTaskShare.icl b/Shares/mTaskShare.icl index ee6282b..4cca134 100644 --- a/Shares/mTaskShare.icl +++ b/Shares/mTaskShare.icl @@ -9,55 +9,75 @@ import Data.List from Data.Func import $ from StdFunc import flip -derive class iTask MTaskShareType, MTaskShare - -manageShares :: [MTaskShare] -> Task () -manageShares shares = withShared Nothing $ \cs->forever $ - (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical) - @! () - -updateShares :: [MTaskShare] -> Task BCValue -updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs - -updateS :: MTaskShare -> Task BCValue -updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $ - viewSharedInformation "Current value" [] (getSDSShare sh) - ||- ( - updateSharedInformation "New value" [] (getSDSShare sh) - >>= \nv->allTasks (map (withDevice treturn) sh.withDevice) - >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs) - >>| treturn nv - ) - <<@ ArrangeHorizontal - -viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue] -viewSharesGrid _ [] = viewInformation "No shares yet" [] [] -viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface) - >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id] - [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh] - >>* [OnValue (withValue $ \s->Just (set (Just s) cs))] - @! fromJust mshs - -viewShare :: MTaskShare -> Task BCValue -viewShare m = viewSharedInformation "" [] (getSDSShare m) - <<@ Title ("SDS: " +++ toString m.identifier) - -getSDSShare :: MTaskShare -> Shared BCValue -getSDSShare s=:{realShare=(MTaskWithShare id),value} = memoryShare id value - -makeShare :: String String Int BCValue -> Task MTaskShare -makeShare withTask withDevice identifier value = treturn - {MTaskShare +derive class iTask MTaskShare + +manageShares :: [MTaskShare] -> Task MTaskShare +manageShares x = enterChoice "" [ChooseFromGrid id] x + >>| manageShares x +//manageShares shares = withShared Nothing $ \cs->forever $ +// (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical) +// @! () + +//updateShares :: [MTaskShare] -> Task BCValue +//updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs + +//updateS :: MTaskShare -> Task BCValue +//updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $ +// viewSharedInformation "Current value" [] (getSDSShare sh) +// ||- ( +// updateSharedInformation "New value" [] (getSDSShare sh) +// >>= \nv->allTasks (map (withDevice treturn) sh.withDevice) +// >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs) +// >>| treturn nv +// ) +// <<@ ArrangeHorizontal + +//viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue] +//viewSharesGrid _ [] = viewInformation "No shares yet" [] [] +//viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface) +// >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id] +// [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh] +// >>* [OnValue (withValue $ \s->Just (set (Just s) cs))] +// @! fromJust mshs + +//viewShare :: MTaskShare -> Task BCValue +//viewShare m = viewSharedInformation "" [] (getSDSShare m) +// <<@ Title ("SDS: " +++ toString m.identifier) + +makeShare :: String String Int BCValue -> MTaskShare +makeShare withTask withDevice identifier value = {MTaskShare |withTask=[withTask] ,withDevice=[withDevice] ,identifier=identifier ,value=value - ,realShare=MTaskWithShare $ "mTaskSDS-" +++ toString identifier - } >>= \sh->set value (getSDSShare sh) >>| treturn sh - -updateShare :: Int BCValue -> Task () -updateShare ident val = get sdsStore - >>= \sh->(case find (\s->s.identifier==ident) sh of - Nothing = abort "Help, no share found with this ident" - Just mts = set val (getSDSShare mts)) - >>| traceValue "Updated" @! () +// ,realShare=MTaskWithShare $ "mTaskSDS-" +++ toString identifier + } + +updateShare :: Int BCValue -> Task [MTaskShare] +updateShare ident val = upd (map $ up ident val) sdsStore + where + up :: Int BCValue MTaskShare -> MTaskShare + up i v s = if (s.identifier == i) {MTaskShare | s & value=val} s + +import GenPrint, StdMisc, StdDebug, TTY +derive gPrint MTaskDevice, MTaskShare, Maybe, MTaskResource, MTaskTask, TaskId, TTYSettings, TCPSettings, DateTime +derive gPrint Parity, BaudRate, ByteSize + +cleanSharesTask :: Int MTaskDevice -> Task [MTaskShare] +cleanSharesTask taskid d +| not (trace_tn $ printToString taskid) = undef +| not (trace_tn $ printToString d.deviceTasks) = undef +| not (trace_tn $ printToString $ getNames taskid d) = undef += upd (map $ up $ getNames taskid d) sdsStore + where + getNames :: Int MTaskDevice -> [String] + getNames i d = [t.MTaskTask.name\\t<-d.deviceTasks|t.ident==i] + + up :: [String] MTaskShare -> MTaskShare + up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]} + +cleanSharesDevice :: String -> Task [MTaskShare] +cleanSharesDevice did = upd (map (up did)) sdsStore + where + up :: String MTaskShare -> MTaskShare + up i s = {MTaskShare | s & withDevice = [wt\\wt<-s.withDevice|wt <> i]} diff --git a/Tasks/mTaskTask.icl b/Tasks/mTaskTask.icl index 845c077..208b123 100644 --- a/Tasks/mTaskTask.icl +++ b/Tasks/mTaskTask.icl @@ -27,7 +27,7 @@ sendTaskToDevice wta mTask (device, timeout) = sharename i = device.deviceChannels +++ "-" +++ toString i toSDSRecords :: [MTaskMSGSend] BCState MTaskDevice -> Task [MTaskShare] - toSDSRecords s st device = sequence "" + toSDSRecords s st device = treturn [makeShare wta device.deviceName sdsi sdsval \\{sdsi,sdspub,sdsval}<-st.sdss , (MTSds sdsi` _)<-s diff --git a/miTask.icl b/miTask.icl index f701b64..ba28987 100644 --- a/miTask.icl +++ b/miTask.icl @@ -35,7 +35,7 @@ demo = viewInformation "" [] "Hello world" mTaskManager :: Task () mTaskManager = startupDevices >>| anyTask [ viewmTasks @! () - , whileUnchanged sdsStore manageShares + , whileUnchanged sdsStore manageShares @! () , whileUnchanged deviceStore $ manageDevices process ] <<@ ApplyLayout (foldl1 sequenceLayouts [arrangeWithSideBar 0 LeftSide 260 True @@ -66,7 +66,7 @@ mTaskManager = startupDevices >>| anyTask proc [m:ms] = traceValue (toString m) >>| (case m of // MTSDSAck i = traceValue (toString m) @! () // MTSDSDelAck i = traceValue (toString m) @! () - MTPub i val = updateShare i val + MTPub i val = updateShare i val @! () MTTaskAck i mem = deviceTaskAcked device i mem MTTaskDelAck i = deviceTaskDeleteAcked device i @! () MTDevSpec s = deviceAddSpec device s @! () diff --git a/todo.txt b/todo.txt index 030fb00..fa5dd3d 100644 --- a/todo.txt +++ b/todo.txt @@ -1,3 +1,2 @@ -delete tasks when deleting device let tasks have a unique name check addition and equality for numbers and bools -- 2.20.1