import serial
import time
import threading
from socketIO_client import SocketIO, LoggingNamespace, BaseNamespace
from threading import Thread
from tendo import singleton
import configparser

me = singleton.SingleInstance()

config = configparser.ConfigParser()
config.read('./config.cfg')

password = config.get('variables', 'password')

media_hub_url = 'http://dev-uos-mediahub.azurewebsites.net'

score = {
        "play": {
            "themes": [
            ],
            "scenes": [
                "586f853ab8678acc10b1595d"
            ]
        }
    }


class PublishMediaFrameworkMessages:
    def on_auth_r(self, *args):
        print('on_auth_r', args)

    def __init__(self):
        self.socketio = SocketIO(media_hub_url, 80, LoggingNamespace)
        self.socketio.emit('auth', {'password': password}, self.on_auth_r)

        self.receive_events_thread = Thread(target=self._receive_events_thread)
        self.receive_events_thread.daemon = True
        self.receive_events_thread.start()

    def _receive_events_thread(self):
        self.socketio.wait()

    def publish(self, should_publish):
        print("publish")
        if not should_publish:
            self.socketio.emit('sendCommand', 'electret', 'showScenesAndThemes', score)


# serial connection
port = '/dev/ttyACM0'
baud = 9600
serial_port = serial.Serial(port, baud)


def handle_data(data):
    try:
        print(data)
        electret_peak_sample = int(data)
        if electret_peak_sample < 50:
            print("very quiet")
        elif electret_peak_sample < 500:
            print("medium noise")
        else:
            print("noisy")
    except:
        print("Error")


def read_from_port(ser):
    # read serial port for data

    ws_messenger = PublishMediaFrameworkMessages()

    should_publish = False

    while True:
        reading = ser.readline()
        electret_peak_sample = reading
        handle_data(electret_peak_sample)
        ws_messenger.publish(should_publish)
        should_publish = True


def main():
    thread = threading.Thread(target=read_from_port, args=(serial_port,))
    # thread.daemon = True
    thread.start()

if __name__ == "__main__":
    main()
@@ -1000,24 +1000,253 @@
 ad.start()%0A%0A
+        yes = False%0A%0A        while True:%0A            reading = serial_port.readline()%0A            electret_peak_sample = reading%0A            handle_data(electret_peak_sample)%0A            self.publish(yes)%0A            yes = True%0A%0A
     def _rec
@@ -1365,16 +1365,46 @@
 blish%22)%0A
+        print(should_publish)%0A
         
@@ -1989,16 +1989,18 @@
 ata%0A%0A   
+ #
  ws_mess
@@ -2044,36 +2044,8 @@
 ()%0A%0A
-    should_publish = False%0A%0A
     
@@ -2177,16 +2177,18 @@
 %0A       
+ #
  ws_mess
@@ -2221,38 +2221,8 @@
 sh)%0A
-        should_publish = True%0A
 %0A%0Ade
@@ -2230,24 +2230,26 @@
  main():%0A   
+ #
  thread = th
@@ -2337,16 +2337,18 @@
 True%0A   
+ #
  thread.
@@ -2356,16 +2356,68 @@
 tart()%0A%0A
+    ws_messenger = PublishMediaFrameworkMessages()%0A%0A
 if __nam
@@ -2441,12 +2441,13 @@
 :%0A    main()
+%0A

rework threads and reading from port