АСУ-Навигация

АСУ-Навигация

Расшифрованная и, чтобы не делала killout всем подряд, немного подхаченная процедура dbo.clcsp_MainCalculator3 из БД TRANSNAVI - Автоматизированная Система Учёта Транспортной Работы, НПП «Транснавигация».

USE [TRANSNAVI]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author: Dmitriy Def
-- Create date: 24.09.2015
-- Description: Encrypt
-- =============================================

--ОСНОВНАЯ ПРОЦЕДУРА ЯДРА
ALTER PROCEDURE [dbo].[clcsp_MainCalculator3Hack]
AS
set nocount on

--временная таблица
create table #NRD(NRD_Identificator int)

declare @CurDate                datetime
declare @CurInt                 int
declare @OldInt                 int
declare @ServiceDay int

set @ServiceDay=0

--Вносим строчку в журнал
insert into Logs values(GetDate(),'ЗАПУСК СЛУЖБЫ',0)

set @OldInt=-1

-------------------------------------------------------------------------------------------------------------------------
--ОСНОВНОЙ ЦИКЛ
-------------------------------------------------------------------------------------------------------------------------

StartService:

--читаем основные параметры
declare @enable_BDI bit
set @enable_BDI=case when dbo.fn_GetParam('enable_BDI','0')='1' then 1 else 0 end
declare @enable_kernel bit
set @enable_kernel=case when dbo.fn_GetParam('enable_kernel','1')='1' then 1 else 0 end
declare @enable_digithandle bit
set @enable_digithandle=case when dbo.fn_GetParam('enable_digithandle','0')='1' then 1 else 0 end
declare @enable_failwindow bit
set @enable_failwindow=case when dbo.fn_GetParam('enable_failwindow','1')='1' then 1 else 0 end
declare @enable_hotwindow bit
set @enable_hotwindow=case when dbo.fn_GetParam('enable_hotwindow','1')='1' then 1 else 0 end
declare @enable_kkphandle bit
set @enable_kkphandle=case when dbo.fn_GetParam('enable_kkphandle','1')='1' then 1 else 0 end
declare @enable_nariadcross bit
set @enable_nariadcross=case when dbo.fn_GetParam('enable_nariadcross','1')='1' then 1 else 0 end
declare @enable_navhandle bit
set @enable_navhandle=case when dbo.fn_GetParam('enable_navhandle','1')='1' then 1 else 0 end
declare @enable_rasphandle bit
set @enable_rasphandle=case when dbo.fn_GetParam('enable_rasphandle','1')='1' then 1 else 0 end
declare @enable_tablohandle bit
set @enable_tablohandle=case when dbo.fn_GetParam('enable_tablohandle','0')='1' then 1 else 0 end
--определяем сколько раз в минуту запускать обработчик
declare @startperminute int
set @startperminute=cast(dbo.fn_GetParam('clc_startperminute','4') as int)
declare @startonmin int
if (@startperminute>0) and (@startperminute<=60)
        set @startonmin=60/@startperminute
else
        set @startonmin=15
declare @failkeep int
set @failkeep=cast(dbo.fn_GetParam('clc_failkeep','7') as int)
declare @handlegraphdelta int
set @handlegraphdelta=cast(dbo.fn_GetParam('clc_handlegraphdelta','30') as int)

--если не разрешают запускать ядро, то выходим
if @enable_kernel=0 return(0)

--ждем время запуска службы
set @CurDate=GetDate()
set @CurInt=DatePart(hour,@CurDate)*3600+DatePart(minute,@CurDate)*60+DatePart(second,@CurDate)
while (@CurInt % @startonmin<>0) or (@CurInt=@OldInt)
begin
        waitfor delay '00:00:01'
        set @CurDate=GetDate()
        set @CurInt=DatePart(hour,@CurDate)*3600+DatePart(minute,@CurDate)*60+DatePart(second,@CurDate)
end
set @OldInt=@CurInt

