private void DiscoverGatewayAndDevices(ResponseCommand cmd) { if (_gatewaySid == null) { if (_gateway == null) { _gateway = new Gateway(cmd.Sid, _transport); } _transport.SetToken(cmd.Token); } else if (_gatewaySid == cmd.Sid) { _gateway = new Gateway(cmd.Sid, _transport); _transport.SetToken(cmd.Token); } if (_gateway == null) { return; } _transport.SendCommand(new ReadDeviceCommand(cmd.Sid)); foreach (var sid in JArray.Parse(cmd.Data)) { _transport.SendCommand(new ReadDeviceCommand(sid.ToString())); Task.Delay(ReadDeviceInterval).Wait(); // need some time in order not to loose message } }
public MiHome(string gatewayPassword = null, string gatewaySid = null) { _commandsToActions = new Dictionary <string, Action <ResponseCommand> > { { "get_id_list_ack", DiscoverGatewayAndDevices }, { "read_ack", ProcessReadAck }, { "heartbeat", ProcessHeartbeat }, { "report", ProcessReport }, }; _gatewaySid = gatewaySid; _transport = new UdpTransport(gatewayPassword); _devicesMap = new Dictionary <string, Func <string, MiHomeDevice> > { { "sensor_ht", sid => new ThSensor(sid) }, { "weather.v1", sid => new WeatherSensor(sid) }, { "motion", sid => new MotionSensor(sid) }, { "plug", sid => new SocketPlug(sid, _transport) }, { "magnet", sid => new DoorWindowSensor(sid) }, { "sensor_wleak.aq1", sid => new WaterLeakSensor(sid) }, { "smoke", sid => new SmokeSensor(sid) }, { "switch", sid => new Switch(sid) }, { "sensor_switch.aq2", sid => new Switch2(sid) }, { "ctrl_neutral2", sid => new WiredDualWallSwitch(sid) }, { "remote.b286acn01", sid => new WirelessDualWallSwitch(sid) }, }; _receiveTask = Task.Run(() => StartReceivingMessages(_cts.Token), _cts.Token); _transport.SendCommand(new DiscoverGatewayCommand()); }
public MiHome(string gatewayPassword = null, string gatewaySid = null) { _commandsToActions = new Dictionary <string, Action <ResponseCommand> > { { "get_id_list_ack", DiscoverGatewayAndDevices }, { "read_ack", ProcessReadAck }, { "heartbeat", ProcessHeartbeat }, { "report", ProcessReport }, }; _gatewaySid = gatewaySid; _transport = new UdpTransport(gatewayPassword); _receiveTask = Task.Run(() => StartReceivingMessages(_cts.Token), _cts.Token); _transport.SendCommand(new DiscoverGatewayCommand()); }