a157e0b1681246d6d8d9098cb86b30158bf2923e
[mTask.git] / Devices / mTaskDevice.icl
1 implementation module Devices.mTaskDevice
2
3 from StdFunc import flip
4 import Generics.gCons
5 import mTaskInterpret
6 import iTasks
7 import iTasksTTY
8 import TTY
9 import qualified Data.Map as DM
10 import Utils.SDS
11 import Utils.Devices
12
13 import GenBimap
14 import Devices.mTaskSerial
15 import Devices.mTaskTCP
16 import iTasks._Framework.Store
17 import iTasks.UI.Definition, iTasks.UI.Editor, iTasks.UI.Editor.Builtin, iTasks.UI.Editor.Common, iTasks.UI.Layout.Default, iTasks.UI.Layout.Common
18
19 from Data.Func import $
20
21 derive class iTask MTaskDevice, MTaskResource, MTaskMSGRecv, MTaskMSGSend, BCShare
22 derive conses MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
23 derive consName MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
24
25 instance == MTaskDevice where
26 (==) a b = a.deviceChannels == b.deviceChannels
27
28 startupDevices :: Task [MTaskDevice]
29 startupDevices = upd (map reset) deviceStore
30 where reset d = {d & deviceTask=Nothing, deviceTasks=[], deviceError=Nothing}
31
32 makeDevice :: String MTaskResource -> Task MTaskDevice
33 makeDevice name res = get randomInt @ \rand->{MTaskDevice
34 |deviceChannels=name +++ toString rand
35 ,deviceName=name
36 ,deviceTasks=[]
37 ,deviceTask=Nothing
38 ,deviceError=Nothing
39 ,deviceData=res}
40
41 getSynFun :: MTaskResource -> ((Shared Channels) -> Task ())
42 getSynFun (TCPDevice t) = synFun t
43 getSynFun (SerialDevice t) = synFun t
44
45 addDevice :: (Shared [MTaskDevice]) (MTaskDevice (Shared Channels) -> Task ()) -> Task String
46 addDevice devices processFun
47 = enterChoice "Device type" [] (map consName{|*|} deviceTypes)
48 >&^ \sh->whileUnchanged sh $ \mty->case mty of
49 Nothing = viewInformation "No type selected yet" [] ""
50 Just ty = enterInformation "Name" [] -&&- deviceSettings ty
51 >>= \(name, settings)->makeDevice name settings
52 >>= \dev->upd (\l->[dev:l]) devices
53 >>| connectDevice processFun dev
54 @! ""
55 where
56 deviceSettings "SerialDevice" = getmTaskSerialDevice
57 deviceSettings "TCPDevice" = getmTaskTCPDevice
58
59 deviceTypes :: [MTaskResource]
60 deviceTypes = conses{|*|}
61
62 connectDevice :: (MTaskDevice (Shared Channels) -> Task ()) MTaskDevice -> Task ()
63 connectDevice pf d = let ch = channels d in appendTopLevelTask 'DM'.newMap True
64 (pf d ch -||- catchAll (getSynFun d.deviceData ch) errorHandle)
65 >>= \tid->withDevices d (\d->{d&deviceTask=Just tid,deviceError=Nothing}) @! ()
66 where
67 errorHandle e = withDevices d (\d->{d&deviceTask=Nothing,deviceError=Just e})
68
69 manageDevices :: (MTaskDevice (Shared Channels) -> Task ()) [MTaskDevice] -> Task ()
70 manageDevices processFun ds = anyTask [
71 addDevice deviceStore processFun <<@ Title "Add new device" @! ():
72 [viewDevice processFun d
73 <<@ Title d.deviceName\\d<-ds]]
74 <<@ ArrangeWithTabs @! ()
75
76 viewDevice :: (MTaskDevice (Shared Channels) -> Task ()) MTaskDevice -> Task ()
77 viewDevice pf d = forever $ traceValue "viewDevice" >>| anyTask
78 [viewInformation "Device settings" [] d @! ()
79 ,viewSharedInformation "Channels" [ViewAs dropEmpty] (channels d) @! ()
80 ,forever $
81 enterChoice "Delete task on device" [ChooseFromGrid id] d.deviceTasks
82 >>* [OnAction (Action "Delete") $ ifValue (\t->t.ident <> -1) (deviceTaskDelete d)]
83 @! ()
84 ] <<@ ArrangeHorizontal
85 >>* [OnAction (Action "Delete Device") (always $ deleteDevice d):
86 if (isJust d.deviceTask) []
87 [OnAction (Action "Connect") (always $ connectDevice pf d)]]
88 where
89 dropEmpty (r,s,ss) = (filter ((=!=)MTEmpty) r,s,ss)
90
91 deleteDevice :: MTaskDevice -> Task ()
92 deleteDevice d = upd (\(r,s,ss)->(r,s,True)) (channels d)
93 >>| maybe (treturn ()) (flip removeTask topLevelTasks) d.deviceTask
94 >>| upd (filter ((==)d)) deviceStore
95 @! ()
96
97 sendToDevice :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task ()
98 sendToDevice wta mTask (device, timeout) =
99 traceValue "starting to send"
100 >>| get bcStateStore @ toMessages timeout o toRealByteCode (unMain mTask)
101 >>= \(msgs, st1)->traceValue "messages generated"
102 >>| set st1 bcStateStore
103 >>| traceValue "bcstate store updated"
104 >>| toSDSRecords st1
105 >>= \sdss->traceValue "Shares created"
106 >>| set sdss sdsStore//MTaskShareaddToSDSShare
107 >>| traceValue "Shares store updated"
108 >>| sendMessages msgs device
109 >>| traceValue "Messages sent"
110 >>| makeTask wta -1
111 >>= \t->traceValue "Task made"
112 >>| withDevices device (addTask t)
113 >>| traceValue "Tasks share updated"
114 @! ()
115 where
116 sharename i = device.deviceChannels +++ "-" +++ toString i
117 toSDSRecords st = sequence "" [makeShare wta sdsi sdsbc\\{sdsi,sdspub,sdsbc}<-st.sdss]// | sdspub]
118
119 addTask :: MTaskTask MTaskDevice -> MTaskDevice
120 addTask task device = {device & deviceTasks=[task:device.deviceTasks]}
121
122 sendMessages :: [MTaskMSGSend] -> (MTaskDevice -> Task Channels)
123 sendMessages msgs = upd (\(r,s,ss)->(r,msgs++s,ss)) o channels
124
125 withDevices :: MTaskDevice (MTaskDevice -> MTaskDevice) -> Task ()
126 withDevices a trans = upd (map \b->if (b == a) (trans b) b) deviceStore @! ()
127
128 deviceTaskAcked :: MTaskDevice Int -> Task ()
129 deviceTaskAcked dev i
130 = withDevices dev (\d->{d&deviceTasks=ackFirst d.deviceTasks})
131 where
132 ackFirst :: [MTaskTask] -> [MTaskTask]
133 ackFirst [] = []
134 ackFirst [t:ts] = if (t.ident == -1)
135 [{t & ident=i}:ts] [t:ackFirst ts]
136
137 deviceTaskDelete :: MTaskDevice MTaskTask -> Task ()
138 deviceTaskDelete dev task = sendMessages [MTTaskDel task.ident] dev @! ()
139
140 deviceTaskDeleteAcked :: MTaskDevice Int -> Task ()
141 deviceTaskDeleteAcked d i = withDevices d $ deleteTask
142 where deleteTask d = {d & deviceTasks=[s\\s<-d.deviceTasks | i <> s.ident]}