george comments
[phd-thesis.git] / top / int.tex
1 \documentclass[../thesis.tex]{subfiles}
2
3 \input{subfilepreamble}
4
5 \begin{document}
6 \input{subfileprefix}
7
8 \chapter{Integration with \texorpdfstring{\gls{ITASK}}{iTask}}%
9 \label{chp:integration_with_itask}
10 \begin{chapterabstract}
11 This chapter shows the integration of \gls{MTASK} with \gls{ITASK} by showing:
12 \begin{itemize}
13 \item an architectural overview \gls{MTASK} applications;
14 \item on the interface for connecting devices;
15 \item the interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks;
16 \item and interface for lifting \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS}.
17 \end{itemize}
18 \end{chapterabstract}
19
20 The \gls{MTASK} language is a multi-view \gls{DSL}, i.e.\ there are multiple interpretations possible for a single \gls{MTASK} term.
21 Using the byte code compiler (\cleaninline{BCInterpret}) \gls{DSL} interpretation, \gls{MTASK} tasks can be fully integrated in \gls{ITASK}.
22 They are executed as if they are regular \gls{ITASK} tasks and they communicate may access \glspl{SDS} from \gls{ITASK} as well.
23 \Gls{MTASK} devices contain a domain-specific \gls{OS} and are little \gls{TOP} engines in their own respect, being able to execute tasks.
24 \Cref{fig:mtask_integration} shows the architectural layout of a typical \gls{IOT} system created with \gls{ITASK} and \gls{MTASK}.
25 The entire system is written as a single \gls{CLEAN} specification where multiple tasks are executed at the same time.
26 Tasks can access \glspl{SDS} according to many-to-many communication and multiple clients can work on the same task.
27 Devices are integrated into the system using the \cleaninline{withDevice} function (see \cref{sec:withdevice}).
28 Using \cleaninline{liftmTask}, \gls{MTASK} tasks are lifted to a device (see \cref{sec:liftmtask}).
29 \Gls{ITASK} \glspl{SDS} are lifted to the \gls{MTASK} device using \cleaninline{liftsds} (see \cref{sec:liftmtask}).
30
31 \begin{figure}[ht]
32 \centering
33 \includestandalone{mtask_integration}
34 \caption{\Gls{MTASK}'s integration with \gls{ITASK}.}%
35 \label{fig:mtask_integration}
36 \end{figure}
37
38 \section{Connecting edge devices}\label{sec:withdevice}
39 When interpreted by the byte code compiler view, an \gls{MTASK} task produces a compiler.
40 This compiler is exceuted at run time so that the resulting byte code can be sent to an edge device.
41 All communication with this device happens through a so-called \emph{channels} \gls{SDS}.
42 The channels contain three fields, a queue of messages that are received, a queue of messages to send and a stop flag.
43 Every communication method that implements the \cleaninline{channelSync} class can provide the communication with an \gls{MTASK} device.
44 As of now, serial port communication, direct \gls{TCP} communication and \gls{MQTT} over \gls{TCP} are supported as communication providers (see \cref{lst:connection_types}).
45 The \cleaninline{withDevice} function transforms such a communication provider and a task that does something with this device to an \gls{ITASK} task.
46 Internally, the task sets up the communication, exchanges specifications with the device, executes the inner task while handling errors, and finally cleans up after closing.
47 \Cref{lst:mtask_device} shows the types and interface to connecting devices.
48
49 \begin{lstClean}[label={lst:mtask_device},caption={Device communication interface in \gls{MTASK}.}]
50 :: MTDevice //abstract
51 :: Channels :== ([MTMessageFro], [MTMessageTo], Bool)
52
53 class channelSync a :: a (Shared sds Channels) -> Task () | RWShared sds
54
55 withDevice :: a (MTDevice -> Task b)
56 -> Task b | iTask b & channelSync, iTask a
57 \end{lstClean}
58
59 \todo{v.b.\ voor withDevice}
60
61 \subsection{Implementation}
62 The \cleaninline{MTDevice} abstract type is internally represented as three \gls{ITASK} \gls{SDS} that contain all the current information about the tasks.
63 The first \gls{SDS} is the information about the \gls{RTS} of the device, i.e.\ metadata on the tasks that are executing, the hardware specification and capabilities, and a list of fresh task identifiers.
64 The second \gls{SDS} is a map storing downstream \gls{SDS} updates.
65 When a lifted \gls{SDS} is updated on the device, a message is sent to the server.
66 This message is initially queued in the map to allow for asynchronous handling of multiple updates.
67 Finally, the \cleaninline{MTDevices} type contains the communication channels.
68
69 The \cleaninline{withDevice} task itself first constructs the \glspl{SDS} using the \gls{ITASK} function \cleaninline{withShared} to create anonymous local \glspl{SDS}.
70 Then, it performs the following four tasks in parallel to monitor the edge device.
71 \begin{enumerate}
72 \item It synchronises the channels using the \cleaninline{channelSync} overloaded function.
73 Errors that occur here are converted to the proper \gls{MTASK} exception.
74 \item Watches the channels for the shutdown flag.
75 If the connection is lost with the device unexpectedly, an \gls{MTASK} exception is thrown.
76 \item Watches the channels for messages and processes them accordingly by changing the device information \gls{SDS} or adding the lifted \gls{SDS} updates to the corresponding \gls{SDS} update queue.
77 \item Sends a request for a specification. Once the specification is received, the device task is run.
78 The task value of this device task is then used as the task value of the \cleaninline{withDevice} task.
79 \end{enumerate}
80
81 If at any stage an unrecoverable device error occurs, an \gls{ITASK} exception is thrown on the \cleaninline{withDevice} task.
82 This exception can be caught in order to device some kind of fail-safe mechanism.
83 For example, when a device fails, the tasks can be sent to another device.
84 \todo[inline]{Example of failsafe?}
85
86 %withDevice spec deviceTask
87 % withShared newMap \sdsupdates->
88 % withShared ([], [MTTSpecRequest], False) \channels->
89 % withShared default \dev->parallel
90 % [ channelSync spec // unexpected disconnect
91 % , watchForShutdown // unexpected disconnect
92 % , watchChannels // process messages
93 % , waitForSpecification
94 % >>| deviceTask
95 % >>* [ifStable: issueShutdown]
96 % ]
97
98 \section{Lifting \texorpdfstring{\gls{MTASK}}{mTask} tasks}\label{sec:liftmtask}
99 Once the connection with the device is established, \gls{MTASK} tasks can be lifted to \gls{MTASK} tasks using the \cleaninline{liftmTask} family of functions (see \cref{lst:liftmtask}).
100 Given an \gls{MTASK} task in the \cleaninline{BCInterpret} view and a device obtained from \cleaninline{withDevice}, an \gls{ITASK} task is returned.
101 This \gls{ITASK} task tethers the \gls{MTASK} task that is executed on the microcontroller.
102 Hence, when for example observing the task value, the actual task value from the microcontroller is observed.
103
104 \begin{lstClean}[label={lst:liftmtask},caption={The interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks.}]
105 liftmTask :: (Main (MTask BCInterpret u)) MTDevice -> Task u | iTask u
106 \end{lstClean}
107
108 Under the hood, \cleaninline{liftmTask}:
109 \begin{itemize}
110 \item Generates a fresh task identifier for the device.
111 \item Compiles the task and fetches the values for the tethered \glspl{SDS}.
112 \item Sends the task to the device
113 \item Watches, in parallel: the tethered \glspl{SDS} in \gls{ITASK}, if they are updated, a message is sent to the device; the \gls{SDS} update queue, if there is a downstream update, the \gls{ITASK} \gls{SDS} it references is updated as well; and the task value.
114 \end{itemize}
115
116 The task value of the \cleaninline{liftmTask} task is the task value of the task on the edge device.
117
118 \todo{v.b.\ voor liftmtask}
119
120 \section{Lifting \texorpdfstring{\gls{ITASK}}{iTask} \texorpdfstring{\glsxtrlongpl{SDS}}{shared data sources}}\label{sec:liftsds}
121 Lifting \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS} is something that mostly happens at the compiler level using the \cleaninline{liftsds} function (see \cref{lst:mtask_itasksds}).
122 \Glspl{SDS} in \gls{MTASK} must always have an initial value.
123 For regular \gls{SDS} this value is given in the source code, for lifted \gls{ITASK} \glspl{SDS} this value is obtained by reading the values once just before sending the task to the edge device.
124 On the device itself, there is just one difference between lifted \glspl{SDS} and regular \glspl{SDS}: after changing \pgls{SDS}, a message is sent to the server containing this new value.
125 The \cleaninline{withDevice} task (see \cref{sec:withdevice}) receives and processes this message by writing to the \gls{ITASK} \gls{SDS}.
126 Tasks watching this \gls{SDS} get notified then through the normal notification mechanism of \gls{ITASK}.
127
128 \begin{lstClean}[label={lst:mtask_itasksds},caption={Lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
129 class liftsds v where
130 liftsds :: ((v (Sds t)) -> In (Shared sds t) (Main (MTask v u)))
131 -> Main (MTask v u) | RWShared sds
132 \end{lstClean}
133
134 The compilation of the code and the serialisation of the data throws away all typing information.
135 \Glspl{SDS} are stored in the compiler state as a map from identifiers to either an initial value or an \cleaninline{MTLens}.
136 The \cleaninline{MTLens} is a type synonym for a \gls{SDS} that represents the typeless serialised value of the underlying \gls{SDS}.
137 This is done so that the \cleaninline{withDevice} task can write the received \gls{SDS} updates to the according \gls{SDS} independently.
138 \Gls{ITASK}'s notification mechanism then takes care of the rest.
139 Such a \gls{SDS} is created by using the \cleaninline{mapReadWriteError} which, given a pair of read and write functions with error handling, produces a \gls{SDS} with the lens embedded.
140 The read function transforms, the function that converts a typed value to a typeless serialised value, just applies the serialisation.
141 The write function, the function that, given the new serialised value and the old typed value, produces a new typed value.
142 It tries to decode the serialised value, if that succeeds, it is written to the underlying \gls{SDS}, an error is thrown otherwise.
143 \Cref{lst:mtask_itasksds_lens} provides the implementation for this:
144
145 % VimTeX: SynIgnore on
146 \begin{lstClean}[label={lst:mtask_itasksds_lens},caption={Lens applied to lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
147 lens :: (Shared sds a) -> MTLens | type a & RWShared sds
148 lens sds = mapReadWriteError
149 ( \r->Ok (fromString (toByteCode{|*|} r)
150 , \w r-> ?Just <$> iTasksDecode (toString w)
151 ) ?None sds
152 \end{lstClean}
153 % VimTeX: SynIgnore off
154
155 \Cref{lst:mtask_itasksds_lift} shows the code for the implementation of \cleaninline{liftsds} that uses the \cleaninline{lens} function shown earlier.
156 First, the \gls{SDS} to be lifted is extracted from the expression by bootstrapping the fixed point with a dummy value.
157 This is safe because the expression on the right-hand side of the \cleaninline{In} is never used.
158 Then, using \cleaninline{addSdsIfNotExist}, the identifier for this particular \gls{SDS} is either retrieved from the compiler state or generated freshly.
159 This identifier is then used to provide a reference to the \cleaninline{def} definition to evaluate the main expression.
160
161 % VimTeX: SynIgnore on
162 \begin{lstClean}[label={lst:mtask_itasksds_lift},caption={Lens applied to lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
163 liftsds def = {main =
164 let (t In _) = def (abort "liftsds: expression too strict")
165 in addSdsIfNotExist (Right $ lens t)
166 >>= \sdsi->let (_ In e) = def (pure (Sds sdsi)) in e.main
167 }
168 where
169 \end{lstClean}
170 % VimTeX: SynIgnore off
171
172 \todo{v.b.\ voor lifted sdss}
173
174 \section{Home automation}
175 This section presents a interactive home automation program (\Cref{lst:example_home_automation}) to illustrate \gls{MTASK}'s integration with \gls{ITASK}.
176 It consists of a web interface for the user to control which tasks may be executed on either of two connected devices: an \gls{ARDUINO} UNO, connected via a serial port; and an ESP8266 based prototyping board called NodeMCU, connected via \gls{TCP} over WiFi.
177
178 \Crefrange{lst:example:spec1}{lst:example:spec2} show the specification for the devices followed by \crefrange{lst:example:task1}{lst:example:task2} containing the actual task.
179 This task first connects the devices (\crefrange{lst:example:conn1}{lst:example:conn2}) followed by a \cleaninline{parallel} task that is visualized as a tabbed window with a shutdown button to terminate the program (\crefrange{lst:example:par1}{lst:example:par2}).
180 The \cleaninline{chooseTask} task (\crefrange{lst:example:ct1}{lst:example:ct2}) allows the user to pick a task, sending it to the specified device.
181 Tasks are picked from the \cleaninline{tasks} list (\crefrange{lst:example:tasks1}{lst:example:tasks2}).
182 For example, the \cleaninline{temperature} task shows the current temperature to the user.
183 When the temperature changes, the \gls{DHT} sensor reports it and the task value for the \cleaninline{temperature} task changes.
184 This change in task value is reflected in the \gls{ITASK} server and the task value of the \cleaninline{liftmTask} task changes accordingly.
185 The task is lifted to an \gls{ITASK} task and the \cleaninline{>\&>}\footnotemark{} \gls{ITASK} combinator transforms the task value into an \gls{SDS} that is displayed to the user using \cleaninline{viewSharedInformation}.
186 \footnotetext{\cleaninline{(>\&>) infixl 1 :: (Task a) ((SDSLens () (? a) ()) -> Task b) -> Task b \| iTask a \& iTask b}}
187 Screenshots of the application are given in \cref{fig:example_screenshots}.
188
189 \begin{figure}[ht]
190 \centering
191 \begin{subfigure}[b]{.3\linewidth}
192 \includegraphics[width=\linewidth]{home_auto1}
193 \caption{Select task.}
194 \end{subfigure}
195 \begin{subfigure}[b]{.3\linewidth}
196 \includegraphics[width=\linewidth]{home_auto2}
197 \caption{Select device.}
198 \end{subfigure}
199 \begin{subfigure}[b]{.3\linewidth}
200 \includegraphics[width=\linewidth]{home_auto3}
201 \caption{View result.}
202 \end{subfigure}
203 \caption{Screenshots of the home automation example program in action.}%
204 \label{fig:example_screenshots}
205 \end{figure}
206
207 \begin{figure}
208 \cleaninputlisting[firstline=12,lastline=52,numbers=left,belowskip=0pt,escapeinside={/*}{*/}]{lst/example.icl}
209 \begin{lstClean}[numbers=left,firstnumber=42,aboveskip=0pt,caption={An example of a home automation program.},label={lst:example_home_automation}]
210 , ...][+\label{lst:example:tasks2}+]
211 \end{lstClean}
212 \end{figure}
213
214 \section{Conclusion}
215
216
217 \input{subfilepostamble}
218 \end{document}