Skip to content

Commit

Permalink
Simplify the operator code & tests a lot. Work on adding events to we…
Browse files Browse the repository at this point in the history
…b-client
  • Loading branch information
locka99 committed Jun 13, 2019
1 parent d39f32e commit 3ef15ff
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 136 deletions.
11 changes: 7 additions & 4 deletions samples/web-client/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@

function handle_event(e) {
let msg = JSON.parse(e);
if ("DataChangeEvent" in msg) {
let data = msg.DataChangeEvent;
if ("DataChange" in msg) {
let data = msg.DataChange;
if (Array.isArray(data)) {
data.forEach(element => {
let value_obj = element.value.value;
Expand All @@ -118,8 +118,10 @@
let value = entry[1];
$('#values > tbody:last-child').append(`<tr><td>${node_id}</td><td>${value}</td></tr>`);
});
} else if ("ConnectionStatusChangeEvent" in msg) {
} else if ("ConnectionStatusChange" in msg) {
connection_status_change(msg.ConnectionStatusChangeEvent);
} else if ("Event" in msg) {
// TODO event
}

}
Expand Down Expand Up @@ -204,7 +206,8 @@ <h2>Events</h2>
</td>
</tr>
</table>
<p>Note: Any Operand containing / will be treated as a path to an Object or Variable on the server leading from the BaseEventType.</p>
<p>Note: Any Operand containing / will be treated as a path to an Object or Variable on the server leading from the
BaseEventType.</p>
</form>

<h1>Notifications</h1>
Expand Down
27 changes: 19 additions & 8 deletions samples/web-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ impl Message for DataChangeEvent {

#[derive(Serialize, Message)]
enum Event {
ConnectionStatusChangeEvent(bool),
DataChangeEvent(Vec<DataChangeEvent>),
ConnectionStatusChange(bool),
DataChange(Vec<DataChangeEvent>),
Event(Vec<EventFieldList>),
}

const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
Expand Down Expand Up @@ -96,8 +97,9 @@ impl Handler<Event> for OPCUASession {
// This is where we receive OPC UA events. It is here they are turned into JSON
// and sent to the attached web socket.
println!("Received event {}", match &msg {
Event::ConnectionStatusChangeEvent(ref connected) => format!("ConnectionStatusChangeEvent({})", connected),
Event::DataChangeEvent(_) => "DataChangeEvent".to_string()
Event::ConnectionStatusChange(ref connected) => format!("ConnectionStatusChangeEvent({})", connected),
Event::DataChange(_) => "DataChangeEvent".to_string(),
Event::Event(_) => "EventEvent".to_string()
});
ctx.text(serde_json::to_string(&msg).unwrap())
}
Expand Down Expand Up @@ -162,7 +164,7 @@ impl OPCUASession {
let addr_for_connection_status_change = addr.clone();
session.set_connection_status_callback(ConnectionStatusCallback::new(move |connected| {
println!("Connection status has changed to {}", if connected { "connected" } else { "disconnected" });
addr_for_connection_status_change.do_send(Event::ConnectionStatusChangeEvent(connected));
addr_for_connection_status_change.do_send(Event::ConnectionStatusChange(connected));
}));
session.set_session_closed_callback(SessionClosedCallback::new(|status| {
println!("Session has been closed, status = {}", status);
Expand All @@ -176,7 +178,7 @@ impl OPCUASession {
false
}
};
addr.do_send(Event::ConnectionStatusChangeEvent(connected));
addr.do_send(Event::ConnectionStatusChange(connected));
}

fn disconnect(&mut self, _ctx: &mut <Self as Actor>::Context) {
Expand Down Expand Up @@ -236,6 +238,16 @@ impl OPCUASession {
};

// Select clause

let addr_for_events = ctx.address();

// TODO create a subscription containing events

let callback = EventCallback::new(move |events| {
// Handle events
let events = events.events.unwrap();
addr_for_events.do_send(Event::Event(events));
});
}

fn subscribe(&mut self, ctx: &mut <Self as Actor>::Context, node_ids: Vec<String>) {
Expand All @@ -261,9 +273,8 @@ impl OPCUASession {
value: item.value().clone(),
}
}).collect::<Vec<_>>();

// Send the changes to the websocket session
addr_for_datachange.do_send(Event::DataChangeEvent(changes));
addr_for_datachange.do_send(Event::DataChange(changes));
})).unwrap();
println!("Created a subscription with id = {}", subscription_id);
// Create some monitored items
Expand Down
Loading

0 comments on commit 3ef15ff

Please sign in to comment.