import asyncio
from aiohttp import web
import socketio
import hexdump
from log import logname
import frame
from hardware import Hardware
from version import version_info
import os

logger = logname("sockets")

class WSnamespace(socketio.AsyncNamespace):
    def __init__(self, namespace='/sockets'):
        super().__init__(namespace)
        self.sio = None
        self.hw = Hardware()

    async def on_connect(self, sid, environ):
        logger.info("connected %s", sid)
        await self.sio.emit('connected', {
            'tcs_ver' : version_info,
            'firmware_ver' : self.hw.getFirmwareVersion(),
            'wifi_dongle' : self.hw.getWirelessAdapterInfo(),
            'video_devices': self.hw.getCameraInfo()
        }, namespace="/sockets")


    async def on_motors(self, sid, payload):
        self.hw.setMotors(payload)
        await self.sio.emit('response', "motors set", namespace="/sockets")

    async def on_manipulator(self, sid, payload):
        self.hw.setManipulator(payload)
        await self.sio.emit('response', 'manipulator set', namespace="/sockets")

    async def on_gripper(self, sid, payload):
        self.hw.setGripper(payload)
        await self.sio.emit('response', 'gripper set', namespace="/sockets")

    async def on_battery(self, sid):
        battery_status =  self.hw.getBattery()
        await self.sio.emit('battery', battery_status, namespace="/sockets")

    async def on_signal(self, sid):
        signal_strength =  self.hw.getSignal()
        await self.sio.emit('signal', signal_strength, namespace="/sockets")

    async def on_temperature(self, sid):
        temperature =  self.hw.getTemperature()
        await self.sio.emit('temperature', temperature, namespace="/sockets")

    async def on_system_shutdown(self, sid):
        os.system('poweroff') 


class WSserver():
    def __init__(self, app):
        super().__init__()
        self.sio = None
        self.app = app
        self.namespace = WSnamespace('/sockets')
    
    def start(self):
        self.sio = socketio.AsyncServer(async_mode='aiohttp')
        self.sio.register_namespace(self.namespace)
        self.namespace.sio = self.sio
        self.sio.attach(self.app)


import asyncio
from aiohttp import web
import socketio
import hexdump
from log import logname
import frame
from hardware import Hardware
from version import version_info
import os
import subprocess

logger = logname("sockets")

class WSnamespace(socketio.AsyncNamespace):
    def __init__(self, namespace='/sockets'):
        super().__init__(namespace)
        self.sio = None
        self.hw = Hardware()

    async def on_connect(self, sid, environ):
        logger.info("connected %s", sid)
        await self.sio.emit('connected', {
            'tcs_ver' : version_info,
            'firmware_ver' : self.hw.getFirmwareVersion(),
            'wifi_dongle' : self.hw.getWirelessAdapterInfo(),
            'video_devices': self.hw.getCameraInfo()
        }, namespace="/sockets")


    async def on_motors(self, sid, payload):
        self.hw.setMotors(payload)
        await self.sio.emit('response', "motors set", namespace="/sockets")

    async def on_manipulator(self, sid, payload):
        self.hw.setManipulator(payload)
        await self.sio.emit('response', 'manipulator set', namespace="/sockets")

    async def on_gripper(self, sid, payload):
        self.hw.setGripper(payload)
        await self.sio.emit('response', 'gripper set', namespace="/sockets")

    async def on_battery(self, sid):
        battery_status =  self.hw.getBattery()
        await self.sio.emit('battery', battery_status, namespace="/sockets")

    async def on_signal(self, sid):
        signal_strength =  self.hw.getSignal()
        await self.sio.emit('signal', signal_strength, namespace="/sockets")

    async def on_temperature(self, sid):
        temperature =  self.hw.getTemperature()
        await self.sio.emit('temperature', temperature, namespace="/sockets")

    async def on_shutdown(self, sid):
        subprocess.run(['poweroff'])


class WSserver():
    def __init__(self, app):
        super().__init__()
        self.sio = None
        self.app = app
        self.namespace = WSnamespace('/sockets')
    
    def start(self):
        self.sio = socketio.AsyncServer(async_mode='aiohttp')
        self.sio.register_namespace(self.namespace)
        self.namespace.sio = self.sio
        self.sio.attach(self.app)


fix shutdown command