public static async void CheckLightStatuses()
        {
            DateTime dateTime = METHOD.DateTimeTZ().DateTime;

            while (true)
            {
                DateTime CurrentDateTime = METHOD.DateTimeTZ().DateTime;
                bool     isLightsTime    = IsDarkTime() && !IsSleepTime();
                //following is checking if one has pushed the button from the PowerApp, then lights are on for 10 minutes
                var durationToForceLights = LightsManuallyOnOff ? (CurrentDateTime - dateTime).TotalMinutes : CONSTANT.OUTSIDE_LIGHTS_MANUAL_DURATION;
                var isLightNotForced      = durationToForceLights >= CONSTANT.OUTSIDE_LIGHTS_MANUAL_DURATION;
                if (isLightNotForced)
                {
                    LightsManuallyOnOff = false;
                    dateTime            = CurrentDateTime;
                    //execute shelly lights only if needed, not in every minute :-)
                    if ((isLightsTime && !TelemetryDataClass.isOutsideLightsOn) || (!isLightsTime && TelemetryDataClass.isOutsideLightsOn))
                    {
                        TelemetryDataClass.isOutsideLightsOn = await Shelly.SetShellySwitch(isLightsTime, Shelly.OutsideLight, nameof(Shelly.OutsideLight));
                    }
                }
                //it's a good place to check also garage light statuses in every minute
                if (TelemetryDataClass.isGarageLightsOn)
                {
                    TelemetryDataClass.isGarageLightsOn = await Shelly.GetShellyState(Shelly.GarageLight);
                }

                await Task.Delay(TimeSpan.FromMinutes(1)); //check statuses every 1 minutes
            }
        }
Ejemplo n.º 2
0
 public Request(string url)
 {
     this.Url     = new UriBuilder(url);
     this.Method  = METHOD.GET;
     this.Body    = new Dictionary <string, string>();
     this.Headers = new Dictionary <string, string>();
 }
        async Task SendMacAddressToCosmos(List <WiFiDevice> wiFiDevices)
        {
            _sendListData = new SendDataAzure();
            double timeOffset = METHOD.DateTimeTZ().Offset.TotalHours;

            //prepare the list to send into CosmosDB
            var AllWiFiDevices = new List <Localdevice>();

            wiFiDevices.ForEach(x => AllWiFiDevices.Add(new Localdevice()
            {
                ActiveDuration = x.ActiveDuration,
                DeviceName     = x.DeviceName,
                DeviceOwner    = x.DeviceOwner,
                DeviceType     = x.DeviceType,
                MacAddress     = x.MacAddress,
                StatusFrom     = METHOD.UnixTimeStampToDateTime(x.StatusUnixTime).AddHours(timeOffset),
                IsPresent      = x.IsPresent,
                SignalType     = x.SignalType,
                AccessPoint    = x.AccessPoint,
                IsChanged      = x.IsChanged
            }));
            TelemetryDataClass.SourceInfo = $"WiFi Devices";
            //send data to CosmosDB
            var monitorData = new
            {
                DeviceID    = "HomeController",
                status      = TelemetryDataClass.SourceInfo,
                DateAndTime = METHOD.DateTimeTZ().DateTime,
                AllWiFiDevices
            };
            await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "output");
        }
Ejemplo n.º 4
0
 public Request(string url)
 {
     this.Url = new UriBuilder(url);
     this.Method = METHOD.GET;
     this.Body = new Dictionary<string, string>();
     this.Headers = new Dictionary<string, string>();
 }
        public static bool IsSleepTime()
        {
            TimeSpan SleepTimeStart = TimeSpan.Parse(CONSTANT.SLEEP_TIME);
            TimeSpan SleepTimeEnd   = TimeSpan.Parse(CONSTANT.WAKEUP_TIME);
            bool     isSleepTime    = METHOD.TimeBetween(METHOD.DateTimeTZ(), SleepTimeStart, SleepTimeEnd);

            return(isSleepTime);
        }
        private static bool IsDarkTime()
        {
            var  coordinate = new Coordinate(Lat, Lon, DateTime.Now);
            var  sunset     = (DateTime)coordinate.CelestialInfo.SunSet;
            var  sunrise    = (DateTime)coordinate.CelestialInfo.SunRise;
            bool isDarkTime = METHOD.TimeBetween(METHOD.DateTimeTZ().ToUniversalTime(), sunset.TimeOfDay, sunrise.TimeOfDay);

            return(isDarkTime);
        }
Ejemplo n.º 7
0
    public void Init(Hashtable hash, hglTags_body hgltags)
    {
        try
        {
            m_method = (METHOD)System.Enum.Parse(typeof(METHOD), (string)hash["method"]);
        }
        catch { }

        m_hglTags = hgltags;
    }
Ejemplo n.º 8
0
 private static void Pin_HeatingRisingAsync() //winter and summer
 {
     if (!IsWaterHeatPinOn)
     {
         dateTimeRoomHeat = METHOD.DateTimeTZ().DateTime;                    //start roomheat
     }
     if (IsWaterHeatPinOn)
     {
         dateTimeWaterHeat = METHOD.DateTimeTZ().DateTime;                   //start waterheat
     }
 }
Ejemplo n.º 9
0
        public async void ReduceHeatingSchedulerAsync()
        {
            var _receiveData        = new ReceiveData();
            var _receiveEnergyPrice = new ReceiveEnergyPrice();
            List <EnergyPriceClass> _realTimeEnergyPrices = await _receiveEnergyPrice.QueryEnergyPriceAsync(); //run on app startup

            while (true)
            {
                DateTimeOffset CurrentDateTime = METHOD.DateTimeTZ();

                if (CurrentDateTime.DateTime.Hour == 00 || !_realTimeEnergyPrices.Any()) //run once every day at 00:00 to get energy prices and heating schedule
                {
                    _realTimeEnergyPrices = await _receiveEnergyPrice.QueryEnergyPriceAsync();

                    Console.WriteLine($"Energy price query {CurrentDateTime.DateTime}");
                }
                int  HeatingMode    = CONSTANT.NORMAL_HEATING;
                bool isHotWaterTime = true;
                //get the current state of heating and hot water
                foreach (var item in _realTimeEnergyPrices)
                {
                    if (item.date.DateTime.Hour == CurrentDateTime.DateTime.Hour)
                    {
                        HeatingMode    = item.heat;
                        isHotWaterTime = item.isHotWaterTime;
                        break;
                    }
                }
                //lets control the heating system according to the heating schedule
                string cmdHeat = null;
                if (HeatingMode == CONSTANT.NORMAL_HEATING)
                {
                    cmdHeat = CommandNames.NORMAL_TEMP_COMMAND;
                }
                if (HeatingMode == CONSTANT.REDUCED_HEATING)
                {
                    cmdHeat = CommandNames.REDUCE_TEMP_COMMAND;
                }
                if (HeatingMode == CONSTANT.EVU_STOP)
                {
                    cmdHeat = CommandNames.TURN_OFF_HEATING;
                }
                _receiveData.ProcessCommand(cmdHeat);

                //lets control hot water based on activity time and weekend
                string cmd = isHotWaterTime ? CommandNames.TURN_ON_HOTWATERPUMP : CommandNames.TURN_OFF_HOTWATERPUMP;
                _receiveData.ProcessCommand(cmd);

                int secondsToNextHour = 3600 - (int)CurrentDateTime.DateTime.TimeOfDay.TotalSeconds % 3600;
                await Task.Delay(TimeSpan.FromSeconds(secondsToNextHour));
            }
        }
        List <WiFiDevice> AddRemoveChangeDevices(List <Localdevice> devicesFromPowerApps, List <WiFiDevice> localWiFiDevices)
        {
            var  tempAddList    = new List <WiFiDevice>();
            var  tempDeleteList = new List <WiFiDevice>();
            bool isNewItem      = true;

            //CRUD operations, data is coming from PowerApps
            foreach (var item in devicesFromPowerApps)
            {
                foreach (var device in localWiFiDevices)
                {
                    if (device.MacAddress == item.MacAddress)
                    {
                        //DELETE item (special type=100)
                        if (item.DeviceType == 100)
                        {
                            tempDeleteList.Add(device);
                            break;
                        }
                        //UPDATE item
                        device.DeviceName     = item.DeviceName;
                        device.DeviceOwner    = item.DeviceOwner;
                        device.DeviceType     = item.DeviceType;
                        device.ActiveDuration = item.ActiveDuration;
                        device.StatusUnixTime = METHOD.DateTimeToUnixTimestamp(item.StatusFrom);
                        device.SignalType     = item.SignalType;
                        isNewItem             = false;
                        break;
                    }
                    else
                    {
                        isNewItem = true;
                    }
                }
                if (isNewItem)
                {
                    //ADD item
                    tempAddList.Add(new WiFiDevice
                                    (
                                        item.MacAddress,
                                        item.DeviceName,
                                        item.DeviceOwner,
                                        item.DeviceType,
                                        item.ActiveDuration,
                                        METHOD.DateTimeToUnixTimestamp(item.StatusFrom)
                                    ));
                }
            }
            localWiFiDevices.AddRange(tempAddList.ToArray());
            localWiFiDevices.RemoveAll(i => tempDeleteList.Any(x => x.MacAddress == i.MacAddress));
            return(localWiFiDevices);
        }