--Определяем дату наряда
declare @NariadDate datetime
declare @ND_Identificator int
declare @CurTime int
set @NariadDate=@CurDate
set @CurTime=DatePart(hour,@NariadDate)*60+DatePart(minute,@NariadDate)-180
set @NariadDate=Cast(Floor(Cast(@NariadDate as float))as datetime)
if @CurTime<0
begin
set @CurTime=1440+@CurTime
set @NariadDate=DATEADD(day,-1,@NariadDate)
end
set @ND_Identificator=dbo.fn_GetNariadIndex(@NariadDate)

--Если время по чисам системы 3:00, то выполняем ряд процедур по инициализации следующего дня
if @CurInt between 10800 and 10860
begin
        if DatePart(day,@CurDate)<>@ServiceDay
        begin
                set @ServiceDay=DatePart(day,@CurDate)
                --Очищаем оперативную навиацию
                truncate table Nav
                --Очищаем таблицу
                truncate table Nav_cur
                --Очищаем LOG
                truncate table Logs
                --очищаем рабочую таблицу связок
                truncate table NRDCPLink
                --Очищаем файл паркового опроса
                if dbo.fn_TableExists('tbParkOpros')=1 truncate table tbParkOpros
                --Очищаем отметки по ККП
                if dbo.fn_TableExists('tbFreeNav')=1 truncate table tbFreeNav
                --Очищаем пересечения наряда
                if dbo.fn_TableExists('tbNariadInfo')=1 delete from tbNariadInfo where ND_Identificator<@ND_Identificator-@failkeep
                --очищаем горячее окно
                if dbo.fn_TableExists('tbErrorLog')=1 delete from tbErrorLog where ND_Identificator<@ND_Identificator-@failkeep
                --очищаем окно нарушений
                delete from tbMainWindow where ND_Identificator<@ND_Identificator-@failkeep
                truncate table tbMainWindow_cur
                --Очищаем оперативную цифровую модель
                if dbo.fn_TableExists('tbDigitPos')=1 truncate table tbDigitPos
                --обрабатываем новые мониторинговые таблицы
                insert into jobs (ND_Identificator,NRD_Identificator,JB_MinCalcTime,JB_MaxCalcTime,JB_MinCalcOrder,JB_MaxCalcOrder,JB_Command,NSIT_UniqueID)
                select ND_Identificator,NRD_Identificator,0,1439,-10000,1000000,1,NSIT_UniqueID
                from tbBaseNariad where ND_Identificator=@ND_Identificator
        end
end

--блок криптографической проверки
if (@CurInt % 3600)=0
begin
        declare @bad bit
        declare @alv_res int
        declare @spid smallint
        declare @spid_str varchar(15)
        set @bad=0
        if not exists(select * from TRANSNAVI.dbo.sysobjects where name='crysp_alv' and xtype='P')  or
                 not exists(select * from TRANSNAVI.dbo.sysobjects where name='crysp_dcb' and xtype='P')  or
                 not exists(select * from TRANSNAVI.dbo.sysobjects where name='crysp_dcl' and xtype='P')  or
                 not exists(select * from TRANSNAVI.dbo.sysobjects where name='crysp_gm' and xtype='P')  or
                 not exists(select * from TRANSNAVI.dbo.sysobjects where name='cryfn_gsn' and xtype='FN') set @bad=1
        else begin
                exec @alv_res=dbo.crysp_alv 255
                if @alv_res<0 set @bad=0
        end
        if @bad=1
        begin
                --всех убить
                declare killcur cursor
                local
                for
                select spid from master.dbo.sysprocesses sp
                inner join master.dbo.sysdatabases db on sp.dbid=db.dbid
                where db.name='TRANSNAVI' and spid<>@@spid
                open killcur
                fetch next from killcur into @spid
                while @@fetch_status=0
                begin
                        set @spid_str=cast(@spid as varchar)
                        exec ('KILL '+@spid_str)
                        fetch next from killcur into @spid
                end
                close killcur
                deallocate killcur
                --и выйти
                return(-1);
        end;
end

