import json

from typing import List

from devicemanager.interactor.dto import DeviceStatusDto


class DeviceStatusFormatter(object):
    @staticmethod
    def format_as_json(
        devices,  # type: List[DeviceStatusDto]
    ):
        # type: (...) -> bytes

        ret = []
        for device in devices:
            response_device = {
                "deviceType": device.type,
                "deviceName": device.name,
                "lastUpdateOperator": device.last_update_operator,
                "lastUpdateTime": device.last_update.isoformat() if device.last_update is not None else None,
                "enabled": device.enabled,
            }
            ret.append(response_device)

        return json.dumps(
            obj={
                "devices": ret,
            },
            separators=(",", ":")
        ).encode("utf-8")