Ejemplo n.º 11
0
        protected string GetResponse(string url, METHOD method, Dictionary<string, string> headers, Dictionary<string, string> bodyParams = null)
        {
            string responseAsString = "";
            HttpWebRequest request = SetHeader(url, method, headers);
            if (bodyParams != null)
            {
                SendBody(request, MakeRequestBody(bodyParams)); // CIIN: asynchronous response.
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //CIIN: may cause timeout (perhaps)
            responseAsString = ConvertResponseToString(response);

            return responseAsString;
        }
		public bool Insert(METHOD obj)
		{
			try
			{
				DataContext.Instance.METHODS.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}
Ejemplo n.º 13
0
        /// <summary>
        /// The CryptoRandom constructor.
        /// </summary>
        /// <param name="method">Specifies the random number generation method (default = CRYPTO1).</param>
        /// <param name="nSeed">Specifies the seed used to initialize the random number generator, only used when <i>bUseCrypto</i> = <i>false</i> (default = 0, which is ignored).</param>
        public CryptoRandom(METHOD method = METHOD.DEFAULT, int nSeed = 0)
        {
            m_method = method;

            if (nSeed != 0)
            {
                if (m_method == METHOD.CRYPTO)
                {
                    m_method = METHOD.SYSTEM;
                }

                m_rand1 = new Random(nSeed);
            }
        }
Ejemplo n.º 14
0
        //below is a bunch of logic which you probaly wont understand later
        //this piece of code is working based on ground-heating system LED-s events
        private static async void Pin_WaterRisingAsync() //winter only
        {
            var _sendData = new Pins();

            if (IsRoomHeatPinOn) //if roomheating LED was on, stop roomheating
            {
                TelemetryDataClass.RoomHeatingInMinutes = (int)(METHOD.DateTimeTZ().DateTime - dateTimeRoomHeat).TotalMinutes;
                TelemetryDataClass.SourceInfo           = $"5. Roomheating {TelemetryDataClass.RoomHeatingInMinutes} min, Start Waterheating";
                await _sendData.SendData();

                TelemetryDataClass.RoomHeatingInMinutes = 0;
                dateTimeWaterHeat = METHOD.DateTimeTZ().DateTime; //start waterheating
            }
        }
        public static void SomeoneAtHomeChanged()
        {
            ReceiveData    _receiveData    = new ReceiveData();
            DateTimeOffset CurrentDateTime = METHOD.DateTimeTZ();

            //if security button is pushed from PowerApps then no automatic security change
            //if vacation mode is pushed from PowerApps, then security is back in automatic mode
            //if automatic mode, then secure home if nobody is at home and unsecure if some known mobile is at home
            if (!IsSecurityManuallyOn)
            {
                string cmd = WiFiProbes.IsAnyMobileAtHome ? CommandNames.TURN_OFF_SECURITY : CommandNames.TURN_ON_SECURITY;
                _receiveData.ProcessCommand(cmd);
            }
            TelemetryDataClass.isSomeoneAtHome = IsSomeoneAtHome;
            Console.WriteLine($"{(IsSecurityManuallyOn ? "Manual security mode." : "Automatic security mode.")} {(IsSomeoneAtHome ? "Someone at home" : "Nobody is home")} {CurrentDateTime.DateTime:G}");
        }
Ejemplo n.º 16
0
 public void add(METHOD M,int Number)
 {
     count++;
     ParsTableNode temp =first;
     if(first == null)
     {
         first = new ParsTableNode(M,Number);
     }
     else
     {
         while(temp.next != null)
         {
           temp =temp.next;
         }
         temp.next= new ParsTableNode(M,Number);
     }
 }
Ejemplo n.º 17
0
        public async Task SendData()
        {
            _sendListData = new SendDataAzure();
            var monitorData = new
            {
                DeviceID = "HomeController",
                status   = TelemetryDataClass.SourceInfo,
                TelemetryDataClass.RoomHeatingInMinutes,
                TelemetryDataClass.WaterHeatingInMinutes,
                TelemetryDataClass.VentilationInMinutes,
                UtcOffset   = METHOD.DateTimeTZ().Offset.Hours,
                DateAndTime = METHOD.DateTimeTZ(),
            };
            await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "outputStream");

            return;
        }
Ejemplo n.º 18
0
        protected override HttpWebRequest SetHeader(string url, METHOD method, Dictionary<string, string> headers)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "application/json";
            request.Method = method.ToString();
            request.Timeout = 5000;
            System.Net.ServicePointManager.Expect100Continue = false; //to prevent from adding ExpectContinue: 100 to Header

            if (headers != null && headers.Count > 0)
            {
                foreach (KeyValuePair<string, string> pair in headers)
                    request.Headers.Add(pair.Key, pair.Value);
            }

            return request;
        }
Ejemplo n.º 19
0
        private string GetTeamLabResponse(string url, EXTENSION ext, METHOD method, Dictionary<string, string> headers, Dictionary<string, string> bodyParams)
        {
            try
            {
                return GetResponse(url + "." + ext.ToString(), method, headers, bodyParams);
            }
            catch (WebException ex)
            {

                if (ex.Message == "The remote server returned an error: (401) Unauthorized.")
                {
                    throw new UnauthorizedException();
                }

                string msgFromServer = null;
                try
                {
                    var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
                    dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(resp);
                    msgFromServer = obj.error.message;
                }
                catch (NullReferenceException)
                {
                    throw new ConnectionFailedException();
                }

                switch (msgFromServer)
                {
                    case "Invalid username or password.":
                        throw new WrongCredentialsException();
                    case "Could not resolve current tenant :-(.":
                        throw new WrongPortalException();
                    case "Not found": //Task was not found on the server
                        throw new TaskNotFoundException();
                    case "Object reference not set to an instance of an object.":
                        throw new ObjectReferenceException();
                    default:
                        throw ex;

                }

            }
        }
Ejemplo n.º 20
0
        private static async void Pin_WaterFallingAsync() //winter only
        {
            IsWaterJustFinished = true;
            var _sendData = new Pins();

            TelemetryDataClass.WaterHeatingInMinutes = (int)(METHOD.DateTimeTZ().DateTime - dateTimeWaterHeat).TotalMinutes;
            TelemetryDataClass.SourceInfo            = $"7. Waterheating {TelemetryDataClass.WaterHeatingInMinutes} min";
            await _sendData.SendData();

            TelemetryDataClass.WaterHeatingInMinutes = 0;

            await Task.Delay(TimeSpan.FromSeconds(3)); //wait for a 3 seconds for the roomheat LED, is it turning on or off?

            IsWaterJustFinished = false;
            //waterheating just finished and roomheating will continue nonstop
            if (IsRoomHeatPinOn)
            {
                dateTimeRoomHeat = METHOD.DateTimeTZ().DateTime;                  //start roomheat
            }
        }
Ejemplo n.º 21
0
    private DNA <T> ChooseParent(METHOD method)
    {
        if (method == METHOD.ABOVEMEDIANONLY || method == METHOD.ABOVEMEDIANSANDBEST)
        {
            int index = UnityEngine.Random.Range((int)(Population.Count / 2) - 1, (Population.Count) - 1);
            return(Population[index]);
        }
        if (method == METHOD.ROULETTEWHEEL)
        {
            float rouletteSlice = UnityEngine.Random.Range(0f, fitnessSum);
            int   index         = 0;
            // need to decrement because high fitness is "bad" in this implementation
            float currentTotal = fitnessSum;
            for (int i = 0; i < Population.Count; i++)
            {
                currentTotal -= Population[i].Fitness;
                if (currentTotal < rouletteSlice)
                {
                    index = i;
                    break;
                }
            }
            return(Population[index]);
        }
        else
        {
            double randomNumber = random.NextDouble() * fitnessSum;

            for (int i = 0; i < Population.Count; i++)
            {
                if (randomNumber < Population[i].Fitness)
                {
                    return(Population[i]);
                }

                randomNumber -= Population[i].Fitness;
            }

            return(Population[random.Next(0, Population.Count)]);
        }
    }
Ejemplo n.º 22
0
        public async Task SendTelemetryAsync()
        {
            _sendListData = new SendDataAzure();

            var monitorData = new
            {
                DeviceID = "HomeController",
                NetatmoDataClass.Co2,
                NetatmoDataClass.Humidity,
                NetatmoDataClass.OutsideHumidity,
                NetatmoDataClass.Temperature,
                NetatmoDataClass.TemperatureOut,
                NetatmoDataClass.Noise,
                NetatmoDataClass.BatteryPercent,
                UtcOffset   = METHOD.DateTimeTZ().Offset.Hours,
                DateAndTime = METHOD.DateTimeTZ(),
                TelemetryDataClass.SourceInfo
            };

            await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "outputStream");
        }
        public async Task <List <EnergyPriceClass> > QueryEnergyPriceAsync()
        {
            Methods = new METHOD();
            string fileToday     = $"CurrentEnergyPrice{METHOD.DateTimeTZ():dd-MM-yyyy}";
            string fileYesterday = $"CurrentEnergyPrice{METHOD.DateTimeTZ().AddDays(-1):dd-MM-yyyy}";

            fileToday = Methods.GetFilePath(fileToday);
            //delete yesterday file, we dont need to collect them
            fileYesterday = Methods.GetFilePath(fileYesterday);
            if (File.Exists(fileYesterday))
            {
                File.Delete(fileYesterday);
            }

            if (File.Exists(fileToday)) //is there already file with today energy prices
            {
                var dataFromFile = await Methods.OpenExistingFile(fileToday);

                energyPriceToday = JsonSerializer.Deserialize <List <EnergyPriceClass> >(dataFromFile.ToString());
            }
            if (!File.Exists(fileToday)) //file with today energy price is missing
            {
                try
                {
                    string MarketPriceToday = METHOD.DateTimeTZ().ToString("dd.MM.yyyy");
                    energyPriceToday = await GetMarketPrice(MarketPriceToday);

                    var jsonString = JsonSerializer.Serialize(energyPriceToday);
                    await Methods.SaveStringToLocalFile(fileToday, jsonString);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Receive energy price from Cosmos exception: " + e.Message);
                }
            }
            return(energyPriceToday);
        }
