import grpc
import sdr_command_pb2
import sdr_command_pb2_grpc

import transmitter_pb2
import radio_config_pb2
import observation_pb2
import flowgraph_config_pb2
import flowgraph_dispatcher_service_pb2_grpc

import google.protobuf.json_format as json_format

def run():
    print("Will try to send command ...")
    with grpc.insecure_channel("localhost:50051") as channel:
        stub = flowgraph_dispatcher_service_pb2_grpc.FlowgraphDispatcherServiceStub(channel)
  
        transmission_mode = transmitter_pb2.TransmissionMode()
        # Unnecessary setting of observation parameters before stopping the observation


        transmission_mode.name = transmitter_pb2.TransmissionMode.FSK
        transmission_mode.modulation.fsk.baudrate = 50e3
        transmission_mode.modulation.fsk.modulation_index = 1
        transmission_mode.framing.ieee802_15_4.generic_options.coding.rs.block_len = 223
        transmission_mode.framing.ieee802_15_4.generic_options.coding.rs.message_len = 255

        transmitter = transmitter_pb2.Transmitter()
        transmitter.name = "PHASMA"
        transmitter.center_freq = 435e6
        transmitter.transmission_mode.append(transmission_mode)

        radio = radio_config_pb2.RadioConfig()
        radio.antenna_name = "RX"
        radio.dev_driver = "rtlsdr"
        radio.freq = 435e6
        radio.gain = 20
        radio.samp_rate = 2.048e6
        radio.channels = 1

        flowgraph = flowgraph_config_pb2.FlowgraphConfig()
        flowgraph.sps = 4
        lo_offset = 100e3

        observation = observation_pb2.Observation()
        # Don't add them to the observation message
        #observation.radio_config.CopyFrom(radio)
        #observation.transmitter.CopyFrom(transmitter)
        #observation.flowgraph_config.CopyFrom(flowgraph)
        #observation.tx_rx = observation_pb2.Observation.RX
        
        # Send command to stop observation
        response = stub.StopObservationService(observation)
        # Check response message
        if (not response.valid):
            print("Client received: Error")
            print(response.error_msg)
        else:
            print("Client received: " + str(response.valid))
  

if __name__ == "__main__":
    run()
