Skip to content

Commit

Permalink
Added examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 24, 2018
1 parent 72fdb86 commit cc4df3d
Show file tree
Hide file tree
Showing 25 changed files with 1,474 additions and 17 deletions.
47 changes: 46 additions & 1 deletion OpenProtocol.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "openprotocol-web-viewer", "ts\examples\openprotocol-web-viewer\openprotocol-web-viewer.njsproj", "{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TS", "TS", "{28734BB6-E0DE-45BB-8A37-2721DF3E06C1}"
EndProject
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "openprotocol-typescript", "ts\src\openprotocol-typescript.njsproj", "{D00C84D2-6A9E-48B5-83F5-8606DABD88C3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{1F4390A6-F3F1-48DE-AFB0-F65967B0BC6F}"
ProjectSection(SolutionItems) = preProject
ts\lib\iChen.OpenProtocol.d.ts = ts\lib\iChen.OpenProtocol.d.ts
ts\lib\iChen.OpenProtocol.js = ts\lib\iChen.OpenProtocol.js
ts\lib\iChen.OpenProtocol.js.map = ts\lib\iChen.OpenProtocol.js.map
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JS", "JS", "{1390BBC3-CF80-49D3-B704-F577637BED6F}"
EndProject
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "openprotocol-web-viewer", "js\examples\openprotocol-web-viewer\openprotocol-web-viewer.njsproj", "{03AA84B0-3284-41C7-AA8C-C5C236C90F4F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F67114DC-715E-4B8E-9729-2A50141A6A7F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{AB85C87A-C351-41D1-81DF-62840758A465}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{CA718389-F7C2-4AE1-BAFB-9835E8EE7952}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{C3D777D9-FD95-44A6-B55A-499F1A7CBDF8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,13 +59,33 @@ Global
{485B1DD2-5C91-4993-BEDD-98BF26630293}.Debug|Any CPU.Build.0 = Debug|Any CPU
{485B1DD2-5C91-4993-BEDD-98BF26630293}.Release|Any CPU.ActiveCfg = Release|Any CPU
{485B1DD2-5C91-4993-BEDD-98BF26630293}.Release|Any CPU.Build.0 = Release|Any CPU
{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15}.Release|Any CPU.Build.0 = Release|Any CPU
{D00C84D2-6A9E-48B5-83F5-8606DABD88C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D00C84D2-6A9E-48B5-83F5-8606DABD88C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D00C84D2-6A9E-48B5-83F5-8606DABD88C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D00C84D2-6A9E-48B5-83F5-8606DABD88C3}.Release|Any CPU.Build.0 = Release|Any CPU
{03AA84B0-3284-41C7-AA8C-C5C236C90F4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03AA84B0-3284-41C7-AA8C-C5C236C90F4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03AA84B0-3284-41C7-AA8C-C5C236C90F4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03AA84B0-3284-41C7-AA8C-C5C236C90F4F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E22B3199-3391-4246-8951-E301947FD3A6} = {489158CE-BA24-44E4-B37A-13BDC6D3A39B}
{485B1DD2-5C91-4993-BEDD-98BF26630293} = {489158CE-BA24-44E4-B37A-13BDC6D3A39B}
{485B1DD2-5C91-4993-BEDD-98BF26630293} = {AB85C87A-C351-41D1-81DF-62840758A465}
{54EB1BE0-7AB9-4D02-9AA1-AA12606E9C15} = {CA718389-F7C2-4AE1-BAFB-9835E8EE7952}
{D00C84D2-6A9E-48B5-83F5-8606DABD88C3} = {F67114DC-715E-4B8E-9729-2A50141A6A7F}
{1F4390A6-F3F1-48DE-AFB0-F65967B0BC6F} = {28734BB6-E0DE-45BB-8A37-2721DF3E06C1}
{03AA84B0-3284-41C7-AA8C-C5C236C90F4F} = {C3D777D9-FD95-44A6-B55A-499F1A7CBDF8}
{F67114DC-715E-4B8E-9729-2A50141A6A7F} = {28734BB6-E0DE-45BB-8A37-2721DF3E06C1}
{AB85C87A-C351-41D1-81DF-62840758A465} = {489158CE-BA24-44E4-B37A-13BDC6D3A39B}
{CA718389-F7C2-4AE1-BAFB-9835E8EE7952} = {28734BB6-E0DE-45BB-8A37-2721DF3E06C1}
{C3D777D9-FD95-44A6-B55A-499F1A7CBDF8} = {1390BBC3-CF80-49D3-B704-F577637BED6F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {580993A6-3410-42FB-96F4-982B654AA74D}
Expand Down
8 changes: 8 additions & 0 deletions cs/OpenProtocol/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OpenProtocol™ .Net Interface Library

Target Runtime: .NET Standard 1.6

.Net assembly to interface with the iChen® System using OpenProtocol™.

This assembly is written in C# but supports all languages supported by .NET.

15 changes: 2 additions & 13 deletions cs/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# OpenProtocol™ .NET Library
# OpenProtocol™ .NET Interface Library

.Net assembly to interface with the iChen® System using OpenProtocol™.

This assembly is written in C# but supports all languages supported by .NET.
The assembly is written in C# but supports all languages supported by .NET.
This assembly targets the .NET Standard Framework version 1.6.


## Example Programs

### OpenProtocolViewer

Location: `examples/OpenProtrocolViewer`

A C# console example program that connects to an iChen® server using OpenProtocol™.
Messages are displayed on-screen.
It also acts as a user authenticator and job cards provider for testing the relevant features.
3 changes: 2 additions & 1 deletion cs/examples/OpenProtocolViewer/OpenProtocolViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -58,6 +58,7 @@
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\OpenProtocol\OpenProtocol.csproj">
Expand Down
2 changes: 1 addition & 1 deletion cs/examples/OpenProtocolViewer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static partial class Program
{
private static void Main ()
{
Console.WriteLine("iChen 4.0 Open Protocol Viewer");
Console.WriteLine("iChen 4.1 Open Protocol Viewer");
Console.WriteLine();

// Read parameters
Expand Down
21 changes: 21 additions & 0 deletions cs/examples/OpenProtocolViewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OpenProtocol&trade; Viewer (C# Console)

.NET Framework Version: 4.6.1 or up

This is a simple console application written in C# that connects to an iChen&reg; System using OpenProtocol&trade;.

OpenProtocol&trade; messages to and from the server are logged on-screen.

This application also acts as a user authentication and job cards provider to test out the operator login and job card features.

## How to Use

Launch `OpenProtocolViewer.exe` in and enter the following information:

**`WebSocket URL`** : URL of the OpenProtocol&trade; interface, usually `ws://MyiChenServerUrl:5788` or `ws://x.x.x.x:5788` (5788 is the default OpenProtocol&trade; interface port).

**`Password`** : A login password to connect to the system. System default is `chenhsong` for the `admin` user with unlimited admin rights (other than MIS rights).
To try out the MIS features (e.g. operator login, job cards), first set up a new user account with the appropriate rights, then login with that password.
Otherwise, the user authentication and job cards provider will not work.

_Warning: If you do not enter a password of a user account that has the appropriate access rights, you'll fail to see all OpenProtocol&trade; messages._
2 changes: 1 addition & 1 deletion cs/examples/OpenProtocolViewer/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
</packages>
33 changes: 33 additions & 0 deletions js/examples/openprotocol-web-viewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# OpenProtocol&trade; Web Viewer (JavaScript)

Language Version: ECMAScript 3

This is a simple web application written in JavaScript that connects to an iChen&reg; System using OpenProtocol&trade;.

OpenProtocol&trade; messages to and from the server are logged on-screen.

This application also acts as a user authentication and job cards provider to test out the operator login and job card features.

## How to Use

### 1. Enter Credentials

Launch `index.html` in a web browser and enter the following information:

**`URL`** : URL of the OpenProtocol&trade; interface, usually `ws://MyiChenServerUrl:5788` or `ws://x.x.x.x:5788` (5788 is the default OpenProtocol&trade; interface port).

**`Password`** : A login password to connect to the system. System default is `chenhsong` for the `admin` user with unlimited admin rights (other than MIS rights).
To try out the MIS features (e.g. operator login, job cards), first set up a new user account with the appropriate rights, then login with that password.
Otherwise, the user authentication and job cards provider will not work.

_Warning: If you do not enter a password of a user account that has the appropriate access rights, you'll fail to see all OpenProtocol&trade; messages._

### 2. Connect

Next, press the `Connect to Server` button to connect to the iChen&reg; System server.

### 3. Other Features

**`Get Mold Data`** reads the current state of the recipe (mold data set) on the controller specified.

**`Read Mold Value`** reads the current value of a particular variable on the controller specified.
181 changes: 181 additions & 0 deletions js/examples/openprotocol-web-viewer/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
document.addEventListener("DOMContentLoaded", function ()
{
/// Mock job scheduling system
var Jobs = [
{ jobCardId: "JOB_CARD_1", moldId: "MOULD_001", progress: 0, total: 8000 },
{ jobCardId: "JOB_CARD_2", moldId: "MOULD_002", progress: 2000, total: 10000 },
{ jobCardId: "JOB_CARD_3", moldId: "MOULD_003", progress: 888, total: 3333 },
{ jobCardId: "JOB_CARD_4", moldId: "MOULD_004", progress: 123, total: 45678 }
];

// DOM Elements
var txtUrl = document.getElementById("txtUrl");
var txtPwd = document.getElementById("txtPwd");
var btnConnect = document.getElementById("btnConnect");
var txtId = document.getElementById("txtId");
var btnGetMoldData = document.getElementById("btnGetMoldData");
var txtField = document.getElementById("txtField");
var btnReadMoldData = document.getElementById("btnReadMoldData");
var divMessages = document.getElementById("messages");

// Hook up buttons
btnConnect.addEventListener("click", run);
btnGetMoldData.addEventListener("click", function () { return getMoldData(parseInt(txtId.value, 10)); });
btnReadMoldData.addEventListener("click", function () { return readMoldData(parseInt(txtId.value, 10), txtField.value); });

// Log text to page
function logText(type, text)
{
var div = document.createElement("div");
div.className = type;

if (type === "message" || type === "command") {
var code = document.createElement("code");
code.textContent = text;
div.appendChild(code);
} else {
div.textContent = text;
}

divMessages.appendChild(div);
}

// Global variables
var seq = 0;
var ws;
var handle = 0;

// Send a message via WebSocket
function sendMessage(msg, type)
{
if (!type) type = "command";
var json = JSON.stringify(msg);
logText(type, "Sent: " + json);
ws.send(json);
}

// Main loop
function run()
{
var url = txtUrl.value.trim(); // WebSocket URL
var password = txtPwd.value.trim(); // Password

if (!url) {
alert('Please enter a valid WebSocket URL in the format "ws://host:port" or "wss://host:port". Example: "ws://192.168.1.1:5788".');
txtUrl.focus();
return;
}
if (!/^wss?\:\/\/.*\:\d+$/.test(url)) {
alert('Please enter a valid WebSocket URL in the format "ws://host:port" or "wss://host:port". Example: "ws://192.168.1.1:5788".');
txtUrl.focus();
return;
}
if (!password) {
alert("Please enter a valid password.");
txtPwd.focus();
return;
}

// Enable/disable UI elements
txtUrl.disabled = txtPwd.disabled = btnConnect.disabled = true;
txtId.disabled = btnGetMoldData.disabled = false;
txtField.disabled = btnReadMoldData.disabled = false;

logText("info", "Connecting to iChen 4.0 Server at " + url + "...");

// Create a WebSocket connection to the server
ws = new WebSocket(url);

ws.onopen = function ()
{
// Initialize handshake with server
logText("info", "WebSocket connection established.");

// Send a JOIN message
sendMessage({ $type: "Join", language: "EN", version: "1.0", password: password, filter: "All, JobCards, Operators", sequence: ++seq });

loopHandle = setInterval(function () { sendMessage({ $type: "Alive", sequence: ++seq, priority: -10 }); }, 5000);
};

// Wire up WebSocket events
ws.onerror = function (ev)
{
console.error(ev);
logText("error", "An error has occurred!");
};
ws.onclose = function (ev)
{
logText("info", "WebSocket connection to iChen 4.0 Server is closed.");
logText("info", "Code = " + ev.code + ", Reason = " + ev.reason);
clearInterval(loopHandle);
};
ws.onmessage = function (msg)
{
try {
logText("message", "Received: " + msg.data);
var reply_message = handleMessage(JSON.parse(msg.data));
if (reply_message) sendMessage(reply_message, "reply");
} catch (err) { console.error(err); }
};
}

// Handle message
function handleMessage(message)
{
switch (message.$type) {
case "JoinResponse": {
// Send a REQ_CNTRLER_LIST message
sendMessage({ $type: "RequestControllersList", sequence: ++seq });
return null;
}

case "LoginOperator": {
// MIS integration - return access level
// Popup prompt to ask for the access level
var resp = prompt("User on machine " + message.controllerId + " tries to login with password " + message.password + ". Access level (0-10)?");

// Pressed cancel --> ignore message
if (resp === null) return null;

var access_level = parseInt(resp, 10);
if (access_level > 10) access_level = 10;
if (access_level <= 0) return { $type: "OperatorInfo", controllerId: message.controllerId, operatorId: 0, name: "Disallowed", level: 0 };
return { $type: "OperatorInfo", controllerId: message.controllerId, operatorId: access_level * 100, name: "User" + access_level, password: message.password, level: access_level, sequence: ++seq };
}

case "RequestJobCardsList": {
// MIS integration - return job cards list
var data = {};
for (var x = 0; x < Jobs.length; x++) data[Jobs[x].jobCardId] = Jobs[x];
return { $type: "JobCardsList", controllerId: message.controllerId, data: data, sequence: ++seq };
}

default: return null;
}
}

// Send command to get mold data
function getMoldData(id)
{
if (id === undefined || id === null || isNaN(id)) {
alert("Please enter a valid machine number.");
txtId.focus();
return;
}

// Send a REQ_CNTRLER_LIST message
sendMessage({ $type: "RequestMoldData", controllerId: id, sequence: ++seq });
}

// Send command to read mold data value
function readMoldData(id, field)
{
if (id === undefined || id === null || isNaN(id)) {
alert("Please enter a valid machine number.");
txtId.focus();
return;
}
// Send a READ_MOLD_DATA message
sendMessage({ $type: "ReadMoldData", controllerId: id, field: field, sequence: ++seq });
}
});
Loading

0 comments on commit cc4df3d

Please sign in to comment.