Ejemplo n.º 24
0
        private static async void Pin_HeatingFallingAsync() //winter and summer
        {
            var _sendData = new Pins();

            if (!IsWaterJustFinished && !IsWaterHeatPinOn) //if waterheating LED is off AND the waterheating counter isnt started then stop roomheating
            {
                TelemetryDataClass.RoomHeatingInMinutes = (int)(METHOD.DateTimeTZ().DateTime - dateTimeRoomHeat).TotalMinutes;
                TelemetryDataClass.SourceInfo           = $"3. Roomheating {TelemetryDataClass.RoomHeatingInMinutes} min";
                await _sendData.SendData();

                TelemetryDataClass.RoomHeatingInMinutes = 0;
            }

            await Task.Delay(TimeSpan.FromSeconds(3)); //wait for a 3 seconds, maybe the waterheating just finished and it is already off?

            if (IsWaterHeatPinOn)                      //heating system in WATERHEATING mode, waterheating LED always on, this is for summer time
            {
                TelemetryDataClass.WaterHeatingInMinutes = (int)(METHOD.DateTimeTZ().DateTime - dateTimeWaterHeat).TotalMinutes;
                TelemetryDataClass.SourceInfo            = $"4. Waterheating {TelemetryDataClass.WaterHeatingInMinutes} min";
                await _sendData.SendData();

                TelemetryDataClass.WaterHeatingInMinutes = 0;
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="AMethod">解析方法类型</param>
        /// <param name="AstrTag">报文标记</param>
        public DatagramResolver(METHOD AMethod, string AstrTag)
        {
            if (startTag == null)
            {
                throw (new ArgumentNullException("报文标记不能为null"));
            }
            if (startTag == "")
            {
                throw (new ArgumentException("报文标记不能为空字符串"));
            }
            switch (AMethod)
            {
            case METHOD.START_TAG:
                this.startTag = AstrTag;
                break;

            case METHOD.END_TAG:
                this.endTag = AstrTag;
                break;

            default:
                break;
            }
        }
Ejemplo n.º 26
0
        public async void CheckHeatingTime()
        {
            while (true)
            {
                if (TelemetryDataClass.SaunaStartedTime == DateTime.MinValue)
                {
                    var filename = Methods.GetFilePath(CONSTANT.FILENAME_SAUNA_TIME);
                    if (File.Exists(filename)) //this mean that sauna has been started and system has suddenly restarted/updated
                    {
                        var result = await Methods.OpenExistingFile(filename);

                        if (DateTime.TryParseExact(result, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out var SaunaStartedTime))
                        {
                            TelemetryDataClass.SaunaStartedTime = SaunaStartedTime;
                        }
                    }
                }
                if (TelemetryDataClass.SaunaStartedTime != DateTime.MinValue)
                {
                    int    TotalTimeSaunaHeatedInMinutes = (int)(METHOD.DateTimeTZ().DateTime - TelemetryDataClass.SaunaStartedTime).TotalMinutes;
                    string cmd = CommandNames.NO_COMMAND;
                    //turn saun on if it has heating time but not turned on
                    if (TotalTimeSaunaHeatedInMinutes < CONSTANT.MAX_SAUNA_HEATING_TIME && !TelemetryDataClass.isSaunaOn)
                    {
                        cmd = CommandNames.TURN_ON_SAUNA;
                    }
                    //turn sauna off if the time is over
                    if (TotalTimeSaunaHeatedInMinutes > CONSTANT.MAX_SAUNA_HEATING_TIME)
                    {
                        cmd = CommandNames.TURN_OFF_SAUNA;
                    }
                    _receiveData.ProcessCommand(cmd);
                }
                await Task.Delay(TimeSpan.FromMinutes(1)); //check every minute
            }
        }
Ejemplo n.º 27
0
 /// <summary>
 /// This method sets header of HTTP request and returns HttpWebRequest
 /// </summary>
 /// <param name="url">Full (host + resource) URL address</param>
 /// <param name="method">REST method</param>
 /// <param name="headers">Additional HTTP headers</param>
 /// <returns>HttpWebRequest</returns>
 protected abstract HttpWebRequest SetHeader(string url, METHOD method, Dictionary<string, string> headers);
 private bool DeleteMethod(METHOD method)
 {
     try
     {
         DeleteRequire(method.ID);
         DeleteEnsure(method.ID);
         DataContext.Instance.METHODS.DeleteObject(method);
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 29
0
 public METHODAttribute(METHOD method)
 {
     Method = method;
 }
        public async void ProcessCommand(string command)
        {
            if (command == CommandNames.NO_COMMAND)
            {
                //_startStopLogic.testing(Pins.redLedPin);
            }
            else if (command == CommandNames.TURN_ON_SAUNA && !Pins.IsSaunaDoorOpen)
            {
                Pins.PinWrite(Pins.saunaHeatOutPin, PinValue.Low);
                TelemetryDataClass.isSaunaOn = true;
                var filename = Methods.GetFilePath(CONSTANT.FILENAME_SAUNA_TIME);
                if (TelemetryDataClass.SaunaStartedTime == DateTime.MinValue) //if sauna hasnt been started before
                {
                    DateTime SaunaStartedTime = METHOD.DateTimeTZ().DateTime;
                    await Methods.SaveStringToLocalFile(filename, SaunaStartedTime.ToString("dd.MM.yyyy HH:mm"));

                    TelemetryDataClass.SaunaStartedTime = SaunaStartedTime;
                }
            }
            else if (command == CommandNames.TURN_OFF_SAUNA)
            {
                var filename = Methods.GetFilePath(CONSTANT.FILENAME_SAUNA_TIME);
                File.Delete(filename);
                Pins.PinWrite(Pins.saunaHeatOutPin, PinValue.High);
                TelemetryDataClass.isSaunaOn        = false;
                TelemetryDataClass.SaunaStartedTime = new DateTime();
            }
            else if (command == CommandNames.TURN_ON_VACATION)
            {
                TelemetryDataClass.isHomeInVacation = true;
                TelemetryDataClass.VacationTime     = METHOD.DateTimeTZ().DateTime;
                ProcessCommand(CommandNames.TURN_OFF_HEATING);
                ProcessCommand(CommandNames.TURN_OFF_SAUNA);
                ProcessCommand(CommandNames.CLOSE_VENT);
                Console.WriteLine($"{(SomeoneAtHome.IsSecurityManuallyOn ? "Manual security mode." : "Automatic security mode.")} Vacation mode on at {TelemetryDataClass.VacationTime:G}");
            }
            else if (command == CommandNames.TURN_OFF_VACATION)
            {
                TelemetryDataClass.isHomeInVacation = false;
                TelemetryDataClass.VacationTime     = new DateTime();
                Console.WriteLine($"{(SomeoneAtHome.IsSecurityManuallyOn ? "Manual security mode." : "Automatic security mode.")} Vacation mode off at {METHOD.DateTimeTZ().DateTime:G}");
            }
            else if (command == CommandNames.TURN_ON_SECURITY)
            {
                TelemetryDataClass.isHomeSecured   = true;
                TelemetryDataClass.HomeSecuredTime = METHOD.DateTimeTZ().DateTime;
                Console.WriteLine($"{(SomeoneAtHome.IsSecurityManuallyOn ? "Manual security mode." : "Automatic security mode.")} Home is secured at: {TelemetryDataClass.HomeSecuredTime:G}");
            }
            else if (command == CommandNames.TURN_OFF_SECURITY)
            {
                TelemetryDataClass.isHomeSecured   = false;
                TelemetryDataClass.HomeSecuredTime = new DateTime();
                Console.WriteLine($"{(SomeoneAtHome.IsSecurityManuallyOn ? "Manual security mode." : "Automatic security mode.")} Home is at normal state at: {METHOD.DateTimeTZ().DateTime:G}");
            }
            else if (command == CommandNames.OPEN_VENT)
            {
                Pins.PinWrite(Pins.ventOutPin, PinValue.High);
                ManualVentLogic.VENT_ON            = true; //to enable 30min ventilation, same behavior as Co2 over 900
                TelemetryDataClass.isVentilationOn = true;
                dateTimeVentilation = METHOD.DateTimeTZ().DateTime;
            }
            else if (command == CommandNames.CLOSE_VENT)
            {
                Pins.PinWrite(Pins.ventOutPin, PinValue.Low);
                ManualVentLogic.VENT_ON = false;
                TelemetryDataClass.VentilationInMinutes = (int)(METHOD.DateTimeTZ().DateTime - dateTimeVentilation).TotalMinutes;
                TelemetryDataClass.SourceInfo           = "Ventilation turned off";
                var _sendData = new Pins();
                await _sendData.SendData();

                TelemetryDataClass.VentilationInMinutes = 0;
                TelemetryDataClass.isVentilationOn      = false;
            }
            else if (command == CommandNames.NORMAL_TEMP_COMMAND)
            {
                if (!TelemetryDataClass.isHomeInVacation)
                {
                    ProcessCommand(CommandNames.TURN_ON_HEATING);
                    Pins.PinWrite(Pins.normalTempOutPin, PinValue.High);
                    Pins.PinWrite(Pins.floorPumpOutPin, PinValue.High);
                    TelemetryDataClass.isNormalHeating = true;
                }
                else
                {
                    ProcessCommand(CommandNames.REDUCE_TEMP_COMMAND);
                    ProcessCommand(CommandNames.TURN_ON_HEATING);
                }
            }
            else if (command == CommandNames.REDUCE_TEMP_COMMAND)
            {
                while (Pins.IsRoomHeatingOn || Pins.IsWaterHeatingOn)
                {
                    ;
                }
                Pins.PinWrite(Pins.normalTempOutPin, PinValue.Low);
                Pins.PinWrite(Pins.floorPumpOutPin, PinValue.Low);
                TelemetryDataClass.isNormalHeating = false;
            }
            else if (command == CommandNames.TURN_ON_HOTWATERPUMP && !TelemetryDataClass.isHomeInVacation)
            {
                Pins.PinWrite(Pins.waterOutPin, PinValue.High);
                ProcessCommand(CommandNames.TURN_ON_HEATING);
                TelemetryDataClass.isWaterHeatingOn = true;
            }
            else if (command == CommandNames.TURN_OFF_HOTWATERPUMP)
            {
                Pins.PinWrite(Pins.waterOutPin, PinValue.Low);
                TelemetryDataClass.isWaterHeatingOn = false;
            }
            else if (command == CommandNames.TURN_ON_HEATING && !TelemetryDataClass.isHomeInVacation)
            {
                Pins.PinWrite(Pins.heatOnOutPin, PinValue.High);
                TelemetryDataClass.isHeatingOn = true;
            }
            else if (command == CommandNames.TURN_OFF_HEATING)
            {
                while (Pins.IsRoomHeatingOn || Pins.IsWaterHeatingOn)
                {
                    ;
                }
                Pins.PinWrite(Pins.heatOnOutPin, PinValue.Low);
                Pins.PinWrite(Pins.floorPumpOutPin, PinValue.Low);
                ProcessCommand(CommandNames.REDUCE_TEMP_COMMAND);
                ProcessCommand(CommandNames.TURN_OFF_HOTWATERPUMP);
                TelemetryDataClass.isHeatingOn = false;
            }
        }
Ejemplo n.º 31
0
 public string GetXMLResponse(string url, METHOD method, Dictionary<string, string> bodyParams)
 {
     return GetTeamLabResponse(url, EXTENSION.xml, method, null, bodyParams);
 }
Ejemplo n.º 32
0
 // This constructor will set the method to GET by default, and initalize
 // the URI to an empty string.
 public Client()
 {
     URI        = "";
     HttpMethod = METHOD.GET;
 }
Ejemplo n.º 33
0
 public ParsTableNode(METHOD M,int Number)
 {
     this.item.method = M;
       this.item.number = Number;
       next = null;
 }
Ejemplo n.º 34
0
        private void KrigingInThread()
        {
            int nRC, nI;
            double dRC, dC0, dC1, dSill, dDistance, dXmin, dXmax, dYmin, dYmax, dXStep, dYStep, dX, dY;

            InitNodeTypeArray();

            try
            {
                //  Set Block Kriging (0 - classic Kriging)
                nI = 1;
                Kriging.KrigingSetBlockKriging(nI);

                // Set data points
                nRC = Kriging.KrigingSetData(GlnCount, Marshal.UnsafeAddrOfPinnedArrayElement(GlardX, 0),
                    Marshal.UnsafeAddrOfPinnedArrayElement(GlardY, 0),
                    Marshal.UnsafeAddrOfPinnedArrayElement(GlardZ, 0));

                if (nRC == KRIGING_RC_ERROR || nRC == KRIGING_RC_NO_MEMORY)
                {
                    System.Windows.Forms.MessageBox.Show("Object Kriging can't set data");
                    return;
                }

                //   Calculate maximum distance between points
                CalculateMaxDistance(out dDistance);
                //   Set range ( The distance which cause the variogram reach plateau is called range)
                dRC = Kriging.KrigingSetRange(dDistance);
                // Debug.Print "Old value of range was " & Trim(Str(dRC))
                dRC = Kriging.KrigingGetRange();
                //Debug.Print "Current value of range is " & Trim(Str(dRC))

                // Get default variogram model
                nRC = Kriging.KrigingGetModel();
                //Debug.Print "Default  model is " & Trim(Str(nRC))

                // Set variogram model
                nRC = Kriging.KrigingSetModel(nRC);

                // Get default sill
                dSill = Kriging.KrigingGetSill(out dC0, out dC1);
                // The default values: dC0 = 1., dC1 = 2., dSill = 3.
                //Debug.Print "Default sill is " & Trim(Str(dSill))
                // Set sill (dSill = dC0 + dC1)
                dSill = Kriging.KrigingSetSill(dC0, dC1);

                //   Calculate the minimum and maximum points coordinates
                CalculateMinMaxPointsCoordinates(out dXmin, out dYmin, out dXmax, out dYmax);

                //   Calculate the steps of grid

                for (int row = 0; row < GlnRowCount; row++)
                {
                    for (int col = 0; col < GlnColumnCount; col++)
                    {
                        Coordinate cor = this.GetXYCell(_rasterGrid, col, row);
                        _rasterGrid.Value[row, col] = 0;

                        GldColumnCoord[col] = cor.X;
                        GldRowCoord[row] = cor.Y;

                    }
                }

                /*
                dXStep = (dXmax - dXmin) / (GlnColumnCount - 1);
                dYStep = (dYmax - dYmin) / (GlnRowCount - 1);

                //   Calculate the coordinates of grid
                dX = dXmin;

                for (nI = 1; nI <= GlnColumnCount; nI++)
                {
                    GldColumnCoord[nI - 1] = dX;
                    dX = dX + dXStep;
                }

                dY = dYmin;
                for (nI = 1; nI <= GlnRowCount; nI++)
                {
                    GldRowCoord[nI - 1] = dY;
                    dY = dY + dYStep;
                }
                 */


                nRC = Kriging.KrigingSetGridCoordinate(GlnColumnCount,
                    Marshal.UnsafeAddrOfPinnedArrayElement(GldColumnCoord, 0),
                    GlnRowCount,
                    Marshal.UnsafeAddrOfPinnedArrayElement(GldRowCoord, 0));

                GlnMaxProgress = Kriging.KrigingGetMaxProgressBar() / 100;
                progressBar1.Maximum = GlnMaxProgress;

                nRC = Kriging.KrigingCalculateInThread();

                timer1.Enabled = true;
                timer1.Interval = 50;   // Set Timer interval.

                progressBar1.Minimum = 0;
                progressBar1.Value = progressBar1.Minimum;
                progressBar1.Visible = false;

                GlnMethod = METHOD.KRIGING;
                return;
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Function KrigingSetData is missing");
                return;
            }
        }
Ejemplo n.º 35
0
 public static uint CTL_CODE(DEVICE_TYPE deviceType, ushort function, METHOD method, FILE_ACCESS access)
 {
     return (uint) ((((((uint) deviceType) << 0x10) | (((byte) access) << 14)) | (function << 2)) | ((long) method));
 }
        public async void ReadTemperature()
        {
            var _receiveData   = new ReceiveData();
            var _sensorsClient = new RinsenOneWireClient();
            var _sendListData  = new SendDataAzure();
            var Methods        = new METHOD();
            //currentSumOfTempDeltas is some bigger number than the delta (0,5) is used to determine temperature changes
            double SumOfTemperatureDeltas = 10;

            //initiate the list with the temps and names
            ListOfAllSensors = await _sensorsClient.ReadSensors();

            //fill out LastTemperatures and initial Temperature trend which is initially always TRUE
            ListOfAllSensors = UpdateSensorsTrendAndLastTemp(ListOfAllSensors, ListOfAllSensors);

            var filename = Methods.GetFilePath(CONSTANT.FILENAME_ROOM_TEMPERATURES);

            if (File.Exists(filename))
            {
                var dataFromFile = await Methods.OpenExistingFile(filename);

                List <SensorReading> SetRoomTemps = JsonSerializer.Deserialize <List <SensorReading> >(dataFromFile);
                SetTemperatures(SetRoomTemps);
            }
            while (true)
            {
                //get a new sensor readings and then update
                SensorReadings sensorReadings = await _sensorsClient.ReadSensors();

                ListOfAllSensors = UpdateSensorsTrendAndLastTemp(ListOfAllSensors, sensorReadings);

                //summing all the room temperature changes together add new deltas until it bigger that 4 degrees
                SumOfTemperatureDeltas += ListOfAllSensors.Temperatures.Where(x => x.isRoom).Sum(x => Math.Abs(x.Temperature - x.LastTemperature));

                //manage Livingroom heating actuator
                if (ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == LIVING).isHeatingRequired)
                {
                    Pins.PinWrite(Pins.livingRoomHeatControlOut, PinValue.High);
                }
                else
                {
                    Pins.PinWrite(Pins.livingRoomHeatControlOut, PinValue.Low);
                }

                //manage Office heating actuator
                if (ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == OFFICE).isHeatingRequired)
                {
                    Pins.PinWrite(Pins.homeOfficeHeatControlOut, PinValue.High);
                }
                else
                {
                    Pins.PinWrite(Pins.homeOfficeHeatControlOut, PinValue.Low);
                }

                //manage Piano heating actuator
                bool isPianoHeatingOn = ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == PIANO).isHeatingRequired;
                await Shelly.SetShellySwitch(isPianoHeatingOn, Shelly.PianoHeating, nameof(Shelly.PianoHeating));

                //manage Bedroom heating actuator
                bool isBedroomHeatingOn = ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == BEDROOM).isHeatingRequired;
                await Shelly.SetShellySwitch(isBedroomHeatingOn, Shelly.BedroomHeating, nameof(Shelly.BedroomHeating));

                //manage sauna temperature
                if (ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == SAUNA).isHeatingRequired&& TelemetryDataClass.isSaunaOn)
                {
                    Pins.PinWrite(Pins.saunaHeatOutPin, PinValue.Low);
                }
                else
                {
                    Pins.PinWrite(Pins.saunaHeatOutPin, PinValue.High);
                }
                //if sauna extremely hot, then turn off
                if (ListOfAllSensors.Temperatures.FirstOrDefault(x => x.RoomName == SAUNA).Temperature > CONSTANT.EXTREME_SAUNA_TEMP)
                {
                    _receiveData.ProcessCommand(CommandNames.TURN_OFF_SAUNA);
                }

                //if all rooms has achieved their target temperature then turn system off
                if (ListOfAllSensors.Temperatures.Where(x => x.isRoom).All(x => !x.isHeatingRequired))
                {
                    _receiveData.ProcessCommand(CommandNames.REDUCE_TEMP_COMMAND);
                }

                //if all room temperatures together has changed more that 3 degrees then send it out to database
                if (Math.Abs(SumOfTemperatureDeltas) > 4)
                {
                    TelemetryDataClass.SourceInfo = $"Room temp changed {SumOfTemperatureDeltas:0.0}";
                    var monitorData = new
                    {
                        DeviceID    = "RoomTemperatures",
                        UtcOffset   = METHOD.DateTimeTZ().Offset.Hours,
                        DateAndTime = METHOD.DateTimeTZ().DateTime,
                        time        = METHOD.DateTimeTZ().ToString("HH:mm"),
                        TelemetryDataClass.SourceInfo,
                        ListOfAllSensors.Temperatures
                    };
                    await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "output");

                    SumOfTemperatureDeltas = 0;            //resetting to start summing up again
                }
                await Task.Delay(TimeSpan.FromMinutes(1)); //check temperatures every minute
            }
        }
