# 0816 trigger OK drop trigger tt_insert_record; drop trigger tt_update_record; # error drop trigger tt_before_facetvalue; drop trigger tt_insert_faceValue; ---1 --- trigger 建立-------------------------- DELIMITER $$ create trigger tt_insert_record after insert on alarmorion_orionalarmrecord FOR EACH ROW begin INSERT INTO `err_main`(`id`, `timestamp`, `datestamp`, `uuidHash`, `uuid`, `isOpen`, `sourceState`, `ackState`, `ackRequired`, `alarmClass`, `priority`, `normalTime`, `ackTime`, `userAccount`, `alarmTransition`, `lastUpdate`) VALUES (new.id, new.`timestamp`, new.`datestamp`, new.`uuidHash`, new.`uuid`, new.`isOpen`, new.`sourceState`, new.`ackState`, new.`ackRequired`, new.`alarmClass`, new.`priority`, new.`normalTime`, new.`ackTime`, new.`userAccount`, new.`alarmTransition`, new.`lastUpdate`); end$$ DELIMITER ; ---2 -------------------------------------------- DROP TRIGGER IF EXISTS tt_update_record; DELIMITER $$ create trigger tt_update_record after update on alarmorion_orionalarmrecord FOR EACH ROW begin DECLARE _siteID varchar(20); DECLARE _devID varchar(20); UPDATE`err_main` SET `timestamp` = new.`timestamp`, `datestamp` = new.`datestamp`, `uuidHash` = new.`uuidHash`, `uuid` = new.`uuid`, `isOpen` = new.`isOpen`, `sourceState` = new.`sourceState`, `ackState` = new.`ackState`, `ackRequired` = new.`ackRequired`, `alarmClass` = new.`alarmClass`, `priority` = new.`priority`, `normalTime` = new.`normalTime`, `ackTime` = new.`ackTime`, `userAccount` = new.`userAccount`, `alarmTransition` = new.`alarmTransition`, `lastUpdate` = new.`lastUpdate` WHERE `id` = new.`id`; # 2.卡片紅綠燈 -- 只做恢復,(正常-->異常)在 faceValue # v2 # 1 Inverter # 2 InverterkWh0 # 3 DUST # 4 Network # 5 FIC # SourceState = 1 異常狀態, 0 異常復歸 # 燈號 HealthStatus: 1:設備正常 Green, 2.設備異常 Yellow , 3:設備斷線 Red # update 主表燈號 --------------- 主要作為恢復使用 ---------------------------- #select site_id into _siteID from err_main where id = new.`id`; -- select SUBSTRING_INDEX(SUBSTRING_INDEX(new.`value`, ':', -1) # : 後面所有string -- ,'S', -1) into _devID; # S 後面所有string -- from alarmorion_orionalarmfacetvalue a -- where a.alarm = new.`id` and a.facetName = 8; select SUBSTRING_INDEX(a.`value`,'S', -1) into _devID from alarmorion_orionalarmfacetvalue a where a.alarm = new.`id` and a.facetName = 8; set _siteID = substring(_devID , 1, 9); -- select substring(SUBSTRING_INDEX(a.errmsg,'S', -1) , -- 1, 9) into _siteID -- from alarmorion_orionalarmfacetvalue a -- where a.alarm = new.`id` and a.facetName = 8; IF(_siteID is not null) THEN # 看看是否有舊有的其他異常 update power_station a join ( select a.id, sourceState, alarmClass, normalTime, site_id, case when (sourceState = 1 and alarmClass = 4) then 3 # Network 控制盒斷線 - 紅燈 when (sourceState = 1 and alarmClass != 4) then 2 # inv, sensor 異常 黃燈 when sourceState = 0 then 1 end healthStatus #, FROM_UNIXTIME(TIMESTAMP/1000, '%Y-%m-%d %H:%i:%s') time1 from alarmorion_orionalarmrecord a join ( select id, alarm, substring( errmsg , 2, 9) site_id from alarmorion_orionalarmfacetvalue where facetName = 8 and substring( errmsg , 2, 9) = _siteID order by 1 desc limit 100 ) b on a.id = b.alarm where sourcestate = 1 # a.id = 185966 #185954 order by healthStatus desc limit 1 )b on a.code = b.site_id set a.HealthStatus = b.healthStatus; -- update power_station a join ( -- select site_id, case when (sourceState = 1 and alarmClass = 4) then 3 # Network 控制盒斷線 - 紅燈 -- when (sourceState = 1 and alarmClass != 4) then 2 # inv, sensor 異常 黃燈 -- when sourceState = 0 then 1 end healthStatus -- from err_main -- where site_id = _siteID -- group by site_id, alarmClass, sourceState -- order by 2 desc # 以燈號嚴重性為主要顯示 -- limit 1 -- ) b on a.`code` = b.`site_id` -- set a.HealthStatus = b.healthStatus; end if; # log INSERT INTO `log_trigger`(`site_id`, `device_id`, `HealthStatus`, alarm_id, `triggerFrom`) VALUES ( _siteID, _devID, case new.sourceState when 1 then 2 # # Network 控制盒斷線 - 紅燈 when 0 then 1 end , new.id, 'record'); # update 紅綠燈 for Inverter update inv_status set `status` = case new.sourceState when 1 then 2 # 異常黃燈 when 0 then 1 end # 異常復歸 where inverterid = _devID; -- IF (NEW.alarmClass = 1 ) THEN # alarmclass = 1 Inverter -- #取出設備 ID -- update inv_status set -- `status` = case new.sourceState when 1 then 2 # 異常黃燈 -- when 0 then 1 end # 異常復歸 -- where inverterid = _devID; -- -- end if; end$$ DELIMITER ; ---- 3 ------------------------------------------------------------ DROP TRIGGER IF EXISTS tt_before_facetvalue; DELIMITER $$ create trigger tt_before_facetvalue before insert on alarmorion_orionalarmfacetvalue FOR EACH ROW begin set new.errmsg = unicode_decode(new.`value`); end$$ DELIMITER ; ---4 ------------------------------------------------------------- DROP TRIGGER IF EXISTS tt_insert_faceValue; DELIMITER $$ create trigger tt_insert_faceValue after insert on alarmorion_orionalarmfacetvalue FOR EACH ROW begin # 022020001 s:S022020001010011 --> 022020001010011 DECLARE _siteID varchar(20); DECLARE _devID varchar(20); DECLARE _msg varchar(30); IF (NEW.facetName = 4 ) THEN # 4 異常原始值 #insert into ts_facevalue(alarm_no, facename, errvalue, created_time) value(NEW.alarm, NEW.facetname, NEW.`value`, now()); update err_main set err_valuekind = left(new.`value`, 1), errValue = SUBSTRING_INDEX( # e:7@{Off=1,... SUBSTRING_INDEX(new.`value`, '@', 1) # @前面所有string --> e:7 , ':', -1) # : 後面所有string --> 7 where id = new.`alarm`; ELSEIF (NEW.facetName = 8) THEN # 8 異常設備 select SUBSTRING_INDEX(SUBSTRING_INDEX(new.`value`, ':', -1) # : 後面所有string ,'S', -1) into _devID; # S 後面所有string set _siteID = substring(_devID , 1, 9); #1. 記錄 異常主表.site_id, 設備編號, 設備廠牌 update err_main set errDevice = _devID, site_id = _siteID, errDeviceBrand = ( select brand from v_company_inv where INVERTERID = _devID ), errDeviceModel = ( select Model from v_company_inv where INVERTERID = _devID ) where id = new.`alarm`; # 2.卡片紅綠燈 -- 新增異常只考慮最新的異常 不考慮已存在的其他異常 #if (_siteID is not null or len(_siteID) > 0) then # 燈號 HealthStatus: 1:設備正常 Green 2:設備異常 Yellow 3.設備斷線 Red update power_station a join err_main b on a.`code` = b.site_id set a.HealthStatus = case when b.alarmClass = 4 then 3 # Network 控制盒斷線 - 紅燈 when b.alarmClass != 4 then 2 end # inv, sensor 異常 黃燈 where b.id = new.`alarm`; #select * from power_station a join err_main b on a.`code` = b.site_id #end; # 3. sensor、inverter 設備狀態 # 3.1 inverter alarmorion_orionalarmrecord update inv_status a set `status` = 2, # 有 insert 一定是異常,復歸在主表Record alarmID = new.`alarm` where inverterid = _devID ; #log 8 INSERT INTO `log_trigger`(`site_id`, `device_id`, `HealthStatus`, alarm_id, `triggerFrom`) VALUES ( _siteID, _devID, null, new.`alarm`, 'facetvalue8'); end if; end$$ DELIMITER ;