Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 3
0
 public static MCP CreateSetGroupTypeCmd(int groupId, McpGroupType type)
 {
     return(MCP.Create(McpCommand.SetValue, groupId, (int)type));
 }
Esempio n. 4
0
 public static MCP CreateLoginCmd(string user, string password)
 {
     return(MCP.Create(McpCommand.Login, user, password));
 }
Esempio n. 5
0
 private static MCP Create(McpCommand cmd, int val, string val2)
 {
     return(MCP.Create(cmd, val, Encoding.UTF8.GetBytes(val2)));
 }
Esempio n. 6
0
 public static MCP CreateSetPortStateCmd(int portId, McpPortState state)
 {
     return(MCP.Create(McpCommand.SetState, portId, (int)state));
 }
Esempio n. 7
0
 public static MCP CreateRemovePortCmd(int portId)
 {
     return(MCP.Create(McpCommand.RemovePort, portId));
 }
Esempio n. 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));
        }
Esempio n. 9
0
 public static MCP CreateSetUserRightsCmd(int userId, IList <byte> groupIds)
 {
     return(MCP.Create(McpCommand.SetUserRights, userId, groupIds.ToArray()));
 }
Esempio n. 10
0
 public static MCP CreateAddUserCmd(string username, string password)
 {
     return(MCP.Create(McpCommand.AddUser, username, password));
 }
Esempio n. 11
0
 public static MCP CreateGetUsersCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_USERS\"}"));
 }
Esempio n. 12
0
 public static MCP CreateScanWifiCmd()
 {
     return(MCP.Create(McpCommand.ScanWifi));
 }
Esempio n. 13
0
 public static MCP CreateGetWifiStateCmd()
 {
     return(MCP.Create(McpCommand.GetWifiState));
 }
Esempio n. 14
0
 public static MCP CreateSetGatewayNameCmd(string name)
 {
     return(MCP.Create(McpCommand.SetName, name));
 }
Esempio n. 15
0
 public static MCP CreateChangePassCmd(string newpassword)
 {
     return(MCP.Create(McpCommand.ChangePasswd, newpassword));
 }
Esempio n. 16
0
 public static MCP CreateSetRequestableCmd(int groupId, int portId)
 {
     return(MCP.Create(McpCommand.SetValue, groupId + 16, portId));
 }
Esempio n. 17
0
 public static MCP CreateSetGroupedPortsCmd(int groupId, IList <byte> portIds)
 {
     return(MCP.Create(McpCommand.SetGroupedPorts, groupId, portIds.ToArray()));
 }
Esempio n. 18
0
 public static MCP CreateRemoveUserCmd(int userId)
 {
     return(MCP.Create(McpCommand.RemoveUser, userId));
 }
Esempio n. 19
0
 public static MCP CreateGetPortsCmd()
 {
     return(MCP.Create(McpCommand.GetPorts));
 }
Esempio n. 20
0
 internal JmcpEventArgs(MCP mcp)
 {
     this.Mcp  = mcp;
     this.Data = Encoding.UTF8.GetString(mcp.Payload);
 }
Esempio n. 21
0
 public static MCP CreateSetPortTypeCmd(int portId, McpPortType type)
 {
     return(MCP.Create(McpCommand.SetType, portId, (int)type));
 }
Esempio n. 22
0
 public static MCP CreateGetGroupsCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_GROUPS\"}"));
 }
Esempio n. 23
0
 public static MCP CreateJmcpGetValuesCmd()
 {
     return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_VALUES\"}"));
 }
Esempio n. 24
0
 public static MCP CreateAddGroupCmd()
 {
     return(MCP.Create(McpCommand.AddGroup));
 }
Esempio n. 25
0
 internal TransitionEventArgs(MCP mcp)
 {
     this.Mcp = mcp;
     //this.Data = Encoding.UTF8.GetString(mcp.Payload);
 }
Esempio n. 26
0
 public static MCP CreateRemoveGroupCmd(int groupId)
 {
     return(MCP.Create(McpCommand.RemoveGroup, groupId));
 }
Esempio n. 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);
                }
            }
        }
Esempio n. 28
0
 public static MCP CreateSetGroupNameCmd(int groupId, string name)
 {
     return(MCP.Create(McpCommand.SetGroupName, groupId, name));
 }