Ejemplo n.º 37
0
        public async Task <MessageResponse> PipeMessage(object inputdata, ModuleClient userContext, string SourceInfo, string output)
        {
            int counterValue = Interlocked.Increment(ref counter);

            if (!(userContext is ModuleClient moduleClient))
            {
                throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
            }
            var messageJson = JsonSerializer.Serialize(inputdata);
            var message     = new Message(Encoding.ASCII.GetBytes(messageJson));

            byte[] messageBytes  = message.GetBytes();
            string messageString = Encoding.UTF8.GetString(messageBytes);

            if (!string.IsNullOrEmpty(messageString))
            {
                //the following piece of code is necessary only if using Twin Desired/Reported properties
                //this desired/reported properties are not used at the moment in my code
                var pipeMessage = new Message(messageBytes);
                foreach (var prop in message.Properties)
                {
                    pipeMessage.Properties.Add(prop.Key, prop.Value);
                }
                await moduleClient.SendEventAsync(output, pipeMessage);

                Console.WriteLine($"Azure IoT Hub message: {counterValue}. {SourceInfo}: {METHOD.DateTimeTZ().DateTime}");
            }
            return(MessageResponse.Completed);
        }
Ejemplo n.º 38
0
 private Uri ProcessURI(METHOD m, string path, Dictionary<string, string> args)
 {
     // Process the URI
     StringBuilder URI = new StringBuilder();
     URI.Append(this.url);
     URI.Append(path);
     if (this.alwaysUseJson & (m == METHOD.GET || m == METHOD.PUT))
     {
         if (args != null && !args.ContainsKey("format"))
         {
             args.Add("format", "json");
         }
     }
     if (args != null && args.Count > 0)
     {
         URI.Append("?");
         List<string> argList = new List<string>();
         foreach (string k in args.Keys)
         {
             argList.Add(k + "=" + HttpUtility.UrlEncode(args[k]));
         }
         URI.Append(string.Join("&", argList.ToArray()));
     }
     return new Uri(URI.ToString());
 }
