X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=Utils%2FSDS.icl;h=e23e767e5bc3caa4e74522b94ce77f129e8cec07;hb=6badd886c4b09ac1805a005e525447a7e910ea56;hp=9c6b46a9923cc05df447d42889cb5f05564b0bda;hpb=5f4c4b61ea1e4062e90715af9e1027da6d1c7a66;p=mTask.git diff --git a/Utils/SDS.icl b/Utils/SDS.icl index 9c6b46a..e23e767 100644 --- a/Utils/SDS.icl +++ b/Utils/SDS.icl @@ -5,25 +5,66 @@ import iTasks._Framework.Store import Devices.mTaskDevice import Shares.mTaskShare import Tasks.Examples +import Data.List import qualified Data.Map as DM from Data.Func import $ +import Data.Tuple -derive class iTask MTaskShare +import StdDebug memoryShare :: String a -> Shared a | iTask a -memoryShare s d = sdsFocus s $ memoryStore "" $ Just d +memoryShare s d = sdsFocus s $ memoryStore s $ Just d -deviceStore :: Shared [MTaskDevice] -deviceStore = sharedStore "mTaskDevices" [] +deviceStoreNP :: Shared [MTaskDevice] +deviceStoreNP = sdsFocus Nothing $ deviceStore -sdsStore :: Shared [MTaskShare] -sdsStore = memoryShare "mTaskShares" [] +import GenPrint, TTY +derive gPrint MTaskDevice, Maybe, MTaskShare, MTaskResource, TaskId, MTaskTask, (,), TTYSettings, TCPSettings, DateTime, Parity, BaudRate, ByteSize, MaybeError, () +gPrint{|Dynamic|} _ st = gPrint{|*|} "**Dynamic**" st -bcStateStore :: Shared BCState -bcStateStore = memoryShare "mTaskBCState" zero +deviceStore :: RWShared (Maybe (MTaskDevice, Int)) [MTaskDevice] [MTaskDevice] +deviceStore = SDSSource {SDSSource + | name = "deviceStore" + , read = realRead + , write= realWrite + } +where + realRead :: (Maybe (MTaskDevice,Int)) *IWorld -> (MaybeError TaskException [MTaskDevice], *IWorld) + realRead p iw = read realDeviceStore iw + + realWrite :: (Maybe (MTaskDevice,Int)) [MTaskDevice] *IWorld -> (MaybeError TaskException (SDSNotifyPred (Maybe (MTaskDevice,Int))), *IWorld) + realWrite mi w iw + | not (trace_tn $ "write called with: " +++ printToString mi +++ " w " +++ printToString w) = undef + # (merr, iw) = write w realDeviceStore iw + | not (trace_tn $ "written to real store: " +++ printToString merr) = undef + | isError merr || isNothing mi = (merr $> notifyPred mi, iw) + # (Just (dev, ident)) = mi + | ident == -1 = (merr $> notifyPred mi, iw) + = case find ((==)dev) w of + Nothing = (Error $ exception "Device doesn't exist anymore", iw) + Just {deviceShares} = case find (\d->d.identifier == ident) deviceShares of + Nothing = (Error $ exception $ "deviceStore: Share doesn't exist: " +++ toString ident, iw) + Just s + | not $ trace_tn "Really sending a message from a share update" = undef + = case sendMessagesIW [MTUpd ident s.MTaskShare.value] dev iw of + (Error e, iw) = (Error e, iw) + (Ok _, iw) = (Ok $ notifyPred mi, iw) + + notifyPred :: (Maybe (MTaskDevice, Int)) (Maybe (MTaskDevice, Int)) -> Bool + // Global watcher looking at a global event + notifyPred Nothing Nothing = True + // Global watcher looking at a local event + notifyPred Nothing (Just _) = False + // Local watcher looking at a global event + notifyPred (Just _) Nothing = False + // Local device watcher looking at a local event + notifyPred (Just (d1, -1)) (Just (d2, _)) = d1 == d2 + // Local share watcher looking at a local share event + notifyPred (Just (d1, i1)) (Just (d2, i2)) = d1 == d2 && i1 == i2 + +realDeviceStore :: Shared [MTaskDevice] +realDeviceStore = sharedStore "mTaskDevices" [] +//realDeviceStore = memoryShare "mTaskDevices" [] mTaskTaskStore :: Shared [String] mTaskTaskStore = memoryShare "mTaskTasks" $ 'DM'.keys allmTasks - -getSDSRecord :: Int -> Task MTaskShare -getSDSRecord i = get sdsStore @ \l->hd [s\\s<-l | s.identifier == i]