-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
209 lines (179 loc) · 7.45 KB
/
Copy pathProgram.cs
File metadata and controls
209 lines (179 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;
namespace CollectDataAP
{
class Program
{
static private AppServiceConnection connection = null;
static void Main(string[] args)
{
InitializeAppServiceConnection();
string s_res;
Console.WriteLine("[1] send data to UWP");
while ((s_res = Console.ReadLine()) != "")
{
int i_res=int.Parse(s_res);
if (i_res == 1)
{
Send2UWP_2("Hi!", "UWP");
}
}
}
/// <summary>
/// Open connection to UWP app service
/// </summary>
static private async void InitializeAppServiceConnection()
{
connection = new AppServiceConnection();
connection.AppServiceName = "SampleInteropService";
connection.PackageFamilyName = Package.Current.Id.FamilyName;
connection.RequestReceived += Connection_RequestReceived;
connection.ServiceClosed += Connection_ServiceClosed;
AppServiceConnectionStatus status = await connection.OpenAsync();
if (status != AppServiceConnectionStatus.Success)
{
//In console, something wrong
Console.WriteLine(status.ToString());
//In app, something went wrong ...
//MessageBox.Show(status.ToString());
//this.IsEnabled = false;
}
}
/// <summary>
/// Handles the event when the desktop process receives a request from the UWP app
/// </summary>
static private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
// retrive the reg key name from the ValueSet in the request
/*
string key = args.Request.Message["KEY"] as string;
if (key == "abcd")
{
// compose the response as ValueSet
ValueSet response = new ValueSet();
response.Add("GOOD", "KEY IS FOUNDED ^^");
// send the response back to the UWP
await args.Request.SendResponseAsync(response);
}
else
{
ValueSet response = new ValueSet();
response.Add("ERROR", "INVALID REQUEST");
await args.Request.SendResponseAsync(response);
}
*/
int? key1 = args.Request.Message["KEY1"] as int?;
int? key2 = args.Request.Message["KEY2"] as int?;
if (key1 != null && key2 != null)
{
int ans = (int)key1 + (int)key2;
// compose the response as ValueSet
ValueSet response = new ValueSet();
response.Add("KEY3", ans);
// send the response back to the UWP
await args.Request.SendResponseAsync(response);
}
}
//multiple key
/*
/// <summary>
/// Handles the event when the desktop process receives a request from the UWP app
/// </summary>
private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
Console.WriteLine("Connection_RequestReceived");
foreach (object key in args.Request.Message.Keys)
{
if ((string)key == "BasicInfo")
{
Console.WriteLine("BasicInfo");
GetInformation getInformation = new GetInformation();
getInformation.InitializeWMIHandler();
if (getInformation.InitGlobalVariable() == 0) //have errpr
{
// compose the response as ValueSet
ValueSet response = new ValueSet();
response.Add("BasicInfo2UWP", "Error! Please restart.");
// send the response back to the UWP
await args.Request.SendResponseAsync(response);
}
else
{
string basicInfo = getInformation.GetInfomation();
// compose the response as ValueSet
ValueSet response = new ValueSet();
response.Add("BasicInfo2UWP", basicInfo);
// send the response back to the UWP
await args.Request.SendResponseAsync(response);
}
}
else if ((string)key == "deviceConfig")
{
DeviceState deviceState = new DeviceState();
uint? deviceCode;
uint state = deviceState.GetDeviceStatePower();
deviceCode = args.Request.Message["deviceConfig"] as uint?;
try
{
foreach (uint device in Enum.GetValues(typeof(DeviceState.DeviceStatePower)))
{
if ((deviceCode & device) == device)
{
state = state ^ (uint)deviceCode;
deviceState.SetDeviceStatePower(state);
}
}
}
}
}
}
*/
/// <summary>
/// Handles the event when the app service connection is closed
/// </summary>
static private void Connection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
{
//In console, connection to the UWP lost, so we shut down the desktop process
Console.WriteLine("UWP lost connection! Please restart.");
Console.ReadLine();
Environment.Exit(0);
//In app, connection to the UWP lost, so we shut down the desktop process
//Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
//{
// Application.Current.Shutdown();
//}));
}
static private async void Send2UWP(double a, double b)
{
// ask the UWP to calculate d1 + d2
ValueSet request = new ValueSet();
request.Add("D1", a);
request.Add("D2", b);
//start sending
AppServiceResponse response = await connection.SendMessageAsync(request);
//get response
double result = (double)response.Message["RESULT"];
Console.WriteLine(result.ToString());
}
static private async void Send2UWP_2(string a, string b)
{
// ask the UWP to calculate d1 + d2
ValueSet request = new ValueSet();
request.Add("s_a", a);
request.Add("s_b", b);
//start sending
AppServiceResponse response = await connection.SendMessageAsync(request);
//get response
string result = response.Message["toConsole_result"] as string;
Console.WriteLine("send data_a to UWP: " + a);
Console.WriteLine("send data_b to UWP: " + b);
Console.WriteLine("getting data from UWP: " + result);
}
}
}