\documentclass[../thesis.tex]{subfiles}

\input{subfilepreamble}

\begin{document}
\input{subfileprefix}

\chapter{Integration with \texorpdfstring{\gls{ITASK}}{iTask}}%
\label{chp:integration_with_itask}
\begin{chapterabstract}
	This chapter shows the integration of \gls{MTASK} with \gls{ITASK} by showing:
	\begin{itemize}
		\item an architectural overview of \gls{MTASK};
		\item on the interface for connecting devices;
		\item the interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks;
		\item and interface for lifting \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS}.
	\end{itemize}
\end{chapterabstract}

The \gls{MTASK} language is a multi-view \gls{DSL}, i.e.\ there are multiple interpretations possible for a single \gls{MTASK} term.
Using the byte code compiler (\cleaninline{BCInterpret}) \gls{DSL} interpretation, \gls{MTASK} tasks can be fully integrated in \gls{ITASK}.
They are executed as if they are regular \gls{ITASK} tasks and they communicate may access \glspl{SDS} from \gls{ITASK} as well.
\Gls{MTASK} devices contain a domain-specific \gls{OS} (\gls{RTS}) and are little \gls{TOP} engines in their own respect, being able to execute tasks.
\Cref{fig:mtask_integration} shows the architectural layout of a typical \gls{IOT} system created with \gls{ITASK} and \gls{MTASK}.
The entire system is written as a single \gls{CLEAN} specification where multiple tasks are executed at the same time.
Tasks can access \glspl{SDS} according to many-to-many communication and multiple clients can work on the same task.
Devices are integrated into the system using the \cleaninline{withDevice} function (see \cref{sec:withdevice}).
Using \cleaninline{liftmTask}, \gls{MTASK} tasks are lifted to a device (see \cref{sec:liftmtask}).
\Gls{ITASK} \glspl{SDS} are lifted to the \gls{MTASK} device using \cleaninline{liftsds} (see \cref{sec:liftmtask}).

\begin{figure}[ht]
	\centering
	\includestandalone{mtask_integration}
	\caption{\Gls{MTASK}'s integration with \gls{ITASK}.}%
	\label{fig:mtask_integration}
\end{figure}

\section{Devices}\label{sec:withdevice}
When interpreted by the byte code compiler view, an \gls{MTASK} task produces a compiler.
This compiler is exceuted at run time so that the resulting byte code can be sent to an edge device.
All communication with this device happens through a so-called \emph{channels} \gls{SDS}.
The channels contain three fields, a queue of messages that are received, a queue of messages to send and a stop flag.
Every communication method that implements the \cleaninline{channelSync} class can provide the communication with an \gls{MTASK} device.
As of now, serial port communication, direct \gls{TCP} communication and \gls{MQTT} over \gls{TCP} are supported as communication providers.
The \cleaninline{withDevice} function transforms a communication provider and a task that does something with this device to an \gls{ITASK} task.
Internally, the task sets up the communication, exchanges specifications, executes the inner task while handling errors, and finally cleans up after closing.
\Cref{lst:mtask_device} shows the types and interface to connecting devices.

\begin{lstClean}[label={lst:mtask_device},caption={Device communication interface in \gls{MTASK}.}]
:: MTDevice //abstract
:: Channels :== ([MTMessageFro], [MTMessageTo], Bool)

class channelSync a :: a (Shared sds Channels) -> Task () | RWShared sds

withDevice :: a (MTDevice -> Task b) -> Task b | iTask b & channelSync, iTask a
\end{lstClean}

\section{Lifting tasks}\label{sec:liftmtask}
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}).
Given an \gls{MTASK} task in the \cleaninline{BCInterpret} view and a device obtained from \cleaninline{withDevice}, an \gls{ITASK} task is returned.
This \gls{ITASK} task tethers the \gls{MTASK} task that is executed on the microcontroller.
Hence, when for example observing the task value, the actual task value from the microcontroller is observed.

\begin{lstClean}[label={lst:liftmtask},caption={The interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks.}]
liftmTask :: (Main (MTask BCInterpret u)) MTDevice -> Task u | iTask u
\end{lstClean}

Under the hood, \cleaninline{liftmTask} compiler the \gls{MTASK} task to byte code, gathers the initial values for the lifted \glspl{SDS} and sends the data to the device.
Once acknowledged, the \gls{MTASK} task value and the \glspl{SDS} are monitored.
If the \gls{ITASK} server updates the value of a lifted \gls{SDS}, the appropriate message is sent to the \gls{MTASK} to notify it of the change.
Furthermore, the \gls{MTASK} device may update the \gls{SDS}, and in that case the message is picked up by the \cleaninline{liftmTask} task and the \gls{SDS} on the \gls{ITASK} server updated accordingly.

\section{Lifting \texorpdfstring{\glsxtrlongpl{SDS}}{shared data sources}}\label{sec:liftsds}
\begin{lstClean}[label={lst:mtask_itasksds},caption={Lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
class liftsds v where
	liftsds :: ((v (Sds t)) -> In (Shared sds t) (Main (MTask v u)))
		-> Main (MTask v u) | RWShared sds
\end{lstClean}

\section{Conclusion}


\input{subfilepostamble}
\end{document}
