コード例 #1
0
 /// <summary>
 /// Turn Relay on or off
 /// </summary>
 /// <param name="moduleLink">relay to operate</param>
 /// <param name="state">True on</param>
 /// <returns>Module information confirming the action</returns>
 public ModuleInformation SetRelayState(SmartLink moduleLink, bool state)
 {
     SendCommand(moduleLink,
                 state
             ? "{\"system\":{\"set_relay_state\":{\"state\":1}}}"
             : "{\"system\":{\"set_relay_state\":{\"state\":0}}}");
     return(GetModuleInformation(moduleLink));
 }
コード例 #2
0
        /// <summary>
        ///     Gets specific information about the module
        /// </summary>
        /// <param name="moduleLink">the link object to use for connection</param>
        /// <returns>null if no connection or data</returns>
        public ModuleInformation GetModuleInformation(SmartLink moduleLink)
        {
            var moduleData = SendCommand(moduleLink, GetSystemInformation);

            return(!string.IsNullOrWhiteSpace(moduleData)
                ? JsonConvert.DeserializeObject <ModuleInformation>(moduleData)
                : null);
        }
コード例 #3
0
        /// <summary>
        /// ask for the current power consumption
        /// </summary>
        /// <param name="moduleLink"></param>
        /// <returns></returns>
        public ModuleInformation GetRealTimeEnergyUsage(SmartLink moduleLink)
        {
            var returnString = SendCommand(moduleLink, GetRealTimeEnergy);
            var model        = !string.IsNullOrWhiteSpace(returnString)
                        ? JsonConvert.DeserializeObject <EnergyMonitor>(returnString)
                        : null;

            if (model == null)
            {
                return(moduleLink.ModuleInformation);
            }
            model.emeter.timeStamp = DateTime.Now;
            moduleLink.ModuleInformation.EnergyStats.Add(model);
            return(moduleLink.ModuleInformation);
        }
コード例 #4
0
        public Boolean CreateSmartLink(String email, String code, DateTime expirationDate, SmartLinkTypes type)
        {
            var smartLinkSearch = Repository.SmartLinks()
                                  .Where(x => !x.ActivatonDate.HasValue)
                                  .Where(x => x.ExpirationDate > DateTime.Now)
                                  .Where(x => x.SmartLinkType == SmartLinkTypes.PasswordRecovery)
                                  .SingleOrDefault(x => x.Code.Equals(code) &&
                                                   x.Email.Equals(email, StringComparison.OrdinalIgnoreCase));

            if (smartLinkSearch != null)
            {
                smartLinkSearch.ExpirationDate = DateTime.Now;

                Repository.Save();
            }

            var smartLink = new SmartLink
            {
                Code = code,

                Email = email,

                CreationDate = DateTime.Now,

                ExpirationDate = expirationDate,

                SmartLinkType = type,

                ActivatonDate = null,
            };

            try
            {
                Repository.Add(smartLink);
                Repository.Save();

                return(true);
            }
            catch (Exception ex)
            {
                //log

                ServiceModelState.AddModelError("", ex.Message);

                return(false);
            }
        }
コード例 #5
0
        private static string SendCommand(SmartLink moduleLink, string command)
        {
            var encryptedData    = Encrypt(command);
            var clientConnection = new TcpClient();
            var readbuf          = new byte[2048];

            try
            {
                clientConnection.Connect(moduleLink.LinkAddress, moduleLink.LinkPort);
                var connectionStream = clientConnection.GetStream();
                var dataToSend       = encryptedData;
                connectionStream.Write(dataToSend, 0, dataToSend.Length);
                connectionStream.ReadTimeout = 100000; // 10 second timeout on the read
                connectionStream.Read(readbuf, 0, 2048);
                connectionStream.Close();
            }
            catch
            {
                clientConnection.Close();
            }
            return(Decrypt(readbuf));
        }
コード例 #6
0
 public void UpdateModuleNameInformation(SmartLink moduleLink, string ModuleName)
 {
     SendCommand(moduleLink, "{\"system\":{\"set_dev_alias\":{\"alias\":\"" + ModuleName + "\"}}}");
     return;
 }
コード例 #7
0
 public void UpdateModuleLedState(SmartLink moduleLink, bool state)
 {
     SendCommand(moduleLink, "{\"system\":{\"set_led_off\":{\"off\":" + (state ? "0": "1") + "}}}");
 }