static async Task <MessageResponse> ModuleLoadedMessageHandler(Message msg, object ctx) { AzureModule module = (AzureModule)ctx; var msgBytes = msg.GetBytes(); var msgString = Encoding.UTF8.GetString(msgBytes); Log.WriteLine("loadModule msg received: '{0}'", msgString); var loadMsg = JsonConvert.DeserializeObject <ModuleLoadedMessage>(msgString); await module.ProcessModuleLoadedMessage(loadMsg); return(MessageResponse.Completed); }
private async Task <MethodResponse> SetModuleLoaded(MethodRequest req, object context) { string data = Encoding.UTF8.GetString(req.Data); Log.WriteLine("Direct Method ModuleLoaded {0}", data); var loadMsg = JsonConvert.DeserializeObject <ModuleLoadedMessage>(data); AzureModule module = (AzureModule)context; await module.ProcessModuleLoadedMessage(loadMsg); // Acknowlege the direct method call with a 200 success message string result = "{\"result\":\"Executed direct method: " + req.Name + "\"}"; return(new MethodResponse(Encoding.UTF8.GetBytes(result), 200)); }
static async Task <int> MainAsync(AppOptions options) { //Log.WriteLine("pause..."); //var x = Console.ReadLine(); Log.WriteLine("Starting async..."); Model model = null; AzureConnection connection = null; MediaFrameReader reader = null; EventWaitHandle evtFrame = null; if (options.List) { await EnumFrameSourcesAsync(); Environment.Exit(3); } await Task.WhenAll( Task.Run(async() => { try { model = await Model.CreateModelAsync( Directory.GetCurrentDirectory() + "\\resources\\office_fruit.onnx", options.Gpu); if (options.Test) { await Task.CompletedTask; } else { connection = await AzureConnection.CreateAzureConnectionAsync(); } } catch (Exception e) { Log.WriteLineError("failed to create model {0}", e.ToString()); Environment.Exit(2); } }), Task.Run(async() => { try { (reader, evtFrame) = await GetFrameReaderAsync(); await AsyncHelper.AsAsync(reader.StartAsync()); } catch (Exception e) { Log.WriteLineError("failed to start frame reader {0}", e.ToString()); Environment.Exit(2); } })); try { AzureModule m = null; EventHandler <string> ModuleLoadedHandler = async(Object sender, string moduleName) => { try { Log.WriteLine("module loaded. resending state"); await connection.NotifyNewModuleOfCurrentStateAsync(); } catch (Exception e) { Log.WriteLineError("failed to notify state {0}", e.ToString()); Environment.Exit(2); } }; if (connection != null) { m = (AzureModule)connection.Module; m.ModuleLoaded += ModuleLoadedHandler; } try { Log.WriteLine("Model loaded, Azure Connection created, and FrameReader Started\n\n\n\n"); await CameraProcessingAsync(model, reader, evtFrame, connection); } finally { if (connection != null) { m.ModuleLoaded -= ModuleLoadedHandler; } } } finally { if (connection != null) { connection.Dispose(); } reader.Dispose(); model.Dispose(); } return(0); }