import numpy as np
import json
import threading

import enaml
import zmq

from io_controller import IOController


class SensorApp(object):

    def __init__(self, ip='192.168.43.48', port=2019):
    #def __init__(self, ip='192.168.1.80', port=2015):
        self.ip = ip
        self.port = port

        self._run = True

        self.io_controller = IOController()

        self.start()

    def start(self):
        self._sensor_client_thread = threading.Thread(
                target=self._sensor_client_worker)
        self._sensor_client_thread.start()

    def stop(self):
        self._run = False

    def _sensor_client_worker(self):
        # XXX Mock sensor values:
        #while self._run:
        #    updates = {'acc_x': int(np.random.random() * 1024),
        #                   'acc_y': int(np.random.random() * 1024),
        #                   'acc_z': int(np.random.random() * 1024),
        #                   'switch': np.random.random() * 360,
        #                   'distance': np.random.random() * 90,
        #                   'potentiometer': np.random.random()}
        #    self.io_controller.set(**updates)
        #    import time
        #    time.sleep(.1)

        context = zmq.Context()

        #  Socket to talk to server
        socket = context.socket(zmq.REQ)
        socket.connect("tcp://{}:{}".format(self.ip, self.port))

        while self._run:
            send = {}
            if self.io_controller.added_links:
                add = self.io_controller.added_links
                self.io_controller.added_links = []
                send['add_link'] = add
            if self.io_controller.removed_links:
                remove = self.io_controller.removed_links
                self.io_controller.removed_links = []
                send['remove_link'] = remove
            if self.io_controller.outputs:
                outputs = self.io_controller.outputs
                self.io_controller.outputs = {}
                send['out'] = outputs
            socket.send(json.dumps(send))
            message = socket.recv()
            self.io_controller.set(**json.loads(message))
        socket.close()


if __name__ == '__main__':
    from enaml.stdlib.sessions import show_simple_view
    with enaml.imports():
        from sensor_view import SensorViewWindow
    sensor_app = SensorApp()
    window = SensorViewWindow(io_controller=sensor_app.io_controller)
    show_simple_view(window)
    sensor_app.stop()

@@ -4,27 +4,20 @@
 ort 
-numpy as np
+json
 %0Aimport 
 json
@@ -8,28 +8,27 @@
 json%0Aimport 
-json
+sys
 %0Aimport thre
@@ -126,24 +126,25 @@
 ject):%0A%0A    
+#
 def __init__
@@ -182,25 +182,24 @@
 =2019):%0A    
-#
 def __init__
@@ -235,10 +235,10 @@
 t=20
-15
+24
 ):%0A 
@@ -640,16 +640,17 @@
        #
+#
  XXX Moc
@@ -664,17 +664,44 @@
 r values
-:
+%0A        #import numpy as np
 %0A       
@@ -2337,16 +2337,54 @@
 wWindow%0A
+    ip, port = sys.argv%5B1%5D.split(':')%0A
     sens
@@ -2402,16 +2402,32 @@
 nsorApp(
+ip=ip, port=port
 )%0A    wi

add cmdline arg for ip and port