Beispiel #1
0
        public async void SendMessage(MCP msg)
        {
            msg.Token = this._token;
            var tpMsg = this._clientId + this._gatewayMac + Helpers.ToHex(msg.ToByteArray());

            tpMsg = tpMsg.ToUpper();
            int checksum = 0;

            foreach (char c in tpMsg)
            {
                checksum += c;
            }
            checksum = checksum & 0xff;
            tpMsg   += checksum.ToString("X2");

            var msgBytes = Encoding.UTF8.GetBytes(tpMsg);
            await _client.Send(msgBytes);
        }
Beispiel #2
0
        public static MCP FromByteArray(byte[] data)
        {
            var ms = new MemoryStream(data);
            var bw = new BinaryReader(ms);

            int length = bw.ReadUInt16();

            var mcp = new MCP();

            mcp.Tag   = bw.ReadByte();
            mcp.Token = bw.ReadUInt32();
            byte commandByte = bw.ReadByte();

            mcp.Command = (McpCommand)(commandByte & 0x7F);
            byte[] buffer = new byte[2048];
            int    read   = bw.Read(buffer, 0, buffer.Length);

            mcp.Payload = new byte[read];
            Array.Copy(buffer, mcp.Payload, read);

            return(mcp);
        }
Beispiel #3
0
 public static MCP CreateSetGroupTypeCmd(int groupId, McpGroupType type)
 {
     return(MCP.Create(McpCommand.SetValue, groupId, (int)type));
 }
Beispiel #4
0
 public static MCP CreateLoginCmd(string user, string password)
 {
     return(MCP.Create(McpCommand.Login, user, password));
 }
Beispiel #5
0
 private static MCP Create(McpCommand cmd, int val, string val2)
 {
     return(MCP.Create(cmd, val, Encoding.UTF8.GetBytes(val2)));
 }
Beispiel #6
0
 public static MCP CreateSetPortStateCmd(int portId, McpPortState state)
 {
     return(MCP.Create(McpCommand.SetState, portId, (int)state));
 }
Beispiel #7
0
 public static MCP CreateRemovePortCmd(int portId)
 {
     return(MCP.Create(McpCommand.RemovePort, portId));
 }
Beispiel #8
0
        //public static MCP CreateSetValueCmd(int address, int value)
        //{
        //    return MCP.Create(McpCommand.SetValue, address, value);
        //}

        public static MCP CreateGetTransitionCmd(int newId)
        {
            return(MCP.Create(McpCommand.HmGetTransition, newId));
        }
Beispiel #9
0
 public static MCP CreateSetUserRightsCmd(int userId, IList <byte> groupIds)
 {
     return(MCP.Create(McpCommand.SetUserRights, userId, groupIds.ToArray()));
 }
Beispiel #10
0
 public static MCP CreateAddUserCmd(string username, string password)
 {
     return(MCP.Create(McpCommand.AddUser, username, password));
 }
Beispiel #11
0
 public static MCP CreateGetUsersCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_USERS\"}"));
 }
Beispiel #12
0
 public static MCP CreateScanWifiCmd()
 {
     return(MCP.Create(McpCommand.ScanWifi));
 }
Beispiel #13
0
 public static MCP CreateGetWifiStateCmd()
 {
     return(MCP.Create(McpCommand.GetWifiState));
 }
Beispiel #14
0
 public static MCP CreateSetGatewayNameCmd(string name)
 {
     return(MCP.Create(McpCommand.SetName, name));
 }
Beispiel #15
0
 public static MCP CreateChangePassCmd(string newpassword)
 {
     return(MCP.Create(McpCommand.ChangePasswd, newpassword));
 }
Beispiel #16
0
 public static MCP CreateSetRequestableCmd(int groupId, int portId)
 {
     return(MCP.Create(McpCommand.SetValue, groupId + 16, portId));
 }
Beispiel #17
0
 public static MCP CreateSetGroupedPortsCmd(int groupId, IList <byte> portIds)
 {
     return(MCP.Create(McpCommand.SetGroupedPorts, groupId, portIds.ToArray()));
 }