Ejemplo n.º 39
0
 public string GetAuthorizedXMLResponse(string url, METHOD method, Dictionary<string, string> bodyParams, string authorizationToken)
 {
     return GetTeamLabResponse(url, EXTENSION.xml, method, GetAuthorizationHeaders(authorizationToken), bodyParams);
 }
Ejemplo n.º 40
0
 /// <summary>
 /// Makes a call to FluidDB using JSON payload
 /// </summary>
 /// <param name="method">The type of HTTP method</param>
 /// <param name="path">The path to call</param>
 /// <param name="body">The body of the request</param>
 /// <param name="args">Any further arguments to append to the URI</param>
 /// <returns>The raw result</returns>
 public HttpWebResponse Call(METHOD m, string path, Dictionary<string, string> args, Dictionary<string, object> body)
 {                        
     // Build the request
     Uri requestUri = ProcessURI(m, path, args);
     WebRequest request = WebRequest.Create(requestUri);
     request.Method = m.ToString();
     //  Make sure the headers are correct
     ((HttpWebRequest)request).UserAgent = ".NET FluidDB Client";
     ((HttpWebRequest)request).Accept = "application/json";
     if (!(this.password == string.Empty & this.username == string.Empty))
     {
         string userpass = username + ":" + password;
         byte[] encUserPass = Encoding.UTF8.GetBytes(userpass);
         string auth = "Basic " + Convert.ToBase64String(encUserPass).Trim();
         ((HttpWebRequest)request).Headers.Add(HttpRequestHeader.Authorization, auth);
     }
     
     if (body == null || body.Count == 0)
     {
         request.ContentType = "text/plain";
     }
     else
     {
         Byte[] byteArray = Encoding.ASCII.GetBytes(this.jss.Serialize(body));
         request.ContentType = "application/json";
         request.ContentLength = byteArray.Length;
         Stream bodyStream = request.GetRequestStream();
         bodyStream.Write(byteArray, 0, byteArray.Length);
         bodyStream.Close();
     }
     // Call FluidDB
     try
     {
         return (HttpWebResponse)request.GetResponse();
     }
     catch (WebException e)
     {
         // I don't want you to raise an exception dammit... I just want the raw 
         // response and I'll process the errorClass from the content and maybe 
         // raise an exception if appropriate elsewhere
         return (HttpWebResponse)e.Response;
     }
 }
        public static async Task <bool> SetShellySwitch(bool turnOn, string ipAddress, string shellyName)
        {
            string TurnOnCommand = turnOn ? "on" : "off";
            bool   ison          = false;

            try
            {
                var    http = new HttpClient();
                string url  = $"http://{ipAddress}/relay/0?turn={TurnOnCommand}";
                HttpResponseMessage response = await http.GetAsync(url);

                var result = response.Content.ReadAsStringAsync();

                //deserialize all content
                var nps = JsonSerializer.Deserialize <JsonElement>(result.Result);
                ison = nps.GetProperty("ison").GetBoolean();

                if (shellyName == "OutsideLight" || shellyName == "GarageLight")
                {
                    Console.WriteLine($"Shelly {shellyName} {(turnOn ? "turned on" : "turned off")} {METHOD.DateTimeTZ().DateTime:T}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Send command to Shelly exception {shellyName} {turnOn}: {e.Message}");
            }
            return(ison);
        }
        public async void QueryWiFiProbes()
        {
            http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")));

            var filename = Methods.GetFilePath(CONSTANT.FILENAME_HOME_DEVICES);

            try
            {
                //open file and -> list of devices from Raspberry if the file exists
                if (File.Exists(filename))
                {
                    var result = await Methods.OpenExistingFile(filename);

                    WiFiDevice.WellKnownDevices = JsonSerializer.Deserialize <List <WiFiDevice> >(result).ToList();
                }
                else
                {
                    double unixTimestamp = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
                    //following will happen only if there is no file on Raspberry and the list will be loaded from the environment variables
                    WiFiDevice.WellKnownDevices.ForEach(x => x.StatusUnixTime = unixTimestamp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"open file {e}");
            }

            await SendMacAddressToCosmos(WiFiDevice.WellKnownDevices);

            bool isAnyDeviceChanged = true;
            var  UnknownDevices     = new List <WiFiDevice>();

            while (true)
            {
                double unixTimestamp = METHOD.DateTimeToUnixTimestamp(DateTime.UtcNow);
                double timeOffset    = METHOD.DateTimeTZ().Offset.TotalHours;

                //if there are some items from PowerApps then proceed
                if (WiFiDevicesFromPowerApps.Any())
                {
                    //Add, Remove or Change WiFiDevices list according to WiFiDevices From PowerApps
                    WiFiDevice.WellKnownDevices = AddRemoveChangeDevices(WiFiDevicesFromPowerApps, WiFiDevice.WellKnownDevices);
                    WiFiDevicesFromPowerApps.Clear();
                    //send all devices to CosmosDB
                    await SendMacAddressToCosmos(WiFiDevice.WellKnownDevices);
                }
                //prepare multimac query
                //create a list of the MAC Addresses for multimac query
                List <string> deviceMacs = new List <string>();
                WiFiDevice.WellKnownDevices.ForEach(x => deviceMacs.Add(x.MacAddress));
                string            jsonMacAddresses     = JsonSerializer.Serialize(deviceMacs);
                string            urlMultiMac          = $"{urlKismet}/devices/multimac/devices.json";
                string            jsonContentFieldsMac = "json={\"fields\":" + jsonFields + ",\"devices\":" + jsonMacAddresses + "}";
                List <WiFiDevice> KismetKnownDevices   = await GetDevices(urlMultiMac, jsonContentFieldsMac);

                //clear devices list.
                WiFiDevicesToPowerApps.Clear();
                //building list of the local devices which are last seen
                foreach (var knownDevice in WiFiDevice.WellKnownDevices)
                {
                    foreach (var kismet in KismetKnownDevices)
                    {
                        if (knownDevice.MacAddress == kismet.MacAddress)
                        {
                            knownDevice.LastUnixTime = kismet.LastUnixTime;
                            knownDevice.LastSignal   = kismet.LastSignal;
                            knownDevice.SignalType   = kismet.SignalType;
                            knownDevice.SSID         = kismet.SSID;
                            knownDevice.WiFiName     = kismet.ProbedSSID;
                            if (kismet.SignalType.Contains("Wi-Fi"))
                            {
                                knownDevice.AccessPoint = WiFiDevice.WellKnownDevices.Where(x => x.MacAddress == kismet.LastBSSID).Select(x => x.DeviceName).DefaultIfEmpty("No AP").First();
                                if (string.IsNullOrEmpty(knownDevice.WiFiName) || knownDevice.WiFiName == "0")
                                {
                                    knownDevice.WiFiName = WiFiDevice.WellKnownDevices.Where(x => x.MacAddress == kismet.LastBSSID).Select(x => x.SSID).DefaultIfEmpty("No AP").First();
                                }
                            }
                            break;
                        }
                    }
                    //checking active devices and setting Active/NonActive statuses. Each device can have individual timewindow
                    knownDevice.IsChanged = false;                                                //this is just for debugging, no other reason
                    var durationUntilNotActive = (unixTimestamp - knownDevice.LastUnixTime) / 60; //timestamp in minutes
                    knownDevice.IsPresent = durationUntilNotActive < knownDevice.ActiveDuration;
                    if (knownDevice.IsPresent && !knownDevice.StatusChange)
                    {
                        knownDevice.StatusUnixTime = unixTimestamp;
                        knownDevice.StatusChange   = true;
                        isAnyDeviceChanged         = true;
                        knownDevice.IsChanged      = true;
                    }
                    if (!knownDevice.IsPresent && knownDevice.StatusChange)
                    {
                        knownDevice.StatusUnixTime = knownDevice.LastUnixTime;
                        knownDevice.StatusChange   = false;
                        isAnyDeviceChanged         = true;
                        knownDevice.IsChanged      = true;
                    }
                    //following list WiFiDevicesToPowerApps is used to send minimal data to PowerApps (sending only userdevices)
                    //this list will be sent as a response after PowerApps asks something or refreshing it's data
                    if (knownDevice.DeviceType != WiFiDevice.DEVICE)
                    {
                        WiFiDevicesToPowerApps.Add(new Localdevice()
                        {
                            DeviceOwner    = knownDevice.DeviceOwner,
                            DeviceName     = knownDevice.DeviceName,
                            DeviceType     = knownDevice.DeviceType,
                            IsPresent      = knownDevice.IsPresent,
                            StatusFrom     = METHOD.UnixTimeStampToDateTime(knownDevice.StatusUnixTime),
                            ActiveDuration = knownDevice.ActiveDuration,
                            MacAddress     = knownDevice.MacAddress,
                            SignalType     = knownDevice.SignalType,
                            IsChanged      = knownDevice.IsChanged
                        });
                    }
                    //build a list with the names who is expected to track
                    //this list will be sent through Azure Functions to the owners e-mail
                    if (knownDevice.DeviceType == WiFiDevice.MOBILE || knownDevice.DeviceType == WiFiDevice.WATCH || knownDevice.DeviceType == WiFiDevice.CAR)
                    {
                        //to be sure that every person is listed just once
                        if (!WiFiDevicesWhoIsChanged.Any(x => x.DeviceOwner == knownDevice.DeviceOwner) || !WiFiDevicesWhoIsChanged.Any())
                        {
                            WiFiDevicesWhoIsChanged.Add(new Localdevice()
                            {
                                DeviceOwner = knownDevice.DeviceOwner,
                                DeviceName  = knownDevice.DeviceName,
                                IsPresent   = knownDevice.IsPresent,
                                StatusFrom  = METHOD.UnixTimeStampToDateTime(knownDevice.StatusUnixTime),
                                SignalType  = knownDevice.SignalType,
                                IsChanged   = knownDevice.IsChanged
                            });
                        }
                    }
                }

                //update the list of the correct statuses, are they home or away
                foreach (var item in WiFiDevicesWhoIsChanged)
                {
                    item.IsChanged = false;
                    foreach (var device in WiFiDevicesToPowerApps)
                    {
                        if (item.DeviceOwner == device.DeviceOwner && (device.DeviceType == WiFiDevice.MOBILE || device.DeviceType == WiFiDevice.WATCH || device.DeviceType == WiFiDevice.CAR))
                        {
                            if (device.IsChanged)
                            {
                                item.IsChanged  = device.IsChanged;
                                item.DeviceName = device.DeviceName;
                                item.StatusFrom = device.StatusFrom.AddHours(timeOffset);
                                item.SignalType = device.SignalType;
                                item.IsPresent  = device.IsPresent;
                            }
                            //if device is exist and has'nt been changed, then mark it as unchanged and move to next item
                            //if device is not exist (not seen) but has been changed then mark it as unchanged and move to next item = do not notify leaving devices
                            //this is XOR
                            if (device.IsPresent ^ device.IsChanged)
                            {
                                item.IsChanged = false;
                                break;
                            }
                        }
                    }
                }

                //save the data locally into Raspberry
                var jsonString = JsonSerializer.Serialize(WiFiDevice.WellKnownDevices);
                await Methods.SaveStringToLocalFile(filename, jsonString);

                //send an e-mail only if someone has arrived at home or left the home
                if (WiFiDevicesWhoIsChanged.Any(x => x.IsChanged))
                {
                    string status = "Look who is at home:\n\n";

                    WiFiDevicesWhoIsChanged.ForEach(x => status += $"{(x.IsPresent ? x.DeviceOwner + " " + x.DeviceName + " at home from" : x.DeviceOwner + " not seen since")} {x.StatusFrom:HH:mm dd.MM.yyyy} \n ");
                    var    x          = WiFiDevicesWhoIsChanged.First(x => x.IsChanged);
                    string whoChanged = $"{(x.IsPresent ? x.DeviceOwner + " at home " : x.DeviceOwner + " not seen since")} {x.StatusFrom:HH:mm dd.MM.yyyy}";
                    _sendListData = new SendDataAzure();
                    TelemetryDataClass.SourceInfo = $"{whoChanged}";
                    //send data to CosmosDB
                    var monitorData = new
                    {
                        DeviceID = "HomeController",
                        TelemetryDataClass.SourceInfo,
                        status,
                        DateAndTime   = METHOD.DateTimeTZ().DateTime,
                        isHomeSecured = true
                    };
                    await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "output");
                }


                //if any mobile phone or watch is present then someone is at home
                IsAnyMobileAtHome = WiFiDevice.WellKnownDevices.Any(x => x.IsPresent && (x.DeviceType == WiFiDevice.MOBILE || x.DeviceType == WiFiDevice.WATCH || x.DeviceType == WiFiDevice.CAR));
                //if security mode automatic and home secured then unsecure home if any known mobile device is seen
                if (IsAnyMobileAtHome && TelemetryDataClass.isHomeSecured && !SomeoneAtHome.IsSecurityManuallyOn)
                {
                    SomeoneAtHome.SomeoneAtHomeChanged();
                }

                #region Known Device Listing, only for debugging

                //for local debugging only show the active/non active devices in console window
                if (isAnyDeviceChanged)
                {
                    isAnyDeviceChanged = false;
                    var sortedList = WiFiDevice.WellKnownDevices.OrderByDescending(y => y.IsPresent).ThenBy(w => w.AccessPoint).ThenBy(z => z.SignalType).ThenBy(x => x.DeviceName).ToList();
                    Console.WriteLine();
                    Console.WriteLine($"All known devices at: {METHOD.DateTimeTZ().DateTime:G}");
                    Console.WriteLine();
                    Console.WriteLine($"   |   From   | Status  |          Device           |        WiFi network      |        AccessPoint       |  SignalType");
                    Console.WriteLine($"   |  ------  | ------- |         --------          |           -----          |           -----          |  --------  ");
                    foreach (var device in sortedList)
                    {
                        if (device.LastUnixTime > 0 && device.SignalType != "Wi-Fi AP" && device.DeviceOwner != "Unknown" && device.DeviceType != WiFiDevice.DEVICE) //show only my own ever seen LocalUserDevices devices
                        {
                            Console.WriteLine($" {(device.IsChanged ? "1" : " ")} " +
                                              $"| {METHOD.UnixTimeStampToDateTime(device.StatusUnixTime).AddHours(timeOffset):T} " +
                                              $"| {(device.IsPresent ? "Active " : "Not Act")} " +
                                              $"| {device.DeviceName}{"".PadRight(26 - (device.DeviceName.Length > 26 ? 26 : device.DeviceName.Length))}" +
                                              $"| {device.WiFiName}{"".PadRight(26 - (!string.IsNullOrEmpty(device.WiFiName) ? (device.WiFiName.Length > 26 ? 26 : device.WiFiName.Length) : 0))}" +
                                              $"| {device.AccessPoint}{"".PadRight(26 - (!string.IsNullOrEmpty(device.AccessPoint) ? (device.AccessPoint.Length > 26 ? 26 : device.AccessPoint.Length) : 0))}" +
                                              $"| {device.SignalType} ");
                        }
                    }
                    Console.WriteLine();
                }

                #endregion

                #region Unknown Devices Debugging

                //prepare last active devices query
                string            urlLastActive       = $"{urlKismet}/devices/last-time/{CONSTANT.ACTIVE_DEVICES_IN_LAST}/devices.json";
                string            jsonContentFields   = "json={\"fields\":" + jsonFields + "}";
                List <WiFiDevice> KismetActiveDevices = await GetDevices(urlLastActive, jsonContentFields);

                //removing all known devices from the last seen devices list
                //known devices are all locally registered devices in the list WiFiDevice.WifiDevices
                var tempDelList      = new List <WiFiDevice>();
                var AllActiveDevices = new List <WiFiDevice>(KismetActiveDevices);
                foreach (var kismet in KismetActiveDevices)
                {
                    foreach (var knownDevice in WiFiDevice.WellKnownDevices)
                    {
                        if (kismet.MacAddress == knownDevice.MacAddress || kismet.LastSignal < CONSTANT.SIGNAL_TRESHOLD || (kismet.CommonName == kismet.MacAddress && kismet.Manufacture == "Unknown"))
                        {
                            tempDelList.Add(kismet);
                            break;
                        }
                    }
                }
                KismetActiveDevices.RemoveAll(i => tempDelList.Contains(i));

                //adding new members to the close devices list
                if (KismetActiveDevices.Any())
                {
                    var  tempAddList = new List <WiFiDevice>();
                    bool isNewItem   = true;
                    foreach (var kismet in KismetActiveDevices)
                    {
                        foreach (var unknownDevice in UnknownDevices)
                        {
                            if (kismet.MacAddress == unknownDevice.MacAddress)
                            {
                                unknownDevice.Count++;
                                unknownDevice.LastUnixTime = kismet.LastUnixTime;
                                unknownDevice.WiFiName     = kismet.ProbedSSID;
                                if (kismet.SignalType.Contains("Wi-Fi"))
                                {
                                    //get device AccessPoint and WIFI network names from Kismet (if reported by device)
                                    List <WiFiDevice> KismetOneMac = new List <WiFiDevice>();
                                    string            urlOneMac    = $"{urlKismet}/devices/by-mac/{kismet.MacAddress}/devices.json";
                                    KismetOneMac = await GetDevices(urlOneMac, jsonContentFields);

                                    //have to check KismetOneMac list because sometimes it is empty
                                    if (KismetOneMac.Any())
                                    {
                                        unknownDevice.AccessPoint = AllActiveDevices.Where(x => x.MacAddress == KismetOneMac.First().LastBSSID).Select(x => x.BaseName).DefaultIfEmpty("No AP").First();
                                        if (string.IsNullOrEmpty(kismet.ProbedSSID) || unknownDevice.WiFiName == "0")
                                        {
                                            unknownDevice.WiFiName = AllActiveDevices.Where(x => x.MacAddress == KismetOneMac.First().LastBSSID).Select(x => x.SSID).DefaultIfEmpty("No AP").First();
                                        }
                                    }
                                }
                                isNewItem = false;
                                break;
                            }
                            else
                            {
                                isNewItem = true;
                            }
                        }
                        if (isNewItem)
                        {
                            tempAddList.Add(kismet);
                        }
                    }
                    UnknownDevices.AddRange(tempAddList.ToArray());

                    UnknownDevices.RemoveAll(x => (unixTimestamp - x.LastUnixTime) / 60 > 120); //remove all entries older that 2 hour

                    //local debugging: show the devices list in console only if some device has been added
                    if (tempAddList.Any() && UnknownDevices.Any())
                    {
                        var sortedList = UnknownDevices.OrderBy(x => x.SignalType).ThenByDescending(y => y.LastUnixTime).ToList();
                        Console.WriteLine();
                        Console.WriteLine($"All unknown devices at: {METHOD.DateTimeTZ().DateTime:G}");
                        Console.WriteLine();
                        Console.WriteLine($"dB  | First | Last  |    Mac Address    |Count |  SignalType   |         WiFi network      |         AccessPoint       |    Common Name     | Manufacturer ");
                        Console.WriteLine($" -  | ----  | ----  |    -----------    | ---  |  ----------   |          ---------        |          ---------        |    -----------     |  -----------  ");

                        foreach (var device in sortedList)
                        {
                            Console.WriteLine($"{device.LastSignal}{"".PadRight(device.LastSignal < 0 ? 1 : 3)}" +
                                              $"| {METHOD.UnixTimeStampToDateTime(device.FirstUnixTime).AddHours(timeOffset):t} " +
                                              $"| {METHOD.UnixTimeStampToDateTime(device.LastUnixTime).AddHours(timeOffset):t} " +
                                              $"| {device.MacAddress} " +
                                              $"| {device.Count}{"".PadRight(device.Count < 10 ? 3 : device.Count < 100 ? 2 : 1)} " +
                                              $"| {device.SignalType}{"".PadRight(14 - (!string.IsNullOrEmpty(device.SignalType) ? (device.SignalType.Length > 14 ? 14 : device.SignalType.Length) : 0))}" +
                                              $"| {device.WiFiName}{"".PadRight(26 - (!string.IsNullOrEmpty(device.WiFiName) ? (device.WiFiName.Length > 26 ? 26 : device.WiFiName.Length) : 0))}" +
                                              $"| {device.AccessPoint}{"".PadRight(26 - (!string.IsNullOrEmpty(device.AccessPoint) ? (device.AccessPoint.Length > 26 ? 26 : device.AccessPoint.Length) : 0))}" +
                                              $"| {device.CommonName}{"".PadRight(19 - (!string.IsNullOrEmpty(device.CommonName) ? (device.CommonName.Length > 19 ? 19 : device.CommonName.Length) : 0))}" +
                                              $"| {device.Manufacture}");
                        }
                        Console.WriteLine();
                    }
                }
                #endregion

                await Task.Delay(TimeSpan.FromSeconds(60)); //check statuses every 10 millisecond
            }
        }
Ejemplo n.º 43
0
        public async void IRSensorsReading()
        {
            _sendListData = new SendDataAzure();
            State NONE = new State {
                DoorValue = false, IRValue = false
            };                                                             //door closed, IR passive
            State DOOR = new State {
                DoorValue = true, IRValue = false
            };                                                            //door open, IR passive
            State ALL = new State {
                DoorValue = true, IRValue = true
            };                                                          //door open, IR active
            State IR = new State {
                DoorValue = false, IRValue = true
            };                                                          //door closed, IR active
            List <State> Entry = new List <State> {
                NONE, DOOR, ALL, IR, NONE
            };
            List <State> Entry2 = new List <State> {
                NONE, DOOR, ALL, DOOR, NONE
            };
            List <State> Exit = new List <State> {
                NONE, IR, NONE, DOOR, ALL, IR, NONE
            };
            List <State> Exit1 = new List <State> {
                NONE, IR, NONE, DOOR, ALL, DOOR, NONE
            };
            List <State> Exit2 = new List <State> {
                NONE, IR, ALL, DOOR, ALL, IR, NONE
            };
            List <State> Exit3 = new List <State> {
                NONE, IR, ALL, IR, NONE
            };
            List <State> Exit4 = new List <State> {
                NONE, IR, ALL, DOOR, NONE
            };
            List <State> DoorOpenClose = new List <State> {
                NONE, DOOR, NONE
            };
            List <State> RepeatDoorAll = new List <State> {
                DOOR, ALL
            };                                                         //repeat
            List <State> RepeatAllDoor = new List <State> {
                ALL, DOOR
            };                                                         //repeat
            List <State> RepeatIRAll = new List <State> {
                IR, ALL
            };                                                     //repeat
            List <State> _queue = new List <State>
            {
                new State {
                    DoorValue = false, IRValue = false
                }
            };
            DateTime lastSentTime = METHOD.DateTimeTZ().DateTime;

            while (true)
            {
                try
                {
                    DateTime CurrentDateTime = METHOD.DateTimeTZ().DateTime;
                    //check the last zone event time to report is there anybody at home
                    Zone LastActiveZone            = Zones.First(x => x.ZoneId == LastActiveZoneID);
                    var  timerInMinutes            = TelemetryDataClass.isHomeSecured ? CONSTANT.TIMER_MINUTES_WHEN_SECURED_HOME_EMPTY : CONSTANT.TIMER_MINUTES_WHEN_HOME_EMPTY;
                    var  DurationUntilHouseIsEmpty = !LastActiveZone.IsZoneOpen ? (CurrentDateTime - LastActiveZone.ZoneEventTime).TotalMinutes : 0;
                    SomeoneAtHome.IsSomeoneAtHome = DurationUntilHouseIsEmpty < timerInMinutes || WiFiProbes.IsAnyMobileAtHome;

                    //check each zone in 2 minutes window to report the zone active time
                    foreach (var zone in Zones)
                    {
                        var durationUntilZoneIsEmpty = !zone.IsZoneOpen ? (CurrentDateTime - zone.ZoneEventTime).TotalSeconds : 0;
                        zone.IsZoneEmpty = durationUntilZoneIsEmpty > CONSTANT.TIMER_SECONDS_WHEN_ZONE_EMPTY;
                        if (zone.IsZoneEmpty && zone.ZoneEmptyDetectTime != DateTime.MinValue)
                        {
                            //add alerting sensors into list
                            alertingSensors.Add(new AlertingZone(zone.ZoneName, zone.ZoneEmptyDetectTime.ToString("dd.MM"), zone.ZoneEmptyDetectTime.ToString("HH:mm"), zone.ZoneEventTime.ToString("HH:mm"), zone.IsHomeSecured));
                            //Console.WriteLine($" {zone.ZoneName} {zone.ZoneEmptyDetectTime:t} - {zone.ZoneEventTime:t} {zone.ZoneEmptyDetectTime:dd.MM} {(zone.IsHomeSecured ? "SECURED" : null)}");
                            zone.ZoneEmptyDetectTime = new DateTime();
                        }
                        if (!zone.IsZoneEmpty && zone.ZoneEmptyDetectTime == DateTime.MinValue)
                        {
                            zone.ZoneEmptyDetectTime = CurrentDateTime;
                        }
                    }

                    //if home secured forcing outside lights ON
                    if (TelemetryDataClass.isHomeSecured && SomeoneAtHome.IsSomeoneAtHome && !TelemetryDataClass.isOutsideLightsOn)
                    {
                        SomeoneAtHome.LightsManuallyOnOff    = true;
                        TelemetryDataClass.isOutsideLightsOn = await Shelly.SetShellySwitch(true, Shelly.OutsideLight, nameof(Shelly.OutsideLight));
                    }

                    var DurationUntilToSendData = alertingSensors.Any() ? (CurrentDateTime - lastSentTime).TotalMinutes : 0;
                    var isTimeToSendData        = DurationUntilToSendData > CONSTANT.TIMER_MINUTES_TO_SEND_ZONE_COSMOS;
                    if (isTimeToSendData)
                    {
                        alertingSensors.Reverse();
                        TelemetryDataClass.SourceInfo = $"Zones activity {alertingSensors.Count}";

                        //check if the last zone was added into list during the home was secured
                        bool         isLastZoneSecured = false;
                        string       lastZoneName      = null;
                        AlertingZone LastZone          = alertingSensors.First();
                        lastZoneName      = LastZone.ZoneName;
                        isLastZoneSecured = LastZone.IsHomeSecured;
                        string sensorsOpen = "";
                        //run the alert only if home is secured and there are some alerting zone
                        if (TelemetryDataClass.isHomeSecured && isLastZoneSecured)
                        {
                            TelemetryDataClass.SourceInfo = $"Home secured {lastZoneName}";
                            sensorsOpen = "Look where someone is moving:\n\n";
                            foreach (var zone in alertingSensors)
                            {
                                //create a string with all zones for an e-mail
                                if (zone.IsHomeSecured)
                                {
                                    sensorsOpen += $" {zone.ZoneName} {zone.TimeStart} - {zone.TimeEnd}\n";
                                }
                            }
                            alertingSensors.ForEach(x => Console.WriteLine($"{x.ZoneName} {x.TimeStart} - {x.TimeEnd} {(x.IsHomeSecured ? "SECURED" : null)}"));
                        }
                        var monitorData = new
                        {
                            DeviceID = "HomeController",
                            TelemetryDataClass.SourceInfo,
                            TelemetryDataClass.isHomeSecured,
                            DateAndTime = CurrentDateTime,
                            alertingSensors,
                            date   = CurrentDateTime.ToString("dd.MM"),
                            time   = CurrentDateTime.ToString("HH:mm"),
                            status = sensorsOpen
                        };
                        await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "output");

                        alertingSensors.Clear();
                        lastSentTime = CurrentDateTime;
                    }

                    //maximum is 1000 items in alertingSensor list
                    if (alertingSensors.Count >= CONSTANT.MAX_ITEMS_IN_ALERTING_LIST)
                    {
                        alertingSensors.RemoveAt(0);
                    }

                    Zone doorZone    = Zones.First(ir => ir.ZoneId == 1);
                    Zone IrZone      = Zones.First(ir => ir.ZoneId == 2);
                    Zone smokeZone   = Zones.First(ir => ir.ZoneId == 7);
                    bool isDoorOpen  = doorZone.IsZoneOpen;
                    bool isIrOpen    = IrZone.IsZoneOpen;
                    bool isSmokeOpen = smokeZone.IsZoneOpen;
                    TelemetryDataClass.isHomeDoorOpen = isDoorOpen;

                    //if door opens or someone in entry then turn on Entry light. Shelly is configured to turn off after 2 minutes
                    if (isDoorOpen || isIrOpen)
                    {
                        await Shelly.SetShellySwitch(true, Shelly.EntryLight, nameof(Shelly.EntryLight));
                    }

                    //if door or IR is closed more that 2 minutes then clear the queue
                    var  LastActive         = doorZone.ZoneEventTime > IrZone.ZoneEventTime ? doorZone.ZoneEventTime : IrZone.ZoneEventTime;
                    var  durationUntilReset = _queue.Count > 1 ? (CurrentDateTime - LastActive).TotalSeconds : 0;
                    bool isClearTime        = durationUntilReset > CONSTANT.TIMER_SECONDS_CLEAR_DOOR_QUEUE;
                    if (isClearTime && !isDoorOpen)
                    {
                        _queue.Clear();
                        _queue.Add(new State {
                            DoorValue = false, IRValue = false
                        });
                        //Console.WriteLine($"{CurrentDateTime:T} queue cleared");
                    }

                    //save the door and IR statuses for the queue
                    State _state = new State {
                        DoorValue = isDoorOpen, IRValue = isIrOpen
                    };

                    if (_queue.Count > 6)
                    {
                        _queue = new List <State>(Helpers.Rotate(_queue, 1));
                        _queue.RemoveAt(_queue.Count - 1);
                    }

                    //if list is empty, then return both open, otherwise last item
                    State lastItem = (_queue.Count != 0) ? _queue[_queue.Count - 1] : new State {
                        DoorValue = true, IRValue = true
                    };
                    string status = "No pattern";

                    if (_state != lastItem)
                    {
                        _queue.Add(_state);
                        if (RemoveDuplicate(_queue, RepeatDoorAll))
                        {
                            Console.WriteLine($"Door-All duplicate removed");
                        }
                        if (RemoveDuplicate(_queue, RepeatAllDoor))
                        {
                            Console.WriteLine($"All-Door duplicate removed");
                        }
                        if (RemoveDuplicate(_queue, RepeatIRAll))
                        {
                            Console.WriteLine($"IR-All duplicate removed");
                        }

                        if (_queue.Count > 2) //only check pattern if there are more than 3 events in queue
                        {
                            if (ContainsPattern(_queue, Entry))
                            {
                                status = "entry";
                            }
                            if (ContainsPattern(_queue, Entry2))
                            {
                                status = "entry 2";
                            }
                            if (ContainsPattern(_queue, Exit))
                            {
                                status = "exit";
                            }
                            if (ContainsPattern(_queue, Exit1))
                            {
                                status = "exit 1";
                            }
                            if (ContainsPattern(_queue, Exit2))
                            {
                                status = "exit 2";
                            }
                            if (ContainsPattern(_queue, Exit3))
                            {
                                status = "exit 3";
                            }
                            if (ContainsPattern(_queue, Exit4))
                            {
                                status = "exit 4";
                            }
                            if (ContainsPattern(_queue, DoorOpenClose))
                            {
                                status = "door open-closed";
                            }
                        }
                    }
                    if (status != "No pattern")
                    {
                        _queue.Clear(); //clear queue and pattern
                        _queue.Add(new State {
                            DoorValue = false, IRValue = false
                        });                                                           //add first all-closed pattern

                        TelemetryDataClass.SourceInfo = $"Home {status}";
                        var monitorData = new
                        {
                            DeviceID = "SecurityController",
                            door     = "Home",
                            status   = TelemetryDataClass.SourceInfo,
                            TelemetryDataClass.isHomeSecured,
                            date        = CurrentDateTime.ToString("dd.MM"),
                            time        = CurrentDateTime.ToString("HH:mm"),
                            DateAndTime = CurrentDateTime
                        };
                        await _sendListData.PipeMessage(monitorData, Program.IoTHubModuleClient, TelemetryDataClass.SourceInfo, "output");

                        status = "No pattern";
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Paradox exception: {e}");
                }
                await Task.Delay(TimeSpan.FromMilliseconds(10)); //check statuses every 10 millisecond
            }
        }
Ejemplo n.º 44
0
        private int LastActiveZoneID = 1; //lets be door, actually this doesnt matter

        public async void ParadoxSecurity()
        {
            string ComPort  = "/dev/ttyAMA0";
            int    baudrate = 9600;

            _serialPort = new SerialPort(ComPort, baudrate);
            try
            {
                _serialPort.Open();
            }
            catch (IOException ex)
            {
                Console.WriteLine($"{ex}");
            }
            byte[] DataStream = new byte[4];
            byte   index      = 0;

            while (true)
            {
                await Task.Delay(TimeSpan.FromMilliseconds(1)); //check statuses every millisecond

                //Spectra message output is always 4 bytes
                try
                {
                    if (_serialPort.BytesToRead < 4)
                    {
                        index = 0;
                        while (index < 4)
                        {
                            DataStream[index++] = (byte)_serialPort.ReadByte();
                        }
                    }
                }
                catch (Exception e) { Console.WriteLine($"Timeout {e}"); }

                int EventId    = DataStream[0] >> 2;
                int CategoryId = ((DataStream[0] & 3) << 4) + (DataStream[1] >> 4);

                string Event         = events.Where(x => x.EventId == EventId).Select(x => x.EventName).DefaultIfEmpty($"Event_{EventId}").First();
                int    EventCategory = events.Where(x => x.EventId == EventId).Select(x => x.EventCategory).DefaultIfEmpty(EventId).First();

                string Message = CategoryId.ToString();

                bool isZoneEvent       = EventCategory == Category.ZONE;
                bool isStatus          = EventCategory == Category.STATUS;
                bool isTrouble         = EventCategory == Category.TROUBLE;
                bool isAccessCode      = EventCategory == Category.ACCESS_CODE;
                bool isSpecialAlarm    = EventCategory == Category.SPECIAL_ALARM;
                bool isSpecialArm      = EventCategory == Category.SPECIAL_ARM;
                bool isSpecialDisarm   = EventCategory == Category.SPECIAL_DISARM;
                bool isNonReportEvents = EventCategory == Category.NON_REPORT_EVENTS;
                bool isSpecialReport   = EventCategory == Category.SPECIAL_REPORT;
                bool isRemoteControl   = EventCategory == Category.REMOTE_CONTROL;

                if (isZoneEvent)
                {
                    bool IsZoneOpen = false;
                    if (EventId == 1)
                    {
                        IsZoneOpen = true;
                    }
                    //update existing list with the IR statuses and activating/closing time
                    Zones.Where(x => x.ZoneId == CategoryId).Select(x => { x.IsZoneOpen = IsZoneOpen; x.IsHomeSecured = TelemetryDataClass.isHomeSecured; x.ZoneEventTime = METHOD.DateTimeTZ().DateTime; return(x); }).ToList();
                    LastActiveZoneID = CategoryId; //last active zone ID used to check if anybody is home
                    Message          = Zones.Where(x => x.ZoneId == CategoryId).Select(x => $"{x.ZoneName}").DefaultIfEmpty($"Zone_{CategoryId}").First();
                }
                if (isStatus)
                {
                    Message = PartitionStatuses.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"Status_{CategoryId}").First();
                }
                if (isTrouble)
                {
                    Message = SystemTroubles.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"Trouble_{CategoryId}").First();
                }
                if (isSpecialAlarm)
                {
                    Message = SpecialAlarms.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"SpecialAlarm_{CategoryId}").First();
                }
                if (isSpecialArm)
                {
                    Message = SpecialArms.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"SpecialArm_{CategoryId}").First();
                }
                if (isSpecialDisarm)
                {
                    Message = SpecialDisarms.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"SpecialDisarm_{CategoryId}").First();
                }
                if (isNonReportEvents)
                {
                    Message = NonReportableEvents.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"NonReportEvent_{CategoryId}").First();
                }
                if (isSpecialReport)
                {
                    Message = SpecialReportings.Where(x => x.CategoryId == CategoryId).Select(x => x.Name).DefaultIfEmpty($"SpecialReporting_{CategoryId}").First();
                }
                if (isRemoteControl)
                {
                    Message = $"Remote_{CategoryId}";
                }
                if (isAccessCode)
                {
                    Message = GetAccessCode(CategoryId);
                }

                if (!(isStatus && (CategoryId == 0 || CategoryId == 1)) && !(EventId == 0 || EventId == 1)) //not show System Ready/Not ready messages and zone open/close messages.
                {
                    //Console.WriteLine($"{METHOD.DateTimeTZ():HH:mm:ss,ff} {Event}, {Message}");
                }
            }
        }
Ejemplo n.º 45
0
 public MethodChecked(METHOD value)
 {
     this.Value = value;
 }
Ejemplo n.º 46
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="AMethod">解析方法类型</param>
 public DatagramResolver(METHOD AMethod)
 {
     _ResolveMethod = AMethod;
 }