-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleClient2.m
More file actions
108 lines (84 loc) · 3.44 KB
/
Copy pathSimpleClient2.m
File metadata and controls
108 lines (84 loc) · 3.44 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
classdef SimpleClient2 < WebSocketClient
%CLIENT Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function obj = SimpleClient2(varargin)
%Constructor
obj@WebSocketClient(varargin{:});
end
end
methods (Access = protected)
function onOpen(obj,message)
% This function simply displays the message received
fprintf('%s\n',message);
end
function onTextMessage(obj,message)
if(exist('FLAGRUN','file')==2)
return;
else
touch('FLAGRUN');
end
% This function simply displays the message received
value = jsondecode(message);
if(~iscell(value))
value = {value};
end
if(exist('AD.mat','file')~=2)
adValueTotal = zeros(12,1);
else
result = load('AD.mat');
adValueTotal = result.adValueTotal;
end
if(exist('IO.mat','file')~=2)
ioValueTotal = zeros(12,1);
else
result = load('IO.mat');
ioValueTotal = result.ioValueTotal;
end
checkCONfunc = @(x) ~isempty(regexp(x,'\[{"ws":{"redirect":"wss://[0-9]{1,2}ws.obniz.io"}}\]', 'once'));
if(checkCONfunc(message))
save('tempReturn.mat','message');
fprintf('Message received:\n%s\n',message);
delete('FLAGRUN');
return;
end
for i= 1:1:length(value)
fn = fieldnames(value{i});
if(~cellfun(@isempty,regexp(fn,'ad[0-9]{1,2}')))
pickADfunc = @(x) value{i}.(x);
adValue = cellfun(pickADfunc, fn);
posADfunc = @(x) sscanf(x,'ad%d');
adPosNum = cellfun(posADfunc,fn);
adValueTotal(adPosNum+1) = adValue;
end
if(~cellfun(@isempty,regexp(fn,'ad[0-9]{1,2}')))
pos = ~cellfun(@isempty,regexp(fn,'io[0-9]{1,2}'));
iofn = fn(pos);
pickIOfunc = @(x) value{i}.(x);
ioValue = cellfun(pickIOfunc, iofn);
posIOfunc = @(x) sscanf(x,'io%d');
ioPosNum = cellfun(posIOfunc,iofn);
ioValueTotal(ioPosNum+1) = ioValue;
end
end
save('AD.mat','adValueTotal');
save('IO.mat','ioValueTotal');
delete('FLAGRUN');
end
function onBinaryMessage(obj,bytearray)
% This function simply displays the message received
fprintf('Binary message received:\n');
fprintf('Array length: %d\n',length(bytearray));
end
function onError(obj,message)
% This function simply displays the message received
fprintf('Error: %s\n',message);
end
function onClose(obj,message)
% This function simply displays the message received
fprintf('%s\n',message);
end
end
end