import Generics.gCons
 import iTasksTTY
 
-derive class iTask MTaskDevice, MTaskDeviceSpec, MTaskResource, MTaskMSGRecv, MTaskMSGSend, BCShare
+derive class iTask MTaskDevice, MTaskResource, MTaskMSGRecv, MTaskMSGSend, BCShare
 derive conses MTaskResource, TCPSettings
 derive consName MTaskResource, TCPSettings
 
 
 
 from Data.Func import $
 
-derive class iTask MTaskDevice, MTaskResource, MTaskDeviceSpec, MTaskMSGRecv, MTaskMSGSend, BCShare
+derive class iTask MTaskDevice, MTaskResource, MTaskMSGRecv, MTaskMSGSend, BCShare
 derive conses MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
 derive consName MTaskResource, TTYSettings, BaudRate, Parity, ByteSize, TCPSettings
 
        where deleteTask d = {d & deviceTasks=[s\\s<-d.deviceTasks | i <> s.ident]}
 
 deviceAddSpec :: MTaskDevice MTaskDeviceSpec -> Task ()
-deviceAddSpec d s = withDevices d $ \r->{r&deviceSpec=Just s}
+deviceAddSpec d s = withDevices d $ \r->{MTaskDevice | r & deviceSpec=Just s}
 
                                onDisconnect=onDisconnect} @! ()
                where
                        onConnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
-                       onConnect acc (msgs,send,sendStopped) = (Ok acc, Just (msgs,[],sendStopped), map encode send, False)
+                       onConnect acc (msgs,send,sendStopped) = (Ok "", Just (msgs,[],sendStopped), map encode send, False)
 
                        whileConnected :: (Maybe String) String ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
                        //We stop
                        = (Ok acc, Just (msgs++map decode nd,[],sendStopped), map encode send, False)
 
                        process :: String -> (String, [String])
-                       process s = case indexOf "\n" s of
+                       process s
+                       | not (trace_tn ("process: " +++ toString (toJSON s))) = undef
+                       = case indexOf "\n" s of
                                -1 = (s, [])
-                               i = appSnd (\ss->[s % (0,i):ss]) (process (s % (i+1, size s)))
+                               i = appSnd (\ss->[s % (0,i-1):ss]) (process (s % (i+1, size s)))
                        
                        onDisconnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool))
                        onDisconnect _ (msgs,send,sendStopped) = (Ok "", Nothing)
 
                        task_register();
                        break;
                case '\0':
+                       die("Server closed connection");
                        break;
                case '\n':
                        break;
 
 
 void spec_send(void)
 {
+       debug("Writing spec");
        write_byte('c');
        write_byte(0 | (HAVELED << 0) | (HAVEAIO << 1) | (HAVEDIO << 2));
        write16(MAXTASKS);
        write16(MAXSDSS);
        write_byte('\n');
+       debug("Done writing spec");
 }
 
 derive consNum BCValue
 
 derive class gCons Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin
-derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode
+derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode, MTaskDeviceSpec
 derive gEditor BCValue
 derive gText BCValue
 derive JSONEncode BCValue
 
 encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
 encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
 
+import StdDebug
 decode :: String -> MTaskMSGRecv
 decode x
+| not (trace_tn ("decoding: " +++ toString (toJSON x))) = undef
 | size x == 0 = MTEmpty
 = case x.[0] of
        't' = MTTaskAck $ fromByteCode x
 gEq{|BCValue|} (BCValue e) (BCValue f) = toByteCode e == toByteCode f
 
 derive class gCons Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin
-derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode
+derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode, MTaskDeviceSpec
 
 op2 :: (ByteCode a p1) (ByteCode a p2) BC -> ByteCode b Expr
 op2 (BC x) (BC y) bc = BC $ x >>| y >>| tell [bc]
 
 
 import TTY, iTasksTTY
 
+//derive class iTask ByteSize, Parity, BaudRate, TTYSettings, TCPSettings
+//derive class iTask MTaskDeviceSpec, MTaskResource, MTaskTask, MTaskDevice
+
 Start :: *World -> *World
-Start world = startEngine (mTaskManager
-       >>* [OnAction (Action "Shutdown") (always $ shutDown)]) world
+Start world = startEngine (viewInformation "" [] md) world
+       where
+               md = 
+                       { deviceTask = Just $ TaskId 0 0
+                       , deviceError = Just ""
+                       , deviceChannels = "abc"
+                       , deviceName = "hoi"
+                       , deviceTasks = []
+                       , deviceData = TCPDevice {TCPSettings
+                               |host="localhost",
+                               port=8888
+                               }
+                       , deviceSpec = Just {MTaskDeviceSpec
+                               |haveLed = True
+                               ,haveAio = True
+                               ,haveDio = True
+                               ,maxTask = 42
+                               ,maxSDS = 5
+                               }
+                       }
+                       
+                       
+
+//Start world = startEngine (mTaskManager
+//     >>* [OnAction (Action "Shutdown") (always $ shutDown)]) world
 
 mTaskManager :: Task ()
 mTaskManager = startupDevices >>| anyTask