--если это первый вызов минуты, то набираем задание
if (@CurInt % 60)=0
begin
        --очищаем таблицу
        truncate table #NRD
        --заполняем ее новыми значениями
        insert into #NRD(NRD_Identificator)
        select NRD_Identificator from tbBaseNariad
        where (ND_Identificator=@ND_Identificator) and (NSIT_UniqueID>0) and
                                (NRD_ExitPark-@handlegraphdelta<=@CurTime and @CurTime<=NRD_EnterPark+@handlegraphdelta)
        --Очищаем окно нарушений от старых данных (которых нет в задании)
        delete from tbMainWindow_cur where not exists(select top 1 * from #NRD where tbMainWindow_cur.NRD_Identificator=#NRD.NRD_Identificator)
end

--Вносим строчку в журнал
insert into Logs values(GetDate(),'ВЫЗОВ',0)

--Проверяем скорость поступления данных
declare @CountBefore int
declare @CountAfter  int
declare @MaxInsSpeed int
set @MaxInsSpeed=cast(dbo.fn_GetParam('clc_maxinsspeed','10') as int)
set @CountBefore=(select count(*) from Nav_buf)
waitfor delay '00:00:01'
set @CountAfter=(select count(*) from Nav_buf)
if @CountAfter-@CountBefore>@MaxInsSpeed
begin
        insert into Logs values(GetDate(),'ВЫЗОВ ОТМЕНЕН',0)
        goto StartService
end

--Обновляем таблицу связанных КП
delete from NRDCPLink
where
not exists
(
        select top 1 * from tbBaseNariad
        where NRDCPLink.NRD_Identificator=tbBaseNariad.NRD_Identificator
)
insert into NRDCPLink(ND_Identificator,NRD_Identificator,NSIT_UniqueID,CP_Identificator,CNRD_EndStation)
select distinct BN.ND_Identificator,BN.NRD_Identificator,BN.NSIT_UniqueID,CN.CP_Identificator,CN.CNRD_EndStation
from tbBaseNariad BN inner join tbCardNariad CN on BN.NRD_Identificator=CN.NRD_Identificator
where not exists
(
        select top 1 * from NRDCPLink
        where NRDCPLink.NRD_Identificator=BN.NRD_Identificator
)

--Этап 0. Цифровая модель
if (@enable_digithandle=1)
begin
        insert into
 Logs values(GetDate(),'Начало цифровой модели',0)
        exec dgtsp_CalcNavPosition @ND_Identificator
        insert into Logs values(GetDate(),'Конец цифровой модели',0)
end

--Этап 1. Окапывание
declare @NRD_identificator int
declare @JobCount int
declare @JB_identificator uniqueidentifier
declare @JB_MinCalcTime    int
declare @JB_MaxCalcTime    int
declare @JB_MinCalcOrder   int
declare @JB_MaxCalcOrder   int
declare @JB_Command                        int
declare @NSIT_UniqueID           int
declare @RowCount        int
declare @RowResult                      int
set @RowCount=cast(dbo.fn_GetParam('clc_rowcount','10000') as int)
declare @MaxRestToCalc  int
set @MaxRestToCalc=cast(dbo.fn_GetParam('clc_maxresttocalc','300') as int)
declare @BufCount int
set @BufCount=(select count(*) from nav_buf)
if (@enable_navhandle=1)
begin
        insert into Logs values(GetDate(),'Начало Окапывания',@BufCount)
        exec clcsp_LinkCurNavToCP3 @RowCount,@RowResult output
        insert into Logs values(GetDate(),'Конец Окапывания',@BufCount)
        if @RowResult>@MaxRestToCalc
        begin
                goto StartService
        end
end

--Этап 2. Обработка
exec clcsp_GetCalcCur3
set @JobCount=(select count(*) from Jobs)
insert into Logs values(GetDate(),'Начало Обработки',@JobCount)
declare JobCur cursor
local
for select JB_Identificator,ND_Identificator,NRD_Identificator,JB_MinCalcTime,JB_MaxCalcTime,JB_MinCalcOrder,JB_MaxCalcOrder,JB_Command,NSIT_UniqueID
from Jobs
open JobCur
fetch next from JobCur into
@JB_Identificator,@ND_Identificator,@NRD_Identificator,@JB_MinCalcTime,@JB_MaxCalcTime,@JB_MinCalcOrder,@JB_MaxCalcOrder,@JB_Command,@NSIT_UniqueID
while @@fetch_status=0
begin
        --пересчитываем график
        if (@enable_rasphandle=1) exec clcsp_Calc3 @ND_Identificator,@NRD_Identificator,@JB_MinCalcOrder,@JB_MaxCalcOrder
        --пересчитываем окно нарушений
        if (@enable_failwindow=1) exec dspsp_MainWindow @NRD_Identificator
        --пересчитываем горячее окно
        if (@enable_hotwindow=1) exec dspsp_HotWindow @NRD_Identificator
        --пересчитываем приезд отъезд от конечных
        if (@JB_Command & 2)<>0
        begin
                if (@enable_kkphandle=1) exec dspsp_CalcFreeNav2 @NSIT_UniqueID
        end
        --вычисляем основные параметры графика (раз в минуту)(кол-во рейсов по плану)
        exec dspsp_CalcGraph_minute @ND_Identificator,@NRD_Identificator,@CurTime
        --расчитываем данные для БДИ
        if (@enable_BDI=1) exec msgsp_FormRasp @NSIT_UniqueID,0
        --удаляем график из задания
        delete from #NRD where NRD_Identificator=@NRD_Identificator
        --удаляем выполненное задание
        delete from Jobs where JB_Identificator=@JB_Identificator
        --переходим к следующему заданию
        fetch next from JobCur into
@JB_Identificator,@ND_Identificator,@NRD_Identificator,@JB_MinCalcTime,@JB_MaxCalcTime,@JB_MinCalcOrder,@JB_MaxCalcOrder,@JB_Command,@NSIT_UniqueID
end
close JobCur
deallocate JobCur
insert into Logs values(GetDate(),'Конец Обработки',@JobCount)

--если это последний вызов минуты, то отрабатываем оставшиеся графики
if (@CurInt % 60)+@startonmin>=60
begin
        set @JobCount=(select count(*) from #NRD)
        insert into Logs values(GetDate(),'Начало Оперы',@JobCount)
        declare cur cursor
        local
        for
        select NRD_Identificator from #NRD
        open cur
        fetch next from cur into @NRD_Identificator
while @@fetch_status=0
        begin
                declare @leftlim  int
                declare @rightlim int
                --перекрашиваем график
                if (@enable_rasphandle=1)
                begin
                        exec clcsp_CalcCurLimits @NRD_Identificator, @CurTime, @CurTime, 3, @leftlim OUTPUT , @rightlim OUTPUT
                        exec clcsp_ColorGraph3 @ND_Identificator,@NRD_Identificator,@leftlim,@rightlim,1
                end
                --пересчитываем окно нарушений
                if (@enable_failwindow=1) exec dspsp_MainWindow @NRD_Identificator
                --пересчитываем горячее окно
                if (@enable_hotwindow=1) exec dspsp_HotWindow @NRD_Identificator
                --вычисляем основные параметры графика (раз в минуту)
                exec dspsp_CalcGraph_minute @ND_Identificator,@NRD_Identificator,@CurTime
                --переходим к следующему графику
                fetch next from cur into @NRD_Identificator
        end
        close
 cur
        deallocate cur
        insert into Logs values(GetDate(),'Конец Оперы',@JobCount)
end

--расчет данных для табло
if (@enable_tablohandle=1)
begin
        insert into Logs values(GetDate(),'Начало табло',0)
        exec tblsp_CalculateTablo
        insert into Logs values(GetDate(),'Конец табло',0)
end

goto StartService

ГЛОНАСС

0 комментариев

Только авторизованные пользователи могут оставлять комментарии.

Пожалуйста, Авторизуйтесь или Зарегистрируйтесь