Beispiel #18
0
 public static MCP CreateRemoveUserCmd(int userId)
 {
     return(MCP.Create(McpCommand.RemoveUser, userId));
 }
Beispiel #19
0
 public static MCP CreateGetPortsCmd()
 {
     return(MCP.Create(McpCommand.GetPorts));
 }
Beispiel #20
0
 internal JmcpEventArgs(MCP mcp)
 {
     this.Mcp  = mcp;
     this.Data = Encoding.UTF8.GetString(mcp.Payload);
 }
Beispiel #21
0
 public static MCP CreateSetPortTypeCmd(int portId, McpPortType type)
 {
     return(MCP.Create(McpCommand.SetType, portId, (int)type));
 }
Beispiel #22
0
 public static MCP CreateGetGroupsCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_GROUPS\"}"));
 }
Beispiel #23
0
 public static MCP CreateJmcpGetValuesCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_VALUES\"}"));
 }
Beispiel #24
0
 public static MCP CreateAddGroupCmd()
 {
     return(MCP.Create(McpCommand.AddGroup));
 }
Beispiel #25
0
 internal TransitionEventArgs(MCP mcp)
 {
     this.Mcp = mcp;
     //this.Data = Encoding.UTF8.GetString(mcp.Payload);
 }
Beispiel #26
0
 public static MCP CreateRemoveGroupCmd(int groupId)
 {
     return(MCP.Create(McpCommand.RemoveGroup, groupId));
 }
Beispiel #27
0
        private async Task SocketReader()
        {
            byte[] buffer = new byte[2048];
            int    offset = 0;

            while (true)
            {
                try
                {
                    offset += await _client.Read(buffer, (uint)offset, (uint)(buffer.Length - offset));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Ex " + ex.StackTrace);
                    throw ex;
                }

                if (offset < (MCP.ADDRESS_SIZE * 2) + MCP.LENGTH_SIZE)
                {
                    continue;
                }

                var tp = Encoding.UTF8.GetString(buffer, 0, offset);
                if (!tp.StartsWith(this._gatewayMac))
                {
                    //not good!
                    continue;
                }

                byte[] tpBytes = Helpers.HexToBytes(tp);
                int    length  = (tpBytes[12] << 8) + tpBytes[12 + 1];

                if (tpBytes.Length - 12 - length - 1 < 0)
                {
                    continue;
                }

                byte[] tpmsgbytes = new byte[length];
                Array.Copy(tpBytes, 12, tpmsgbytes, 0, length);
                var test = MCP.FromByteArray(tpmsgbytes);
                Array.Copy(buffer, (12 + length + 1) * 2, buffer, 0, offset - (12 + length + 1) * 2);
                offset = offset - (12 + length + 1) * 2;

                //
                if (test.Command == McpCommand.Login)
                {
                    // remember access token to be used on consecutive calls
                    this._token = BitConverter.ToUInt32(test.Payload, 1);

                    this.OnLogin?.Invoke(this, EventArgs.Empty);

                    //var msg = MCP.CreateJmcpGetValuesCmd();
                    //var msg = MCP.CreateGetGroupsCmd();
                    //var msg = MCP.CreateGetUsersCmd();
                    //var msg = MCP.CreateGetPortsCmd();
                    //var msg = MCP.CreateGetWifiStateCmd();
                    var msg = MCP.CreateGetTransitionCmd(0);
                    this.SendMessage(msg);
                }
                else if (test.Command == McpCommand.Logout)
                {
                    this.OnLogout?.Invoke(this, EventArgs.Empty);
                }
                else if (test.Command == McpCommand.Jmcp)
                {
                    this.OnJmcpMessage?.Invoke(this, new JmcpEventArgs(test));
                }
                else if (test.Command == McpCommand.HmGetTransition)
                {
                    this.OnTransition?.Invoke(this, new TransitionEventArgs(test));
                }
                else if (test.Command == McpCommand.Error)
                {
                    this.OnError?.Invoke(this, EventArgs.Empty);
                }
            }
        }
Beispiel #28
0
 public static MCP CreateSetGroupNameCmd(int groupId, string name)
 {
     return(MCP.Create(McpCommand.SetGroupName, groupId, name));
 }