# -*- python -*-

from endless.project_1.can_switch import CANSwitch
from endless.project_1.can_temp_sensor import CAN_TemperatureSensor
from endless.project_1.can_hum_temp_sensor import CAN_HumidityTemperatureSensor
from endless.project_1.hum_temp_control import HumidityTemperature2Control
from endless.project_1.mqtt_helper import transform_hum_temp_to_json, transform_temp_to_json
from endless.project_1.dbus_server import DBusServer

from endless.framework.can_reader import CANReader
from endless.framework.can_writer import CANWriter
from endless.framework.mqtt import MQTTClient, MQTT_PublishSampleTagToTopic
from endless.framework.sample_broadcaster import SampleBroadcaster
from endless.framework.sample_filter import SampleFilter
from endless.framework.sample_history import SampleHistory
from endless.framework.hysteresis import Hysteresis
from endless.framework.switch_stdout import StdoutSwitch
from endless.framework.sink_stdout import StdoutSink


# CAN_IFACE = 'can0'
CAN_IFACE = 'mein-test-can'

# MQTT_ADDR = '192.168.220.142'
MQTT_ADDR = '127.0.0.1'


COMPONENTS = []

can_reader = CANReader(CAN_IFACE)
humtemp_sensor_0x33 = CAN_HumidityTemperatureSensor(can_id=0x33, tag='CAN@0x33')
humtemp_sensor_0x34 = CAN_HumidityTemperatureSensor(can_id=0x34, tag='CAN@0x34')
temp_sensor_0x42 = CAN_TemperatureSensor(can_id=0x42, tag='CAN@0x42')
COMPONENTS.extend([can_reader, humtemp_sensor_0x33, humtemp_sensor_0x34, temp_sensor_0x42])

can_reader.frame_out.connect(humtemp_sensor_0x33.can_in)
can_reader.frame_out.connect(humtemp_sensor_0x34.can_in)

humtemp_sensor_0x33.sample_out.connect(history_0x33.sample_in)
humtemp_sensor_0x34.sample_out.connect(history_0x34.sample_in)

broadcaster = SampleBroadcaster()
COMPONENTS.append(broadcaster)

history_0x33.sample_out.connect(broadcaster.sample_in)
history_0x34.sample_out.connect(broadcaster.sample_in)

# broadcast_debug = StdoutSink(prefix='AFTER BROADCAST')
# COMPONENTS.append(broadcast_debug)
# broadcaster.sample_out.connect(broadcast_debug.sample_in)

can33_filter = SampleFilter(lambda sample: sample if sample.tag == 'CAN@0x33' else None)
COMPONENTS.append(can33_filter)
broadcaster.sample_out.connect(can33_filter.sample_in)

humtemp2ctl = HumidityTemperature2Control()
COMPONENTS.append(humtemp2ctl)
can33_filter.sample_out.connect(humtemp2ctl.sample_in)

hysteresis = Hysteresis(20, 40)
COMPONENTS.append(hysteresis)
humtemp2ctl.control.connect(hysteresis.control)

# fixme: SwitchBroadcaster (send switch notifications over mqtt)

# switch = StdoutSwitch('Hi Switch:')
# COMPONENTS.append(switch)
# hysteresis.switch.connect(switch.switch)

switch = CANSwitch(can_id=0x40, number=3)
COMPONENTS.append(switch)
hysteresis.switch.connect(switch.switch)

can_writer = CANWriter(can_iface=CAN_IFACE)
COMPONENTS.append(can_writer)
switch.frame_out.connect(can_writer.frame_in)

humtemp2json = SampleFilter(transform_hum_temp_to_json)
COMPONENTS.append(humtemp2json)
broadcaster.sample_out.connect(humtemp2json.sample_in)

temp2json = SampleFilter(transform_temp_to_json)
COMPONENTS.append(temp2json)
temp_sensor_0x42.sample_out.connect(temp2json.sample_in)

publish_by_tag = MQTT_PublishSampleTagToTopic({
    'CAN@0x33': 'can-0x33',
    'CAN@0x34': 'can-0x34',
    'CAN@0x42': 'can-0x42',
})
COMPONENTS.append(publish_by_tag)
humtemp2json.sample_out.connect(publish_by_tag.sample_in)
temp2json.sample_out.connect(publish_by_tag.sample_in)

mqtt = MQTTClient(host=MQTT_ADDR)
COMPONENTS.append(mqtt)
publish_by_tag.publisher.connect(mqtt.publisher)

dbus_server = DBusServer(busname='org.project_1.Things')
COMPONENTS.append(dbus_server)
dbus_server.switch_counter.connect(switch.counter)
dbus_server.hysteresis_config.connect(hysteresis.config)
dbus_server.measurements_controllerA.connect(history_0x33.sample_list)
dbus_server.measurements_controllerB.connect(history_0x34.sample_list)
